-
Notifications
You must be signed in to change notification settings - Fork 63
/
FlxUIPopup.hx
315 lines (265 loc) · 7.25 KB
/
FlxUIPopup.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
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
package flixel.addons.ui;
import flixel.addons.ui.interfaces.IFlxUIButton;
import flixel.addons.ui.interfaces.IFlxUIState;
import flixel.addons.ui.interfaces.IFlxUIWidget;
import flixel.math.FlxPoint;
import flixel.util.FlxDirectionFlags;
class FlxUIPopup extends FlxUISubState implements IFlxUIWidget
{
public var name:String;
public var broadcastToFlxUI:Bool = true;
/**STUBS TO MAKE THE INTERFACE HAPPY:**/
public var immovable(default, set):Bool;
private function set_immovable(Immovable:Bool):Bool
{
return immovable;
}
public var angle(default, set):Float;
public var facing(default, set):FlxDirectionFlags;
public var moves(default, set):Bool;
public var offset(default, set):FlxPoint;
public var origin(default, set):FlxPoint;
public var scale(default, set):FlxPoint;
public var velocity:FlxPoint;
public var maxVelocity:FlxPoint;
public var acceleration:FlxPoint;
public var drag:FlxPoint;
public var scrollFactor(default, set):FlxPoint;
private function set_x(Value:Float):Float
{
return x = Value;
}
private function set_y(Value:Float):Float
{
return y = Value;
}
private function get_width():Float
{
return _width;
}
private function get_height():Float
{
return _height;
}
private function set_width(Value:Float):Float
{
return _width = Value;
}
private function set_height(Value:Float):Float
{
return _height = Value;
}
private function set_angle(Value:Float):Float
{
return angle = Value;
}
private function set_alpha(Value:Float):Float
{
return alpha = Value;
}
private function set_facing(Value:Int):Int
{
return facing = Value;
}
private function set_moves(Value:Bool):Bool
{
return moves = Value;
}
private function set_offset(Value:FlxPoint):FlxPoint
{
return offset = Value;
}
private function set_origin(Value:FlxPoint):FlxPoint
{
return origin = Value;
}
private function set_scale(Value:FlxPoint):FlxPoint
{
return scale = Value;
}
private function set_scrollFactor(Value:FlxPoint):FlxPoint
{
return scrollFactor = Value;
}
public function reset(X:Float, Y:Float):Void
{
setPosition(X, Y);
}
public function setPosition(X:Float = 0, Y:Float = 0):Void
{
x = X;
y = Y;
}
public var x(default, set):Float = 0;
public var y(default, set):Float = 0;
public var params:Array<Dynamic> = null;
public var alpha(default, set):Float = 1;
public var width(get, set):Float;
public var height(get, set):Float;
private var _width:Float = 0;
private var _height:Float = 0;
public static inline var CLICK_EVENT:String = "click_popup";
/**************************************/
public override function create():Void
{
if (_xml_id == "")
{
_xml_id = FlxUIAssets.XML_DEFAULT_POPUP_ID;
}
scrollFactor = FlxPoint.get(0, 0);
getTextFallback = myGetTextFallback;
super.create();
if (_quickSetupParams != null)
{
_doQuickSetup();
}
scrollFactor.set(0, 0);
_ui.setScrollFactor(0, 0);
}
/**
* Assuming you use the default format, puts this information in the popup.
* This function ONLY works if you are using the default_popup.xml, OR your
* custom xml contains the following assets:
* 2 texts, ids: "title","body"
* 3 buttons, ids: "btn0","btn1","btn2"
* 3 modes, ids: "1btn","2btn","3btn"
* @param title title text
* @param body body text
* @param button_labels up to three button labels - if fewer, shows less buttons
*/
public function quickSetup(title:String, body:String, button_labels:Array<String>):Void
{
/* if this sub state isn't active yet, it just stores the params and then
* does the real work as soon as it's created.
* So call this, then activate it!
*/
_quickSetupParams = {title: title, body: body, button_labels: button_labels};
}
public override function getEvent(id:String, sender:IFlxUIWidget, data:Dynamic, ?eventParams:Array<Dynamic>):Void
{
if (eventParams == null)
{
if (params != null)
{
eventParams = [];
}
}
if (params != null)
{
eventParams = eventParams.concat(params);
}
switch (id)
{
case FlxUITypedButton.CLICK_EVENT:
var str = "";
if (eventParams != null)
{
if ((eventParams[0] is String))
{
str = Std.string(eventParams[0]);
}
var buttonAmount:Int = Std.int(eventParams[0]);
if (str == "affirm" || str == "cancel" || str == "alt")
{
if ((_parentState is IFlxUIState))
{
// This fixes a bug where the event was being sent to this popup rather than the state that created it
castParent().getEvent(CLICK_EVENT, this, buttonAmount, eventParams);
}
else
{
// This is a generic fallback in case something goes wrong
FlxUI.event(CLICK_EVENT, this, buttonAmount, eventParams);
}
close();
}
}
}
super.getEvent(id, sender, data, eventParams);
}
private function castParent():IFlxUIState
{
return cast _parentState;
}
private var _quickSetupParams:{title:String, body:String, button_labels:Array<String>} = null;
// This function is passed into the UI object as a default in case the user is not using FireTongue
private function myGetTextFallback(flag:String, context:String = "ui", safe:Bool = true):String
{
switch (flag)
{
case "$POPUP_YES":
return "Yes";
case "$POPUP_NO":
return "No";
case "$POPUP_OK":
return "Ok";
case "$POPUP_CANCEL":
return "Cancel";
case "$POPUP_TITLE_DEFAULT":
return "Alert!";
case "$POPUP_BODY_DEFAULT":
return
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam consectetur vehicula pellentesque. Phasellus at blandit augue. Suspendisse vel leo ut elit imperdiet eleifend ut quis purus. Quisque imperdiet turpis vitae justo hendrerit molestie. Quisque tempor ante eget posuere viverra.";
}
return flag;
}
private function _doQuickSetup():Void
{
if (_ui.hasAsset("title"))
{
var text_title:FlxUIText = cast _ui.getAsset("title");
text_title.text = _quickSetupParams.title;
}
if (_ui.hasAsset("body"))
{
var text_body:FlxUIText = cast _ui.getAsset("body");
text_body.text = _quickSetupParams.body;
}
_doQuickSetupButtons();
// cleanup
_quickSetupParams.button_labels = null;
_quickSetupParams = null;
}
private function _doQuickSetupButtons():Void
{
var arr:Array<String> = ["btn0", "btn1", "btn2"];
var i:Int = 0;
switch (_quickSetupParams.button_labels.length)
{
case 1:
_ui.setMode("1btn");
case 2:
_ui.setMode("2btn");
case 3:
_ui.setMode("3btn");
}
for (btn in arr)
{
var the_btn:IFlxUIButton;
if (_ui.hasAsset(btn))
{
the_btn = cast _ui.getAsset(btn);
if (_quickSetupParams.button_labels.length > i)
{
var btnlabel:String = _quickSetupParams.button_labels[i];
var newlabel:String = btnlabel;
// localize common flags:
switch (btnlabel)
{
case "<yes>", "<no>", "<cancel>":
btnlabel = btnlabel.substr(1, btnlabel.length - 2).toUpperCase(); // carve off the "<>", so "yes", "no", etc
newlabel = "$POPUP_" + btnlabel; // make it "$POPUP_YES", "$POPUP_NO", etc
newlabel = _ui.getText(newlabel, "ui", false); // localize it
if (newlabel == null || newlabel == "")
{ // if failed
newlabel = btnlabel;
}
btnlabel = newlabel;
}
U.setButtonLabel(the_btn, newlabel);
}
}
i++;
}
}
}