-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBasicButton.hx
More file actions
450 lines (380 loc) · 13.1 KB
/
BasicButton.hx
File metadata and controls
450 lines (380 loc) · 13.1 KB
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
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.controls;
import feathers.core.FeathersControl;
import feathers.core.IMeasureObject;
import feathers.core.IStateContext;
import feathers.core.IStateObserver;
import feathers.core.IUIControl;
import feathers.core.IValidating;
import feathers.events.FeathersEvent;
import feathers.events.TriggerEvent;
import feathers.layout.Measurements;
import feathers.skins.IProgrammaticSkin;
import feathers.utils.KeyToState;
import feathers.utils.MeasurementsUtil;
import feathers.utils.PointerToState;
import feathers.utils.PointerTrigger;
import openfl.display.DisplayObject;
import openfl.display.InteractiveObject;
import openfl.events.MouseEvent;
import openfl.events.TouchEvent;
/**
A simple button control with states, but no content, that is useful for
purposes like skinning. For a more full-featured button, with a label and
icon, see `feathers.controls.Button` instead.
@event feathers.events.FeathersEvent.STATE_CHANGE Dispatched when
`BasicButton.currentState` changes.
@event feathers.events.TriggerEvent.TRIGGER Dispatched when the the user
taps or clicks the button. The pointer must remain within the bounds of the
button on release to register as a tap or a click. If focus management is
enabled, the button may also be triggered by pressing the spacebar while the
button has focus.
@since 1.0.0
@see `feathers.controls.Button`
**/
@:event(feathers.events.FeathersEvent.STATE_CHANGE)
@:event(feathers.events.TriggerEvent.TRIGGER)
class BasicButton extends FeathersControl implements ITriggerView implements IStateContext<ButtonState> {
/**
Creates a new `BasicButton` object.
@since 1.0.0
**/
public function new(?triggerListener:(TriggerEvent) -> Void) {
super();
// MouseEvent.CLICK is dispatched only if the same object is under the
// pointer for both MouseEvent.MOUSE_DOWN and MouseEvent.MOUSE_UP. The
// button might change skins between ButtonState.UP and
// ButtonState.DOWN, and this would prevent MouseEvent.CLICK.
// setting mouseChildren to false keeps the button as the target.
this.mouseChildren = false;
// when focused, keyboard space/enter trigger MouseEvent.CLICK
this.buttonMode = true;
// a hand cursor only makes sense for hyperlinks
this.useHandCursor = false;
this.addEventListener(MouseEvent.CLICK, basicButton_clickHandler);
this.addEventListener(TouchEvent.TOUCH_TAP, basicButton_touchTapHandler);
if (triggerListener != null) {
this.addEventListener(TriggerEvent.TRIGGER, triggerListener);
}
}
private var _currentState:ButtonState = UP;
/**
The current state of the button.
When the value of the `currentState` property changes, the button will
dispatch an event of type `FeathersEvent.STATE_CHANGE`.
@see `feathers.controls.ButtonState`
@see `feathers.events.FeathersEvent.STATE_CHANGE`
@since 1.0.0
**/
@:bindable("stateChange")
public var currentState(get, never):#if flash Dynamic #else ButtonState #end;
private function get_currentState():#if flash Dynamic #else ButtonState #end {
return this._currentState;
}
override private function set_enabled(value:Bool):Bool {
super.enabled = value;
if (this._enabled) {
if (this._currentState == DISABLED) {
this.changeState(UP);
}
} else {
this.changeState(DISABLED);
}
return this._enabled;
}
private var _pointerToState:PointerToState<ButtonState> = null;
private var _keyToState:KeyToState<ButtonState> = null;
private var _pointerTrigger:PointerTrigger = null;
private var _backgroundSkinMeasurements:Measurements = null;
private var _currentBackgroundSkin:DisplayObject = null;
/**
Determines if a pressed button should remain in the down state if the
pointer moves outside of the button's bounds. Useful for controls like
`HSlider`, `VSlider`, or `ToggleSwitch` to keep a thumb in the down
state while it is being dragged around by the pointer.
The following example ensures that the button's down state remains
active on roll out.
```haxe
button.keepDownStateOnRollOut = true;
```
@since 1.0.0
**/
@:style
public var keepDownStateOnRollOut:Bool = false;
/**
The display object to use as the background skin for the button.
To render a different background skin, depending on the button's current
state, pass additional skins to `setSkinForState()`.
The following example passes a bitmap for the button to use as a
background skin:
```haxe
button.backgroundSkin = new Bitmap(bitmapData);
```
@default null
@see `BasicButton.getSkinForState()`
@see `BasicButton.setSkinForState()`
@since 1.0.0
**/
@:style
public var backgroundSkin:DisplayObject = null;
private var _stateToSkin:Map<ButtonState, DisplayObject> = new Map();
/**
Gets the skin to be used by the button when its `currentState` property
matches the specified state value.
If a skin is not defined for a specific state, returns `null`.
@see `BasicButton.setSkinForState()`
@see `BasicButton.backgroundSkin`
@see `BasicButton.currentState`
@see `feathers.controls.ButtonState`
@since 1.0.0
**/
public function getSkinForState(state:ButtonState):DisplayObject {
return this._stateToSkin.get(state);
}
/**
Set the skin to be used by the button when its `currentState` property
matches the specified state value.
If a skin is not defined for a specific state, the value of the
`backgroundSkin` property will be used instead.
@see `BasicButton.getSkinForState()`
@see `BasicButton.backgroundSkin`
@see `BasicButton.currentState`
@see `feathers.controls.ButtonState`
@since 1.0.0
**/
@style
public function setSkinForState(state:ButtonState, skin:DisplayObject):Void {
if (!this.setStyle("setSkinForState", state)) {
return;
}
var oldSkin = this._stateToSkin.get(state);
if (oldSkin != null && oldSkin == this._currentBackgroundSkin) {
this.removeCurrentBackgroundSkin(oldSkin);
this._currentBackgroundSkin = null;
}
if (skin == null) {
this._stateToSkin.remove(state);
} else {
this._stateToSkin.set(state, skin);
}
this.setInvalid(STYLES);
}
override private function initialize():Void {
super.initialize();
if (this._pointerToState == null) {
this._pointerToState = new PointerToState(this, this.changeState, UP, DOWN, HOVER);
}
if (this._keyToState == null) {
this._keyToState = new KeyToState(this, this.changeState, UP, DOWN);
}
if (this._pointerTrigger == null) {
this._pointerTrigger = new PointerTrigger(this);
}
}
override private function update():Void {
this.commitChanges();
this.measure();
this.layoutContent();
}
private function commitChanges():Void {
var stylesInvalid = this.isInvalid(STYLES);
var stateInvalid = this.isInvalid(STATE);
if (stylesInvalid || stateInvalid) {
this.refreshBackgroundSkin();
}
if (stylesInvalid) {
this.refreshInteractivity();
}
for (i in 0...numChildren) {
var child = getChildAt(i);
if ((child is InteractiveObject)) {
(cast child : InteractiveObject).doubleClickEnabled = this.doubleClickEnabled;
}
}
}
private function layoutContent():Void {
this.layoutBackgroundSkin();
}
private function refreshInteractivity():Void {
this._pointerToState.keepDownStateOnRollOut = this.keepDownStateOnRollOut;
}
private function refreshBackgroundSkin():Void {
var oldSkin = this._currentBackgroundSkin;
this._currentBackgroundSkin = this.getCurrentBackgroundSkin();
if (this._currentBackgroundSkin == oldSkin) {
return;
}
this.removeCurrentBackgroundSkin(oldSkin);
this.addCurrentBackgroundSkin(this._currentBackgroundSkin);
}
private function getCurrentBackgroundSkin():DisplayObject {
var result = this._stateToSkin.get(this._currentState);
if (result != null) {
return result;
}
return this.backgroundSkin;
}
private function addCurrentBackgroundSkin(skin:DisplayObject):Void {
if (skin == null) {
this._backgroundSkinMeasurements = null;
return;
}
if ((skin is IUIControl)) {
(cast skin : IUIControl).initializeNow();
}
if (this._backgroundSkinMeasurements == null) {
this._backgroundSkinMeasurements = new Measurements(skin);
} else {
this._backgroundSkinMeasurements.save(skin);
}
if ((skin is IProgrammaticSkin)) {
(cast skin : IProgrammaticSkin).uiContext = this;
}
if ((skin is IStateObserver)) {
(cast skin : IStateObserver).stateContext = this;
}
this.addChildAt(skin, 0);
}
private function removeCurrentBackgroundSkin(skin:DisplayObject):Void {
if (skin == null) {
return;
}
if ((skin is IProgrammaticSkin)) {
(cast skin : IProgrammaticSkin).uiContext = null;
}
if ((skin is IStateObserver)) {
(cast skin : IStateObserver).stateContext = null;
}
// we need to restore these values so that they won't be lost the
// next time that this skin is used for measurement
this._backgroundSkinMeasurements.restore(skin);
if (skin.parent == this) {
this.removeChild(skin);
}
}
private function measure():Bool {
var needsWidth = this.explicitWidth == null;
var needsHeight = this.explicitHeight == null;
var needsMinWidth = this.explicitMinWidth == null;
var needsMinHeight = this.explicitMinHeight == null;
var needsMaxWidth = this.explicitMaxWidth == null;
var needsMaxHeight = this.explicitMaxHeight == null;
if (!needsWidth && !needsHeight && !needsMinWidth && !needsMinHeight && !needsMaxWidth && !needsMaxHeight) {
return false;
}
if (this._currentBackgroundSkin != null) {
MeasurementsUtil.resetFluidlyWithParent(this._backgroundSkinMeasurements, this._currentBackgroundSkin, this);
}
var measureSkin:IMeasureObject = null;
if ((this._currentBackgroundSkin is IMeasureObject)) {
measureSkin = cast this._currentBackgroundSkin;
}
if ((this._currentBackgroundSkin is IValidating)) {
(cast this._currentBackgroundSkin : IValidating).validateNow();
}
var newWidth = this.explicitWidth;
if (needsWidth) {
if (this._currentBackgroundSkin != null) {
newWidth = this._currentBackgroundSkin.width;
} else {
newWidth = 0.0;
}
}
var newHeight = this.explicitHeight;
if (needsHeight) {
if (this._currentBackgroundSkin != null) {
newHeight = this._currentBackgroundSkin.height;
} else {
newHeight = 0.0;
}
}
var newMinWidth = this.explicitMinWidth;
if (needsMinWidth) {
if (measureSkin != null) {
newMinWidth = measureSkin.minWidth;
} else if (this._backgroundSkinMeasurements != null && this._backgroundSkinMeasurements.minWidth != null) {
newMinWidth = this._backgroundSkinMeasurements.minWidth;
} else {
newMinWidth = 0.0;
}
}
var newMinHeight = this.explicitMinHeight;
if (needsMinHeight) {
if (measureSkin != null) {
newMinHeight = measureSkin.minHeight;
} else if (this._backgroundSkinMeasurements != null && this._backgroundSkinMeasurements.minHeight != null) {
newMinHeight = this._backgroundSkinMeasurements.minHeight;
} else {
newMinHeight = 0.0;
}
}
var newMaxWidth = this.explicitMaxWidth;
if (needsMaxWidth) {
if (measureSkin != null) {
newMaxWidth = measureSkin.maxWidth;
} else if (this._backgroundSkinMeasurements != null && this._backgroundSkinMeasurements.maxWidth != null) {
newMaxWidth = this._backgroundSkinMeasurements.maxWidth;
} else {
newMaxWidth = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
}
var newMaxHeight = this.explicitMaxHeight;
if (needsMaxHeight) {
if (measureSkin != null) {
newMaxHeight = measureSkin.maxHeight;
} else if (this._backgroundSkinMeasurements != null && this._backgroundSkinMeasurements.maxHeight != null) {
newMaxHeight = this._backgroundSkinMeasurements.maxHeight;
} else {
newMaxHeight = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
}
return this.saveMeasurements(newWidth, newHeight, newMinWidth, newMinHeight, newMaxWidth, newMaxHeight);
}
private function layoutBackgroundSkin():Void {
if (this._currentBackgroundSkin == null) {
return;
}
this._currentBackgroundSkin.x = 0.0;
this._currentBackgroundSkin.y = 0.0;
// don't set the width or height explicitly unless necessary because if
// our explicit dimensions are cleared later, the measurement may not be
// accurate anymore
if (this._currentBackgroundSkin.width != this.actualWidth) {
this._currentBackgroundSkin.width = this.actualWidth;
}
if (this._currentBackgroundSkin.height != this.actualHeight) {
this._currentBackgroundSkin.height = this.actualHeight;
}
if ((this._currentBackgroundSkin is IValidating)) {
(cast this._currentBackgroundSkin : IValidating).validateNow();
}
}
private function changeState(state:ButtonState):Void {
if (!this._enabled) {
state = DISABLED;
}
if (this._currentState == state) {
return;
}
this._currentState = state;
this.setInvalid(STATE);
FeathersEvent.dispatch(this, FeathersEvent.STATE_CHANGE);
}
private function basicButton_clickHandler(event:MouseEvent):Void {
if (!this._enabled) {
event.stopImmediatePropagation();
return;
}
}
private function basicButton_touchTapHandler(event:TouchEvent):Void {
if (!this._enabled) {
event.stopImmediatePropagation();
return;
}
}
}