-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
60 lines (59 loc) · 1.36 KB
/
ui.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
class Screen {
constructor(width,height) {
if (width && height) {
this.width = width;
this.height = height;
this.screen = [...new Array(this.width)].map(e => [...new Array(this.height)].map(e=>[]));
this.elms = [];
this.binders = {};
}
}
addElement(el) {
this.elms.push(el);
}
setpixel(x,y,id) {
if (x>=0 && x<this.width && y>=0 && y<this.height) {
this.screen[x][y].push(id);
}
}
bind(id,func) {
if (this.binders[id]) this.binders[id].push(func);
else this.binders[id] = [func];
}
click(x,y) {
var len = this.screen[x][y].length;
if (len!=0) this.binders[this.screen[x][y][len-1]][0]();
}
render() {
var el;
for (el of this.elms) el.render();
}
}
class Element {
constructor(x,y,w,h,id,screen) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.id = id;
this.screen = screen;
screen.addElement(this);
}
render() {
var i,j;
for (i=this.x; i<this.x+this.w; i++) {
for (j=this.y; j<this.y+this.h; j++) {
this.screen.setpixel(i,j,this.id);
var rgb = this.style(i,j);
canvas.getContext("2d").fillStyle = "rgb("+rgb[0]+","+rgb[1]+","+rgb[2]+")";
canvas.getContext("2d").fillRect(i,j,1,1);
}
}
}
style(func) {
this.style=func;
}
onclick(func) {
this.screen.bind(this.id,func);
}
}