New Setup 📦
11
home/.config/awesome/libs/rubato/.editorconfig
Normal file
|
@ -0,0 +1,11 @@
|
|||
root = true
|
||||
|
||||
[*.lua]
|
||||
charset = utf-8
|
||||
intent_style = tab
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
max_line_length = 120
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
1
home/.config/awesome/libs/rubato/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
tags
|
21
home/.config/awesome/libs/rubato/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021 andOrlando
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
415
home/.config/awesome/libs/rubato/README.md
Normal file
|
@ -0,0 +1,415 @@
|
|||
# rubato
|
||||
|
||||
- [Background and explanation](#background)
|
||||
- [How to actually use it](#usage)
|
||||
- [But why though?](#why)
|
||||
- [Arguments and Methods](#arguments-methods)
|
||||
- [Custom Easing Functions](#easing)
|
||||
- [Installation](#install)
|
||||
- [Why the name?](#name)
|
||||
- [Todo](#todo)
|
||||
|
||||
Basically like [awestore](https://github.com/K4rakara/awestore) but not really.
|
||||
|
||||
Join the cool curve crew
|
||||
|
||||
<!-- look colleges might see this and think its distasteful so I'm commenting it out for the moment
|
||||
<img src="https://cdn.discordapp.com/attachments/702548961780826212/879022533314216007/download.jpeg" height=160>-->
|
||||
|
||||
<h1 id="background">Background and Explanation</h1>
|
||||
|
||||
The general premise of this is that I don't understand how awestore works. That and I really wanted to be able to have an interpolator that didn't have a set time. That being said, I haven't made an interpolator that doesn't have a set time yet, so I just have this instead. It has a similar function to awestore but the method in which you actually go about doing the easing is very different.
|
||||
|
||||
When creating an animation, the goal is to make it as smooth as humanly possible, but I was finding that with conventional methods, should the animation be interrupted with another call for animation, it would look jerky and inconsistent. You can see this jerkiness everywhere in websites made by professionals and it makes me very sad. I didn’t want that for my desktop so I used a bit of a different method.
|
||||
|
||||
This jerkiness is typically caused by discontinuous velocity graphs. One moment it’s slowing down, and the next it’s way too fast. This is caused by just lazily starting the animation anew when already in the process of animating. This kind of velocity graph looks like this:
|
||||
|
||||
<img src="images/disconnected_graph.png" alt="Disconnected Velocity Graph" height=160/>
|
||||
|
||||
Whereas rubato takes into account this initial velocity and restarts animation taking it into account. In the case of one wanting to interpolate from one point to another and then back, it would look like this:
|
||||
|
||||
<img src="images/connected_graph.png" alt="Connected Velocity Graph" height=160/>
|
||||
|
||||
<sub><sup>okay maybe my graph consistancy is trash, what can I do...</sup></sub>
|
||||
|
||||
These are what they would look like with forwards-and-back animations. A forwards-than-forwards animation would look more like this, just for reference:
|
||||
|
||||
<img src="images/forwards_forwards_graph.png" alt="Forwards ForwardsGraph" height=160/>
|
||||
|
||||
To ask one of you to give these graphs as inputs, however, would be really dumb. So instead we define an intro function and its duration, which in the figure above is the `y=x` portion, an outro function and its duration, which is the `y=-x` portion, and the rest is filled with constant velocity. The area under the curve for this must be equal to the position for this to end up at the correct position (antiderivative of velocity is position). If we know the area under the curve for the intro and outro functions, the only component we need to ensure that the antiderivative is equal to the position would be the height of the graph. We find that with this formula:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}m=\frac{d %2B ib(F_i(1)-1)}{i(F_i(1)-1) %2B o(F_o(1)-1) %2B t}" height=50>
|
||||
|
||||
where `m` is the height of the plateau, `i` is intro duration, `F_i` is the antiderivative of the intro easing function, `o` is outro duration, `F_o` is the antiderivative of the outro easing function, `d` is the total distance needed to be traveled, `b` is the initial slope, and `t` is the total duration.
|
||||
|
||||
We then simulate the antiderivative by adding `v(t)` (or the y-value at time `t` on the slope graph) to the current position 30 times per second (by default, but I recommend 60). There is some inaccuracy since it’s not a perfect antiderivative and there’s some weirdness when going from positive slopes to negative slopes that I don’t know how to intelligently fix (I have to simulate the antiderivative beforehand and multiply everything by a coefficient to prevent weird errors), but overall it results in good looking interruptions and I get a dopamine hit whenever I see it in action.
|
||||
|
||||
There are two main small issues that I can’t/don’t know how to fix mathematically:
|
||||
- It’s not perfectly accurate (it is perfectly accurate as `dt` goes to zero) which I don’t think is possible to fix unless I stop simulating the antiderivative and actually calc out the function, which seems time inefficient
|
||||
- When going from a positive m to a negative m, or in other words going backwards after going forwards in the animation, it will always undershoot by some value. I don’t know what that value is, I don’t know where it comes from, I don’t know how to fix it except for lots and lots of time-consuming testing, but it’s there. To compensate for this, whenever there’s a situation in which this will happen, I simulate the animation beforehand and multiply the entire animation by a corrective coefficient to make it do what I want
|
||||
- Awesome is kinda slow at redrawing imaages, so 60 redraws per second is realistically probably not going to happen. If you were to, for example, set the redraws per second to 500 or some arbitrarily large value, if I did nothing to dt, it would take forever to complete an animaiton. So since I can't fix awesome, I just (by default but this is optional) limit the rate based on the time it takes for awesome to render the first frame of the animation (Thanks Kasper for pointing this out and showing me a solution).
|
||||
|
||||
So that’s how it works. I’d love any contributions anyone’s willing to give. I also have plans to create an interpolator without a set duration called `target` as opposed to `timed` when I have the time (or need it for my rice).
|
||||
|
||||
<h1 id="usage">How to actually use it</h1>
|
||||
|
||||
So to actually use it, just create the object, give it a couple parameters, give it some function to
|
||||
execute, and then run it by updating `target`! In practice it'd look like this:
|
||||
|
||||
```lua
|
||||
timed = rubato.timed {
|
||||
duration = 1/2, --half a second
|
||||
intro = 1/6, --one third of duration
|
||||
override_dt = true, --better accuracy for testing
|
||||
subscribed = function(pos) print(pos) end
|
||||
}
|
||||
|
||||
--you can also achieve the same effect as the `subscribed` parameter with this:
|
||||
--timed:subscribe(function(pos) print(pos) end)
|
||||
|
||||
--target is initially 0 (unless you set pos otherwise)
|
||||
timed.target = 1
|
||||
-- Here it prints out this:
|
||||
-- 0
|
||||
-- 0
|
||||
-- 0.02
|
||||
-- 0.06
|
||||
-- 0.12
|
||||
-- 0.2
|
||||
-- 0.3
|
||||
-- 0.4
|
||||
-- 0.5
|
||||
-- 0.6
|
||||
-- 0.7
|
||||
-- 0.8
|
||||
-- 0.88
|
||||
-- 0.94
|
||||
-- 0.98
|
||||
-- 1
|
||||
-- 1
|
||||
-- First 0 is because when you initially subscribe a function
|
||||
-- it calls that function at the current position, which is 0
|
||||
-- Last zero is because it'll snap to the exact position in
|
||||
-- case of minor error which can come about from floating point
|
||||
-- math or correcting for frameskips
|
||||
|
||||
--When called after it finishes printing, this would print out
|
||||
--the same numbers but in reverse, sending it back from 1 to 0
|
||||
timed.target = 0
|
||||
```
|
||||
|
||||
If you're familiar with the awestore api and don't wanna use what I've got, you can use those methods
|
||||
instead if you set `awestore_compat = true`. It’s a drop-in replacement, so your old code should work perfectly with it. If it doesn’t, please make an issue and I’ll do my best to fix it. Please include the broken code so I can try it out myself.
|
||||
|
||||
So how do the animations actually look? Let’s check out what I (at one point) use(ed) for my workspaces:
|
||||
|
||||
```lua
|
||||
timed = rubato.timed {
|
||||
intro = 0.1,
|
||||
duration = 0.3
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
The above is very subtly eased. A somewhat more pronounced easing would look more like this:
|
||||
|
||||
```lua
|
||||
timed = rubato.timed {
|
||||
intro = 0.5,
|
||||
duration = 1,
|
||||
easing = rubato.quadratic --quadratic slope, not easing
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
The first animation’s velocity graph looks like a trapezoid, while the second looks like the graph shown below. Note the lack of a plateau and longer duration which gives the more pronounced easing:
|
||||
|
||||

|
||||
|
||||
<h1 id="why">But why though?</h1>
|
||||
|
||||
Why go through all this hassle? Why not just use awestore? That's a good question and to be fair you can use whatever interpolator you so choose. That being said, rubato is solely focused on animation, has mathematically perfect interruptions and I’ve been told it also looks smoother.
|
||||
|
||||
Furthermore, if you use rubato, you get to brag about how annoying it was to set up a monstrous derivative just to write a custom easing function, like the one shown in [Custom Easing Function](#easing)'s example. That's a benefit, not a downside, I promise.
|
||||
|
||||
Also maybe hopefully the code should be almost digestible kinda maybe. I tried my best to comment and documentate, but I actually have no idea how to do lua docs or anything.
|
||||
|
||||
Also it has a cooler name
|
||||
|
||||
<h1 id="arguments-methods">Arguments and Methods</h1>
|
||||
|
||||
**For rubato.timed**:
|
||||
|
||||
Arguments (in the form of a table):
|
||||
- `duration`: the total duration of the animation
|
||||
- `rate`: the number of times per second the timer executes. Higher rates mean smoother animations and less error.
|
||||
- `prop_intro`: when `true`, `intro`, `outro` and `inter` represent proportional values; 0.5 would be half the duration. (def. `false`)
|
||||
- `pos`: the initial position of the animation (def. `0`)
|
||||
- `intro`: the duration of the intro
|
||||
- `outro`: the duration of the outro (def. same as `intro`\*)
|
||||
- inter: the duration of intermittent animations (def. same as `intro`\*)
|
||||
- `easing`: the easing table (def. `interpolate.linear`)
|
||||
- `easing_outro`: the outro easing table (def. as `easing`)
|
||||
- `easing_inter`: the "intermittent" easing table, which defines which easing to use in the case of animation interruptions (def. same as `easing`)
|
||||
- `subscribed`: a function to subscribe at initialization (def. `nil`)
|
||||
- `override_simulate`: when `true`, will simulate everything instead of just when `dx` and `b` have opposite signs at the cost of having to do a little more work (and making my hard work on finding the formula for `m` worthless :slightly_frowning_face:) (def. `false`)
|
||||
- `rapid_set`:
|
||||
- `override_dt`: overrides the difference in time it takes to redraw the screen and just uses 1/rate no matter what. This results in slightly more accurate animations but they may take longer if awesome takes too long to redraw the screen. (def. `false`)
|
||||
- `awestore_compat`: make api even *more* similar to awestore's (def. `false`)
|
||||
- `log`: it would print additional logs, but there aren't any logs to print right now so it kinda just sits there (def. `false`)
|
||||
- `debug`: basically just tags the timed instance. I use it in tandem with `manager.timed.override.forall`
|
||||
|
||||
All of these values (except awestore_compat and subscribed) are mutable and changing them will change how the animation looks. I do not suggest changing `pos`, however, unless you change the position of what's going to be animated in some other way
|
||||
|
||||
\*unless `outro + intro > 1`, it will instead go for the largest allowable outro time. Ex: duration = 1, intro = 0.6, then outro will default to 0.4.
|
||||
|
||||
Properties:
|
||||
- `target`: when set, sets the target and starts the animation, otherwise returns the target
|
||||
- `pause`: if true, the timer will have its animation suspedned until set to false again
|
||||
- `running`: immutable, returns true if an animation is in progress
|
||||
|
||||
Methods are as follows:
|
||||
- `timed:subscribe(func)`: subscribe a function to be ran every refresh of the animation
|
||||
- `timed:unsubscribe(func)`: unsubscribe a function
|
||||
- `timed:fire()`: run all subscribed functions at current position (you may provide it with arguments `pos`, `time` and `dx` manually if you wish, otherwise it'll use the values of the timed object)
|
||||
- `timed:abort()`: stop the animation at the current position
|
||||
|
||||
Awestore compatibility functions (`awestore_compat` must be true):
|
||||
- `timed:set(target_new)`: sets the position the animation should go to, effectively the same as setting target
|
||||
- `timed:initial()`: returns the intiial position
|
||||
- `timed:last()`: returns the target position, effectively the same as `timed.target`
|
||||
|
||||
Awestore compatibility properties:
|
||||
- `timed.started`: subscribable table which is called when the animation starts or is interrupted
|
||||
+ `timed.started:subscribe(func)`: subscribes a function
|
||||
+ `timed.started:unsubscribe(func)`: unsubscribes a function
|
||||
+ `timed.started:fire()`: runs all subscribed functions
|
||||
- `timed.ended`: subscribable table which is called when the animation ends
|
||||
+ `timed.ended:subscribe(func)`: subscribes a function
|
||||
+ `timed.ended:unsubscribe(func)`: unsubscribes a function
|
||||
+ `timed.ended:fire()`: runs all subscribed functions
|
||||
|
||||
**builtin easing functions**
|
||||
- `easing.zero`: linear easing, zero slope
|
||||
- `easing.linear`: linear slope, quadratic easing
|
||||
- `easing.quadratic`: quadratic slope, cubic easing
|
||||
- `easing.bouncy`: the bouncy thing as shown in the example
|
||||
|
||||
**functions for setting default values**
|
||||
- (DEPRECIATED) `rubato.set_def_rate(rate)`: set default rate for all interpolators, takes an `int`. Please use instead `manager.timed.default.rate = rate`
|
||||
- (DEPRECIATED) `rubato.set_override_dt(value))`: set default for override_dt for all interpolators, takes a `bool`. Please use instead `manager.timed.default.override_dt = value`
|
||||
|
||||
**For rubato.manager**
|
||||
|
||||
- `manager.timed.default`: a table containing properties of timed objects as keys and their default values as values. Updating values in this table changes those defaults. Ex: `manager.timed.default.rate = 60` sets default rate to 60 fps
|
||||
- `manager.timed.override`: a table with accessors which set properties of all tables. Updating values in this table changes the properties of all tables. Ex: `manager.timed.override.is_instant = true` makes all animations instantaneous globally
|
||||
- `manager.timed.override.clear()`: resets all timeds to their initial values
|
||||
- `manager.timed.override.forall(func)`: run some function for all timed objects. Parameter to function is a single timed object. Ex: `manager.timed.override.forall(function(timed) print(timed) end)` prints all timeds
|
||||
|
||||
<h1 id="easing">Custom Easing Functions</h1>
|
||||
|
||||
To make a custom easing function, it's pretty easy. You just need a table with two values:
|
||||
|
||||
- `easing`, which is the function of the slope curve you want. So if you want quadratic easing
|
||||
you'd take the derivative, which would result in linear easing. **Important:** `f(0)=0` and
|
||||
`f(1)=1` must be true for it to look nice.
|
||||
- `F`, which is basically just the value of the antiderivative of the easing function at `x=1`.
|
||||
This is the antiderivative of the scaled function (``(0,0) ⋃ (1,1) ∈ f``), however, so be wary of that.
|
||||
|
||||
In practice, creating your own easing would look like this:
|
||||
|
||||
1. Go to [easings.net](https://easings.net)
|
||||
|
||||
For the sake of this tutorial, we'll do both an easy easing and a complex one. The easy easing will
|
||||
be the beautifully simple and quite frankly obvious quadratic. The much worse easing will be "ease
|
||||
in elastic."
|
||||
|
||||
2. Find the necessary information
|
||||
|
||||
For quadratic we already know the function: `y=x^2`. I don't even need to use latex it's that easy.
|
||||
|
||||
For ease in elastic, we use the function given [here](https://easings.net/#easeInElastic):
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}f(x)=-2^{10 \, x - 10}\times \sin\left(-\frac{43}{6} \, \pi %2B \frac{20}{3} \, \pi x\right))">
|
||||
|
||||
3. Take the derivative
|
||||
|
||||
Quadratic: `y=2x`, easy as that.
|
||||
|
||||
**Important:** Look. Computers aren't the greatest at indefinite mathematics. As such, it's
|
||||
possible that, like myself, you will have a hard time getting the correct derivative if it's as
|
||||
complicated as these here. Don't be discouraged, however! Sagemath (making sure not to factor
|
||||
anything) could correctly do out this math, even if I had a bit of a scare realizing that when I
|
||||
was factoring it I was just being saved by `override_simulate` being accidentally set to true.
|
||||
|
||||
Anyways, use sagemath and jupyter notebook. I don't know if all sagemaths come with it
|
||||
preinstalled, but nix makes it so easy that all I have to do is `sage -n jupyter` and it'll open it
|
||||
right up. `%display latex` in jupiter makes it look pretty, whereas `%display ascii_art` will make
|
||||
it look *presentable* in tui sagemath.
|
||||
|
||||
The derivative (via sagemath) is as follows:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}f^\prime (x)=-\frac{20}{3} \, \pi 2^{10 \, x - 10} \cos\left(-\frac{43}{6} \, \pi %2B \frac{20}{3} \, \pi x\right) - 10 \cdot 2^{10 \, x - 10} \log\left(2\right) \sin\left(-\frac{43}{6} \, \pi %2B \frac{20}{3} \, \pi x\right)">
|
||||
|
||||
4. Ensure that `(0,0) ∈ f'`
|
||||
|
||||
Quadratic: `2*0 = 0` so we're good
|
||||
|
||||
Ease in elastic not so much, however:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}f^\prime (0)=\frac{5}{1536} \, \sqrt{3} \pi - \frac{5}{1024} \, \log\left(2\right)">
|
||||
|
||||
We'll subtract this value from `f(x)` so that our new `f(x)`, let's say `f_2(x)` is such that `(0,0) ∈ f_2`
|
||||
|
||||
5. Ensure that `(1,1) ∈ f_2`
|
||||
|
||||
Quadratic: This means we have to do a wee bit of work: `f(1)=2`, so to counteract this,
|
||||
we'll create a new (and final) function that we can call `f_e` (easing function) by dividing `f(x)`
|
||||
by `f(1)` (scaling it down).
|
||||
|
||||
```
|
||||
f(1)=2
|
||||
f(x)/f(1) = 2x / 2 = x,
|
||||
f_e(x)=x
|
||||
```
|
||||
|
||||
Easy as that!
|
||||
|
||||
Or so you thought. Now let's check the same for ease in elastic:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}f_2(1)=-\frac{5}{1536} \, \sqrt{3} \pi %2B \frac{10245}{1024} \, \log\left(2\right)">
|
||||
|
||||
Hence the need for sagemath. Once we divide the two we get our final easing function, this:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}f_e(x)=\frac{4096 \, \pi 2^{10 \, x - 10} \cos\left(-\frac{43}{6} \, \pi %2B \frac{20}{3} \, \pi x\right) %2B 6144 \cdot 2^{10 \, x - 10} \log\left(2\right) \sin\left(-\frac{43}{6} \, \pi %2B \frac{20}{3} \, \pi x\right) %2B 2 \, \sqrt{3} \pi - 3 \, \log\left(2\right)}{2 \, \sqrt{3} \pi - 6147 \, \log\left(2\right)}">
|
||||
|
||||
What on god's green earth is that. Well whatever, at least it works.
|
||||
|
||||
6. Finally, we get the definite integral from 0 to 1 of our `f(x)`
|
||||
|
||||
For `f(x)=x` we can do that in our heads, it's just `1/2`.
|
||||
|
||||
Ease in elastic is a bit trickier to do in your head. You can do this with sagemath and eventually get this:
|
||||
|
||||
<img src="https://render.githubusercontent.com/render/math?math=\color{blue}\frac{20 \, \sqrt{3} \pi - 30 \, \log\left(2\right) - 6147}{10 \, {\left(2 \, \sqrt{3} \pi - 6147 \, \log\left(2\right)\right)}}">
|
||||
|
||||
So this all looked pretty daunting probably, and to be honest it took me hours of either not using
|
||||
sage (I tried with wolfram alpha for a good hour) or using sage incorrectly (it took three months
|
||||
to realize that this entire section of the readme was wrong and that using `factor` made it
|
||||
incorrect), but now that I have this easy little code snippet you can use for sage it shouldn't be
|
||||
as much of a hastle for you.
|
||||
|
||||
```python
|
||||
from sage.symbolic.integration.integral import definite_integral
|
||||
function('f')
|
||||
f(x)='''your function goes here'''
|
||||
f(x)=derivative(f(x), x)
|
||||
f(x)=f(x)-f(0)
|
||||
f(x)=f(x)/f(1)
|
||||
print(f(x)) # easing
|
||||
print(definite_integral(f(x), x, 0, 1)) # F
|
||||
```
|
||||
|
||||
So the thing with using `factor` is that, while on some weird other version of sage I was geting a
|
||||
bunch of 0.49999s which wouldn't round to .5, the result was straight up wrong. So I advise against
|
||||
it, and if you can't do the derivative then sucks to suck I guess (just lmk in an issue or
|
||||
something and I'll try my very best).
|
||||
|
||||
7. Now we just have to translate this into an actual lua table.
|
||||
|
||||
Quadratic, as usual, is easy.
|
||||
```lua
|
||||
local quadratic = {
|
||||
F = 1/2 -- F(1) = 1/2
|
||||
easing = function(t) return t end -- f(x) = x, I just use t for "time"
|
||||
}
|
||||
```
|
||||
|
||||
Ease in elastic, as usual, is not. At one point I had the willpower to try and optimize operations,
|
||||
but I really don't want to simplify those equations and I can't trust `factor`, so for now it stays
|
||||
as is. If it irks you, make a pull request and save us both.
|
||||
|
||||
```lua
|
||||
local bouncy = {
|
||||
F = (20*math.sqrt(3)*math.pi-30*math.log(2)-6147) /
|
||||
(10*(2*math.sqrt(3)*math.pi-6147*math.log(2))),
|
||||
easing = function(t) return
|
||||
(4096*math.pi*math.pow(2, 10*t-10)*math.cos(20/3*math.pi*t-43/6*math.pi)
|
||||
+6144*math.pow(2, 10*t-10)*math.log(2)*math.sin(20/3*math.pi*t-43/6*math.pi)
|
||||
+2*math.sqrt(3)*math.pi-3*math.log(2)) /
|
||||
(2*math.pi*math.sqrt(3)-6147*math.log(2))
|
||||
end
|
||||
}
|
||||
|
||||
-- how it would actually look in a timed object
|
||||
timed = rubato.timed {
|
||||
intro = 0, --we'll use this as an outro, since it's weird as an intro
|
||||
outro = 0.7,
|
||||
duration = 1,
|
||||
easing = bouncy
|
||||
}
|
||||
```
|
||||
|
||||
We did it! Now to check whether or not it actually works
|
||||
|
||||

|
||||
|
||||
While you can't see its full glory in 25 fps gif form, it really is pretty cool. Furthermore, if it
|
||||
works with *that* function, it'll probably work with anything. As long as you have the correct
|
||||
antiderivative and it's properly scaled, you can probably use any (real, differentiable) function
|
||||
under the sun.
|
||||
|
||||
Note that if it's not properly scaled, this can be worked around (if you're lazy and don't care
|
||||
about a bit of a performance decrease). You can set `override_simulaton` to true. However, it is
|
||||
possible that it will not perform exactly as you expected if you do this so do your best to just
|
||||
find the derivative and antiderivative of the derivative.
|
||||
|
||||
<h1 id="install">Installation</h1>
|
||||
|
||||
So actually telling people how to install this is important, isn't it
|
||||
|
||||
It supports luarocks, so that'll cut it if you want a really really easy install, but it'll install
|
||||
it in some faraway lua bin where you'll probably leave it forever if you either stop using rubato or
|
||||
stop using awesome. However, it's certainly the easiest way to go about it. I personally don't like
|
||||
doing this much because it adds it globally and I'm only gonna be using this with awesome, but it's
|
||||
a really easy install.
|
||||
|
||||
```
|
||||
luarocks install rubato
|
||||
```
|
||||
|
||||
Otherwise, somewhere in your awesome directory, (I use `~/.config/awesome/lib`) you can run this
|
||||
command:
|
||||
|
||||
```
|
||||
git clone https://github.com/andOrlando/rubato.git
|
||||
```
|
||||
|
||||
Then, whenever you actually want to use rubato, do this at the start of the lua file: `local rubato
|
||||
= require "lib.rubato"`
|
||||
|
||||
<h1 id="name">Why the name?</h1>
|
||||
|
||||
Because I play piano so this kinda links up with other stuff I do, and rubato really well fits the
|
||||
project. In music, it means "push and pull of tempo" basically, which really is what easing is all
|
||||
about in the first place. Plus, it'll be the first of my projects without garbage names
|
||||
("minesweperSweeper," "Latin Learning").
|
||||
|
||||
<h1 id="todo">Todo</h1>
|
||||
|
||||
- [ ] add `target` function, which rather than a set time has a set distance.
|
||||
- [x] improve intro and outro arguments (asserts, default values, proportional intros/outros)
|
||||
- [x] get a better name... (I have a cool name now!)
|
||||
- [x] make readme cooler
|
||||
- [x] have better documentation and add to luarocks
|
||||
- [x] remove gears dependency
|
||||
- [ ] only apply corrective coefficient to plateau
|
||||
- [x] Do `prop_intro` more intelligently so it doesn't have to do so many comparisons (done maybe kinda?)
|
||||
- [x] Make things like `abort` more useful
|
||||
- [x] Merge that pr by @Kasper so instant works
|
||||
- [x] Add a manager (this proceeds the above todo thing)
|
||||
- [ ] Make forall more useable and add tags and stuff
|
||||
- [ ] Fix that bug where you could set stuff manually (this might already be fixed I just haven't tested it)
|
||||
- [ ] Make is_instant even faster by just short circuiting `set`
|
40
home/.config/awesome/libs/rubato/easing.lua
Normal file
|
@ -0,0 +1,40 @@
|
|||
--- Linear easing (in quotes).
|
||||
local linear = {
|
||||
F = 0.5,
|
||||
easing = function(t) return t end
|
||||
}
|
||||
|
||||
--- Sublinear (?) easing.
|
||||
local zero = {
|
||||
F = 1,
|
||||
easing = function() return 1 end
|
||||
}
|
||||
|
||||
--- Quadratic easing.
|
||||
local quadratic = {
|
||||
F = 1/3,
|
||||
easing = function(t) return t * t end
|
||||
}
|
||||
|
||||
-- Okay look. It works. It's not terribly slow because computers can do math
|
||||
-- quick. The other one decidedly does not work (thanks sagemath, I trusted
|
||||
-- you...) so this will have to do. I may try to fix it up at some point, I may
|
||||
-- just leave it be and laugh to myself whenever I see this. As they say, if
|
||||
-- As they say, if you want something fixed that badly, make a pull request lol
|
||||
local bouncy = {
|
||||
F = (20*math.sqrt(3)*math.pi-30*math.log(2)-6147) /
|
||||
(10*(2*math.sqrt(3)*math.pi-6147*math.log(2))),
|
||||
easing = function(t) return
|
||||
(4096*math.pi*math.pow(2, 10*t-10)*math.cos(20/3*math.pi*t-43/6*math.pi)
|
||||
+6144*math.pow(2, 10*t-10)*math.log(2)*math.sin(20/3*math.pi*t-43/6*math.pi)
|
||||
+2*math.sqrt(3)*math.pi-3*math.log(2)) /
|
||||
(2*math.pi*math.sqrt(3)-6147*math.log(2))
|
||||
end
|
||||
}
|
||||
|
||||
return {
|
||||
linear = linear,
|
||||
zero = zero,
|
||||
quadratic = quadratic,
|
||||
bouncy = bouncy
|
||||
}
|
BIN
home/.config/awesome/libs/rubato/images/beautiful.gif
Normal file
After Width: | Height: | Size: 809 KiB |
BIN
home/.config/awesome/libs/rubato/images/connected_graph.png
Normal file
After Width: | Height: | Size: 7 KiB |
BIN
home/.config/awesome/libs/rubato/images/disconnected_graph.png
Normal file
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
BIN
home/.config/awesome/libs/rubato/images/normal_graph.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
home/.config/awesome/libs/rubato/images/quadratic_easing.gif
Normal file
After Width: | Height: | Size: 248 KiB |
BIN
home/.config/awesome/libs/rubato/images/slope_graph.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
home/.config/awesome/libs/rubato/images/trapezoid_easing.gif
Normal file
After Width: | Height: | Size: 97 KiB |
BIN
home/.config/awesome/libs/rubato/images/triangleish.png
Normal file
After Width: | Height: | Size: 6.4 KiB |
14
home/.config/awesome/libs/rubato/init.lua
Normal file
|
@ -0,0 +1,14 @@
|
|||
if not RUBATO_DIR then RUBATO_DIR = (...):match("(.-)[^%.]+$").."rubato." end
|
||||
if not RUBATO_MANAGER then RUBATO_MANAGER = require(RUBATO_DIR.."manager") end
|
||||
|
||||
return {
|
||||
--depreciated
|
||||
set_def_rate = function(rate) RUBATO_MANAGER.timed.defaults.rate = rate end,
|
||||
set_override_dt = function(value) RUBATO_MANAGER.timed.defaults.override_dt = value end,
|
||||
|
||||
--Modules
|
||||
timed = require(RUBATO_DIR.."timed"),
|
||||
easing = require(RUBATO_DIR.."easing"),
|
||||
subscribable = require(RUBATO_DIR.."subscribable"),
|
||||
manager = RUBATO_MANAGER,
|
||||
}
|
55
home/.config/awesome/libs/rubato/manager.lua
Normal file
|
@ -0,0 +1,55 @@
|
|||
if not RUBATO_DIR then RUBATO_DIR = (...):match("(.-)[^%.]+$") end
|
||||
|
||||
local easing = require(RUBATO_DIR.."easing")
|
||||
|
||||
local function make_props_immutable(table)
|
||||
setmetatable(table, {
|
||||
__index = function(self, key)
|
||||
if self._props[key] then return self._props[key]
|
||||
else return rawget(self, key) end
|
||||
end,
|
||||
__newindex = function(self, key, value)
|
||||
if self._props[key] then return
|
||||
else self._props[key] = value end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
local function manager()
|
||||
local obj = {_props = {}}
|
||||
make_props_immutable(obj)
|
||||
|
||||
obj._props.timeds = {}
|
||||
|
||||
obj._props.timed = {_props = {}}
|
||||
obj._props.timed._props.defaults = {
|
||||
duration = 1,
|
||||
pos = 0,
|
||||
prop_intro = false,
|
||||
intro = 0.2,
|
||||
easing = easing.linear,
|
||||
awestore_compat = false,
|
||||
log = function() end,
|
||||
override_simulate = false,
|
||||
override_dt = false,
|
||||
rate = 60,
|
||||
}
|
||||
make_props_immutable(obj.timed)
|
||||
obj._props.timed._props.override = {_props = {
|
||||
clear = function() for _, timed in pairs(obj.timeds) do timed:reset_values() end end,
|
||||
forall = function(func) for _, timed in pairs(obj.timeds) do func(timed) end end,
|
||||
}}
|
||||
|
||||
setmetatable(obj.timed.override, {
|
||||
__index = function(self, key) return self._props[key] end,
|
||||
__newindex = function(self, key, value)
|
||||
for _, timed in pairs(obj.timeds) do timed[key] = value end
|
||||
self._props[key] = value
|
||||
end
|
||||
})
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
if not RUBATO_MANAGER then RUBATO_MANAGER = manager() end
|
||||
return RUBATO_MANAGER
|
25
home/.config/awesome/libs/rubato/rubato-1.2-1.rockspec
Normal file
|
@ -0,0 +1,25 @@
|
|||
package = "rubato"
|
||||
version = "1.2-1"
|
||||
source = {
|
||||
url = "git+https://github.com/andOrlando/rubato.git"
|
||||
}
|
||||
description = {
|
||||
detailed = [[
|
||||
Create smooth animations based off of a slope curve for near perfect interruptions. Similar to awestore, but solely dedicated to interpolation. Also has a cool name. Check out the README on github for more informaiton. Has (basically) complete compatibility with awestore.
|
||||
|
||||
If not ran from awesomeWM, you must have lgi installed. Otherwise you're good
|
||||
]],
|
||||
homepage = "https://github.com/andOrlando/rubato",
|
||||
license = "MIT"
|
||||
}
|
||||
dependencies = {}
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
["rubato"] = "init.lua",
|
||||
["rubato.easing"] = "easing.lua",
|
||||
["rubato.timed"] = "timed.lua",
|
||||
["rubato.subscribable"] = "subscribable.lua",
|
||||
["rubato.manager"] = "manager.lua"
|
||||
}
|
||||
}
|
33
home/.config/awesome/libs/rubato/subscribable.lua
Normal file
|
@ -0,0 +1,33 @@
|
|||
-- Kidna copying awesotre's stores on a surface level for added compatibility
|
||||
local function subscribable(base)
|
||||
local obj = base or {}
|
||||
|
||||
obj._subscribed = {}
|
||||
|
||||
-- Subscrubes a function to the object so that it's called when `fire` is
|
||||
-- Calls subscribe_callback if it exists as well
|
||||
function obj:subscribe(func)
|
||||
local id = tostring(func):gsub("function: ", "")
|
||||
self._subscribed[id] = func
|
||||
|
||||
if self.subscribe_callback then self.subscribe_callback(func) end
|
||||
end
|
||||
|
||||
-- Unsubscribes a function and calls unsubscribe_callback if it exists
|
||||
function obj:unsubscribe(func)
|
||||
if not func then
|
||||
self._subscribed = {}
|
||||
else
|
||||
local id = tostring(func):gsub("function: ", "")
|
||||
self._subscribed[id] = nil
|
||||
end
|
||||
|
||||
if self.unsubscribe_callback then self.unsubscribe_callback(func) end
|
||||
end
|
||||
|
||||
function obj:fire(...) for _, func in pairs(self._subscribed) do func(...) end end
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
return subscribable
|
354
home/.config/awesome/libs/rubato/timed.lua
Normal file
|
@ -0,0 +1,354 @@
|
|||
if not RUBATO_DIR then RUBATO_DIR = (...):match("(.-)[^%.]+$") end
|
||||
if not RUBATO_MANAGER then RUBATO_MANAGER = require(RUBATO_DIR.."manager") end
|
||||
|
||||
local subscribable = require(RUBATO_DIR.."subscribable")
|
||||
local glib = require("lgi").GLib
|
||||
|
||||
--- Get the slope (this took me forever to find).
|
||||
-- i is intro duration
|
||||
-- o is outro duration
|
||||
-- t is total duration
|
||||
-- d is distance to cover
|
||||
-- F_1 is the value of the antiderivate at 1: F_1(1)
|
||||
-- F_2 is the value of the outro antiderivative at 1: F_2(1)
|
||||
-- b is the y-intercept
|
||||
-- m is the slope
|
||||
-- @see timed
|
||||
local function get_slope(i, o, t, d, F_1, F_2, b)
|
||||
return (d + i * b * (F_1 - 1)) / (i * (F_1 - 1) + o * (F_2 - 1) + t)
|
||||
end
|
||||
|
||||
--- Get the dx based off of a bunch of factors
|
||||
-- @see timed
|
||||
local function get_dx(time, duration, intro, intro_e, outro, outro_e, m, b)
|
||||
-- Intro math. Scales by difference between initial slope and target slope
|
||||
if time <= intro then
|
||||
return intro_e(time / intro) * (m - b) + b
|
||||
|
||||
-- Outro math
|
||||
elseif (duration - time) <= outro then
|
||||
return outro_e((duration - time) / outro) * m
|
||||
|
||||
-- Otherwise (it's in the plateau)
|
||||
else return m end
|
||||
end
|
||||
|
||||
--weak table for memoizing results
|
||||
local simulate_easing_mem = {}
|
||||
setmetatable(simulate_easing_mem, {__mode="kv"})
|
||||
|
||||
--- Simulates the easing to get the result to find an error coefficient
|
||||
-- Uses the coefficient to adjust dx so that it's guaranteed to hit the target
|
||||
-- This must be called when the sign of the target slope is changing
|
||||
-- @see timed
|
||||
local function simulate_easing(pos, duration, intro, intro_e, outro, outro_e, m, b, dt)
|
||||
local ps_time = 0
|
||||
local ps_pos = pos
|
||||
local dx
|
||||
print("simulating")
|
||||
|
||||
|
||||
-- Key for cacheing results
|
||||
local key = string.format("%f %f %f %s %f %s %f %f",
|
||||
pos, duration,
|
||||
intro, tostring(intro_e),
|
||||
outro, tostring(outro_e),
|
||||
m, b)
|
||||
|
||||
-- Short circuits if it's already done the calculation
|
||||
if simulate_easing_mem[key] then return simulate_easing_mem[key] end
|
||||
|
||||
-- Effectively runs the exact same code to find what the target will be
|
||||
while duration - ps_time >= dt / 2 do
|
||||
--increment time
|
||||
ps_time = ps_time + dt
|
||||
|
||||
--get dx, but use the pseudotime as to not mess with stuff
|
||||
dx = get_dx(ps_time, duration,
|
||||
intro, intro_e,
|
||||
outro, outro_e,
|
||||
m, b)
|
||||
|
||||
--increment pos by dx
|
||||
ps_pos = ps_pos + dx * dt
|
||||
end
|
||||
|
||||
simulate_easing_mem[key] = ps_pos
|
||||
return ps_pos
|
||||
end
|
||||
|
||||
--RUBATO_TIMEOUTS contains multiple timeouts for different rates
|
||||
--function creates timers which run in the background handling animations at distinct rates
|
||||
--this allows for rates to change dynamiclally during runtime should you wanna set everything's
|
||||
--rate to like 2fps for some reason or if you wanna switch from 144Hz to 60Hz or something
|
||||
--first index is normal timeouts, second index is override_dt timeouts
|
||||
if not RUBATO_TIMEOUTS then RUBATO_TIMEOUTS = {{}, {}} end
|
||||
|
||||
--create_timeout is called whenever a timer tries to start an animation and there's not a timeout
|
||||
--with the correct rate already in RUBATO_TIMEOUTS
|
||||
local function create_timeout(rate, override_dt)
|
||||
local time_last = glib.get_monotonic_time()
|
||||
local initial_dt = 1 / rate
|
||||
return glib.timeout_add(glib.PRIORITY_DEFAULT, initial_dt * 1000, function()
|
||||
|
||||
local dt = initial_dt
|
||||
if not override_dt then
|
||||
local time = (glib.get_monotonic_time() - time_last) / 1000000
|
||||
if time >= initial_dt * 1.05 then dt = time end --give it 5% moe
|
||||
end
|
||||
|
||||
for _, obj in pairs(RUBATO_MANAGER.timeds) do
|
||||
|
||||
if obj.rate == rate and obj.override_dt == override_dt and obj._time ~= obj.duration and not obj.pause then
|
||||
|
||||
--increment time
|
||||
obj._time = obj._time + dt
|
||||
|
||||
--get dx
|
||||
obj._dx = get_dx(obj._time, obj.duration,
|
||||
(obj._is_inter and obj.inter or obj.intro) * (obj.prop_intro and obj.duration or 1),
|
||||
obj._is_inter and obj.easing_inter.easing or obj.easing.easing,
|
||||
obj.outro * (obj.prop_intro and obj.duration or 1),
|
||||
obj.easing_outro.easing,
|
||||
obj._m, obj._b)
|
||||
|
||||
--increment pos by dx
|
||||
--scale by dt and correct with coef if necessary
|
||||
obj.pos = obj.pos + obj._dx * dt * obj._coef
|
||||
|
||||
--sets up when to stop by time
|
||||
--weirdness is to try to get as close to duration as possible
|
||||
if obj.duration - obj._time < dt / 2 --[[or obj.is_instant]] then
|
||||
obj.pos = obj._props.target --snaps to target in case of small error
|
||||
obj._time = obj.duration --snaps time to duration
|
||||
|
||||
obj._is_inter = false --resets intermittent
|
||||
|
||||
--run subscribed in functions
|
||||
--snap time to duration at end
|
||||
obj:fire(obj.pos, obj.duration, obj._dx)
|
||||
|
||||
-- awestore compatibility
|
||||
if obj.awestore_compat then obj.ended:fire(obj.pos, obj.duration, obj._dx) end
|
||||
|
||||
--otherwise it just fires normally
|
||||
else obj:fire(obj.pos, obj._time, obj._dx) end
|
||||
|
||||
end
|
||||
end
|
||||
time_last = glib.get_monotonic_time()
|
||||
return true
|
||||
end) end
|
||||
|
||||
--- INTERPOLATE. bam. it still ends in a period. But this one is timed.
|
||||
-- So documentation isn't super necessary here since it's all on the README and idk how to do
|
||||
-- documentation correctly, so please see the README or read the code to better understand how
|
||||
-- it works
|
||||
local function timed(args)
|
||||
|
||||
local obj = subscribable()
|
||||
|
||||
function obj:reset_values()
|
||||
--set up default arguments
|
||||
self.duration = args.duration or RUBATO_MANAGER.timed.defaults.duration
|
||||
self.pos = args.pos or RUBATO_MANAGER.timed.defaults.pos
|
||||
|
||||
self.prop_intro = args.prop_intro or RUBATO_MANAGER.timed.defaults.prop_intro
|
||||
|
||||
self.intro = args.intro or (RUBATO_MANAGER.timed.defaults.intro > self.duration * 0.5 and self.duration * 0.5 or RUBATO_MANAGER.timed.defaults.intro)
|
||||
self.inter = args.inter or args.intro
|
||||
|
||||
--set args.outro nicely based off how large args.intro is
|
||||
if self.intro > (self.prop_intro and 0.5 or self.duration) and not args.outro then
|
||||
self.outro = math.max((self.prop_intro and 1 or self.duration - self.intro), 0)
|
||||
|
||||
elseif not args.outro then self.outro = self.intro
|
||||
else self.outro = args.outro end
|
||||
|
||||
--assert that these values are valid
|
||||
--deal with 0.1+0.2!=0.3 somehow??
|
||||
assert(self.intro + self.outro <= self.duration or self.prop_intro, "Intro and Outro must be less than or equal to total duration")
|
||||
assert(self.intro + self.outro <= 1 or not self.prop_intro, "Proportional Intro and Outro must be less than or equal to 1")
|
||||
|
||||
self.easing = args.easing or RUBATO_MANAGER.timed.defaults.easing
|
||||
self.easing_outro = args.easing_outro or self.easing
|
||||
self.easing_inter = args.easing_inter or self.easing
|
||||
|
||||
--dev interface changes
|
||||
self.log = args.log or RUBATO_MANAGER.timed.defaults.log
|
||||
self.debug = args.debug
|
||||
self.awestore_compat = args.awestore_compat or RUBATO_MANAGER.timed.defaults.awestore_compat
|
||||
|
||||
--animation logic changes
|
||||
self.override_simulate = args.override_simulate or RUBATO_MANAGER.timed.defaults.override_simulate
|
||||
self.rapid_set = args.rapid_set == nil and self.awestore_compat or args.rapid_set
|
||||
self.is_instant = args.is_instant
|
||||
|
||||
-- hidden properties
|
||||
self._props = {
|
||||
target = self.pos,
|
||||
rate = args.rate or RUBATO_MANAGER.timed.defaults.rate,
|
||||
override_dt = args.override_dt or RUBATO_MANAGER.timed.defaults.override_dt
|
||||
}
|
||||
|
||||
end
|
||||
obj:reset_values()
|
||||
|
||||
-- awestore compatibility
|
||||
if obj.awestore_compat then
|
||||
obj._initial = obj.pos
|
||||
obj._last = 0
|
||||
|
||||
function obj:initial() return obj._initial end
|
||||
function obj:last() return obj._last end
|
||||
|
||||
obj.started = subscribable()
|
||||
obj.ended = subscribable()
|
||||
|
||||
end
|
||||
|
||||
-- Variables used in calculation, defined once bcz less operations
|
||||
obj._time = 0 -- current time
|
||||
obj._dt = 1 / obj._props.rate -- change in time
|
||||
obj._dt_index = obj._props.override_dt and 2 or 1 --index in RUBATO_TIMEOUTS
|
||||
obj._dx = 0 -- value of slope at current time
|
||||
obj._m = 0 -- slope
|
||||
obj._b = 0 -- y-intercept
|
||||
obj._is_inter = false --whether or not it's in an intermittent state
|
||||
|
||||
-- Variables used in simulation
|
||||
obj._ps_pos = 0 -- pseudoposition
|
||||
obj._coef = 1 -- corrective coefficient TODO: apply to plateau
|
||||
|
||||
-- Set target and begin interpolation
|
||||
local function set(value)
|
||||
|
||||
--if it's instant just do it lol, no need to go through all this
|
||||
if obj.is_instant then obj:fire(value, obj.duration, obj.pos - value); return end
|
||||
|
||||
--disallow setting it twice (because it makes it go wonky sometimes)
|
||||
if not obj.rapid_set and obj._props.target == value then return end
|
||||
|
||||
--animation values
|
||||
obj._time = 0 --resets time
|
||||
obj._coef = 1 --resets coefficient
|
||||
|
||||
--ensure that timer for specific rate exists, then set it
|
||||
if not RUBATO_TIMEOUTS[obj._dt_index][obj.rate] then RUBATO_TIMEOUTS[obj._dt_index][obj.rate] = create_timeout(obj.rate, obj.override_dt) end
|
||||
obj._dt = 1 / obj.rate
|
||||
|
||||
--does awestore compatibility
|
||||
if obj.awestore_compat then
|
||||
obj._last = value
|
||||
obj.started:fire(obj.pos, obj._time, obj._dx)
|
||||
end
|
||||
|
||||
-- if the animation is in motion (pos != target) reflect that in is_inter
|
||||
obj._is_inter = obj.pos ~= obj._props.target
|
||||
|
||||
--set initial position if interrupting another animation
|
||||
obj._b = obj._is_inter and obj._dx or 0
|
||||
|
||||
--get the slope of the plateau
|
||||
obj._m = get_slope((obj._is_inter and obj.inter or obj.intro) * (obj.prop_intro and obj.duration or 1),
|
||||
obj.outro * (obj.prop_intro and obj.duration or 1),
|
||||
obj.duration,
|
||||
value - obj.pos,
|
||||
obj._is_inter and obj.easing_inter.F or obj.easing.F,
|
||||
obj.easing_outro.F,
|
||||
obj._b)
|
||||
|
||||
--if it will make a mistake (or override_simulate is true), fix it
|
||||
--it should only make a mistake when switching direction
|
||||
--b ~= zero protection so that I won't get any NaNs (because NaN ~= NaN)
|
||||
if obj.override_simulate or (obj._b ~= 0 and obj._b / math.abs(obj._b) ~= obj._m / math.abs(obj._m)) then
|
||||
obj._ps_pos = simulate_easing(obj.pos, obj.duration,
|
||||
(obj._is_inter and obj.inter or obj.intro) * (obj.prop_intro and obj.duration or 1),
|
||||
obj._is_inter and obj.easing_inter.easing or obj.easing.easing,
|
||||
obj.outro * (obj.prop_intro and obj.duration or 1),
|
||||
obj.easing_outro.easing,
|
||||
obj._m, obj._b, obj._dt)
|
||||
|
||||
--get coefficient by calculating ratio of theoretical range : experimental range
|
||||
obj._coef = (obj.pos - value) / (obj.pos - obj._ps_pos)
|
||||
if obj._coef ~= obj._coef then obj._coef = 1 end --check for div by 0 resulting in NaN
|
||||
end
|
||||
|
||||
--set target, triggering timeout since pos != target
|
||||
obj._props.target = value --sets target
|
||||
|
||||
--finally, fire it once with initial values
|
||||
obj:fire(obj.pos, obj._time, obj._dx)
|
||||
|
||||
end
|
||||
|
||||
if obj.awestore_compat then function obj:set(target) set(target) end end
|
||||
|
||||
-- Functions for setting state
|
||||
-- Completely resets the timer
|
||||
-- this is more like an "abort" than a "reset" since I don't keep track of intiial position
|
||||
function obj:abort()
|
||||
obj._time = 0
|
||||
obj._props.target = obj.pos
|
||||
obj._dx = 0
|
||||
obj._m = nil
|
||||
obj._b = nil
|
||||
obj._is_inter = false
|
||||
obj._coef = 1
|
||||
obj._dt = 1 / obj.rate
|
||||
obj:fire(obj.pos, obj.time, obj._dx) --fire once to reset visually too
|
||||
end
|
||||
|
||||
--override to allow calling fire with no arguments
|
||||
local unpack = unpack or table.unpack
|
||||
function obj:fire(...) args = ({...})[1] and {...} or {obj.pos, obj._time, obj._dx}; for _, func in pairs(obj._subscribed) do func(unpack(args)) end end
|
||||
|
||||
--subscribe stuff initially and add callback
|
||||
obj.subscribe_callback = function(func) func(obj.pos, obj._time, obj._dt) end
|
||||
if args.subscribed ~= nil then obj:subscribe(args.subscribed) end
|
||||
|
||||
-- Metatable for cooler api
|
||||
local mt = {}
|
||||
function mt:__index(key)
|
||||
-- Returns the state value
|
||||
if key == "running" then
|
||||
if obj.pause then return false
|
||||
else return obj._props.target ~= obj.pos end
|
||||
|
||||
-- If it's in _props return it from props
|
||||
elseif self._props[key] then return self._props[key]
|
||||
|
||||
-- Otherwise just be nice
|
||||
else return rawget(self, key) end
|
||||
end
|
||||
function mt:__newindex(key, value)
|
||||
-- Don't allow for setting state
|
||||
if key == "running" then return
|
||||
|
||||
-- Changing target should call set
|
||||
elseif key == "target" then set(value) --set target
|
||||
|
||||
-- Changing rate should also update dt
|
||||
elseif key == "rate" then
|
||||
self._props.rate = value
|
||||
self._dt = 1 / value
|
||||
|
||||
-- Changing override_dt should also update dt_state
|
||||
elseif key == "override_dt" then
|
||||
self._props.override_dt = value
|
||||
self._dt_index = self._props.override_dt and 2 or 1
|
||||
-- If it's in _props set it there
|
||||
elseif self._props[key] ~= nil then self._props[key] = value
|
||||
|
||||
-- Otherwise just set it normally
|
||||
else rawset(self, key, value) end
|
||||
end
|
||||
|
||||
setmetatable(obj, mt)
|
||||
|
||||
table.insert(RUBATO_MANAGER.timeds, obj)
|
||||
return obj
|
||||
|
||||
end
|
||||
|
||||
return timed
|