forked from HaxeFlixel/flixel-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlxUIDropDownMenu.hx
575 lines (497 loc) · 13.1 KB
/
FlxUIDropDownMenu.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package flixel.addons.ui;
import openfl.geom.Rectangle;
import flixel.addons.ui.interfaces.IFlxUIClickable;
import flixel.addons.ui.interfaces.IFlxUIWidget;
import flixel.addons.ui.interfaces.IHasParams;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.math.FlxMath;
import flixel.ui.FlxButton;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxStringUtil;
#if (flixel < version("5.7.0"))
import flixel.ui.FlxButton.NORMAL;
import flixel.ui.FlxButton.HIGHLIGHT;
import flixel.ui.FlxButton.PRESSED;
#else
import flixel.ui.FlxButton.FlxButtonState.NORMAL;
import flixel.ui.FlxButton.FlxButtonState.HIGHLIGHT;
import flixel.ui.FlxButton.FlxButtonState.PRESSED;
import flixel.ui.FlxButton.FlxButtonState.DISABLED;
#end
/**
* @author larsiusprime
*/
class FlxUIDropDownMenu extends FlxUIGroup implements IFlxUIWidget implements IFlxUIClickable implements IHasParams
{
public var skipButtonUpdate(default, set):Bool;
private function set_skipButtonUpdate(b:Bool):Bool
{
skipButtonUpdate = b;
header.button.skipButtonUpdate = b;
return b;
}
public var selectedId(get, set):String;
public var selectedLabel(get, set):String;
private var _selectedId:String;
private var _selectedLabel:String;
private function get_selectedId():String
{
return _selectedId;
}
private function set_selectedId(str:String):String
{
if (_selectedId == str)
return str;
var i:Int = 0;
for (btn in list)
{
if (btn != null && btn.name == str)
{
var item:FlxUIButton = list[i];
_selectedId = str;
if (item.label != null)
{
_selectedLabel = item.label.text;
header.text.text = item.label.text;
}
else
{
_selectedLabel = "";
header.text.text = "";
}
return str;
}
i++;
}
return str;
}
private function get_selectedLabel():String
{
return _selectedLabel;
}
private function set_selectedLabel(str:String):String
{
if (_selectedLabel == str)
return str;
var i:Int = 0;
for (btn in list)
{
if (btn.label.text == str)
{
var item:FlxUIButton = list[i];
_selectedId = item.name;
_selectedLabel = str;
header.text.text = str;
return str;
}
i++;
}
return str;
}
/**
* The header of this dropdown menu.
*/
public var header:FlxUIDropDownHeader;
/**
* The list of items that is shown when the toggle button is clicked.
*/
public var list:Array<FlxUIButton> = [];
/**
* The background for the list.
*/
public var dropPanel:FlxUI9SliceSprite;
public var params(default, set):Array<Dynamic>;
private function set_params(p:Array<Dynamic>):Array<Dynamic>
{
return params = p;
}
public var dropDirection(default, set):FlxUIDropDownMenuDropDirection = Automatic;
private function set_dropDirection(dropDirection):FlxUIDropDownMenuDropDirection
{
this.dropDirection = dropDirection;
updateButtonPositions();
return dropDirection;
}
public static inline var CLICK_EVENT:String = "click_dropdown";
public var callback:String->Void;
// private var _ui_control_callback:Bool->FlxUIDropDownMenu->Void;
/**
* This creates a new dropdown menu.
*
* @param X x position of the dropdown menu
* @param Y y position of the dropdown menu
* @param DataList The data to be displayed
* @param Callback Optional Callback
* @param Header The header of this dropdown menu
* @param DropPanel Optional 9-slice-background for actual drop down menu
* @param ButtonList Optional list of buttons to be used for the corresponding entry in DataList
* @param UIControlCallback Used internally by FlxUI
*/
public function new(X:Float = 0, Y:Float = 0, DataList:Array<StrNameLabel>, ?Callback:String->Void, ?Header:FlxUIDropDownHeader,
?DropPanel:FlxUI9SliceSprite, ?ButtonList:Array<FlxUIButton>, ?UIControlCallback:Bool->FlxUIDropDownMenu->Void)
{
super(X, Y);
callback = Callback;
header = Header;
dropPanel = DropPanel;
if (header == null)
header = new FlxUIDropDownHeader();
if (dropPanel == null)
{
var rect = new Rectangle(0, 0, header.background.width, header.background.height);
dropPanel = new FlxUI9SliceSprite(0, 0, FlxUIAssets.IMG_BOX, rect, [1, 1, 14, 14]);
}
if (DataList != null)
{
for (i in 0...DataList.length)
{
var data = DataList[i];
list.push(makeListButton(i, data.label, data.name));
}
selectSomething(DataList[0].name, DataList[0].label);
}
else if (ButtonList != null)
{
for (btn in ButtonList)
{
list.push(btn);
btn.resize(header.background.width, header.background.height);
btn.x = 1;
}
}
updateButtonPositions();
dropPanel.resize(header.background.width, getPanelHeight());
dropPanel.visible = false;
add(dropPanel);
for (btn in list)
{
add(btn);
btn.visible = false;
}
// _ui_control_callback = UIControlCallback;
header.button.onUp.callback = onDropdown;
add(header);
}
private function updateButtonPositions():Void
{
var buttonHeight = header.background.height;
dropPanel.y = header.background.y;
if (dropsUp())
dropPanel.y -= getPanelHeight();
else
dropPanel.y += buttonHeight;
var offset = dropPanel.y;
for (button in list)
{
button.y = offset;
offset += buttonHeight;
}
}
override function set_visible(Value:Bool):Bool
{
var vDropPanel = dropPanel.visible;
var vButtons = [];
for (i in 0...list.length)
{
if (list[i] != null)
{
vButtons.push(list[i].visible);
}
else
{
vButtons.push(false);
}
}
super.set_visible(Value);
dropPanel.visible = vDropPanel;
for (i in 0...list.length)
{
if (list[i] != null)
{
list[i].visible = vButtons[i];
}
}
return Value;
}
private function dropsUp():Bool
{
return dropDirection == Up || (dropDirection == Automatic && exceedsHeight());
}
private function exceedsHeight():Bool
{
return y + getPanelHeight() + header.background.height > FlxG.height;
}
private function getPanelHeight():Float
{
return list.length * header.background.height;
}
/**
* Change the contents with a new data list
* Replaces the old content with the new content
*/
public function setData(DataList:Array<StrNameLabel>):Void
{
var i:Int = 0;
if (DataList != null)
{
for (data in DataList)
{
var recycled:Bool = false;
if (list != null)
{
if (i <= list.length - 1)
{ // If buttons exist, try to re-use them
var btn:FlxUIButton = list[i];
if (btn != null)
{
btn.label.text = data.label; // Set the label
list[i].name = data.name; // Replace the name
recycled = true; // we successfully recycled it
}
}
}
else
{
list = [];
}
if (!recycled)
{ // If we couldn't recycle a button, make a fresh one
var t:FlxUIButton = makeListButton(i, data.label, data.name);
list.push(t);
add(t);
t.visible = false;
}
i++;
}
// Remove excess buttons:
if (list.length > DataList.length)
{ // we have more entries in the original set
for (j in DataList.length...list.length)
{ // start counting from end of list
var b:FlxUIButton = list.pop(); // remove last button on list
b.visible = false;
b.active = false;
remove(b, true); // remove from widget
b.destroy(); // destroy it
b = null;
}
}
selectSomething(DataList[0].name, DataList[0].label);
}
dropPanel.resize(header.background.width, getPanelHeight());
updateButtonPositions();
}
private function selectSomething(name:String, label:String):Void
{
header.text.text = label;
selectedId = name;
selectedLabel = label;
}
private function makeListButton(i:Int, Label:String, Name:String):FlxUIButton
{
var t:FlxUIButton = new FlxUIButton(0, 0, Label);
t.broadcastToFlxUI = false;
t.onUp.callback = onClickItem.bind(i);
t.name = Name;
t.loadGraphicSlice9([FlxUIAssets.IMG_INVIS, FlxUIAssets.IMG_HILIGHT, FlxUIAssets.IMG_HILIGHT], Std.int(header.background.width),
Std.int(header.background.height), [[1, 1, 3, 3], [1, 1, 3, 3], [1, 1, 3, 3]], FlxUI9SliceSprite.TILE_NONE);
t.labelOffsets[PRESSED].y -= 1; // turn off the 1-pixel depress on click
t.up_color = FlxColor.BLACK;
t.over_color = FlxColor.WHITE;
t.down_color = FlxColor.WHITE;
t.resize(header.background.width - 2, header.background.height - 1);
t.label.alignment = "left";
t.autoCenterLabel();
t.x = 1;
for (offset in t.labelOffsets)
{
offset.x += 2;
}
return t;
}
/*public function setUIControlCallback(UIControlCallback:Bool->FlxUIDropDownMenu->Void):Void {
_ui_control_callback = UIControlCallback;
}*/
public function changeLabelByIndex(i:Int, NewLabel:String):Void
{
var btn:FlxUIButton = getBtnByIndex(i);
if (btn != null && btn.label != null)
{
btn.label.text = NewLabel;
}
}
public function changeLabelById(name:String, NewLabel:String):Void
{
var btn:FlxUIButton = getBtnById(name);
if (btn != null && btn.label != null)
{
btn.label.text = NewLabel;
}
}
public function getBtnByIndex(i:Int):FlxUIButton
{
if (i >= 0 && i < list.length)
{
return list[i];
}
return null;
}
public function getBtnById(name:String):FlxUIButton
{
for (btn in list)
{
if (btn.name == name)
{
return btn;
}
}
return null;
}
public override function update(elapsed:Float):Void
{
super.update(elapsed);
#if FLX_MOUSE
if (dropPanel.visible && FlxG.mouse.justPressed)
{
if (!FlxG.mouse.overlaps(this))
{
showList(false);
}
}
#end
}
override public function destroy():Void
{
super.destroy();
dropPanel = FlxDestroyUtil.destroy(dropPanel);
list = FlxDestroyUtil.destroyArray(list);
// _ui_control_callback = null;
callback = null;
}
private function showList(b:Bool):Void
{
for (button in list)
{
button.visible = b;
button.active = b;
}
dropPanel.visible = b;
FlxUI.forceFocus(b, this); // avoid overlaps
}
private function onDropdown():Void
{
(dropPanel.visible) ? showList(false) : showList(true);
}
private function onClickItem(i:Int):Void
{
var item:FlxUIButton = list[i];
selectSomething(item.name, item.label.text);
showList(false);
if (callback != null)
{
callback(item.name);
}
if (broadcastToFlxUI)
{
FlxUI.event(CLICK_EVENT, this, item.name, params);
}
}
/**
* Helper function to easily create a data list for a dropdown menu from an array of strings.
*
* @param StringArray The strings to use as data - used for both label and string ID.
* @param UseIndexID Whether to use the integer index of the current string as ID.
* @return The StrIDLabel array ready to be used in FlxUIDropDownMenu's constructor
*/
public static function makeStrIdLabelArray(StringArray:Array<String>, UseIndexID:Bool = false):Array<StrNameLabel>
{
var strIdArray:Array<StrNameLabel> = [];
for (i in 0...StringArray.length)
{
var ID:String = StringArray[i];
if (UseIndexID)
{
ID = Std.string(i);
}
strIdArray[i] = new StrNameLabel(ID, StringArray[i]);
}
return strIdArray;
}
}
/**
* Header for a FlxUIDropDownMenu
*/
class FlxUIDropDownHeader extends FlxUIGroup
{
/**
* The background of the header.
*/
public var background:FlxSprite;
/**
* The text that displays the currently selected item.
*/
public var text:FlxUIText;
/**
* The button that toggles the visibility of the dropdown panel.
*/
public var button:FlxUISpriteButton;
/**
* Creates a new dropdown header to be used in a FlxUIDropDownMenu.
*
* @param Width Width of the dropdown - only relevant when no back sprite was specified
* @param Back Optional sprite to be placed in the background
* @param Text Optional text that displays the current value
* @param Button Optional button that toggles the dropdown list
*/
public function new(Width:Int = 120, ?Background:FlxSprite, ?Text:FlxUIText, ?Button:FlxUISpriteButton)
{
super();
background = Background;
text = Text;
button = Button;
// Background
if (background == null)
{
background = new FlxUI9SliceSprite(0, 0, FlxUIAssets.IMG_BOX, new Rectangle(0, 0, Width, 20), [1, 1, 14, 14]);
}
// Button
if (button == null)
{
button = new FlxUISpriteButton(0, 0, new FlxSprite(0, 0, FlxUIAssets.IMG_DROPDOWN));
button.loadGraphicSlice9([FlxUIAssets.IMG_BUTTON_THIN], 80, 20, [FlxStringUtil.toIntArray(FlxUIAssets.SLICE9_BUTTON)],
FlxUI9SliceSprite.TILE_NONE, -1, false, FlxUIAssets.IMG_BUTTON_SIZE, FlxUIAssets.IMG_BUTTON_SIZE);
}
button.resize(background.height, background.height);
button.x = background.x + background.width - button.width;
// Reposition and resize the button hitbox so the whole header is clickable
button.width = Width;
button.offset.x -= (Width - button.frameWidth);
button.x = offset.x;
button.label.offset.x += button.offset.x;
// Text
if (text == null)
{
text = new FlxUIText(0, 0, Std.int(background.width));
}
text.setPosition(2, 4);
text.color = FlxColor.BLACK;
add(background);
add(button);
add(text);
}
override public function destroy():Void
{
super.destroy();
background = FlxDestroyUtil.destroy(background);
text = FlxDestroyUtil.destroy(text);
button = FlxDestroyUtil.destroy(button);
}
}
enum FlxUIDropDownMenuDropDirection
{
Automatic;
Down;
Up;
}