Skip to content

Commit

Permalink
prepare for tweens
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesrje committed Nov 22, 2024
1 parent a00a508 commit 4e38cf7
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 899 deletions.
838 changes: 0 additions & 838 deletions src/Animation/Spr.luau

This file was deleted.

71 changes: 23 additions & 48 deletions src/Animation/Spring.luau
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
local Mode = RunService:IsClient() and "Client" or "Server"
local Connection: ((number) -> ())?

local TargetLinearPosition = self._type.ToLinear(self._State:Get())
local CurrentValue = self._State:Get()
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._type.ToLinear(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
Expand All @@ -59,7 +59,7 @@ function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
Connection = Scheduler:Add(Mode, function(delta: number)
if self and (self :: any)._Update then
(self :: any):_Update(TargetLinearPosition, delta);
(instance :: any)[prop] = self._type.FromLinear(self._CurrentLinearPosition)
(instance :: any)[prop] = self._DataType.FromLinear(self._CurrentLinearPosition)
else
if Connection then
Scheduler:Remove(Mode, Connection)
Expand Down Expand Up @@ -134,36 +134,19 @@ function Class._Update(self: Types.Spring, targetLinearPosition: { number }, del
end

--[[
### `Spring:Get()`
Returns the runtime position of the spring object.
#### Returns
- `Position`
#### Examples
```luau
local MyState = Aegis.state(Vector3.new(0, 5, 0))
local MySpring = Aegis.spring({ State = MyState })
print(MySpring:Get()) -- 0, 5, 0
```
Gets the end goal of the spring object.
[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._type.FromLinear(self._CurrentLinearPosition)
return self._DataType.FromLinear(self._CurrentLinearPosition)
end

--[[
### `Spring:Destroy()`
Destroys and removes the spring.
#### Returns
- `nil`
Destroys the spring object.
#### Example
```luau
local MySpring = Aegis.spring({ ... })
MySpring:Destroy() -- spring no longer valid.
```
[Learn More](https://luminlabsdev.github.io/ui-framework/api/spring/#destroy)
]]
function Class.Destroy(self: Types.Spring): nil
(self :: any)._State:Destroy()
Expand All @@ -172,34 +155,26 @@ function Class.Destroy(self: Types.Spring): nil
end

--[=[
Creates a new spring.
Creates a new spring with a set goal.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#spring)
]=]
return function(state: Types.State, damping: number?, frequency: number?): Types.Spring
-- Checks
if Is.Constructor(springInfo.State :: any) == false then
Debugger.Error("TypeMismatch", "Aegis.State", typeof(springInfo.State))
end

local Type = Storage.AnimatableDataTypes[typeof(springInfo.State:Get())]

if not Type then
Debugger.Error("NotAnimatable", typeof(springInfo.State:Get()))
end

local self = setmetatable({} :: Types.SpringProps, { __index = Class } :: { __index: Types.SpringFunc })
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()))

self.ConstructorClass = "spring"
local self = setmetatable({}, { __index = Class })

self._Damping = springInfo.Damping or 1
self._Frequency = springInfo.Frequency or 1
self._State = springInfo.State
self._Type = "spring"
self._Damping = damping or 1
self._Frequency = frequency or 1
self._State = goal

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

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

return self
return self :: any
end
11 changes: 11 additions & 0 deletions src/Animation/Tween.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Variables
local Root = script.Parent.Parent
local Utility = require(Root.Utility)
local Types = require(Root.Types)

-- Functions

-- Module
return function(goal: Types.State, tweenInfo: TweenInfo)

end
2 changes: 1 addition & 1 deletion src/Keys/Change.luau
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ return function(prop: string)
Apply = function(instance: Instance, callback: (changed: any) -> ())
local Success, Event = pcall(instance.GetPropertyChangedSignal, instance :: any, prop :: any)
if not Success or type(callback) ~= "function" then
Debugger.Fatal("")
Debugger.Fatal("InvalidPropOrEvent", prop)
end
Event:Connect(function()
callback((instance :: any)[prop])
Expand Down
11 changes: 10 additions & 1 deletion src/Keys/Clean.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Variables
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Is = require(Root.Is)
local Debugger = require(Root.Parent.debugger)

-- Module
Expand All @@ -16,7 +17,15 @@ return function()
Name = "Clean",
Apply = function(instance: Instance, values: { any })
Debugger.Assert(type(values) == "table", "InvalidType", "table", type(values))
instance.Destroying:Once(function() end)
instance.Destroying:Once(function()
for _, value in values do
if Is.Constructor(value) or typeof(value) == "Instance" then
value:Destroy()
elseif typeof(value) == "RBXScriptConnection" then
value:Disconnect()
end
end
end)
end,
}
end
Expand Down
3 changes: 2 additions & 1 deletion src/Keys/Event.luau
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ return function(event: string)
if not Keys["Event"] then
Keys["Event"] = {
Name = "Event",
Connection = nil,
Apply = function(instance: Instance, callback: (...any) -> ())
local Success, Event = pcall(Find, instance :: any, event :: any)
if not Success or type(callback) ~= "function" then
Debugger.Fatal("")
Debugger.Fatal("InvalidPropOrEvent", event)
end
Event:Connect(callback)
end,
Expand Down
3 changes: 2 additions & 1 deletion src/Logs.luau
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
return {
IncompatibleType = "Data type '%s' is incompatible",
NotAnimatable = "%s is not an animatable object",
NotAnimatable = "%s is not an animatable object type",
FailedCreation = "%s could not be created; '%s'",

InvalidType = "Expected type %s; got '%s'",
InvalidKey = "Expected valid key type; got '%s'",
InvalidClass = "'%s' is not an instance class name",
InvalidPropOrEvent = "'%s' is not a property/event of the %s class"
}
10 changes: 5 additions & 5 deletions src/State/Value.luau → src/State/State.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local Class = {}
--[=[
Returns the current value of the object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/value/#get)
[Learn More](https://luminlabsdev.github.io/ui-framework/api/state/#get)
]=]
function Class.Get(self: Types.State): any
return self._State
Expand All @@ -20,7 +20,7 @@ end
--[=[
Sets the current value of the object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/value/#set)
[Learn More](https://luminlabsdev.github.io/ui-framework/api/state/#set)
]=]
function Class.Set(self: Types.State, newValue: any): Types.State
local Type = type(newValue)
Expand Down Expand Up @@ -51,7 +51,7 @@ end
--[=[
Listens to changes of state within the object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/value/#listen)
[Learn More](https://luminlabsdev.github.io/ui-framework/api/state/#listen)
]=]
function Class.Listen(self: Types.State, listener: (new: any, old: any) -> ()): () -> ()
table.insert(self._Listeners, listener) -- Add a listener
Expand Down Expand Up @@ -80,7 +80,7 @@ end
--[=[
Destroys the value object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/value/#destroy)
[Learn More](https://luminlabsdev.github.io/ui-framework/api/state/#destroy)
]=]
function Class.Destroy(self: Types.State): nil
Utility.CleanMetatable(self :: any)
Expand All @@ -92,7 +92,7 @@ end
--[=[
Creates a new value/state object that dynamically changes in UI when changed itself.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#value)
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#state)
]=]
return function(initial: any): Types.StateExport
local Type = type(initial)
Expand Down
15 changes: 14 additions & 1 deletion src/Types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type Spring = typeof(setmetatable(
_Frequency: number,
_Velocity: { number },
_CurrentLinearPosition: { number },
_type: {
_DataType: {
ToLinear: (value: Animatable) -> { number },
FromLinear: (value: { number }) -> Animatable,
},
Expand All @@ -70,6 +70,19 @@ export type Spring = typeof(setmetatable(
}
))

export type TweenExport = {

} & Constructor<TweenExport>

export type Tween = typeof(setmetatable(
{} :: {

},
{} :: {
__index: TweenExport
}
))

export type ComputeExport = Constructor<ComputeExport>

export type Compute = typeof(setmetatable(
Expand Down
2 changes: 1 addition & 1 deletion src/Utility.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local function CheckTypeAndCall(expected: any, received: any, interaction: () ->
if expected == nil or received == nil then
interaction(...) -- Let the value `nil` to be set to anything
else
Debugger.Warn("TypeMismatch", typeof(expected), typeof(received))
Debugger.Warn("InvalidType", typeof(expected), typeof(received))
return
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ return table.freeze({
New = require(Instances.New),
Update = require(Instances.Update),

Value = require(State.Value),
State = require(State.State),
Compute = require(State.Compute),

Spring = require(Animation.Spring),
-- Tween = require(Animation.Tween),
Tween = require(Animation.Tween),

Event = require(Keys.Event),
Change = require(Keys.Change),
Expand Down

0 comments on commit 4e38cf7

Please sign in to comment.