This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Animations.lua
executable file
·231 lines (189 loc) · 6.75 KB
/
Animations.lua
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
local _, addon = ...
---@cast addon MoneyToastAddon
local defaultAnimation = {
name = "default",
id = "unregistered",
animationGroup = 0,
context = {},
requiredContextKeys = {},
active = false,
complete = true,
elapsed = 0.0,
startValue = 0.0,
currentValue = 0.0,
targetValue = 1.0,
delayStart = 0.0,
delayComplete = 0.0,
delta = 1.0,
duration = 1.0,
resetOnPlay = true,
preserveValue = false,
OnStart = function(self) end,
OnUpdate = function(self) end,
OnComplete = function(self) end,
}
---@param self AnimationsModule
---@param name string
---@param options Animation
local createAnimation = function(self, name, options)
local animation = addon.Utilities.ShallowCopy(defaultAnimation)
animation.name = name
animation.context = {}
for optKey, optValue in pairs(options) do
if defaultAnimation[optKey] ~= nil then
animation[optKey] = optValue
end
end
self.animationTemplates[name] = animation
end
---@param self AnimationsModule
---@param animationTemplateName string
---@param animationGroup number | string
---@param context table
local registerAnimation = function(self, animationTemplateName, animationGroup, context)
local animationTemplate = self:_GetAnimationTemplate(animationTemplateName)
local animation = addon.Utilities.ShallowCopy(animationTemplate)
local expectedContextKeys = animation.requiredContextKeys
animation.context = {}
for _, contextKey in ipairs(expectedContextKeys) do
if context[contextKey] == nil then
error(format("Animation registration failed: Context '%s' missing"))
end
animation.context[contextKey] = context[contextKey]
end
animation.id = animationTemplate.name .. animationGroup
animation.animationGroup = animationGroup
self.registeredAnimations[animation.id] = animation
end
---@param self AnimationsModule
---@param animationName string
---@param animationGroup number | string
local getAnimation = function(self, animationName, animationGroup)
local animation = self.registeredAnimations[animationName .. animationGroup]
if animation == nil then
error(format("Animation not found: %s for animation group %d.", animationName, animationGroup))
end
return animation
end
---@param self AnimationsModule
---@param templateName string
---@return Animation
local getAnimationTemplate = function(self, templateName)
local template = self.animationTemplates[templateName]
if template == nil then
error(format("Animation template '%s' not found.", templateName))
end
return template
end
---@param self AnimationsModule
---@param animationName string
---@param animationGroup number | string
---@param targetValue number | nil
local playAnimation = function(self, animationName, animationGroup, targetValue)
local animation = self:_GetAnimation(animationName, animationGroup)
animation:OnStart()
if animation.resetOnPlay and animation.complete then
animation.startValue = animation.currentValue
end
if animation.currentValue == nil then
animation.currentValue = animation.startValue
end
if targetValue ~= nil then
animation.targetValue = targetValue
end
animation.delta = animation.targetValue - animation.startValue
if animation.resetOnPlay or animation.complete then
animation.elapsed = 0
end
animation.active = true
animation.complete = false
end
---@param self AnimationsModule
---@param animationName string
---@param animationGroup number | string
---@param triggerCompletionCallback boolean | nil
local cancelAnimation = function(self, animationName, animationGroup, triggerCompletionCallback)
local animation = self:_GetAnimation(animationName, animationGroup)
if not animation.active then
return
end
animation.complete = true
animation.active = false
if triggerCompletionCallback then
animation:OnComplete()
end
end
---@param self AnimationsModule
---@param animationName string
---@param animationGroup number | string
---@param value number
local setAnimationValue = function(self, animationName, animationGroup, value)
local animation = self:_GetAnimation(animationName, animationGroup)
animation.currentValue = value
animation:OnUpdate(value)
end
---@param self AnimationsModule
---@param animationName string
---@param animationGroup number | string
local skipAnimation = function(self, animationName, animationGroup)
local animation = self:_GetAnimation(animationName, animationGroup)
animation:OnComplete()
end
---@param animation Animation
local updateValue = function(animation)
local animationProgression = animation.elapsed - animation.delayStart
local ongoingAnimation = animationProgression < animation.duration
if ongoingAnimation then
local valueProgression = (animationProgression / animation.duration) * animation.delta
animation.currentValue = animation.startValue + valueProgression
animation:OnUpdate(animation.currentValue)
return
end
animation.currentValue = animation.preserveValue and animation.targetValue or animation.startValue
animation:OnUpdate(animation.targetValue)
end
---@param animation Animation
local completeAnimation = function(animation)
animation.complete = true
animation:OnComplete()
end
---@param self AnimationsModule
---@param animation Animation
---@param elapsed number
local animationTick = function(self, animation, elapsed)
if not animation.active or animation.complete then
return
end
animation.elapsed = animation.elapsed + elapsed
if animation.elapsed >= animation.delayStart then
updateValue(animation)
end
local fullAnimationDuration = animation.duration + animation.delayStart + animation.delayComplete
if animation.elapsed >= fullAnimationDuration then
completeAnimation(animation)
end
end
---@param self AnimationsModule
---@param elapsed number
local onUpdate = function(self, elapsed)
for _, animation in pairs(self.registeredAnimations) do
self:_AnimationTick(animation, elapsed)
end
end
---@class AnimationsModule
local animationsModule = {
animationTemplates = {}, ---@type table<string, Animation>
registeredAnimations = {}, ---@type table<string, Animation>
CreateAnimation = createAnimation,
RegisterAnimation = registerAnimation,
PlayAnimation = playAnimation,
CancelAnimation = cancelAnimation,
SetAnimationValue = setAnimationValue,
SkipAnimation = skipAnimation,
_GetAnimation = getAnimation,
_GetAnimationTemplate = getAnimationTemplate,
_CompleteAnimation = completeAnimation,
_AnimationTick = animationTick,
OnUpdate = onUpdate,
}
addon.Animations = animationsModule