Skip to content

Commit

Permalink
Update tweener functions, add tweener.cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Nov 12, 2024
1 parent e7988b0 commit f7db46b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ tweener.tween(go.EASING_OUTSINE, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)

tweener.tween({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, function(value, is_final_call)
local tween_id = tweener.tween({0, 0.2, 0.4, 0.8, 0.9, 1}, 0, 100, 1.5, function(value, is_final_call)
print("Tween value: " .. value)
end)
-- You can cancel the tween by calling tweener.cancel
tweener.cancel(tween_id)
```


Expand Down
26 changes: 21 additions & 5 deletions tweener/tweener.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ local TYPE_USERDATA = "userdata"

---@param easing number[]|vector @The array of easing values, Example: {0, 0.5, 1, 2, 1}. Must have at least two elements.
---@return easing_function @The easing function
local function get_custom_easing(easing)
local function get_custom_ease(easing)
local sample_count = #easing
if sample_count < 2 then
error("Easing table must have at least two elements.")
end

return function(t, b, c, d)
return M.custom_easing(easing, t, b, c, d)
return M.custom_ease(easing, t, b, c, d)
end
end

Expand All @@ -42,7 +42,7 @@ function M.tween(easing_function, from, to, time, callback, update_delta_time)
easing_function = M.DEFOLD_EASINGS[easing_function] or M[easing_function] or easing_function
local easing_type = type(easing_function)
if easing_type == TYPE_USERDATA or easing_type == TYPE_TABLE then
easing_function = get_custom_easing(easing_function --[[@as vector]])
easing_function = get_custom_ease(easing_function --[[@as vector]])
end

local time_elapsed = 0
Expand Down Expand Up @@ -86,13 +86,29 @@ function M.ease(easing_function, from, to, time, time_elapsed)
easing_function = M.DEFOLD_EASINGS[easing_function] or M[easing_function] or easing_function
local easing_type = type(easing_function)
if easing_type == TYPE_USERDATA or easing_type == TYPE_TABLE then
return M.custom_easing(easing_function, time_elapsed, from, to - from, time)
---@cast easing_function number[]
return M.custom_ease(easing_function, time_elapsed, from, to - from, time)
end
return easing_function(time_elapsed, from, to - from, time)
end


function M.custom_easing(easing, t, b, c, d)
---Cancel a previous running tween.
---@param tween_id hash the tween handle returned by `tween` function
---@return boolean true if the tween was active, false if the tween is already cancelled / complete
function M.cancel(tween_id)
return timer.cancel(tween_id)
end


---@private
---@param easing number[] @The array of easing values, Example: {0, 0.5, 1, 2, 1}. Must have at least two elements.
---@param t number @current time [0 .. t - from]
---@param b number @beginning value
---@param c number @change in value
---@param d number @duration
---@return number @The result of easing
function M.custom_ease(easing, t, b, c, d)
if d == 0 then
error("Division by zero: 'd' must not be zero.")
end
Expand Down

0 comments on commit f7db46b

Please sign in to comment.