forked from 26F-Studio/Zenitha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.lua
317 lines (284 loc) · 9.1 KB
/
scene.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
---@class Zenitha.Scene
---@field widgetList? Zenitha.WidgetArg[]|Zenitha.Widget.base[]
---@field scrollHeight? number|nil
---
---@field enter? function
---@field leave? function
---@field mouseDown? function
---@field mouseMove? function
---@field mouseUp? function
---@field mouseClick? function
---@field wheelMove? function Able to interrupt WIDGET._scroll
---@field touchDown? function
---@field touchUp? function
---@field touchMove? function
---@field touchClick? function
---@field keyDown? function Able to interrupt cursor & widget control
---@field keyUp? function
---@field textInput? function Able to interrupt widget control
---@field imeChange? function Able to interrupt widget control
---@field gamepadDown? function
---@field gamepadUp? function
---@field fileDrop? function
---@field folderDrop? function
---@field lowMemory? function
---@field resize? function
---@field focus? function
---@field update? function
---@field draw? function
---@class Zenitha.SceneSwap
---@field duration number
---@field timeChange number
---@field draw function called with timeRemain(duration~0)
---@type table<string, Zenitha.Scene>
local scenes={}
local eventNames={
'enter','leave',
'mouseDown','mouseMove','mouseUp','mouseClick','wheelMove',
'touchDown','touchMove','touchUp','touchClick',
'keyDown','keyUp',
'textInput','imeChange',
'gamepadDown','gamepadUp',
'fileDrop','folderDrop',
'lowMemory',
'resize','focus',
'update','draw',
}
local SCN={
mainTouchID=nil, -- First touching ID(userdata)
maxScroll=0,
curScroll=0,
swapping=false, -- If Swapping
state={
tar=false, -- Swapping target
style=false, -- Swapping style
draw=false, -- Swap draw func
timeRem=false, -- Swap time remain
timeChange=false, -- Loading point
},
stack={}, -- Scene stack
prev=false,
cur=false,
args={}, -- Arguments from previous scene
scenes=scenes,
}
local defaultSwap='fade'
-- Scene swapping animations
local swap={
none={
duration=0,timeChange=0,
draw=function() end,
},
flash={
duration=.16,timeChange=.08,
draw=function() GC.clear(1,1,1) end,
},
fade={
duration=.5,timeChange=.25,
draw=function(t)
GC.setColor(.1,.1,.1,t>.25 and 2-t*4 or t*4)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end,
},
fastFade={
duration=.2,timeChange=.1,
draw=function(t)
GC.setColor(.1,.1,.1,t>.1 and 2-t*10 or t*10)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end,
},
slowFade={
duration=3,timeChange=1.5,
draw=function(t)
GC.setColor(.1,.1,.1,t>1.5 and (3-t)/1.5 or t/1.5)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end,
},
swipeL={
duration=.5,timeChange=.25,
draw=function(t)
t=t*2
GC.setColor(.1,.1,.1,1-math.abs(t-.5))
t=t*t*(3-2*t)*2-1
GC.rectangle('fill',t*SCR.w,0,SCR.w,SCR.h)
end,
},
swipeR={
duration=.5,timeChange=.25,
draw=function(t)
t=t*2
GC.setColor(.1,.1,.1,1-math.abs(t-.5))
t=t*t*(2*t-3)*2+1
GC.rectangle('fill',t*SCR.w,0,SCR.w,SCR.h)
end,
},
swipeD={
duration=.5,timeChange=.25,
draw=function(t)
t=t*2
GC.setColor(.1,.1,.1,1-math.abs(t-.5))
t=t*t*(2*t-3)*2+1
GC.rectangle('fill',0,t*SCR.h,SCR.w,SCR.h)
end,
},
}
---Add a scene
---@param name string
---@param scene Zenitha.Scene
function SCN.add(name,scene)
assertf(type(name)=='string',"SCN.add(name,scene): name need string, got %s",type(name))
assertf(type(scene)=='table',"SCN.add(name,scene): scene need table, got %s",type(scene))
assertf(not scenes[name],"SCN.add(name,scene): scene '%s' already exists",name)
if scene.widgetList==nil then scene.widgetList={} end
-- Check each field in scene object
for k in next,scene do
if k=='widgetList' then
assertf(type(scene.widgetList)=='table',"SCN.add: scene[%s].widgetList need table",name)
for kw,w in next,scene.widgetList do
assertf(type(w)=='table',"SCN.add: scene[%s].widgetList need list<widgetArgTable|widgetObj>",name)
if not w._widget then
scene.widgetList[kw]=WIDGET.new(w)
end
end
elseif k=='scrollHeight' then
assertf(type(scene.scrollHeight)=='number' and scene.scrollHeight>0,"SCN.add: scene[%s].scrollHeight need >0",name)
elseif not TABLE.find(eventNames,k) then
errorf("SCN.add(name,scene): Invalid key '%s' in scene[%s]",k,name)
end
end
for i=1,#eventNames do assertf(scene[eventNames[i]]==nil or type(scene[eventNames[i]])=='function',"SCN.add: scene[%s].%s need function",name,eventNames[i]) end
scenes[name]=scene
end
---Add a scene swapping animation
---@param name string
---@param swp Zenitha.SceneSwap
function SCN.addSwap(name,swp)
assertf(type(name)=='string',"SCN.addSwap(name,swp): name need string")
assertf(not swap[name],"SCN.addSwap(name,swp): Swap '%s' already exist",name)
assertf(type(swp)=='table',"SCN.addSwap(name,swp): swp need table")
assertf(type(swp.duration)=='number' and swp.duration>=0,"SCN.addSwap(name,swp): swp.duration need >=0")
assertf(type(swp.timeChange)=='number' and swp.timeChange>=0,"SCN.addSwap(name,swp): swp.timeChange need >=0")
assertf(type(swp.draw)=='function',"SCN.addSwap(name,swp): swp.draw need function")
swap[name]=swp
end
---Set max scroll area of current scene, default to 0
---@param height? number
function SCN.setScroll(height)
SCN.maxScroll=height or 0
SCN.curScroll=MATH.clamp(SCN.curScroll,0,SCN.maxScroll)
end
---Update scene swapping animation (called by Zenitha)
---@param dt number
function SCN._swapUpdate(dt)
local S=SCN.state
S.timeRem=S.timeRem-dt
if S.timeRem<S.timeChange and S.timeRem+dt>=S.timeChange then
-- Actually load scene at this moment
SCN.stack[#SCN.stack]=S.tar
SCN.cur=S.tar
SCN._load(S.tar)
SCN.mainTouchID=nil
end
if S.timeRem<0 then
SCN.swapping=false
end
end
---Load a scene, replace all events and fresh scrolling, widgets
---@param name string
function SCN._load(name)
love.keyboard.setTextInput(false)
local S=scenes[name]
SCN.maxScroll=S.scrollHeight or 0
SCN.curScroll=0
WIDGET._setWidgetList(S.widgetList)
for i=1,#eventNames do
SCN[eventNames[i]]=S[eventNames[i]]
end
if S.enter then S.enter() end
end
---Push a scene to stack
---@param tar? string
function SCN._push(tar)
table.insert(SCN.stack,tar or SCN.stack[#SCN.stack-1])
end
---Pop a scene from stack
function SCN._pop()
table.remove(SCN.stack)
end
---Swap to a sceene without add current scene to stack (cannot go back)
---@param tar string
---@param style? string
---@param ... any Arguments passed to new scene
function SCN.swapTo(tar,style,...)
if scenes[tar] then
if not SCN.swapping then
SCN.prev=SCN.stack[#SCN.stack]
style=style or defaultSwap
if not swap[style] then
MSG.new('error',"No swap style named '"..style.."'")
style=defaultSwap
end
SCN.swapping=true
SCN.args={...}
local S=SCN.state
S.tar,S.style=tar,style
S.timeRem=swap[style].duration
S.timeChange=swap[style].timeChange
S.draw=swap[style].draw
end
else
MSG.new('warn',"No Scene: "..tar)
end
end
---Go to a scene
---@param tar string
---@param style? string
---@param ... any Arguments passed to new scene
function SCN.go(tar,style,...)
if scenes[tar] then
if not SCN.swapping then
SCN._push(SCN.stack[#SCN.stack] or '_')
SCN.swapTo(tar,style,...)
end
else
MSG.new('warn',"No Scene: "..tar)
end
end
---Back to previous scene
---@param style? string
---@param ... any Arguments passed to previous scene
function SCN.back(style,...)
if SCN.swapping then return end
local m=#SCN.stack
if m>1 then
-- Leave scene
if SCN.leave then SCN.leave() end
-- Poll&Back to previous Scene
SCN.swapTo(SCN.stack[#SCN.stack-1],style,...)
SCN._pop()
else
ZENITHA._quit(style)
end
end
---Back to a specific scene
---@param tar string
---@param style? string
---@param ... any Arguments passed to target scene
function SCN.backTo(tar,style,...)
if SCN.swapping then return end
-- Poll&Back to previous Scene
while SCN.stack[#SCN.stack-1]~=tar and #SCN.stack>1 do
SCN._pop()
end
SCN.swapTo(SCN.stack[#SCN.stack],style,...)
end
---Print current scene stack to console
function SCN.printStack()
for i=1,#SCN.stack do print(SCN.stack[i]) end
end
function SCN.setDefaultSwap(anim)
assertf(type(anim)=='string',"SCN.setDefaultSwap(anim): Need string, got %s",anim)
assertf(swap[anim],"SCN.setDefaultSwap(anim): No swap style '%s'",anim)
defaultSwap=anim
end
return SCN