-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUI.js
More file actions
500 lines (418 loc) · 12.2 KB
/
UI.js
File metadata and controls
500 lines (418 loc) · 12.2 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(function(window, document, undefined) {
var UI = (function() {
var canvas;
var ctx;
var tree;
var currentTheme = "default";
var themes = {
"default": {
borderColor: hexToRgb("#000000"),
borderSize: 0,
borderRadius: 0,
backgroundColor: "transparent",
headerColor: hexToRgb("#aaaaaa"),
padding: 0,
margin: 0,
fontSize: "12px",
fontFamily: "Arial",
fontColor: hexToRgb("#000000"),
textBaseline: "top",
lineHeight: 20
}
};
var isFullscreen = false;
var originalDimensions;
function hexToRgb(str) {
if(str.charAt(0) === "#") {
str = str.substr(1);
}
var r = parseInt(str.substr(0, 2), 16);
var g = parseInt(str.substr(2, 2), 16);
var b = parseInt(str.substr(4, 2), 16);
return "rgb(" + r + "," + g + "," + b + ")";
}
/*
* Draw the UI to the canvas
*/
function draw(p) {
if(!p) {
p = tree;
}
var children = p._children;
var len = children.length;
var child;
for(var i = 0; i < len; ++i) {
child = children[i];
calculate(p, child);
child.draw(ctx);
draw(children[i]);
}
}
/**
* Evaluate an expression
* Must seperate with spaces and use units
*/
function expr(str, p, w, h) {
//if number, use that
if(typeof str === "number") return str;
var tokens = str.split(' ');
var a, b;
//evaluate the first number
a = parseInt(tokens[0], 10);
if(tokens[0].indexOf("%") !== -1) {
a = p * (a / 100);
} else if(tokens[0] === "w" || tokens[0] === "width") {
a = w;
} else if(tokens[0] === "h" || tokens[0] === "height") {
a = h;
}
//if no expression
if(tokens.length === 1) return a;
//evaluate the second number
b = parseInt(tokens[2], 10)
if(tokens[2].indexOf("%") !== -1) {
b = p * (b / 100);
} else if(tokens[2] === "w" || tokens[2] === "width") {
b = w;
} else if(tokens[2] === "h" || tokens[2] === "height") {
b = h;
}
switch(tokens[1]) {
case "+": return a + b
case "-": return a - b;
case "*": return a * b;
case "/": return a / b;
}
}
function calculate(p, c) {
var width, height;
var outerWidth, outerHeight;
var minWidth, maxWidth, minHeight, maxHeight;
var x, y;
//calculate widths/heights
outerWidth = expr(c.width, p._actual.width);
width = outerWidth - (p.paddingLeft + p.paddingRight + c.borderSizeLeft + c.borderSizeRight + c.marginLeft + c.marginRight);
outerHeight = expr(c.height, p._actual.height);
height = outerHeight - (p.paddingTop + p.paddingBottom + c.borderSizeTop + c.borderSizeBottom + c.marginTop + c.marginBottom);
//calculate the min/max width/heights
if(c.minWidth !== undefined) minWidth = expr(c.minWidth, p._actual.width);
if(c.minHeight !== undefined) minHeight = expr(c.minHeight, p._actual.height);
if(c.maxWidth !== undefined) maxWidth = expr(c.maxWidth, p._actual.width);
if(c.maxHeight !== undefined) maxHeight = expr(c.maxHeight, p._actual.height);
//calculate the position
x = expr(c.x, p._actual.width, outerWidth, outerHeight) + p.paddingLeft + c.marginLeft + p._actual.x + c.borderSizeLeft;
y = expr(c.y, p._actual.height, outerWidth, outerHeight) + p.paddingTop + c.marginTop + p._actual.y + c.borderSizeTop;
//make sure size within min and max
if(width < (minWidth || 0)) width = minWidth || 0;
if(height < (minHeight || 0)) height = minHeight || 0;
if(maxWidth && width > maxWidth) width = maxWidth;
if(maxHeight && height > maxHeight) height = maxHeight;
c._actual = {
x: ~~x,
y: ~~y,
width: ~~width,
height: ~~height
};
if(c.calculate) c.calculate();
}
/**
* Determine the actualy property values
* from CSS style syntax
* e.g. margin: 0 0 0 5; padding: 5; border-width: 5 10
*/
function parseProperty(node, prop) {
var p = node[prop];
if(p === undefined) return;
var top, right, bottom, left;
var tokens;
//set all sides to same value
if(typeof p === "number") {
top = right = bottom = left = p;
} else if(typeof p === "string") {
//evaluate values seperated by spaces
tokens = p.split(/\s+/);
if(tokens.length === 1) {
top = right = bottom = left = parseInt(tokens[0], 10);
} else if(tokens.length === 2) {
top = bottom = parseInt(tokens[0], 10);
left = right = parseInt(tokens[1], 10);
} else if(tokens.length === 4) {
top = parseInt(tokens[0], 10);
right = parseInt(tokens[1], 10);
bottom = parseInt(tokens[2], 10);
left = parseInt(tokens[3], 10);
} else throw "Too many values in " + prop;
} else throw "Incorrect value for " + prop + ": " + p.toString();
node[prop + "Top"] = top;
node[prop + "Right"] = right;
node[prop + "Bottom"] = bottom;
node[prop + "Left"] = left;
}
/**
* A UI node in the display tree
*/
function node(opts) {
if(!opts) return;
//copy all properties
for(var key in opts) {
this[key] = opts[key];
}
if(!this.width) this.width = "100%";
if(!this.height) this.height = "100%";
if(!this.x) this.x = 0;
if(!this.y) this.y = 0;
//save the actual props for the root
if(!tree) {
//actual coords
this._actual = {
x: 0,
y: 0,
width: this.width,
height: this.height
};
}
//copy the theme properties
var theme = themes[opts.theme || currentTheme];
console.log(opts.theme, currentTheme, opts, theme, this);
for(var key in theme) {
if(this[key] === undefined)
this[key] = theme[key];
}
parseProperty(this, "padding");
parseProperty(this, "margin");
parseProperty(this, "borderSize");
parseProperty(this, "borderRadius");
//array of child nodes
this._children = [];
//if no parent, take the root
if(!this.parent) {
if(tree) tree.addChild(this);
} else {
this.parent.addChild(this);
}
}
/*
* Add functions to the node
*/
node.prototype = {
addChild: function(n) {
this._children.push(n);
n.parent = this;
},
removeChild: function(n) {
var child = this._children;
var len = child.length;
for(var i = 0; i < len; ++i) {
if(child[i] === n) {
child.splice(i, 1);
n.parent = null;
return;
}
}
},
roundRect: function(ctx, x, y, width, height, radiusTop, radiusRight, radiusBottom, radiusLeft, fill) {
ctx.beginPath();
ctx.moveTo(x + radiusTop, y);
ctx.lineTo(x + width - radiusRight, y);
ctx.quadraticCurveTo(x + width, y, x + width , y + radiusRight);
ctx.lineTo(x + width , y + height - radiusBottom);
ctx.quadraticCurveTo(x + width, y + height, x + width - radiusBottom, y + height);
ctx.lineTo(x + radiusLeft, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radiusLeft);
ctx.lineTo(x, y + radiusTop);
ctx.quadraticCurveTo(x, y, x + radiusTop, y);
ctx.closePath();
ctx.fillStyle = fill;
ctx.fill();
},
draw: function(ctx) {
ctx.save();
//only draw a border when needed
if(this.borderSizeLeft || this.borderSizeTop || this.borderSizeRight || this.borderSizeBottom) {
this.roundRect(
ctx,
this._actual.x - this.borderSizeLeft,
this._actual.y - this.borderSizeTop,
this._actual.width + this.borderSizeRight + this.borderSizeLeft,
this._actual.height + this.borderSizeBottom + this.borderSizeTop,
//short circuit logic to only add the size if the radius is > 0
this.borderRadiusTop && this.borderRadiusTop + this.borderSizeTop,
this.borderRadiusRight && this.borderRadiusRight + this.borderSizeRight,
this.borderRadiusBottom && this.borderRadiusBottom + this.borderSizeBottom,
this.borderRadiusLeft && this.borderRadiusLeft + this.borderSizeLeft,
this.borderColor
);
}
if(this.backgroundColor !== "transparent") {
//main rect
this.roundRect(
ctx,
this._actual.x,
this._actual.y,
this._actual.width,
this._actual.height,
this.borderRadiusTop,
this.borderRadiusRight,
this.borderRadiusBottom,
this.borderRadiusLeft,
this.backgroundColor
);
}
if(this.text) {
var lines = this.text.split("\n");
var len = lines.length;
var line;
ctx.font = this.fontSize + " " + this.fontFamily;
ctx.fillStyle = this.fontColor;
ctx.textBaseline = this.textBaseline;
for(var i = 0; i < len; ++i) {
ctx.fillText(
lines[i],
this._actual.x + this.paddingLeft + this.borderSizeLeft,
(this._actual.y + this.paddingTop + this.borderSizeTop) + i * this.lineHeight
);
}
}
ctx.restore();
}
};
return {
/**
* Initialize UI.js
* Pass in a string for the ID, a canvas object
* or default to id of "canvas"
*/
init: function(cv) {
if(cv) {
if(typeof cv === "string") {
canvas = document.getElementById(cv);
} else {
canvas = cv;
}
} else {
canvas = document.getElementById("canvas");
}
ctx = canvas.getContext("2d");
tree = new node({
width: canvas.width,
height: canvas.height,
parent: null
});
//save the original dimensions
originalDimensions = {
width: canvas.width,
height: canvas.height
};
},
/**
* Define a widget for the UI library
*/
e: function(name, def) {
var c = function(opts) {
opts.type = name;
node.call(this, opts);
if(this.init) this.init(opts);
};
c.prototype = new node;
c.prototype.constructor = c;
c.prototype.supr = node.prototype;
this[name] = function(opts) {
return new c(opts);
}
for(var key in def) {
c.prototype[key] = def[key];
}
},
debug: function() {
console.log(tree, canvas, ctx, themes, currentTheme);
},
createTheme: function(name, parent, def) {
if(arguments.length === 2) {
def = parent;
parent = "default";
}
//get the theme object
parent = themes[parent];
var theme = {};
//copy properties from parent
for(var key in parent) {
theme[key] = parent[key];
}
//copy properties from definition
for(var key in def) {
theme[key] = def[key];
}
themes[name] = theme;
},
setTheme: function(theme) {
currentTheme = theme;
},
reflow: function() {
if(isFullscreen) {
tree.width = canvas.width = window.innerWidth;
tree.height = canvas.height = window.innerHeight;
tree._actual.width = tree.width;
tree._actual.height = tree.height;
}
UI.repaint();
},
fullscreen: function(q) {
if(q === undefined) {
q = !isFullscreen;
}
if(q) {
tree.width = canvas.width = window.innerWidth;
tree.height = canvas.height = window.innerHeight;
isFullscreen = true;
window.onresize = this.reflow;
} else {
tree.width = canvas.width = originalDimensions.width;
tree.height = canvas.height = originalDimensions.height;
isFullscreen = false;
window.onresize = null;
}
//update the actual dims
tree._actual.width = tree.width;
tree._actual.height = tree.height;
this.repaint();
},
repaint: draw
};
})();
//end UI def
/**
* UI Panel Definition
*/
UI.e("panel", {
draw: function(ctx) {
//call parent
this.supr.draw.call(this, ctx);
}
});
UI.e("image", {
_src: null,
init: function(opts) {
console.log("Image", opts);
this._src = new Image();
this._src.src = opts.src;
this._src.onload = function() {
UI.repaint();
}
},
calculate: function() {
if(!this._src.complete) return;
this.width = this._src.width;
this.height = this._src.height;
},
draw: function(ctx) {
this.supr.draw.call(this, ctx);
if(this._src.complete === false) return;
ctx.drawImage(
this._src,
this._actual.x,
this._actual.y
);
}
});
window.UI = UI;
})(window, window.document);