-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasui.js
351 lines (325 loc) · 11 KB
/
canvasui.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
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
'use strict';
// Requires util2d.js
/**
* Class for rendering and interacting with UI elements on a canvas.
*/
var CanvasUI = function(options) {
var defaults = {
element: null,
getCanvasPositionFromEvent: null
};
for(var key in defaults) {
if (!options.hasOwnProperty(key)) {
this[key] = defaults[key];
} else {
this[key] = options[key];
}
}
this.clear();
if (this.element !== null && this.getCanvasPositionFromEvent !== null) {
var that = this;
this.element.addEventListener('mousemove', function(event) {
that.setCursorPosition(that.getCanvasPositionFromEvent(event));
});
this.element.addEventListener('touchmove', function(event) {
that.setCursorPosition(that.getCanvasPositionFromEvent(event));
event.preventDefault();
});
this.element.addEventListener('mousedown', function(event) {
that.down(that.getCanvasPositionFromEvent(event));
});
this.element.addEventListener('touchstart', function(event) {
that.down(that.getCanvasPositionFromEvent(event));
event.preventDefault();
});
this.element.addEventListener('mouseup', function(event) {
that.release(that.getCanvasPositionFromEvent(event));
});
this.element.addEventListener('touchend', function(event) {
that.release(undefined);
event.preventDefault();
});
}
};
/**
* Update UI element state and animations.
* @param {number} deltaTime Time passed since the last update in seconds.
*/
CanvasUI.prototype.update = function(deltaTime) {
for (var i = 0; i < this.uiElements.length; ++i) {
this.uiElements[i].update(deltaTime);
}
};
/**
* Render the UI.
* @param {CanvasRenderingContext2D} ctx The canvas rendering context to use.
*/
CanvasUI.prototype.render = function(ctx) {
var draggedElements = [];
var i;
for (i = 0; i < this.uiElements.length; ++i) {
if (!this.uiElements[i].dragged) {
this.uiElements[i].render(ctx, this.cursorX, this.cursorY);
} else {
draggedElements.push(this.uiElements[i]);
}
}
for (i = 0; i < draggedElements.length; ++i) {
draggedElements[i].render(ctx, this.cursorX, this.cursorY);
}
};
/**
* Clear the UI from all elements.
*/
CanvasUI.prototype.clear = function() {
this.uiElements = [];
this.cursorX = 0;
this.cursorY = 0;
this.downButton = null;
};
/**
* Set the cursor position.
* @param {Object|Vec2} vec New position to set. Needs to have x and y coordinates. Relative to the canvas coordinate
* space.
*/
CanvasUI.prototype.setCursorPosition = function(vec) {
this.cursorX = vec.x;
this.cursorY = vec.y;
if (this.downButton !== null && this.downButton.draggable) {
this.downButton.draggedX = this.downButton.centerX + (this.cursorX - this.dragStartX);
this.downButton.draggedY = this.downButton.centerY + (this.cursorY - this.dragStartY);
}
};
/**
* Handle a mouse / touch down event.
* @param {Object|Vec2} vec New position to set. Needs to have x and y coordinates. Relative to the canvas coordinate
* space.
*/
CanvasUI.prototype.down = function(vec) {
this.setCursorPosition(vec);
for (var i = 0; i < this.uiElements.length; ++i) {
if (this.uiElements[i].active && this.uiElements[i].hitTest(this.cursorX, this.cursorY)) {
this.downButton = this.uiElements[i];
this.downButton.down();
if (this.uiElements[i].draggable) {
this.downButton.dragged = true;
this.dragStartX = this.cursorX;
this.dragStartY = this.cursorY;
}
}
}
this.setCursorPosition(vec);
};
/**
* Handle a mouse / touch up event.
* @param {Object|Vec2=} vec New position to set. Needs to have x and y coordinates. Relative to the canvas coordinate
* space. May be undefined, in which case the last known position will be used to evaluate the effects.
*/
CanvasUI.prototype.release = function(vec) {
if (vec !== undefined) {
this.setCursorPosition(vec);
}
if (this.downButton !== null) {
var clicked = false;
for (var i = 0; i < this.uiElements.length; ++i) {
if (this.uiElements[i].active && this.uiElements[i].hitTest(this.cursorX, this.cursorY)) {
if (this.downButton === this.uiElements[i]) {
clicked = true;
} else if (this.uiElements[i].dragTargetCallback !== null && this.downButton.dragged) {
this.uiElements[i].dragTargetCallback(this.downButton.draggedObjectFunc());
}
}
}
this.downButton.release(clicked);
this.downButton.dragged = false;
this.downButton = null;
}
console.log(this.cursorX, this.cursorY);
};
CanvasUI.prototype.addElement = function(element) {
this.uiElements.push(element);
};
/**
* The default font for UI elements.
*/
CanvasUI.defaultFont = 'sans-serif';
/**
* Minimum interval between clicks on the same button in seconds.
*/
CanvasUI.minimumClickInterval = 0.5;
/**
* A single UI element to draw on a canvas, typically either a button or a label.
* Will be rendered with text by default, but can also be drawn with a custom rendering function renderFunc.
*/
var CanvasUIElement = function(options) {
var defaults = {
label: 'Button',
labelFunc: null, // Function that returns the current text to draw on the element. Overrides label if set.
renderFunc: null,
centerX: 0,
centerY: 0,
width: 100,
height: 50,
clickCallback: null,
dragTargetCallback: null, // Called when something is dragged onto this object, with the dragged object as parameter.
draggedObjectFunc: null,
active: true, // Active elements are visible and can be interacted with. Inactive elements can't be interacted with.
draggable: false,
fontSize: 20, // In pixels
font: CanvasUI.defaultFont,
appearance: undefined // One of CanvasUIElement.Appearance. By default the appearance is determined based on callbacks.
};
for(var key in defaults) {
if (!options.hasOwnProperty(key)) {
this[key] = defaults[key];
} else {
this[key] = options[key];
}
}
this.draggedX = this.centerX;
this.draggedY = this.centerY;
this.dragged = false;
this.time = 0.5;
this.isDown = false;
this.lastClick = 0;
if (this.appearance === undefined) {
if (this.clickCallback !== null) {
this.appearance = CanvasUIElement.Appearance.BUTTON;
} else {
this.appearance = CanvasUIElement.Appearance.LABEL;
}
}
};
CanvasUIElement.Appearance = {
BUTTON: 0,
LABEL: 1
};
/**
* Update UI element state and animations.
* @param {number} deltaTime Time passed since the last update in seconds.
*/
CanvasUIElement.prototype.update = function(deltaTime) {
this.time += deltaTime;
};
/**
* Render the element. Will call renderFunc if it is defined.
* @param {CanvasRenderingContext2D} ctx Context to render to.
* @param {number} cursorX Cursor horizontal coordinate in the canvas coordinate system.
* @param {number} cursorY Cursor vertical coordinate in the canvas coordinate system.
*/
CanvasUIElement.prototype.render = function(ctx, cursorX, cursorY) {
if (!this.active) {
return;
}
var pressedExtent = this.isDown ? (this.time - this.lastDownTime) * 8.0 : 1.0 - (this.time - this.lastUpTime) * 3.0;
pressedExtent = mathUtil.clamp(0, 1, pressedExtent);
var cursorOn = this.hitTest(cursorX, cursorY);
if (this.renderFunc !== null) {
this.renderFunc(ctx, this, cursorOn, pressedExtent);
return;
}
if (this.appearance === CanvasUIElement.Appearance.BUTTON) {
var rect = this.getRect();
ctx.fillStyle = '#000';
if (pressedExtent > 0) {
ctx.globalAlpha = 1.0 - pressedExtent * 0.2;
} else if (cursorOn) {
ctx.globalAlpha = 1.0;
} else {
ctx.globalAlpha = 0.5;
}
ctx.fillRect(rect.left, rect.top, rect.width(), rect.height());
ctx.lineWidth = 3;
ctx.strokeStyle = '#fff';
if (!this.canClick()) {
ctx.globalAlpha *= 0.6;
}
ctx.strokeRect(rect.left, rect.top, rect.width(), rect.height());
}
ctx.globalAlpha = 1.0;
ctx.textAlign = 'center';
ctx.fillStyle = '#fff';
ctx.font = this.fontSize + 'px ' + this.font;
var label = this.label;
if (this.labelFunc) {
label = this.labelFunc();
}
ctx.fillText(label, this.centerX, this.centerY + 7);
};
/**
* @return {number} The horizontal position to draw the element at. May be different from the logical position if the
* element is being dragged.
*/
CanvasUIElement.prototype.visualX = function() {
if (this.dragged) {
return this.draggedX;
} else {
return this.centerX;
}
};
/**
* @return {number} The vertical position to draw the element at. May be different from the logical position if the
* element is being dragged.
*/
CanvasUIElement.prototype.visualY = function() {
if (this.dragged) {
return this.draggedY;
} else {
return this.centerY;
}
};
/**
* @return {boolean} True when the element is being dragged.
*/
CanvasUIElement.prototype.isDragged = function() {
return this.dragged;
};
/**
* @param {number} x Horizontal coordinate to test.
* @param {number} y Vertical coordinate to test.
* @return {boolean} Whether the coordinate is within the area of the element.
*/
CanvasUIElement.prototype.hitTest = function(x, y) {
if (this.clickCallback !== null) {
return this.getRect().containsVec2(new Vec2(x, y));
}
return false;
};
/**
* @return boolean True if the element can generate click events right now. False if the click cooldown hasn't
* completed.
*/
CanvasUIElement.prototype.canClick = function() {
var sinceClicked = this.time - this.lastClick;
return sinceClicked >= CanvasUI.minimumClickInterval;
};
CanvasUIElement.prototype.getRect = function() {
return new Rect(
this.centerX - this.width * 0.5,
this.centerX + this.width * 0.5,
this.centerY - this.height * 0.5,
this.centerY + this.height * 0.5
);
};
/**
* Mark the element as down, for visual purposes only.
*/
CanvasUIElement.prototype.down = function() {
this.isDown = true;
this.lastDownTime = this.time;
};
/**
* Mark the element as up. Will generate a click event if clicked is true.
* @param {boolean} clicked True when clicked, false when the cursor position has left the area of the element.
*/
CanvasUIElement.prototype.release = function(clicked) {
this.isDown = false;
this.lastUpTime = this.time;
if (!clicked || !this.canClick()) {
return;
}
this.lastClick = this.time;
if (this.clickCallback !== null) {
this.clickCallback();
}
};