-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathKeyToState.hx
More file actions
253 lines (206 loc) · 6.1 KB
/
KeyToState.hx
File metadata and controls
253 lines (206 loc) · 6.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
/*
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.utils;
import feathers.core.IFocusObject;
import feathers.core.IStateContext;
import openfl.display.InteractiveObject;
import openfl.display.Stage;
import openfl.events.Event;
import openfl.events.FocusEvent;
import openfl.events.KeyboardEvent;
import openfl.ui.Keyboard;
/**
Changes a target's state based on keyboard events, like a button.
@see `feathers.controls.Button`
@see `feathers.utils.PointToState`
@since 1.0.0
**/
class KeyToState<T> {
/**
Creates a new `KeyToState` object with the given arguments.
@since 1.0.0
**/
public function new(target:InteractiveObject = null, callback:(T) -> Void = null, upState:T, downState:T) {
this.target = target;
if (upState != null) {
this._upState = upState;
}
if (downState != null) {
this.downState = downState;
}
this._currentState = this._upState;
this.callback = callback;
}
private var _target:InteractiveObject = null;
/**
The target component that should change state based on pointer (mouse or
touch) events.
@since 1.0.0
**/
public var target(get, set):InteractiveObject;
private function get_target():InteractiveObject {
return this._target;
}
private function set_target(value:InteractiveObject):InteractiveObject {
if (this._target == value) {
return this._target;
}
if (this._target != null) {
this._target.removeEventListener(Event.REMOVED_FROM_STAGE, keyToState_target_removedFromStageHandler);
this._target.removeEventListener(FocusEvent.FOCUS_OUT, keyToState_target_focusOutHandler);
this._target.removeEventListener(KeyboardEvent.KEY_DOWN, keyToState_target_keyDownHandler);
}
this._target = value;
if (this._target != null) {
this._currentState = this._upState;
this._target.addEventListener(Event.REMOVED_FROM_STAGE, keyToState_target_removedFromStageHandler);
this._target.addEventListener(FocusEvent.FOCUS_OUT, keyToState_target_focusOutHandler);
this._target.addEventListener(KeyboardEvent.KEY_DOWN, keyToState_target_keyDownHandler);
}
return this._target;
}
private var _keyDownStage:Stage = null;
private var _callback:(T) -> Void = null;
/**
The function to call when the state is changed.
The callback is expected to have the following signature:
```haxe
String -> Void
```
@since 1.0.0
**/
public var callback(get, set):(T) -> Void;
private function get_callback():(T) -> Void {
return this._callback;
}
private function set_callback(value:(T) -> Void):(T) -> Void {
if (this._callback == value) {
return this._callback;
}
this._callback = value;
if (this._callback != null) {
this._callback(this._currentState);
}
return this._callback;
}
private var _currentState:T;
/**
The current state of the utility. May be different than the state of the
target.
@since 1.0.0
**/
public var currentState(get, never):T;
private function get_currentState():T {
return this._currentState;
}
private var _upState:T = null;
/**
The value for the "up" state.
@since 1.0.0
**/
public var upState(get, set):T;
private function get_upState():T {
return this._upState;
}
private function set_upState(value:T):T {
this._upState = value;
return this._upState;
}
private var _downState:T = null;
/**
The value for the "down" state.
@since 1.0.0
**/
public var downState(get, set):T;
private function get_downState():T {
return this._downState;
}
private function set_downState(value:T):T {
this._downState = value;
return this._downState;
}
private var _enabled:Bool = true;
/**
May be set to `false` to disable the state changes temporarily until set
back to `true`.
@default true
@since 1.0.0
**/
public var enabled(get, set):Bool;
private function get_enabled():Bool {
return this._enabled;
}
private function set_enabled(value:Bool):Bool {
if (this._enabled == value) {
return this._enabled;
}
this._enabled = value;
if (!this._enabled) {
this.resetKeyState();
}
return this._enabled;
}
private var _downKeyCode:Null<Int> = null;
private function changeState(value:T):Void {
var oldState = this._currentState;
if ((this._target is IStateContext)) {
oldState = (cast this._target : IStateContext<Dynamic>).currentState;
}
this._currentState = value;
if (oldState == value) {
return;
}
if (this._callback != null) {
this._callback(value);
}
}
private function resetKeyState():Void {
if (this._downKeyCode == null) {
// not down, don't do anything
return;
}
if (this._keyDownStage != null) {
this._keyDownStage.removeEventListener(KeyboardEvent.KEY_UP, keyToState_stage_keyUpHandler);
}
this._downKeyCode = null;
this.changeState(this._upState);
}
private function keyToState_target_removedFromStageHandler(event:Event):Void {
this.resetKeyState();
}
private function keyToState_target_focusOutHandler(event:FocusEvent):Void {
this.resetKeyState();
}
private function keyToState_target_keyDownHandler(event:KeyboardEvent):Void {
if (!this._enabled
|| this._downKeyCode != null
|| this._target.stage == null
|| (event.keyCode != Keyboard.SPACE && event.keyCode != Keyboard.ENTER)) {
return;
}
if ((this._target is IFocusObject)) {
var focusObject:IFocusObject = cast this._target;
var focusManager = focusObject.focusManager;
if (focusManager != null && focusManager.focus != focusObject) {
return;
}
}
this._downKeyCode = event.keyCode;
// we don't listen to KeyboardEvent.KEY_UP on the target because the
// target might not actually have focus anymore, but we need to know
// when the key is back up. the stage will always get KEY_UP events.
this._keyDownStage = this._target.stage;
this._keyDownStage.addEventListener(KeyboardEvent.KEY_UP, keyToState_stage_keyUpHandler, false, 0, true);
this.changeState(this._downState);
}
private function keyToState_stage_keyUpHandler(event:KeyboardEvent):Void {
if (event.keyCode != this._downKeyCode) {
return;
}
this.resetKeyState();
}
}