-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
146 lines (124 loc) · 2.93 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
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
// Phosphor - a browser-based microcomputer
// Copyright (c) 2018 Marc Lepage
'use strict';
// Depth first mostly spatial search
// Return true if node handled event
function handle(node, event, name) {
if (!node.hit(event.x, event.y)) {
return false;
}
if (node.children) {
for (let i = 0; i < node.children.length; ++i) {
const child = node.children[i];
if (handle(child, event, name)) {
return true;
}
}
}
const handler = node[name];
if (handler) {
handler.call(node, event);
}
return true;
}
module.exports = class Ui {
constructor(...args) {
const target = args.shift();
const P = target.P;
const root = new Node();
args.forEach((node) => root.addChild(node));
target.draw = () => root.draw(P);
target.onPointerDown = (e) => handle(root, e, 'onPointerDown');
target.onPointerUp = (e) => handle(root, e, 'onPointerUp');
}
};
class Node {
constructor() {
}
addChild(child) {
if (!this.children) {
this.children = [];
}
this.children.push(child);
}
draw(P) {
if (this.children) {
this.children.forEach((node) => node.draw(P));
}
}
hit(x, y) {
return true;
}
};
class Background extends Node {
constructor(bg) {
super();
this.bg = bg;
}
draw(P) {
P.clear(this.bg);
super.draw(P);
}
hit(x, y) {
return false;
}
};
class Grid extends Node {
constructor(...args) {
super();
this.x = args.shift();
this.y = args.shift();
this.w = args.shift();
this.h = args.shift();
this.iw = args.shift();
this.ih = args.shift();
out: while (true) {
switch (args[0]) {
case 'draw': args.shift(); this._draw = args.shift(); break;
case 'outline': args.shift(); this.outline = args.shift(); break;
default: break out;
}
}
}
draw(P) {
if (this.outline !== undefined) {
P.rect(this.x-1, this.y-1, this.w+2, this.h+2, null, this.outline);
}
for (let y = this.y, i = 0; y < this.y+this.h; y+=this.ih, ++i) {
for (let x = this.x, j = 0; x < this.x+this.w; x+=this.iw, ++j) {
this._draw(x, y, this.iw, this.ih, i, j);
}
}
super.draw();
}
hit(x, y) {
return this.x <= x && x < this.x+this.w && this.y <= y && y < this.y+this.h;
}
onPointerDown(e) {
console.log('grid.onPointerDown', this);
}
onPointerUp(e) {
console.log('grid.onPointerUp', this);
}
};
class Rect extends Node {
constructor(...args) {
super();
this.x = args.shift();
this.y = args.shift();
this.w = args.shift();
this.h = args.shift();
this.c1 = args.shift();
this.c2 = args.shift();
}
draw(P) {
P.rect(this.x, this.y, this.w, this.h, this.c1, this.c2);
super.draw();
}
hit(x, y) {
return this.x <= x && x < this.x+this.w && this.y <= y && y < this.y+this.h;
}
};
module.exports.Background = Background;
module.exports.Grid = Grid;
module.exports.Rect = Rect;