-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPointerTrigger.hx
More file actions
170 lines (138 loc) · 4.32 KB
/
PointerTrigger.hx
File metadata and controls
170 lines (138 loc) · 4.32 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
/*
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.events.TriggerEvent;
import openfl.display.InteractiveObject;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.events.TouchEvent;
#if air
import openfl.ui.Multitouch;
#end
/**
Dispatches `TriggerEvent.TRIGGER` (or a custom event type) when the target
is clicked or tapped.
@see `feathers.controls.Button`
@see `feathers.events.TriggerEvent.TRIGGER`
@since 1.0.0
**/
class PointerTrigger {
/**
Creates a new `PointerTrigger` object with the given arguments.
@since 1.0.0
**/
public function new(target:InteractiveObject = null, ?eventFactory:() -> Event) {
this.target = target;
this.eventFactory = eventFactory;
}
private var _target:InteractiveObject = null;
/**
The target component that should dispatch the event.
@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(MouseEvent.CLICK, pointerTrigger_target_clickHandler);
this._target.removeEventListener(TouchEvent.TOUCH_TAP, pointerTrigger_target_touchTapHandler);
}
this._target = value;
if (this._target != null) {
this._target.addEventListener(MouseEvent.CLICK, pointerTrigger_target_clickHandler);
#if (openfl >= "9.0.0")
this._target.addEventListener(TouchEvent.TOUCH_TAP, pointerTrigger_target_touchTapHandler);
#end
}
return this._target;
}
private var _eventFactory:() -> Event = null;
/**
The event type to dispatch on trigger. If `null`, dispatches an instance
of `TriggerEvent`.
@since 1.0.0
**/
public var eventFactory(get, set):() -> Event;
private function get_eventFactory():() -> Event {
return this._eventFactory;
}
private function set_eventFactory(value:() -> Event):() -> Event {
if (this._eventFactory == value) {
return this._eventFactory;
}
this._eventFactory = value;
return this._eventFactory;
}
private var _enabled:Bool = true;
/**
May be set to `false` to disable the trigger event 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 {
this._enabled = value;
return this._enabled;
}
private var _customHitTest:(stageX:Float, stageY:Float) -> Bool;
/**
In addition to the normal hit testing for mouse/touch events, a custom
function may impose additional rules that determine if the target
should be triggered.
The function should return `true` if the target should be triggered, and
`false` if it should not be triggered.
@default null
@since 1.0.0
**/
public var customHitTest(get, set):(stageX:Float, stageY:Float) -> Bool;
private function get_customHitTest():(stageX:Float, stageY:Float) -> Bool {
return this._customHitTest;
}
private function set_customHitTest(value:(stageX:Float, stageY:Float) -> Bool):(stageX:Float, stageY:Float) -> Bool {
this._customHitTest = value;
return this._customHitTest;
}
private function pointerTrigger_target_clickHandler(event:MouseEvent):Void {
if (!this._enabled) {
return;
}
if (this._customHitTest != null && !this._customHitTest(event.stageX, event.stageY)) {
return;
}
if (this._eventFactory != null) {
this._target.dispatchEvent(this._eventFactory());
return;
}
TriggerEvent.dispatchFromMouseEvent(this._target, event);
}
private function pointerTrigger_target_touchTapHandler(event:TouchEvent):Void {
if (!this._enabled) {
return;
}
if (event.isPrimaryTouchPoint #if air && Multitouch.mapTouchToMouse #end) {
// ignore the primary one because MouseEvent.CLICK will catch it
return;
}
if (this._customHitTest != null && !this._customHitTest(event.stageX, event.stageY)) {
return;
}
if (this._eventFactory != null) {
this._target.dispatchEvent(this._eventFactory());
return;
}
TriggerEvent.dispatchFromTouchEvent(this._target, event);
}
}