master
GianptDev 2022-08-21 16:51:03 +02:00
parent bbe514dfb7
commit 6ea1b61892
5 changed files with 444 additions and 414 deletions

View File

@ -47,3 +47,5 @@
- creating icons to give the api a cooler look.
- - Inspirations:
- - https://github.com/godotengine/godot/blob/master/editor/icons/Tween.svg
- converted every function declaration in a more friendly way.

View File

@ -31,7 +31,7 @@ minetest.register_chatcommand(
return true
end
return true, "BeTween Api v1.1.1"
return true, "BeTween Api v1.2 (wip)"
end
}
)

View File

@ -13,181 +13,185 @@ BeTweenApi.debug = {
--- list of all players that are using the debug tween view.
hud_running_tweens = {},
}
--- show the list of all easing functions to this player.
--- @param player_name string
show_functions = function (_, player_name)
-- hud already enabled.
if (_.hud_interpolation[player_name] ~= nil) then
return
end
--- show the list of all easing functions to this player.
--- @param player_name string
function BeTweenApi.debug.show_functions (_, player_name)
local player = minetest.get_player_by_name(player_name)
local index = 0
local start, finish = 32, 256
local scale = { x = 2, y = 2}
-- hud already enabled.
if (_.hud_interpolation[player_name] ~= nil) then
return
end
local visual = {
tweens = {}, -- contain list of interpolation name and hud items used.
}
local player = minetest.get_player_by_name(player_name)
local index = 0
local start, finish = 32, 256
local scale = { x = 2, y = 2}
_.hud_interpolation[player_name] = visual
local visual = {
tweens = {}, -- contain list of interpolation name and hud items used.
}
for _, interpolation in pairs(BeTweenApi.interpolation) do
local y = 64 + (24 * index)
_.hud_interpolation[player_name] = visual
local start_icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:10x10^[colorize:#0000ff",
scale = scale,
offset = { x = start, y = y },
position = { x = 0, y = 0 },
alignment = { x = 0, y = 0 },
})
for _, interpolation in pairs(BeTweenApi.interpolation) do
local y = 64 + (24 * index)
local stop_icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:10x10^[colorize:#0000ff",
scale = scale,
offset = { x = finish, y = y },
position = { x = 0, y = 0 },
alignment = { x = 0, y = 0 },
})
local icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:6x6^[colorize:#ff0000",
scale = scale,
offset = { x = 32, y = y },
position = { x = 0, y = 0 },
alignment = { x = 0, y = 0 },
})
local title = player:hud_add({
hud_elem_type = "text",
text = tostring(_),
position = { x = 0, y = 0 },
offset = { x = finish + 32, y = y },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
})
visual.tweens[_] = BeTweenApi.tween(
interpolation,
{ start, finish },
4.0,
true,
{
on_step = function (step, tween)
local item = player:hud_get(icon)
player:hud_change(icon, "offset", { x = step, y = item.offset.y })
end,
on_stop = function (tween)
player:hud_remove(start_icon)
player:hud_remove(stop_icon)
player:hud_remove(icon)
player:hud_remove(title)
end,
}
)
index = index + 1
end
--- make the cool title
visual.title = player:hud_add({
hud_elem_type = "text",
text = string.format("BeTween Api Debug : Easing functions : %d", index + 1),
local start_icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:10x10^[colorize:#0000ff",
scale = scale,
offset = { x = start, y = y },
position = { x = 0, y = 0 },
offset = { x = start, y = 32 },
alignment = { x = 0, y = 0 },
})
local stop_icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:10x10^[colorize:#0000ff",
scale = scale,
offset = { x = finish, y = y },
position = { x = 0, y = 0 },
alignment = { x = 0, y = 0 },
})
local icon = player:hud_add({
hud_elem_type = "image",
text = "^[resize:6x6^[colorize:#ff0000",
scale = scale,
offset = { x = 32, y = y },
position = { x = 0, y = 0 },
alignment = { x = 0, y = 0 },
})
local title = player:hud_add({
hud_elem_type = "text",
text = tostring(_),
position = { x = 0, y = 0 },
offset = { x = finish + 32, y = y },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
})
--- start all tweens
for _, tween in pairs(visual.tweens) do
tween:start()
end
end,
visual.tweens[_] = BeTweenApi.tween(
interpolation,
{ start, finish },
4.0,
true,
{
on_step = function (step, tween)
local item = player:hud_get(icon)
player:hud_change(icon, "offset", { x = step, y = item.offset.y })
end,
--- hide the list of all defined easing functions from this player.
--- @param player_name string
hide_functions = function (_, player_name)
on_stop = function (tween)
player:hud_remove(start_icon)
player:hud_remove(stop_icon)
player:hud_remove(icon)
player:hud_remove(title)
end,
}
)
-- hud already disabled.
if (_.hud_interpolation[player_name] == nil) then
return
end
local player = minetest.get_player_by_name(player_name)
player:hud_remove(_.hud_interpolation[player_name].title)
for _, tween in pairs(_.hud_interpolation[player_name].tweens) do
tween:stop()
end
_.hud_interpolation[player_name] = nil
end,
--- show the list of all active tweens to this player.
--- @param player_name string
show_tweens = function (_, player_name)
-- hud already enabled.
if (_.hud_running_tweens[player_name] ~= nil) then
return
end
local player = minetest.get_player_by_name(player_name)
_.hud_running_tweens[player_name] = {
title = player:hud_add({
hud_elem_type = "text",
text = "BeTween Api Debug : Tweens",
position = { x = 0.5, y = 0 },
offset = { x = 0, y = 32 },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
}),
tween_title = player:hud_add({
hud_elem_type = "text",
position = { x = 0.5, y = 0 },
offset = { x = 0, y = 64 },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
}),
update = {},
}
end,
--- hide the list of all active tweens from this player.
--- @param player_name string
hide_tweens = function (_, player_name)
local visual = _.hud_running_tweens[player_name]
-- hud already disabled.
if (visual == nil) then
return
end
local player = minetest.get_player_by_name(player_name)
player:hud_remove(visual.title)
player:hud_remove(visual.tween_title)
for _, id in pairs(visual.update) do
player:hud_remove(id)
end
_.hud_running_tweens[player_name] = nil
index = index + 1
end
}
--- make the cool title
visual.title = player:hud_add({
hud_elem_type = "text",
text = string.format("BeTween Api Debug : Easing functions : %d", index + 1),
position = { x = 0, y = 0 },
offset = { x = start, y = 32 },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
})
--- start all tweens
for _, tween in pairs(visual.tweens) do
tween:start()
end
end
--- hide the list of all defined easing functions from this player.
--- @param player_name string
function BeTweenApi.debug.hide_functions (_, player_name)
-- hud already disabled.
if (_.hud_interpolation[player_name] == nil) then
return
end
local player = minetest.get_player_by_name(player_name)
player:hud_remove(_.hud_interpolation[player_name].title)
for _, tween in pairs(_.hud_interpolation[player_name].tweens) do
tween:stop()
end
_.hud_interpolation[player_name] = nil
end
--- show the list of all active tweens to this player.
--- @param player_name string
function BeTweenApi.debug.show_tweens (_, player_name)
-- hud already enabled.
if (_.hud_running_tweens[player_name] ~= nil) then
return
end
local player = minetest.get_player_by_name(player_name)
_.hud_running_tweens[player_name] = {
title = player:hud_add({
hud_elem_type = "text",
text = "BeTween Api Debug : Tweens",
position = { x = 0.5, y = 0 },
offset = { x = 0, y = 32 },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
}),
tween_title = player:hud_add({
hud_elem_type = "text",
position = { x = 0.5, y = 0 },
offset = { x = 0, y = 64 },
alignment = { x = 1, y = 0 },
number = 0xFFFFFF,
style = 1,
}),
update = {},
}
end
--- hide the list of all active tweens from this player.
--- @param player_name string
function BeTweenApi.debug.hide_tweens (_, player_name)
local visual = _.hud_running_tweens[player_name]
-- hud already disabled.
if (visual == nil) then
return
end
local player = minetest.get_player_by_name(player_name)
player:hud_remove(visual.title)
player:hud_remove(visual.tween_title)
for _, id in pairs(visual.update) do
player:hud_remove(id)
end
_.hud_running_tweens[player_name] = nil
end
minetest.register_globalstep(

View File

@ -9,192 +9,205 @@
interpolation.ease_in(0, 8, 0.5) -- 2
interpolation.ease_out(0, 8, 0.5) -- 4.5
]]
BeTweenApi.interpolation = {
--- straight increment from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
linear = function (x, y, t)
return x + t * (y - x)
end,
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quadratic_in = function (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 2)
end,
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quadratic_out = function (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t * (2.0 - t))
end,
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quadratic_in_out = function (x, y, t)
x = BeTweenApi.interpolation.quadratic_in(x, y, t)
y = BeTweenApi.interpolation.quadratic_out(x, y, t)
return BeTweenApi.interpolation.linear(x, y, t)
end,
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
cubic_in = function (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 3)
end,
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
cubic_out = function (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, 1.0 + (t ^ 3))
end,
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
cubic_in_out = function (x, y, t)
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 3))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 3 + 2.0))
end,
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quartic_in = function (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 4)
end,
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quartic_out = function (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, 1.0 - (t ^ 4))
end,
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
quartic_in_out = function (x, y, t)
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 4))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, -0.5 * (t ^ 4 - 2.0))
end,
--- movement jump from x to y like an elastic, movement is very fast but slow down in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
elastic = function (x, y, t)
t = (2.0 ^ -(10.0 * t) * math.sin((t - 0.1) * (2.0 * math.pi) / 0.4)) + 1.0
return BeTweenApi.interpolation.linear(x, y, t)
end,
--- movement increment faster from x to y in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
sinusoidal_in = function (x, y, t)
t = 1.0 - math.cos(t * math.pi / 2.0)
return BeTweenApi.interpolation.linear(x, y, t)
end,
--- movement increment faster from x to y in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
sinusoidal_out = function (x, y, t)
t = math.sin(t * math.pi / 2.0)
return BeTweenApi.interpolation.linear(x, y, t)
end,
--- movement is faster between x to y but slow down on begin and end in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
sinusoidal_in_out = function (x, y, t)
t = 0.5 * (1.0 - math.cos(math.pi * t))
return BeTweenApi.interpolation.linear(x, y, t)
end,
BeTweenApi.interpolation = {}
--- movement increment faster esponentially from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
circular_in = function (x, y, t)
t = 1.0 - math.sqrt(1.0 - t * t)
return BeTweenApi.interpolation.linear(x, y, t)
end,
--- straight increment from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.linear (x, y, t)
return x + t * (y - x)
end
--- movement increment slower esponentially from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
circular_out = function (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, math.sqrt(1.0 - (t * t)))
end,
--- movement increment slower from the start to the end but speed up esponentially in the middle.
--- @param x number
--- @param y number
--- @param t number
--- @return number
circular_in_out = function (x, y, t)
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quadratic_in (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 2)
end
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, -0.5 * (math.sqrt(1.0 - t * t) - 1.0))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, 0.5 * (math.sqrt(1.0 - t * t) + 1.0))
end,
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quadratic_out (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t * (2.0 - t))
end
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quadratic_in_out (x, y, t)
x = BeTweenApi.interpolation.quadratic_in(x, y, t)
y = BeTweenApi.interpolation.quadratic_out(x, y, t)
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.cubic_in (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 3)
end
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.cubic_out (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, 1.0 + (t ^ 3))
end
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.cubic_in_out (x, y, t)
}
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 3))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 3 + 2.0))
end
--- movement increment faster from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quartic_in (x, y, t)
return BeTweenApi.interpolation.linear(x, y, t ^ 4)
end
--- movement increment slower from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quartic_out (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, 1.0 - (t ^ 4))
end
--- movement is faster between x to y but slow down on begin and end in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.quartic_in_out (x, y, t)
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, 0.5 * (t ^ 4))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, -0.5 * (t ^ 4 - 2.0))
end
--- movement jump from x to y like an elastic, movement is very fast but slow down in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.elastic (x, y, t)
t = (2.0 ^ -(10.0 * t) * math.sin((t - 0.1) * (2.0 * math.pi) / 0.4)) + 1.0
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement increment faster from x to y in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.sinusoidal_in (x, y, t)
t = 1.0 - math.cos(t * math.pi / 2.0)
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement increment faster from x to y in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.sinusoidal_out (x, y, t)
t = math.sin(t * math.pi / 2.0)
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement is faster between x to y but slow down on begin and end in time, the movement has the shape of a curve.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.sinusoidal_in_out (x, y, t)
t = 0.5 * (1.0 - math.cos(math.pi * t))
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement increment faster esponentially from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.circular_in (x, y, t)
t = 1.0 - math.sqrt(1.0 - t * t)
return BeTweenApi.interpolation.linear(x, y, t)
end
--- movement increment slower esponentially from x to y in time.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.circular_out (x, y, t)
t = t - 1.0
return BeTweenApi.interpolation.linear(x, y, math.sqrt(1.0 - (t * t)))
end
--- movement increment slower from the start to the end but speed up esponentially in the middle.
--- @param x number
--- @param y number
--- @param t number
--- @return number
function BeTweenApi.interpolation.circular_in_out (x, y, t)
t = t * 2
if (t < 1.0) then
return BeTweenApi.interpolation.linear(x, y, -0.5 * (math.sqrt(1.0 - t * t) - 1.0))
end
t = t - 2.0
return BeTweenApi.interpolation.linear(x, y, 0.5 * (math.sqrt(1.0 - t * t) + 1.0))
end

