Skip to content

Commit

Permalink
Update readme, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Nov 12, 2024
1 parent f7db46b commit cb20b2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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)
Expand Down Expand Up @@ -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:**

Expand All @@ -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)
```
Expand Down
33 changes: 15 additions & 18 deletions tweener/tweener.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)

Expand Down

0 comments on commit cb20b2b

Please sign in to comment.