Skip to content

Commit ae172a6

Browse files
committed
WIP added preliminary GUI element and test
1 parent 024df03 commit ae172a6

File tree

9 files changed

+356
-29
lines changed

9 files changed

+356
-29
lines changed

Diff for: dev/assets/button_down.png

49 KB
Loading

Diff for: dev/assets/button_up.png

49.7 KB
Loading

Diff for: src/Display/Bound.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Matrix} from 'Geom/Matrix'
44
import {Rectangle} from 'Geom/Rectangle'
55

66
export class Bound {
7-
private _transform: Transform
7+
private _transform: Transform = Transform.IDENTITY
88
private _isAABBDirty: boolean
99
private _aabb: Rectangle = new Rectangle()
1010

Diff for: src/Display/Container.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class Container extends Spatial {
1010

1111
public dispose() {
1212
this.removeAllChildren()
13+
this._children = null
1314

1415
super.dispose()
1516
}
@@ -37,10 +38,8 @@ export class Container extends Spatial {
3738

3839
public removeAllChildren() {
3940
for (let child of this._children) {
40-
child.parent = null
41-
child.dispose()
41+
this.removeChild(child)
4242
}
43-
this._children = null
4443
}
4544

4645
// override

Diff for: src/Display/Spatial.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {Transform} from 'Geom/Transform'
22
import {Bound} from './Bound'
33
import {Point} from 'Geom/Point'
44
import {Timer} from 'Utils/Timer'
5+
import {EventDispatcher} from 'Events/EventDispatcher'
56

6-
export abstract class Spatial {
7+
export abstract class Spatial extends EventDispatcher {
78
public name: string = ''
89

910
// internal use only
@@ -120,7 +121,7 @@ export abstract class Spatial {
120121
this._parent = value
121122
}
122123

123-
public update(initiator: boolean): void {
124+
public update(initiator: boolean = true): void {
124125
let dt: number = Timer.deltaSeconds
125126

126127
if (!this.velocity.isZero) {

Diff for: src/Display/Sprite.ts

+131-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Color} from './Color'
77
import {AnchorType} from './AnchorType'
88
import {Texture} from './Texture'
99
import {Bound} from './Bound'
10+
import {Rectangle} from 'Geom/Rectangle'
1011

1112
export class Sprite extends Container {
1213
public alpha: number = 1
@@ -27,8 +28,8 @@ export class Sprite extends Container {
2728
protected _mask: HTMLImageElement
2829

2930
// TODO Dynamic allocation
30-
private _width: number = 100
31-
private _height: number = 100
31+
private _width: number
32+
private _height: number
3233

3334
protected _isDirty: boolean = true
3435

@@ -52,14 +53,7 @@ export class Sprite extends Container {
5253
this.graphics = this.canvas.getContext('2d')
5354

5455
if (texture) {
55-
//TODO [GJP] Tidy all this up
56-
this._texture = texture
57-
this._targetCanvas.width = texture.width
58-
this.canvas.width = texture.width
59-
this._targetCanvas.height = texture.height
60-
this.canvas.height = texture.height
61-
this._width = texture.width
62-
this._height = texture.height
56+
this.texture = texture
6357
} else {
6458
this._texture = new Texture()
6559
}
@@ -74,7 +68,7 @@ export class Sprite extends Container {
7468
}
7569

7670
public dispose(): void {
77-
this._modelBound.dispose()
71+
//this._modelBound.dispose()
7872
}
7973

8074
public get texture(): Texture {
@@ -222,28 +216,145 @@ export class Sprite extends Container {
222216
}
223217

224218
public get width(): number {
225-
return this._targetCanvas.width
219+
return this._worldBound.aabb.width
226220
}
227221

228222
public set width(value: number) {
229-
if (this._isDirty) {
230-
this.redraw()
231-
}
232223
this.scaleX = value / this._targetCanvas.width
233224
}
234225

235226
public get height(): number {
236-
return this._targetCanvas.height
227+
return this._worldBound.aabb.height
237228
}
238229

239230
public set height(value: number) {
240-
if (this._isDirty) {
241-
this.redraw()
242-
}
243231
this.scaleY = value / this._targetCanvas.height
244232
}
245233

246-
public set anchor(value: AnchorType) {
234+
public get left(): number {
235+
return this._worldBound.aabb.x
236+
}
237+
238+
public get right(): number {
239+
return this.left + this.width
240+
}
241+
242+
public get top(): number {
243+
return this._worldBound.aabb.y
244+
}
245+
246+
public get bottom(): number {
247+
return this.top + this.height
248+
}
249+
250+
public get center(): Point {
251+
return new Point(
252+
(this.left + this.width) / 2,
253+
(this.top + this.height) / 2
254+
)
255+
}
256+
257+
public intersectsXY(x: number, y: number): boolean {
258+
if (this.bottom < y) {
259+
return false
260+
}
261+
262+
if (this.top > y) {
263+
return false
264+
}
265+
266+
if (this.right < x) {
267+
return false
268+
}
269+
270+
if (this.left > x) {
271+
return false
272+
}
273+
274+
return true
275+
}
276+
277+
public intersectsPoint(point: Point): boolean {
278+
return this.intersectsXY(point.x, point.y)
279+
}
280+
281+
/**
282+
* TODO [GJP] This is fast and dirty! Fix this up
283+
* by implementing a full physics system
284+
*/
285+
public intersects(sprite: Sprite): boolean {
286+
if (this.bottom < sprite.top) {
287+
return false
288+
}
289+
290+
if (this.top > sprite.bottom) {
291+
return false
292+
}
293+
294+
if (this.right < sprite.left) {
295+
return false
296+
}
297+
298+
if (this.left > sprite.right) {
299+
return false
300+
}
301+
302+
return true
303+
}
304+
305+
/**
306+
* TODO [GJP] This is fast and dirty! Fix this up
307+
* by implementing a full physics system
308+
*/
309+
public intersectsRectangle(rect: Rectangle): boolean {
310+
if (this.bottom < rect.top) {
311+
return false
312+
}
313+
314+
if (this.top > rect.bottom) {
315+
return false
316+
}
317+
318+
if (this.right < rect.left) {
319+
return false
320+
}
321+
322+
if (this.left > rect.right) {
323+
return false
324+
}
325+
}
326+
327+
public intersectsRadiusAdvanced(
328+
sprite: Sprite,
329+
radius: number,
330+
radiusOther: number,
331+
tolerance: number
332+
): boolean {
333+
let center1: Point = this.center
334+
let center2: Point = sprite.center
335+
let xdiff: number = center2.x - center1.x
336+
let ydiff: number = center2.y - center1.y
337+
let dCentreSq: number = ydiff * ydiff + xdiff * xdiff
338+
let rSumSq: number = radius + radiusOther
339+
rSumSq *= rSumSq
340+
341+
return dCentreSq - rSumSq <= tolerance * tolerance
342+
}
343+
344+
public intersectsRadius(sprite: Sprite, radius?: number) {
345+
if (radius) {
346+
return this.intersectsRadiusAdvanced(sprite, radius, radius, 0)
347+
} else {
348+
return this.intersectsRadiusAdvanced(
349+
sprite,
350+
(this.width + this.height) / 4,
351+
(sprite.width + sprite.height) / 4,
352+
0
353+
)
354+
}
355+
}
356+
357+
public set anchorType(value: AnchorType) {
247358
let width: number = this._width
248359
let height: number = this._height
249360
let halfWidth: number = this._width / 2

Diff for: src/Events/EventType.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ export class EventType {
44
public static TIMEOUT: string = 'timeout'
55
public static COMPLETE: string = 'complete'
66
public static STARTED: string = 'started'
7+
public static CLICK: string = 'click'
8+
public static MOUSE_DOWN: string = 'mousedown'
9+
public static MOUSE_UP: string = 'mouseup'
10+
public static MOUSE_OVER: string = 'mouseover'
11+
public static MOUSE_OUT: string = 'mouseout'
712
}

0 commit comments

Comments
 (0)