-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeatre.lua
291 lines (229 loc) · 7.1 KB
/
theatre.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
local function logStr(format, ...)
return "[THEATRE:" .. debug.getinfo(2, "n").name .. "] " .. string.format(format, ...)
end
local Theatre = {}
Theatre.__index = Theatre
---@type table<string, Theatre.State>
Theatre.states = {}
---@type string
Theatre.previous = ""
---@type string
Theatre.current = ""
---@type table<string>
Theatre.stack = {}
---Initializes Theatre gamestate library
---@param dir string Directory of gamestate files
---@param startState? string Initial gamestate
function Theatre:new(dir, startState)
assert(love.filesystem.getInfo(dir) ~= nil, logStr("Directory '%s' doesn't exists!", dir))
local files = love.filesystem.getDirectoryItems(dir)
if #files < 1 then
io.stderr:write(logStr("WARNING: No gamestates found in '%s'", dir))
return
end
for _, v in pairs(files) do
if string.sub(v, #v - 3) == ".lua" then
local name = string.sub(v, 0, #v - 4)
---@type boolean, Theatre.State
local err, state = pcall(require, dir .. "." .. name)
if not err then
error(logStr("Couldn't load gamestate '%s':\n%s", name, state))
end
state.__visited = false
self.states[name] = state
end
end
if startState then
self.stack[1] = startState
self:switch(startState)
end
end
function Theatre:setupLogger(oldprint)
self.oldprint = oldprint
_G.print = self.print
end
function Theatre.print(msg, ...)
Theatre.oldprint("[" .. Theatre.current .. "] " .. string.format(msg, ...))
end
function Theatre:switch(stateName, ...)
assert(self.states[stateName], logStr("State '%s' doesn't exists!", stateName))
self:exit()
self.previous = self.current
self.current = stateName
self.stack[#self.stack] = self.current
if not self.states[stateName].__visited then
self:init()
end
self:start(self.previous, ...)
end
function Theatre:push(stateName, ...)
assert(self.states[stateName], logStr("State '%s' doesn't exists!", stateName))
self:pause()
self.current = stateName
self.stack[#self.stack + 1] = self.current
if not self.states[stateName].__visited then
self:init()
end
self:start(self.stack[#self.stack - 1], ...)
end
function Theatre:pop(...)
self:exit()
self.stack[#self.stack] = nil
self.current = self.stack[#self.stack]
self:resume(...)
end
local function safeCall(fName, ...)
if Theatre.states[Theatre.current] and Theatre.states[Theatre.current][fName] then
Theatre.states[Theatre.current][fName](...)
end
end
-- Callbacks {{{
---@private
function Theatre:init(...)
safeCall("init", ...)
end
---@private
function Theatre:start(previous, ...)
safeCall("start", previous, ...)
end
---@private
function Theatre:pause(...)
safeCall("pause", ...)
end
---@private
function Theatre:resume(...)
safeCall("resume", ...)
end
---@private
function Theatre:exit(...)
safeCall("exit", ...)
end
---@param dt number
function Theatre:update(dt)
safeCall("update", dt)
end
function Theatre:draw()
safeCall("draw")
end
function Theatre:keypressed(...)
safeCall("keypressed", ...)
end
function Theatre:keyreleased(...)
safeCall("keyreleased", ...)
end
function Theatre:textedited(...)
safeCall("textedited", ...)
end
function Theatre:textinput(...)
safeCall("textinput", ...)
end
function Theatre:directorydropped(...)
safeCall("directorydropped", ...)
end
function Theatre:displayrotated(...)
safeCall("displayrotated", ...)
end
function Theatre:filedropped(...)
safeCall("filedropped", ...)
end
function Theatre:focus(...)
safeCall("focus", ...)
end
function Theatre:mousefocus(...)
safeCall("mousefocus", ...)
end
function Theatre:resize(...)
safeCall("resize", ...)
end
function Theatre:visible(...)
safeCall("visible", ...)
end
function Theatre:mousemoved(...)
safeCall("mousemoved", ...)
end
function Theatre:mousepressed(...)
safeCall("mousepressed", ...)
end
function Theatre:mousereleased(...)
safeCall("mousereleased", ...)
end
function Theatre:wheelmoved(...)
safeCall("wheelmoved", ...)
end
function Theatre:gamepadaxis(...)
safeCall("gamepadaxis", ...)
end
function Theatre:gamepadpressed(...)
safeCall("gamepadpressed", ...)
end
function Theatre:gamepadreleased(...)
safeCall("gamepadreleased", ...)
end
function Theatre:joystickadded(...)
safeCall("joystickadded", ...)
end
function Theatre:joystickaxis(...)
safeCall("joystickaxis", ...)
end
function Theatre:joystickhat(...)
safeCall("joystickhat", ...)
end
function Theatre:joystickpressed(...)
safeCall("joystickpressed", ...)
end
function Theatre:joystickreleased(...)
safeCall("joystickreleased", ...)
end
function Theatre:joystickremoved(...)
safeCall("joystickremoved", ...)
end
function Theatre:touchmoved(...)
safeCall("touchmoved", ...)
end
function Theatre:touchpressed(...)
safeCall("touchpressed", ...)
end
function Theatre:touchreleased(...)
safeCall("touchreleased", ...)
end
--}}}
return Theatre -- NOTE: Maybe use `setmetatable({}, Theatre)`
---Gamestate
---See [Love2D Callbacks](https://love2d.org/wiki/love#Callbacks)
---@class Theatre.State
---@field __visited boolean (PRIVATE)
---@field init function Fires ONLY ON FIRST switch to this gamestate
---@field start function Fires ON EVERY switch to this gamestate
---@field pause function Fires when pushing from this gamestate
---@field resume function Fires when poping to this gamestate
---@field exit function Fires ON EVERY switch from this gamestate
---@field update function Love2D callback
---@field draw function Love2D callback
---@field keypressed function Love2D callback
---@field keyreleased function Love2D callback
---@field textedited function Love2D callback
---@field textinput function Love2D callback
---@field directorydropped function Love2D callback
---@field displayrotated function Love2D callback
---@field filedropped function Love2D callback
---@field focus function Love2D callback
---@field mousefocus function Love2D callback
---@field resize function Love2D callback
---@field visible function Love2D callback
---@field mousemoved function Love2D callback
---@field mousepressed function Love2D callback
---@field mousereleased function Love2D callback
---@field wheelmoved function Love2D callback
---@field gamepadaxis function Love2D callback
---@field gamepadpressed function Love2D callback
---@field gamepadreleased function Love2D callback
---@field joystickadded function Love2D callback
---@field joystickaxis function Love2D callback
---@field joystickhat function Love2D callback
---@field joystickpressed function Love2D callback
---@field joystickreleased function Love2D callback
---@field joystickremoved function Love2D callback
---@field touchmoved function Love2D callback
---@field touchpressed function Love2D callback
---@field touchreleased function Love2D callback
-- vim:set fen fdm=marker: