-
Notifications
You must be signed in to change notification settings - Fork 4
/
WynScreen.hx
241 lines (189 loc) · 4.24 KB
/
WynScreen.hx
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
package wyn;
import kha.math.FastVector2;
import kha.graphics2.Graphics;
import wyn.util.WynUtil;
class WynScreen
{
// For debuggings
public static var ID:Int = 0;
public var isMostFront:Bool = false;
public var id:Int;
public var name:String = "";
public var alive:Bool = false; // flags whether this screen can be removed from queue
// Objects are arranged by layer, followed by array order
var children:Array<Array<WynObject>> = [];
// If true, will continue to update/render even if covered by other screens
public var persistentUpdate:Bool = false;
public var persistentRender:Bool = false;
// for scrolling the screen.
public var scrollX:Float = 0;
public var scrollY:Float = 0;
// for screen shake
var shakeHorizontal:Bool = false;
var shakeVertical:Bool = false;
var intensity:Float = 0;
var weakenRate:Float = 0;
public var shakeX:Float = 0;
public var shakeY:Float = 0;
// for subscreens
public var openCallbacks:Array<Void->Void>;
public var closeCallbacks:Array<Void->Void>;
public function new ()
{
id = ++ID;
children = [];
openCallbacks = [];
closeCallbacks = [];
}
public function update ()
{
// Handle camera shake logic
if (intensity > 0)
{
var point:FastVector2 = WynUtil.randomInCircle().mult(intensity);
shakeX = (shakeHorizontal) ? point.x : 0;
shakeY = (shakeVertical) ? point.y : 0;
intensity -= Wyngine.dt * weakenRate;
if (intensity <= 0)
{
shakeX = 0;
shakeY = 0;
}
}
// handle object updates
for (layer in children)
{
if (layer == null)
continue;
for (o in layer)
{
if (o.enabled && o.active)
o.update();
}
}
}
public function render (g:Graphics)
{
// handle object renders
for (layer in children)
{
if (layer == null)
continue;
for (o in layer)
tryRender(o, g);
}
}
function tryRender (o:WynObject, g:Graphics)
{
if (o.enabled && o.visible)
{
for (r in o.renderers)
r(g);
for (child in o.children)
tryRender(child, g);
}
}
public function destroy ()
{
for (layer in children)
{
for (o in layer)
o.destroy();
}
children = [];
openCallbacks = [];
closeCallbacks = [];
}
public function swapLayers (layer1:Int, layer2:Int)
{
// UNTESTED
if (children[layer1] != null &&
children[layer2] != null)
{
var t1 = children[layer1];
children[layer1] = children[layer2];
children[layer2] = t1;
}
}
public function addAt (o:WynObject, layer:Int=0, index:Int)
{
if (children[layer] == null)
children[layer] = [];
o.screen = this;
children[layer].insert(index, o);
}
public function addToFront (o:WynObject, layer:Int=0, offset:Int=0)
{
if (children[layer] == null)
children[layer] = [];
o.screen = this;
if (offset==0)
children[layer].push(o);
else
children[layer].insert(offset, o);
}
public function addToBack (o:WynObject, layer:Int=0, offset:Int=0)
{
if (children[layer] == null)
children[layer] = [];
o.screen = this;
if (offset==0)
children[layer].unshift(o);
else
children[layer].insert(children.length-offset, o);
}
public function remove (o:WynObject, layer:Int=0)
{
if (children[layer] == null)
{
trace("layer not found : " + layer);
return;
}
// remove target child from screen
o.screen = null;
children[layer].remove(o);
}
public function open ()
{
// override this to init or animate the screen before it appears
onOpen();
}
function onOpen ()
{
// override this if necessary
alive = true;
for (f in openCallbacks)
f();
}
public function close ()
{
// override this to init or animate the screen
onClose();
}
function onClose ()
{
// override this if necessary
for (f in closeCallbacks)
f();
// Wyngine will check for "alive" state -- if it's dead,
// will be removed from the queue
alive = false;
}
inline public function shake (_intensity:Float=10, _weakenRate:Float=20, _shakeHorizontal:Bool=true, _shakeVertical:Bool=true)
{
intensity = _intensity;
weakenRate = _weakenRate;
shakeHorizontal = _shakeHorizontal;
shakeVertical = _shakeVertical;
}
inline public function cancelEffects ()
{
// cancel screenshake
shakeHorizontal = false;
shakeVertical = false;
intensity = 0;
weakenRate = 0;
shakeX = 0;
shakeY = 0;
}
}