This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
scene.lua
223 lines (209 loc) · 5.37 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
local scenes={}
local SCN={
mainTouchID=nil, -- First touching ID(userdata)
swapping=false, -- If Swapping
state={
tar=false, -- Swapping target
style=false, -- Swapping style
changeTime=false,-- Loading point
time=false, -- Full swap time
draw=false, -- Swap draw func
},
stack={},-- Scene stack
prev=false,
cur=false,
args={},-- Arguments from previous scene
scenes=scenes,
-- Events
update=false,
draw=false,
mouseClick=false,
touchClick=false,
mouseDown=false,
mouseMove=false,
mouseUp=false,
wheelMoved=false,
touchDown=false,
touchUp=false,
touchMove=false,
keyDown=false,
keyUp=false,
gamepadDown=false,
gamepadUp=false,
fileDropped=false,
directoryDropped=false,
resize=false,
}-- Scene datas, returned
function SCN.add(name,scene)
scenes[name]=scene
if scene.widgetList then
setmetatable(scene.widgetList,WIDGET.indexMeta)
end
end
function SCN.swapUpdate(dt)
local S=SCN.state
S.time=S.time-dt
if S.time<S.changeTime and S.time+dt>=S.changeTime then
-- Scene swapped this frame
SCN.stack[#SCN.stack]=S.tar
SCN.cur=S.tar
SCN.init(S.tar)
SCN.mainTouchID=nil
end
if S.time<0 then
SCN.swapping=false
end
end
function SCN.init(s)
love.keyboard.setTextInput(false)
local S=scenes[s]
WIDGET.setScrollHeight(S.widgetScrollHeight)
WIDGET.setWidgetList(S.widgetList)
SCN.enter=S.enter
SCN.leave=S.leave
SCN.mouseDown=S.mouseDown
SCN.mouseMove=S.mouseMove
SCN.mouseUp=S.mouseUp
SCN.mouseClick=S.mouseClick
SCN.wheelMoved=S.wheelMoved
SCN.touchDown=S.touchDown
SCN.touchUp=S.touchUp
SCN.touchMove=S.touchMove
SCN.touchClick=S.touchClick
SCN.keyDown=S.keyDown
SCN.keyUp=S.keyUp
SCN.gamepadDown=S.gamepadDown
SCN.gamepadUp=S.gamepadUp
SCN.fileDropped=S.fileDropped
SCN.directoryDropped=S.directoryDropped
SCN.resize=S.resize
SCN.update=S.update
SCN.draw=S.draw
if S.enter then
S.enter()
end
end
function SCN.push(tar)
table.insert(SCN.stack,tar or SCN.stack[#SCN.stack])
end
function SCN.pop()
table.remove(SCN.stack)
end
local swap={
none={
duration=0,changeTime=0,
draw=function() end
},
flash={
duration=.16,changeTime=.08,
draw=function() GC.clear(1,1,1) end
},
fade={
duration=.5,changeTime=.25,
draw=function(t)
t=t>.25 and 2-t*4 or t*4
GC.setColor(0,0,0,t)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end
},
fade_togame={
duration=2,changeTime=.5,
draw=function(t)
t=t>.5 and (2-t)/1.5 or t*.5
GC.setColor(0,0,0,t)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end
},
slowFade={
duration=3,changeTime=1.5,
draw=function(t)
t=t>1.5 and (3-t)/1.5 or t/1.5
GC.setColor(0,0,0,t)
GC.rectangle('fill',0,0,SCR.w,SCR.h)
end
},
swipeL={
duration=.5,changeTime=.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,changeTime=.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,changeTime=.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
},
}-- Scene swapping animations
function SCN.swapTo(tar,style,...)-- Parallel scene swapping, cannot back
if scenes[tar] then
if not SCN.swapping then
SCN.prev=SCN.stack[#SCN.stack]
style=style or 'fade'
SCN.swapping=true
SCN.args={...}
local S=SCN.state
S.tar,S.style=tar,style
S.time=swap[style].duration
S.changeTime=swap[style].changeTime
S.draw=swap[style].draw
end
else
MES.new('warn',"No Scene: "..tostring(tar))
end
end
function SCN.go(tar,style,...)-- Normal scene swapping, can back
if scenes[tar] then
if not SCN.swapping then
SCN.push(SCN.stack[#SCN.stack] or '_')
SCN.swapTo(tar,style,...)
end
else
MES.new('warn',"No Scene: "..tar)
end
end
function SCN.back(style,...)
if SCN.swapping then return end
-- Leave scene
if SCN.leave then
SCN.leave()
end
-- Poll&Back to previous Scene
if #SCN.stack>1 then
SCN.pop()
SCN.swapTo(SCN.stack[#SCN.stack],style,...)
else
SCN.swapTo('quit','slowFade')
end
end
function SCN.backTo(tar,style,...)
if SCN.swapping then return end
-- Leave scene
if SCN.leave then
SCN.leave()
end
-- Poll&Back to previous Scene
while SCN.stack[#SCN.stack]~=tar and #SCN.stack>1 do
SCN.pop()
end
SCN.swapTo(SCN.stack[#SCN.stack],style,...)
end
function SCN.printStack()
for i=0,#SCN.stack+1 do print(SCN.stack[i] or "-------") end
end
return SCN