/
uibaseelement.js
executable file
·276 lines (249 loc) · 7.91 KB
/
uibaseelement.js
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
import Container from "../container.js";
import timer from "../../system/timer.js";
import { on, off, POINTERMOVE } from "../../system/event.js";
import { registerPointerEvent, releasePointerEvent} from "./../../input/input.js";
import pool from "../../system/pooling.js";
/**
* @classdesc
* This is a basic clickable and draggable container which you can use in your game UI.
* Use this for example if you want to display a panel that contains text, images or other UI elements.
* @augments Container
*/
export default class UIBaseElement extends Container {
/**
*
* @param {number} x - The x position of the container
* @param {number} y - The y position of the container
* @param {number} w - width of the container
* @param {number} h - height of the container
*/
constructor(x, y, w, h) {
super(x, y, w, h);
/**
* object can be clicked or not
* @type {boolean}
* @default true
*/
this.isClickable = true;
/**
* object can be clicked or not
* @type {boolean}
* @default false
*/
this.isDraggable = false;
/**
* Tap and hold threshold timeout in ms
* @type {number}
* @default 250
*/
this.holdThreshold = 250;
/**
* object can be tap and hold
* @type {boolean}
* @default false
*/
this.isHoldable = false;
/**
* true if the pointer is over the object
* @type {boolean}
* @default false
*/
this.hover = false;
/**
* false if the pointer is down, or true when the pointer status is up
* @type {boolean}
* @default false
*/
this.released = true;
/**
* UI base elements use screen coordinates by default
* (Note: any child elements added to a UIBaseElement should have their floating property to false)
* @see Renderable.floating
* @type {boolean}
* @default true
*/
this.floating = true;
// object has been updated (clicked,etc..)
this.holdTimeout = -1;
// enable event detection
this.isKinematic = false;
// update container and children bounds automatically
this.enableChildBoundsUpdate = true;
}
/**
* function callback for the pointerdown event
* @ignore
*/
clicked(event) {
// Check if left mouse button is pressed
if (event.button === 0 && this.isClickable) {
this.isDirty = true;
this.released = false;
if (this.isHoldable) {
timer.clearTimeout(this.holdTimeout);
this.holdTimeout = timer.setTimeout(
() => this.hold(),
this.holdThreshold,
false
);
this.released = false;
}
if (this.isDraggable) {
this.grabOffset.set(event.gameX, event.gameY);
this.grabOffset.sub(this.pos);
}
return this.onClick(event);
}
}
/**
* function called when the object is pressed (to be extended)
* @param {Pointer} event - the event object
* @returns {boolean} return false if we need to stop propagating the event
*/
onClick(event) { // eslint-disable-line no-unused-vars
return true;
}
/**
* function callback for the pointerEnter event
* @ignore
*/
enter(event) {
this.hover = true;
this.isDirty = true;
if (this.isDraggable === true) {
on(POINTERMOVE, this.pointerMove, this);
// to memorize where we grab the object
this.grabOffset = pool.pull("Vector2d", 0, 0);
}
return this.onOver(event);
}
/**
* pointermove function
* @ignore
*/
pointerMove(event) {
if (this.hover === true && this.released === false) {
// follow the pointer
this.pos.set(event.gameX, event.gameY, this.pos.z);
this.pos.sub(this.grabOffset);
// mark the container for redraw
this.isDirty = true;
return this.onMove(event);
}
}
/**
* function called when the pointer is moved over the object
* @param {Pointer} event - the event object
*/
onMove(event) { // eslint-disable-line no-unused-vars
// to be extended
}
/**
* function called when the pointer is over the object
* @param {Pointer} event - the event object
*/
onOver(event) { // eslint-disable-line no-unused-vars
// to be extended
}
/**
* function callback for the pointerLeave event
* @ignore
*/
leave(event) {
this.hover = false;
this.isDirty = true;
if (this.isDraggable === true) {
// unregister on the global pointermove event
off(POINTERMOVE, this.pointerMove);
pool.push(this.grabOffset);
this.grabOffset = undefined;
}
this.release(event);
return this.onOut(event);
}
/**
* function called when the pointer is leaving the object area
* @param {Pointer} event - the event object
*/
onOut(event) { // eslint-disable-line no-unused-vars
// to be extended
}
/**
* function callback for the pointerup event
* @ignore
*/
release(event) {
if (this.released === false) {
this.released = true;
this.isDirty = true;
timer.clearTimeout(this.holdTimeout);
this.holdTimeout = -1;
return this.onRelease(event);
}
}
/**
* function called when the object is pressed and released (to be extended)
* @returns {boolean} return false if we need to stop propagating the event
*/
onRelease() {
return true;
}
/**
* function callback for the tap and hold timer event
* @ignore
*/
hold() {
timer.clearTimeout(this.holdTimeout);
this.holdTimeout = -1;
this.isDirty = true;
if (!this.released) {
this.onHold();
}
}
/**
* function called when the object is pressed and held<br>
* to be extended <br>
*/
onHold() {}
/**
* function called when added to the game world or a container
* @ignore
*/
onActivateEvent() {
// register pointer events
registerPointerEvent("pointerdown", this, (e) => this.clicked(e));
registerPointerEvent("pointerup", this, (e) => this.release(e));
registerPointerEvent("pointercancel", this, (e) => this.release(e));
registerPointerEvent("pointerenter", this, (e) => this.enter(e));
registerPointerEvent("pointerleave", this, (e) => this.leave(e));
// call the parent function
super.onActivateEvent();
}
/**
* function called when removed from the game world or a container
* @ignore
*/
onDeactivateEvent() {
// release pointer events
releasePointerEvent("pointerdown", this);
releasePointerEvent("pointerup", this);
releasePointerEvent("pointercancel", this);
releasePointerEvent("pointerenter", this);
releasePointerEvent("pointerleave", this);
timer.clearTimeout(this.holdTimeout);
this.holdTimeout = -1;
// unregister on the global pointermove event
// note: this is just a precaution, in case
// the object is being remove from his parent
// container before the leave function is called
if (this.isDraggable === true) {
off(POINTERMOVE, this.pointerMove);
if (typeof this.grabOffset !== "undefined") {
pool.push(this.grabOffset);
this.grabOffset = undefined;
}
}
// call the parent function
super.onDeactivateEvent();
}
}