Skip to content

Commit

Permalink
working springs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesrje committed Dec 12, 2024
1 parent ba05332 commit ba336fb
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 249 deletions.
13 changes: 6 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Mkdocs
venv

# Wally
Packages
DevPackages
wally.lock
# Pesde
.pesde
roblox_packages
lune_packages
luau_packages
pesde.lock

# Rojo
sourcemap.json
6 changes: 0 additions & 6 deletions default.project.json

This file was deleted.

21 changes: 15 additions & 6 deletions dev.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"Packages": {
"$path": "Packages",
"ui-framework": {
"ui-framework": {
"$className": "Folder",
"src": {
"$path": "src"
}
},
"roblox_packages": {
"$path": "roblox_packages"
}
},
"Tests": {
"$path": "tests"
"tests": {
"$className": "Folder",
"Client": {
"$path": "tests/client"
},
"Server": {
"$path": "tests/server"
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions pesde.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name = "lumin/uiframework"
version = "0.1.0"
description = "A lightweight and embeddable UI framework"
authors = ["2jammers"]
repository = "https://github.com/luminlabsdev/ui-framework"
license = "MIT"

[target]
environment = "roblox"

[indices]
default = "https://github.com/daimond113/pesde-index"

[scripts]
roblox_sync_config_generator = ".pesde/scripts/roblox_sync_config_generator.luau"
sourcemap_generator = ".pesde/scripts/sourcemap_generator.luau"

[dev_dependencies]
scripts = { name = "pesde/scripts_rojo", version = "^0.1.0", target = "lune" }
rojo = { name = "pesde/rojo", version = "^7.4.4", target = "lune" }

[dependencies]
debugger = { name = "lumin/debugger", version = "^0.5.0" }
10 changes: 0 additions & 10 deletions rokit.toml

This file was deleted.

147 changes: 13 additions & 134 deletions src/Animation/Spring.luau
Original file line number Diff line number Diff line change
@@ -1,148 +1,34 @@
-- Forked from dervexdev/advancedspring
-- Variables
local Root = script.Parent.Parent
local Packages = script.Parent.Parent.Parent.Parent.Packages

local RunService = game:GetService("RunService")

local Runtime = Root.Runtime
local Scheduler = require(Runtime.Scheduler)
local Storage = require(Runtime.Storage)
local Packages = script.Parent.Parent.Parent.roblox_packages

local Debugger = require(Packages.debugger)
local Utils = require(Root.Utility)
local Is = require(Root.Is)
local Types = require(Root.Types)

local Spring = require(script.Parent.Spr)
local Spr = require(script.Parent.Spr)

local Class = {}

local Epsilon = 1e-4

local sqrt = math.sqrt
local exp = math.exp
local cos = math.cos
local sin = math.sin
local pi = math.pi

-- Functions
local function IsSpringSettled(currentLinearPosition: { number }, targetLinearPosition: { number }): boolean
for index, value in currentLinearPosition do
if math.abs(value - targetLinearPosition[index]) > Epsilon then
-- If the difference is more than the epsilon, then return false
return false
end
end

return true
end

function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
local Mode = RunService:IsClient() and "Client" or "Server"
local Connection: ((number) -> ())?

local TargetLinearPosition = self._DataType.ToLinear((self._State :: any):Get())
local CurrentValue = (self._State :: any):Get()

if CurrentValue ~= nil then
(instance :: any)[prop] = CurrentValue
end
self._CurrentLinearPosition = TargetLinearPosition
;
(self._State :: any):Listen(function(newValue)
TargetLinearPosition = self._DataType.ToLinear(newValue)

if Connection ~= nil then
return -- The value changed when the spring was playing; don't create another connection, let the new one play
end

if not Connection then
Connection = Scheduler:Add(Mode, function(delta: number)
if self and (self :: any)._Update then
(self :: any):_Update(TargetLinearPosition, delta);
(instance :: any)[prop] = self._DataType.FromLinear(self._CurrentLinearPosition)
else
if Connection then
Scheduler:Remove(Mode, Connection)
return
end
end

if IsSpringSettled(self._CurrentLinearPosition, TargetLinearPosition) == true and Connection then
Scheduler:Remove(Mode, Connection)
Connection = nil;
(instance :: any)[prop] = (self :: any)._State:Get()

for index = 1, #self._Velocity do
self._Velocity[index] = 0
end
end
end)
end
end)
end

function Class._Update(self: Types.Spring, targetLinearPosition: { number }, delta: number)
for index = 1, #targetLinearPosition do
-- DO NOT CHANGE ANYTHING IN THE CALCULATION UNLESS YOU KNOW WHAT YOU ARE DOING
--[[
d = damping
f = frequency
v = velocity
c = constant
i = cosine_angle
j = sine_angle
r1 = root1
r2 = root2
c2 = coefficient2
c1 = coefficient1
e1 = exp1
e2 = exp2
]]

local d = self._Damping
local f = self._Frequency * pi * 2
local g = targetLinearPosition[index]
local p = self._CurrentLinearPosition[index]
local v = self._Velocity[index]

local offset = p - g
local decay = exp(-delta * d * f)

if d == 1 then
self._CurrentLinearPosition[index] = (v * delta + offset * (f * delta + 1)) * decay + g
self._Velocity[index] = (v - (offset * f + v) * (f * delta)) * decay
elseif d < 1 then
local c = sqrt(1 - d * d)
local i = cos(delta * f * c)
local j = sin(delta * f * c)

self._CurrentLinearPosition[index] = (offset * i + (v + offset * (d * f)) * j / (f * c)) * decay + g
self._Velocity[index] = (v * (i * c) - (v * d + offset * f) * j) * (decay / c)
else
local c = sqrt(d * d - 1)
local r1 = -f * (d - c)
local r2 = -f * (d + c)
local co2 = (v - offset * r1) / (2 * f * c)
local co1 = offset - co2
local e1 = co1 * exp(r1 * delta)
local e2 = co2 * exp(r2 * delta)

self._CurrentLinearPosition[index] = e1 + e2 + g
self._Velocity[index] = r1 * e1 + r2 * e2
end
end
(instance :: any)[prop] = self._Goal:Get()
self._Goal:Listen(function(new)
Spr.target(instance, self._Damping, self._Frequency, {
[prop] = new
})
end)
end

--[[
Gets the end goal of the spring object.
Gets the current value that the spring is moving.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/spring/#get)
]]
function Class.Get(self: Types.Spring): Types.Animatable
-- Return the unpacked format for improved clarity
return self._DataType.FromLinear(self._CurrentLinearPosition)
return 1
end

--[[
Expand All @@ -151,31 +37,24 @@ end
[Learn More](https://luminlabsdev.github.io/ui-framework/api/spring/#destroy)
]]
function Class.Destroy(self: Types.Spring)
(self :: any)._State:Destroy()
(self :: any)._Goal:Destroy()
Utils.CleanMetatable(self :: any)
end

--[=[
Creates a new spring with a set goal.
Creates a new spring with a set goal. Can be used in place of a tween.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#spring)
]=]
return function(goal: Types.State, damping: number?, frequency: number?): Types.SpringExport
Debugger.Assert(Is.Constructor(goal :: any), "InvalidType", "State", type(goal))
local Type = Storage.AnimatableDataTypes[typeof((goal :: any):Get())]
Debugger.Assert(Type, "NotAnimatable", type((goal :: any):Get()))

local self = setmetatable({}, { __index = Class })

self._Type = "spring"
self._Damping = damping or 1
self._Frequency = frequency or 1
self._State = goal

self._DataType = Type
self._CurrentLinearPosition = Type.ToLinear((goal :: any):Get())

self._Velocity = table.create(#self._CurrentLinearPosition, 0)
self._Goal = goal

return self :: any
end
2 changes: 1 addition & 1 deletion src/Instances/New.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Root = script.Parent.Parent
local Utility = require(Root.Utility)
local Types = require(Root.Types)

local Debugger = require(Root.Parent.debugger)
local Debugger = require(Root.Parent.roblox_packages.debugger)
local Default = require(Root.Instances.Default)

-- Module
Expand Down
4 changes: 2 additions & 2 deletions src/Keys/Change.luau
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Variables
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Debugger = require(Root.Parent.debugger)
local Keys = require(script.Parent.KeyList)
local Debugger = require(Root.Parent.roblox_packages.debugger)

-- Module

Expand Down
4 changes: 2 additions & 2 deletions src/Keys/Clean.luau
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Variables
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Keys = require(script.Parent.KeyList)
local Is = require(Root.Is)
local Debugger = require(Root.Parent.debugger)
local Debugger = require(Root.Parent.roblox_packages.debugger)

-- Module

Expand Down
4 changes: 2 additions & 2 deletions src/Keys/Event.luau
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Variables
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Debugger = require(Root.Parent.debugger)
local Keys = require(script.Parent.KeyList)
local Debugger = require(Root.Parent.roblox_packages.debugger)

-- Functions
local function Find(instance: Instance, property: string)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Keys/Tag.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Variables
local Keys = require(script.Parent.List)
local Keys = require(script.Parent.KeyList)

-- Module

Expand Down
40 changes: 0 additions & 40 deletions src/Runtime/Scheduler.luau

This file was deleted.

2 changes: 1 addition & 1 deletion src/State/Compute.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Variables
local Root = script.Parent.Parent
local Debugger = require(Root.Parent.debugger)
local Debugger = require(Root.Parent.roblox_packages.debugger)
local Utility = require(Root.Utility)
local Is = require(Root.Is)
local Types = require(Root.Types)
Expand Down
2 changes: 1 addition & 1 deletion src/State/State.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local Root = script.Parent.Parent
local Types = require(Root.Types)
local Utility = require(Root.Utility)
local Debugger = require(Root.Parent.debugger)
local Debugger = require(Root.Parent.roblox_packages.debugger)

local Class = {}

Expand Down
Loading

0 comments on commit ba336fb

Please sign in to comment.