From cb20b2ba262c2a3100b37b5ef9ec2a0a7d3e331e Mon Sep 17 00:00:00 2001 From: Insality Date: Tue, 12 Nov 2024 21:01:05 +0100 Subject: [PATCH] Update readme, code cleanup --- README.md | 8 +++++--- tweener/tweener.lua | 33 +++++++++++++++------------------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index db3405e..b901e26 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ https://github.com/Insality/defold-tweener/archive/refs/tags/3.zip ### Global Update Frequency -Optionally, you can setup global default update freqency in your game.project. It's `60` by default. +Optionally, you can setup global default update frequency in your game.project. It's `60` by default. Add next `tweener` section to your `game.project` in text mode: @@ -57,7 +57,8 @@ update_frequency = 60 ### Quick API Reference ```lua -local tween_function = tweener.linear or go.EASING_LINEAR or gui.EASING_LINEAR or {0, 0.2, 0.4, 0.8, 0.9, 1} +-- Tween function can be a string, a predefined easing function, or a custom easing function +local tween_function = "linear" or tweener.linear or go.EASING_LINEAR or gui.EASING_LINEAR or {0, 0.2, 0.4, 0.8, 0.9, 1} tweener.tween(tween_function, from, to, time, callback, [dt]) tweener.ease(tween_function, from, to, time, time_elapsed) @@ -92,7 +93,7 @@ This function initiates a tween operation immediately. Here's how to use it: - `dt` (optional): The time interval for updating the tween, in seconds. - **Return Value:** - - `timer_id`: A timer id from `timer.delay` if you wish to cancel the tween. + - `tweener_id`: A tweener id from `timer.delay` if you wish to cancel the tween. - **Usage Example:** @@ -108,6 +109,7 @@ end) 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) ``` diff --git a/tweener/tweener.lua b/tweener/tweener.lua index b5e1377..409b1d4 100644 --- a/tweener/tweener.lua +++ b/tweener/tweener.lua @@ -3,7 +3,7 @@ local UPDATE_FREQUENCY = sys.get_config_int("tweener.update_frequency", 60) ---@class tweener local M = {} ----@alias easing_function (fun(current: number, from: number, to: number, time: number): number)|constant|number[] +---@alias easing_function (fun(current: number, from: number, to: number, time: number): number)|string|constant|number[] ---@class hash ---@class constant @@ -13,20 +13,6 @@ local TYPE_TABLE = "table" 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_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_ease(easing, t, b, c, d) - end -end - - ---Starts a tweening operation. ---@param easing_function easing_function ---@param from number @@ -42,7 +28,12 @@ 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_ease(easing_function --[[@as vector]]) + ---@cast easing_function number[] + local custom_easing = function(t, b, c, d) + return M.custom_ease(easing_function, t, b, c, d) + end + + easing_function = custom_easing end local time_elapsed = 0 @@ -89,6 +80,7 @@ function M.ease(easing_function, from, to, time, time_elapsed) ---@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 @@ -113,15 +105,20 @@ function M.custom_ease(easing, t, b, c, d) error("Division by zero: 'd' must not be zero.") end + local sample_count = #easing + if sample_count < 2 then + error("Easing table must have at least two elements.") + end + local time_progress = t / d if time_progress >= 1 then - return (c + b) * easing[#easing] + return (c + b) * easing[sample_count] end if time_progress <= 0 then return b * easing[1] end - local sample_index = #easing - 1 + local sample_index = sample_count - 1 local index1 = math.floor(time_progress * sample_index) local index2 = math.min(index1 + 1, sample_index)