-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathFlxUICheckBox.hx
298 lines (251 loc) · 6.1 KB
/
FlxUICheckBox.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
package flixel.addons.ui;
import flixel.addons.ui.FlxUI.NamedBool;
import flixel.addons.ui.interfaces.ICursorPointable;
import flixel.addons.ui.interfaces.IFlxUIButton;
import flixel.addons.ui.interfaces.IFlxUIClickable;
import flixel.addons.ui.interfaces.IHasParams;
import flixel.addons.ui.interfaces.ILabeled;
import flixel.FlxSprite;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxTimer;
/**
* @author Lars Doucet
*/
class FlxUICheckBox extends FlxUIGroup implements ILabeled implements IFlxUIClickable implements IHasParams implements ICursorPointable
{
public var box:FlxSprite;
public var mark:FlxSprite;
public var button:FlxUIButton;
public var max_width:Float = -1;
public var checked(default, set):Bool = false;
public var params(default, set):Array<Dynamic>;
// Set this to false if you just want the checkbox itself to be clickable
public var textIsClickable:Bool = true;
public var checkbox_dirty:Bool = false;
public var textX(default, set):Float = 0;
public var textY(default, set):Float = 0;
public var box_space:Float = 2;
public var skipButtonUpdate(default, set):Bool = false;
public var callback:Void->Void;
public static inline var CLICK_EVENT:String = "click_check_box";
private function set_skipButtonUpdate(b:Bool):Bool
{
skipButtonUpdate = b;
button.skipButtonUpdate = skipButtonUpdate;
return skipButtonUpdate;
}
private function set_params(p:Array<Dynamic>):Array<Dynamic>
{
params = p;
if (params == null)
{
params = [];
}
var nb:NamedBool = {name: "checked", value: false};
params.push(nb);
return params;
}
private override function set_color(Value:Int):Int
{
if (button != null)
{
button.label.color = Value;
}
return super.set_color(Value);
}
public function new(X:Float = 0, Y:Float = 0, ?Box:Dynamic, ?Check:Dynamic, ?Label:String, ?LabelW:Int = 100, ?Params:Array<Dynamic>, ?Callback:Void->Void)
{
super();
callback = Callback;
params = Params;
if (Box == null)
{
// if null create a simple checkbox outline
Box = FlxUIAssets.IMG_CHECK_BOX;
}
if ((Box is FlxSprite))
{
box = cast Box;
}
else
{
box = new FlxSprite();
box.loadGraphic(Box, true);
}
button = new FlxUIButton(0, 0, Label, _clickCheck);
// set default checkbox label format
button.label.setFormat(null, 8, 0xffffff, "left", OUTLINE);
button.label.fieldWidth = LabelW;
button.up_color = 0xffffff;
button.down_color = 0xffffff;
button.over_color = 0xffffff;
button.up_toggle_color = 0xffffff;
button.down_toggle_color = 0xffffff;
button.over_toggle_color = 0xffffff;
button.loadGraphicSlice9(["", "", ""], Std.int(box.width + box_space + LabelW), Std.int(box.height));
max_width = Std.int(box.width + box_space + LabelW);
button.onUp.callback = _clickCheck; // for internal use, check/uncheck box, bubbles up to _externalCallback
if (Check == null)
{
// if null load from default assets:
Check = FlxUIAssets.IMG_CHECK_MARK;
}
if ((Check is FlxSprite))
{
mark = cast Check;
}
else
{
mark = new FlxSprite();
mark.loadGraphic(Check);
}
add(box);
add(mark);
add(button);
anchorLabelX();
anchorLabelY();
checked = false;
// set all these to 0
button.setAllLabelOffsets(0, 0);
x = X;
y = Y;
textX = 0;
textY = 0; // forces anchorLabel() to be called and upate correctly
}
/*
public function copy(?Params:Array<Dynamic>,?Callback:Void->Void):FlxUICheckBox {
var boxAsset:String = box != null ? box.cachedGraphics.key : null;
var checkAsset:String = mark != null ? mark.cachedGraphics.key : null;
var label:String = (button != null && button.label != null) ? button.label.text : null;
var labelW:Int = (label != null) ? Std.int(button.label.width) : 100;
return new FlxUICheckBox(x, y, boxAsset, checkAsset, label, labelW, Params, Callback);
}*/
/**For ILabeled:**/
public function setLabel(t:FlxUIText):FlxUIText
{
if (button == null)
{
return null;
}
button.label = t;
return button.label;
}
public function getLabel():FlxUIText
{
if (button == null)
{
return null;
}
return button.label;
}
private override function set_visible(Value:Bool):Bool
{
// don't cascade to my members
visible = Value;
return visible;
}
private function anchorTime(f:FlxTimer):Void
{
anchorLabelY();
}
private function set_textX(n:Float):Float
{
textX = n;
anchorLabelX();
return textX;
}
private function set_textY(n:Float):Float
{
textY = n;
anchorLabelY();
return textY;
}
public function anchorLabelX():Void
{
if (button != null)
{
button.label.offset.x = -((box.width + box_space) + textX);
}
}
public function anchorLabelY():Void
{
if (button != null)
{
button.y = box.y + (box.height - button.height) / 2 + textY;
}
}
public override function destroy():Void
{
super.destroy();
if (mark != null)
{
mark.destroy();
mark = null;
}
if (box != null)
{
box.destroy();
box = null;
}
if (button != null)
{
button.destroy();
button = null;
}
}
public var text(get, set):String;
private function get_text():String
{
return button.label.text;
}
private function set_text(value:String):String
{
button.label.text = value;
checkbox_dirty = true;
return value;
}
public override function update(elapsed:Float):Void
{
super.update(elapsed);
if (checkbox_dirty)
{
if (button.label != null)
{
if ((button.label is FlxUIText))
{
var ftu:FlxUIText = cast button.label;
ftu.drawFrame(); // force update
}
anchorLabelX();
anchorLabelY();
button.width = box.frameWidth + box_space +
button.label.textField.textWidth; // makes the clickable size exactly match the visible size of checkbox+label
checkbox_dirty = false;
}
}
}
/*****GETTER/SETTER***/
private function set_checked(b:Bool):Bool
{
mark.visible = b;
return checked = b;
}
/*****PRIVATE******/
private function _clickCheck():Void
{
if (!visible)
{
return;
}
checked = !checked;
if (callback != null)
{
callback();
}
if (broadcastToFlxUI)
{
FlxUI.event(FlxUICheckBox.CLICK_EVENT, this, checked, params);
}
}
}