View File

@ -22,7 +22,7 @@ BeTweenApi.active_tweens = {}
--- @param loop boolean | integer false
--- @param callbacks table nil
--- @return table | nil
BeTweenApi.tween = function (method, movement, time, loop, callbacks)
function BeTweenApi.tween (method, movement, time, loop, callbacks)
-- movement require two values, the initial position and the final position.
if ((movement[1] == nil) or (movement[2] == nil)) then
@ -69,81 +69,92 @@ BeTweenApi.tween = function (method, movement, time, loop, callbacks)
--- @type function | nil
on_loop = callbacks.on_loop,
-- start the tween to work by adding it from the active list.
start = function (_)
if (_:is_running() == true) then
minetest.log("action", string.format("Tried to start tween '%p' twince.", _))
return
end
table.insert(BeTweenApi.active_tweens, _)
minetest.log("action", string.format("Tween '%d' ~ '%p' is now running.", _:index(), _))
if (_.on_start ~= nil) then
_.on_start(_)
end
end,
--- stop the tween to work by removing it from the active list.
---
--- @param reset boolean false
stop = function (_, reset)
local index = _:index()
if (reset == nil) then
reset = false
end
if (index == nil) then
minetest.log("action", string.format("Tried to stop tween '%p' wich wasn't running.", _))
return
end
if (reset == true) then
_.timer = 0
end
table.remove(BeTweenApi.active_tweens, index)
minetest.log("action", string.format("Tween '%p' has been stopped.", _))
if (_.on_stop ~= nil) then
_.on_stop(_)
end
end,
--- check if the tween is currently running.
--- @return boolean
is_running = function (_)
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return true
end
end
return false
end,
--- get the running index of the tween, if the tween isn't running nil is returned.
--- @return integer | nil
index = function (_)
local index = 1
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return index
end
index = index + 1
end
return nil
end,
}
--- -----------------------------------------------------------
--- tween methods here.
-- start the tween to work by adding it from the active list.
function tween.start (_)
if (_:is_running() == true) then
minetest.log("action", string.format("Tried to start tween '%p' twince.", _))
return
end
table.insert(BeTweenApi.active_tweens, _)
minetest.log("action", string.format("Tween '%d' ~ '%p' is now running.", _:index(), _))
if (_.on_start ~= nil) then
_.on_start(_)
end
end
--- stop the tween to work by removing it from the active list.
---
--- @param reset boolean false
function tween.stop (_, reset)
local index = _:index()
if (reset == nil) then
reset = false
end
if (index == nil) then
minetest.log("action", string.format("Tried to stop tween '%p' wich wasn't running.", _))
return
end
if (reset == true) then
_.timer = 0
end
table.remove(BeTweenApi.active_tweens, index)
minetest.log("action", string.format("Tween '%p' has been stopped.", _))
if (_.on_stop ~= nil) then
_.on_stop(_)
end
end
--- check if the tween is currently running.
--- @return boolean
function tween.is_running (_)
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return true
end
end
return false
end
--- get the running index of the tween, if the tween isn't running nil is returned.
--- @return integer | nil
function tween.index (_)
local index = 1
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return index
end
index = index + 1
end
return nil
end
--- -----------------------------------------------------------
minetest.log("action", string.format("New tween '%p' created.", tween))
return tween
end