-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwings.js
295 lines (235 loc) · 8.4 KB
/
wings.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
/**
* Wings
*
* Object-Oriented Component-based UI Library for Canvas built in JavaScript (inspired by Java Swing)
*
* @author manuelbarzi
* @version 2.0.1
*/
const Wings = (() => {
class Component {
constructor() {
this.x = this.y = this.width = this.height = 0
this.mouse = {
entered: false,
pressed: false,
dragging: false
}
this.behaviors = []
this.parent = null
this.children = []
this.visible = true
this.backgroundColor = null
this.borderColor = null
this.borderWidth = 0
}
get absoluteX() { return this.parent ? this.x + this.parent.x : this.x }
get absoluteY() { return this.parent ? this.y + this.parent.y : this.y }
add(component) {
if (!(component instanceof Component)) throw new TypeError(`${component} is not a Component`)
this.children.push(component)
component.parent = this
}
on(event, behavior) {
if (typeof event !== 'string') throw new TypeError(`${event} is not a string`)
if (typeof behavior !== 'function') throw new TypeError(`${behavior} is not a function`);
(this.behaviors[event] || (this.behaviors[event] = [])).push(behavior)
}
fireEvent(event, value) {
const behaviors = this.behaviors[event]
if (behaviors && behaviors.length > 0)
for (const behavior of behaviors)
behavior(value)
}
isPointed(x, y) {
const { absoluteX, absoluteY } = this
return absoluteX <= x && x <= absoluteX + this.width
&& absoluteY <= y && y <= absoluteY + this.height
}
mouseMove(event) {
if (this.visible) {
if (this.isPointed(event.x, event.y)) {
if (!this.mouse.entered) {
this.mouse.entered = true
this.fireEvent('MouseEnter', event)
}
this.fireEvent('MouseMove', event)
if (this.mouse.pressed) {
this.mouse.dragging = true
this.fireEvent('MouseDrag', event)
}
} else {
if (this.mouse.entered) {
this.mouse.entered = false
this.fireEvent('MouseLeave', event)
}
if (this.mouse.dragging)
this.fireEvent('MouseDrag', event)
}
if (this.children.length > 0)
for (const child of this.children)
child.mouseMove(event)
}
}
mouseDown(event) {
if (this.visible) {
if (this.isPointed(event.x, event.y)) {
event.originalEvent.preventDefault()
this.mouse.pressed = true
this.fireEvent('MouseDown', event)
}
if (this.children.length > 0)
for (const child of this.children)
child.mouseDown(event)
}
}
mouseUp(event) {
this.releaseMouse()
if (this.visible) {
if (this.isPointed(event.x, event.y))
this.fireEvent('MouseUp', event)
if (this.children.length > 0)
for (const child of this.children)
child.mouseUp(event)
}
}
releaseMouse() {
this.mouse.pressed = false
this.mouse.dragging = false
if (this.children.length > 0)
for (const child of this.children)
child.releaseMouse()
}
mouseClick(event) {
if (this.visible) {
if (this.isPointed(event.x, event.y))
this.fireEvent('MouseClick', event)
if (this.children.length > 0)
for (const child of this.children)
child.mouseClick(event)
}
}
keyDown(event) {
if (this.visible) {
this.fireEvent('KeyDown', event)
if (this.children.length > 0)
for (const child of this.children)
child.keyDown(event)
}
}
keyUp(event) {
if (this.visible) {
this.fireEvent('KeyUp', event)
if (this.children.length > 0)
for (const child of this.children)
child.keyUp(event)
}
}
keyPress(event) {
if (this.visible) {
this.fireEvent('KeyPress', event)
if (this.children.length > 0)
for (const child of this.children)
child.keyPress(event)
}
}
beforePaint(context) {
context.translate(this.x, this.y)
}
paint(context) {
context.beginPath()
if (this.backgroundColor) {
context.rect(0, 0, this.width, this.height)
context.fillStyle = this.backgroundColor
context.fill()
}
if (this.borderColor) {
context.rect(0, 0, this.width, this.height)
context.lineWidth = this.borderWidth
context.strokeStyle = this.borderColor
context.stroke()
}
}
afterPaint(context) {
context.translate(-this.x, -this.y)
}
paintAll(context) {
this.beforePaint(context)
this.paint(context)
if (this.children.length > 0)
for (const child of this.children)
child.paintAll(context)
this.afterPaint(context)
}
}
class MouseEvent {
constructor(element, event) {
const rect = element.getBoundingClientRect()
this.x = event.clientX - rect.left
this.y = event.clientY - rect.top
this.originalEvent = event
}
}
class KeyEvent {
constructor(event) {
this.key = event.which || event.keyCode
}
}
class View extends Component {
constructor(canvas) {
super()
this.width = canvas.width
this.height = canvas.height
this.backgroundColor = 'cyan'
this.context = canvas.getContext('2d')
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 600)
}
})()
window.addEventListener('mousemove', event => {
this.mouseMove(new MouseEvent(canvas, event))
this.refresh()
})
window.addEventListener('mousedown', event => {
this.mouseDown(new MouseEvent(canvas, event))
this.refresh()
})
window.addEventListener('mouseup', event => {
this.mouseUp(new MouseEvent(canvas, event))
this.refresh()
})
window.addEventListener('click', event => {
this.mouseClick(new MouseEvent(canvas, event))
this.refresh()
})
window.addEventListener('keydown', event => {
this.keyDown(new KeyEvent(event))
this.refresh()
})
window.addEventListener('keyup', event => {
this.keyUp(new KeyEvent(event))
this.refresh()
})
window.addEventListener('keypress', event => {
this.keyPress(new KeyEvent(event))
this.refresh()
})
setTimeout(() => {
this.refresh()
}, 100)
}
refresh() {
window.requestAnimFrame(() => {
this.context.clearRect(0, 0, self.width, self.height)
super.paintAll(this.context)
})
}
}
return {
Component,
View
}
})()