-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneObject.ts
251 lines (203 loc) · 5.86 KB
/
SceneObject.ts
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
import Screen from './Screen';
export default class SceneObject {
public onUpdate: ((dt: number) => boolean) | null = null;
public onDraw: ((screen: Screen) => void) | null = null;
public onMouseDown: ((x: number, y: number) => void) | null = null;
public onKeyDown: ((key: string, keyCode: number) => void) | null = null;
public onKeyUp: ((key: string, keyCode: number) => void) | null = null;
private needRedraw = true;
private x: number = 0;
private y: number = 0;
private width: number = 0;
private height: number = 0;
private scale: number = 1;
private sortIndex: number = 1;
private visible: boolean = true;
private parent: SceneObject | null = null;
private childs: SceneObject[] = [];
private pinned: boolean = false;
constructor(width: number, height: number) {
this.setWidth(width);
this.setHeight(height);
}
public setPosition(x: number, y: number): SceneObject {
if (this.x !== x || this.y !== y) {
this.redraw();
}
this.x = x;
this.y = y;
return this;
}
public setX(x: number): SceneObject {
if (this.x !== x) {
this.redraw();
}
this.x = x;
return this;
}
public setY(y: number): SceneObject {
if (this.y !== y) {
this.redraw();
}
this.y = y;
return this;
}
public setPinned(pinned: boolean): SceneObject {
if (this.pinned !== pinned) {
this.redraw();
}
this.pinned = pinned;
return this;
}
public getPinned(): boolean {
if (!this.parent) {
return this.pinned;
} else {
return this.pinned || this.parent.getPinned();
}
}
public setSize(width: number, height: number): SceneObject {
if (this.width !== width || this.height !== height) {
this.redraw();
}
this.width = width;
this.height = height;
return this;
}
public setWidth(width: number): SceneObject {
if (this.width !== width) {
this.redraw();
}
this.width = width;
return this;
}
public setHeight(height: number): SceneObject {
if (this.height !== height) {
this.redraw();
}
this.height = height;
return this;
}
public getNeedRedraw(): boolean {
return this.needRedraw;
}
public getX(): number { return this.x; }
public getY(): number { return this.y; }
public getWidth(): number { return this.width; }
public getHeight(): number { return this.height; }
public getGlobalX(): number {
if (!this.parent) {
return (this.x * this.scale);
} else {
return (this.x * this.scale) + this.parent.getGlobalX();
}
}
public getGlobalY(): number {
if (!this.parent) {
return (this.y * this.scale);
} else {
return (this.y * this.scale) + this.parent.getGlobalY();
}
}
public setScale(scale: number): SceneObject {
if (this.scale !== scale) {
this.redraw();
}
this.scale = scale;
return this;
}
public getScale(): number {
return this.scale;
}
public getGlobalScale(): number {
if (!this.parent) {
return this.scale;
} else {
return this.scale * this.parent.getGlobalScale();
}
}
public setSortIndex(sortIndex: number): SceneObject {
if (this.sortIndex !== sortIndex) {
this.redraw();
}
this.sortIndex = sortIndex;
return this;
}
public getSortIndex(): number {
return this.sortIndex;
}
public getGlobalSortIndex(): number {
if (!this.parent) {
return this.sortIndex;
} else {
return this.sortIndex + this.parent.getGlobalSortIndex();
}
}
public setVisible(visible: boolean): SceneObject {
if (this.visible !== visible) {
this.redraw();
}
this.visible = visible;
return this;
}
public getVisible(): boolean {
return this.visible;
}
public setParent(object: SceneObject | null): SceneObject {
if (this.parent !== object) {
this.redraw();
}
this.parent = object;
return this;
}
public getParent(): SceneObject | null {
return this.parent;
}
public addChild(object: SceneObject): SceneObject {
this.redraw();
object.setParent(this);
this.childs.push(object);
return object;
}
public traverse(list: SceneObject[]) {
if (this.visible) {
list.push(this);
for (const child of this.childs) {
child.traverse(list);
}
}
}
public redraw() {
this.needRedraw = true;
}
public update(dt: number): boolean {
let result = this.needRedraw;
if (this.onUpdate) {
if (this.onUpdate(dt)) {
result = true;
}
}
for (const child of this.childs) {
if (child.update(dt)) {
result = true;
}
}
this.needRedraw = false;
return result;
}
public pickSceneObject(x: number, y: number): SceneObject | null {
if (x >= this.getGlobalX() && y >= this.getGlobalY() &&
x < this.getGlobalX() + this.width && y < this.getGlobalY() + this.height) {
for (const child of this.childs) {
const result = child.pickSceneObject(x, y);
if (result) {
return result;
}
}
// There is no child inside the point
return this;
} else {
return null;
}
}
}