forked from rojo-rbx/rojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindingUtil.lua
More file actions
59 lines (49 loc) · 1.28 KB
/
bindingUtil.lua
File metadata and controls
59 lines (49 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local Rojo = script:FindFirstAncestor("Rojo")
local Packages = Rojo.Packages
local Roact = require(Packages.Roact)
local Log = require(Packages.Log)
local LERP_DATA_TYPES = {
Color3 = true,
UDim = true,
UDim2 = true,
Vector2 = true,
Vector3 = true,
}
local function fromMotor(motor)
local motorBinding, setMotorBinding = Roact.createBinding(motor:getValue())
motor:onStep(setMotorBinding)
return motorBinding
end
local function mapLerp(binding, value1, value2)
local valueType = typeof(value1)
if valueType ~= typeof(value2) then
Log.error("Type mismatch between values ({}, {}})", valueType, typeof(value2))
end
return binding:map(function(position)
if valueType == "number" then
return value1 - (value2 - value1) * position
elseif LERP_DATA_TYPES[valueType] then
return value1:lerp(value2, position)
else
Log.error("Unable to interpolate type {}", valueType)
end
end)
end
local function deriveProperty(binding, propertyName)
return binding:map(function(values)
return values[propertyName]
end)
end
local function blendAlpha(alphaValues)
local alpha = 0
for _, value in alphaValues do
alpha = alpha + (1 - alpha) * value
end
return alpha
end
return {
fromMotor = fromMotor,
mapLerp = mapLerp,
deriveProperty = deriveProperty,
blendAlpha = blendAlpha,
}