-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeline.lua
124 lines (99 loc) · 4.16 KB
/
Timeline.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
local class = require "com.class"
---@class Timeline
---@overload fun():Timeline
local Timeline = class:derive("Timeline")
-- Place your imports here
local Vec2 = require("Vector2")
---Constructs a Timeline.
---Timelines can be launched and when updated, they will alter the node states.
---Right now, very much a test! This is a whole segment of code that is just starting off!!!
---The first Timeline animates the positions and alphas of Nodes/Widgets.
function Timeline:new()
self.events = {
{time = 1, type = "setNodeProperty", node = "TitleH1", property = "pos", value = Vec2(160, 35), duration = 0.5},
{time = 1, type = "setNodeProperty", node = "TitleH2", property = "pos", value = Vec2(160, 35), duration = 0.5},
{time = 1.5, type = "setNodeProperty", node = "TitleH1", property = "visible", value = false},
{time = 1.5, type = "setNodeProperty", node = "TitleH2", property = "visible", value = false},
{time = 1.5, type = "setNodeProperty", node = "Title", property = "visible", value = true},
{time = 1.5, type = "setNodeProperty", node = "TitleDigit", property = "visible", value = true},
{time = 1.5, type = "setNodeProperty", node = "Flash", property = "visible", value = true},
{time = 1.5, type = "setWidgetProperty", node = "Flash", property = "alpha", value = 1},
{time = 1.5, type = "setWidgetProperty", node = "Flash", property = "alpha", value = 0, duration = 1},
{time = 2.5, type = "setNodeProperty", node = "Node", property = "visible", value = true},
{time = 3, type = "setWidgetProperty", node = "TypeText", property = "typeInProgress", value = 1, duration = 1.5}
}
self.nodeNames = {
"Title",
"TitleH1",
"TitleH2",
"TitleDigit",
"Flash",
"Node",
"TypeText"
}
self.playbackTime = nil
self.playbackStep = nil
end
---Updates this Timeline.
---@param dt number Time delta in seconds.
function Timeline:update(dt)
if not self.playbackTime then
-- This Timeline is not playing.
return
end
self.playbackTime = self.playbackTime + dt
-- Process the events until either we've run out of them or the next one should not be executed yet.
while self.events[self.playbackStep] and self.events[self.playbackStep].time <= self.playbackTime do
self:processEvent(self.events[self.playbackStep])
self.playbackStep = self.playbackStep + 1
end
-- If all events have been finished, stop the playback.
if self.playbackStep > #self.events then
self.playbackTime = nil
self.playbackStep = nil
end
end
---Processes a single Timeline Event.
---@param event table The event to be processed.
function Timeline:processEvent(event)
if event.type == "setNodeProperty" then
local node = _UI:findChildByName(event.node)
if not node then
print(string.format("Could not find node %s to animate, skipping", event.node))
return
end
node.properties:animateValue(event.property, nil, event.value, event.duration)
elseif event.type == "setWidgetProperty" then
local node = _UI:findChildByName(event.node)
if not node then
print(string.format("Could not find node %s to animate, skipping", event.node))
return
end
node.widget.properties:animateValue(event.property, nil, event.value, event.duration)
end
end
---Plays the Timeline from the beginning.
function Timeline:play()
self.playbackTime = 0
self.playbackStep = 1
end
---Stops the Timeline from playing.
function Timeline:stop()
self.playbackTime = nil
self.playbackStep = nil
end
---Returns a table, keyed by node names, of all events they have, in order of being specified in the `events` list.
---@return table
function Timeline:getInfo()
local info = {}
for i, event in ipairs(self.events) do
-- Create a list for a node if that's the first time we see it.
if not info[event.node] then
info[event.node] = {}
end
-- Add the event to the appropriate list.
table.insert(info[event.node], event)
end
return info
end
return Timeline