noble engine migration
This commit is contained in:
81
source/libraries/noble/utilities/Ease.lua
Normal file
81
source/libraries/noble/utilities/Ease.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
--- Extensions to `playdate.easingFunctions`, aliased as `Ease` in Noble Engine.
|
||||
-- See the Playdate SDK for a list of easing functions.
|
||||
-- @module Ease
|
||||
|
||||
local componentFunctions = {
|
||||
[Ease.inOutQuad] = { enter = Ease.inQuad, exit = Ease.outQuad },
|
||||
[Ease.inOutCubic] = { enter = Ease.inCubic, exit = Ease.outCubic },
|
||||
[Ease.inOutQuart] = { enter = Ease.inQuart, exit = Ease.outQuart },
|
||||
[Ease.inOutQuint] = { enter = Ease.inQuint, exit = Ease.outQuint },
|
||||
[Ease.inOutSine] = { enter = Ease.inSine, exit = Ease.outSine },
|
||||
[Ease.inOutExpo] = { enter = Ease.inExpo, exit = Ease.outExpo },
|
||||
[Ease.inOutCirc] = { enter = Ease.inCirc, exit = Ease.outCirc },
|
||||
[Ease.inOutElastic] = { enter = Ease.inElastic, exit = Ease.outElastic },
|
||||
[Ease.inOutBack] = { enter = Ease.inBack, exit = Ease.outBack },
|
||||
[Ease.inOutBounce] = { enter = Ease.inBounce, exit = Ease.outBounce },
|
||||
[Ease.outInQuad] = { enter = Ease.outQuad, exit = Ease.inQuad },
|
||||
[Ease.outInCubic] = { enter = Ease.outCubic, exit = Ease.inCubic },
|
||||
[Ease.outInQuart] = { enter = Ease.outQuart, exit = Ease.inQuart },
|
||||
[Ease.outInQuint] = { enter = Ease.outQuint, exit = Ease.inQuint },
|
||||
[Ease.outInSine] = { enter = Ease.outSine, exit = Ease.inSine },
|
||||
[Ease.outInExpo] = { enter = Ease.outExpo, exit = Ease.inExpo },
|
||||
[Ease.outInCirc] = { enter = Ease.outCirc, exit = Ease.inCirc },
|
||||
[Ease.outInElastic] = { enter = Ease.outElastic, exit = Ease.inElastic },
|
||||
[Ease.outInBack] = { enter = Ease.outBack, exit = Ease.inBack },
|
||||
[Ease.outInBounce] = { enter = Ease.outBounce, exit = Ease.inBounce },
|
||||
[Ease.linear] = { enter = Ease.linear, exit = Ease.linear }
|
||||
}
|
||||
|
||||
local reverseFunctions = {
|
||||
[Ease.inQuad] = Ease.outQuad,
|
||||
[Ease.inCubic] = Ease.outCubic,
|
||||
[Ease.inQuart] = Ease.outQuart,
|
||||
[Ease.inQuint] = Ease.outQuint,
|
||||
[Ease.inSine] = Ease.outSine,
|
||||
[Ease.inExpo] = Ease.outExpo,
|
||||
[Ease.inCirc] = Ease.outCirc,
|
||||
[Ease.inElastic] = Ease.outElastic,
|
||||
[Ease.inBack] = Ease.outBack,
|
||||
[Ease.inBounce] = Ease.outBounce,
|
||||
[Ease.outQuad] = Ease.inQuad,
|
||||
[Ease.outCubic] = Ease.inCubic,
|
||||
[Ease.outQuart] = Ease.inQuart,
|
||||
[Ease.outQuint] = Ease.inQuint,
|
||||
[Ease.outSine] = Ease.inSine,
|
||||
[Ease.outExpo] = Ease.inExpo,
|
||||
[Ease.outCirc] = Ease.inCirc,
|
||||
[Ease.outElastic] = Ease.inElastic,
|
||||
[Ease.outBack] = Ease.inBack,
|
||||
[Ease.outBounce] = Ease.inBounce,
|
||||
[Ease.linear] = Ease.linear
|
||||
}
|
||||
|
||||
--- Returns the first half of an "inOut" or "outIn" easing function.
|
||||
-- Returns `nil` for any easing function that isn't in the form of `Ease.inOutXxxx` or `Ease.outInXxxx`. `Ease.linear` returns itself.
|
||||
-- @usage
|
||||
-- local ease = Ease.outInQuad
|
||||
-- local easeEnter = Ease.enter(ease) -- Returns "Ease.outQuad"
|
||||
function Ease.enter(__easingFunction)
|
||||
if (componentFunctions[__easingFunction] == nil) then return nil end
|
||||
return componentFunctions[__easingFunction].enter
|
||||
end
|
||||
|
||||
--- Returns the second half of an "inOut" or "outIn" easing function.
|
||||
-- Returns `nil` for any easing function that isn't in the form of `Ease.inOutXxxx` or `Ease.outInXxxx`. `Ease.linear` returns itself.
|
||||
-- @usage
|
||||
-- local ease = Ease.outInQuad
|
||||
-- local easeExit = Ease.exit(ease) -- Returns "Ease.inQuad"
|
||||
function Ease.exit(__easingFunction)
|
||||
if (componentFunctions[__easingFunction] == nil) then return nil end
|
||||
return componentFunctions[__easingFunction].exit
|
||||
end
|
||||
|
||||
--- Returns the reverse function of the provided function.
|
||||
-- Returns `nil` for any easing function that isn't in the form of `Ease.inXxxx` or `Ease.outXxxx`. `Ease.linear` returns itself.
|
||||
-- @usage
|
||||
-- local ease = Ease.inQuad
|
||||
-- local reverseEase = Ease.reverse(ease) -- Returns "Ease.outQuad"
|
||||
function Ease.reverse(__easingFunction)
|
||||
if (reverseFunctions[__easingFunction] == nil) then return nil end
|
||||
return reverseFunctions[__easingFunction]
|
||||
end
|
197
source/libraries/noble/utilities/Utilities.lua
Normal file
197
source/libraries/noble/utilities/Utilities.lua
Normal file
@@ -0,0 +1,197 @@
|
||||
--
|
||||
-- Utilities.lua
|
||||
--
|
||||
-- Useful helper functions and API extensions.
|
||||
-- Place your own utilities in "utilities/Utilities.lua"
|
||||
--
|
||||
|
||||
Utilities = {}
|
||||
|
||||
function Utilities.copy(obj, seen)
|
||||
if type(obj) ~= 'table' then return obj end
|
||||
if seen and seen[obj] then return seen[obj] end
|
||||
local s = seen or {}
|
||||
local res = setmetatable({}, getmetatable(obj))
|
||||
s[obj] = res
|
||||
for k, v in pairs(obj) do res[Utilities.copy(k, s)] = Utilities.copy(v, s) end
|
||||
return res
|
||||
end
|
||||
|
||||
function Utilities.isNull(__object)
|
||||
return __object == nil or __object == ''
|
||||
end
|
||||
function Utilities.isNotNull(__object)
|
||||
return __object ~= nil and __object ~= ''
|
||||
end
|
||||
|
||||
function Utilities.startsWith(__string, __start)
|
||||
return __string:sub(1, #__start) == __start
|
||||
end
|
||||
|
||||
function Utilities.endsWith(__string, __ending)
|
||||
return __ending == "" or __string:sub(-#__ending) == __ending
|
||||
end
|
||||
|
||||
function Utilities.handleOptionalBoolean(__argument, __default)
|
||||
if (__argument == nil) then
|
||||
return __default
|
||||
elseif (__argument == true or __argument == false) then
|
||||
return __argument
|
||||
else
|
||||
error("BONK: You shouldn’t pass non-boolean value as a boolean parameter.")
|
||||
return __argument
|
||||
end
|
||||
end
|
||||
|
||||
Utilities.varName = {}
|
||||
setmetatable(
|
||||
Utilities.varName,
|
||||
{
|
||||
__index = function(self, __key, __value)
|
||||
return string.format('%s', __key)
|
||||
end
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
function Utilities.getHypotenuse(__x,__y)
|
||||
return math.sqrt((__x*__x)+(__y*__y))
|
||||
end
|
||||
|
||||
function Utilities.average(__a, __b)
|
||||
return (__a + __b) / 2
|
||||
end
|
||||
|
||||
function Utilities.screenshot()
|
||||
return Graphics.getDisplayImage()
|
||||
end
|
||||
|
||||
function Utilities.autoTable(__numberOfDimensions)
|
||||
local metatable = {};
|
||||
for i = 1, __numberOfDimensions do
|
||||
metatable[i] = {
|
||||
__index = function(__table, __key)
|
||||
if i < __numberOfDimensions then
|
||||
__table[__key] = setmetatable({}, metatable[i+1])
|
||||
return __table[__key];
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
return setmetatable({}, metatable[1]);
|
||||
end
|
||||
|
||||
function Utilities.newUUID()
|
||||
local func = function(x)
|
||||
local random = math.random(16) - 1
|
||||
random = (x == "x") and (random + 1) or (random % 4) + 9
|
||||
return ("0123456789abcdef"):sub(random, random)
|
||||
end
|
||||
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", func))
|
||||
end
|
||||
|
||||
|
||||
-- New math methods
|
||||
--
|
||||
|
||||
function math.clamp(__value, __min, __max)
|
||||
if (__min > __max) then
|
||||
__min, __max = __max, __min
|
||||
end
|
||||
return math.max(__min, math.min(__max, __value))
|
||||
end
|
||||
|
||||
function math.ring(__value, __min, __max)
|
||||
if (__min > __max) then
|
||||
__min, __max = __max, __min
|
||||
end
|
||||
return __min + (__value - __min) % (__max - __min)
|
||||
end
|
||||
|
||||
function math.ringInt(__value, __min, __max)
|
||||
return math.ring(__value, __min, __max + 1)
|
||||
end
|
||||
|
||||
function math.approach(_value, __target, __step)
|
||||
if (_value == __target) then
|
||||
return _value, true
|
||||
end
|
||||
local d = __target - _value
|
||||
if (d > 0) then
|
||||
_value = _value + __step
|
||||
if (_value >= __target) then
|
||||
return __target, true
|
||||
else
|
||||
return _value, false
|
||||
end
|
||||
elseif (d < 0) then
|
||||
_value = _value - __step
|
||||
if (_value <= __target) then
|
||||
return __target, true
|
||||
else
|
||||
return _value, false
|
||||
end
|
||||
else
|
||||
return _value, true
|
||||
end
|
||||
end
|
||||
|
||||
function math.infiniteApproach(at_zero, at_infinite, x_halfway, x)
|
||||
return at_infinite - (at_infinite - at_zero) * 0.5 ^ (x / x_halfway)
|
||||
end
|
||||
|
||||
function math.round(__value, __bracket)
|
||||
local bracket = __bracket or 1
|
||||
return math.floor(__value/bracket + math.sign(__value) * 0.5) * bracket
|
||||
end
|
||||
|
||||
function math.sign(__value)
|
||||
return (__value >= 0 and 1) or -1
|
||||
end
|
||||
|
||||
function math.lerp(a, b, t)
|
||||
return a + (b - a) * t
|
||||
end
|
||||
|
||||
-- New array/table methods
|
||||
--
|
||||
|
||||
function table.merge(__table1, __table2)
|
||||
local mergedTable = {}
|
||||
for k,v in pairs(__table1) do
|
||||
mergedTable[k] = v
|
||||
end
|
||||
for k,v in pairs(__table2) do
|
||||
mergedTable[k] = v
|
||||
end
|
||||
return mergedTable
|
||||
end
|
||||
|
||||
function table.random(__table)
|
||||
if (type(__table) ~= "table") then return nil end
|
||||
return __table[math.ceil(math.random(#__table))]
|
||||
end
|
||||
function table.each(__table, __function)
|
||||
if (type(__function) ~= "function") then return end
|
||||
for _, e in pairs(__table) do
|
||||
__function(e)
|
||||
end
|
||||
end
|
||||
|
||||
function table.filter(__table, __filter)
|
||||
local out = {}
|
||||
for _, value in pairs(__table) do
|
||||
--if filterIter(v, k, table) then out[k] = v end
|
||||
if (__filter(value)) then
|
||||
__table.insert (out,value)
|
||||
end
|
||||
end
|
||||
|
||||
return out
|
||||
end
|
||||
|
||||
function table.getSize(__table)
|
||||
local count = 0
|
||||
for _, _ in pairs(__table) do count += 1 end
|
||||
return count
|
||||
end
|
Reference in New Issue
Block a user