diff --git a/mod.ts b/mod.ts index 624c594..8a8a056 100644 --- a/mod.ts +++ b/mod.ts @@ -18,6 +18,7 @@ export { Sprite, Text, TextureSprite, + Button } from "./src/entities/mod.ts"; export { GravityForce, NormalForce } from "./src/physics/mod.ts"; export { Vector } from "./src/math/mod.ts"; diff --git a/src/World.ts b/src/World.ts index 54d7c3e..855e4c4 100644 --- a/src/World.ts +++ b/src/World.ts @@ -11,6 +11,7 @@ import { Text, TextureSprite, Animation, + Button } from "../mod.ts"; import { hexToRGBA } from "./utils/mod.ts"; @@ -28,7 +29,7 @@ export abstract class World extends Canvas { for await (const event of this) { switch (event.type) { case "mouse_button_down": - this.mouseDown(event); + this._mouseDown(event); break; case "draw": this._draw(); @@ -185,11 +186,28 @@ export abstract class World extends Canvas { for (const child of entity.children) { this._render(child); } + } else if (entity instanceof Button) { + this._render(entity.child) } } public keyDown(_e: KeyEvent): void { return; } + private _mouseDown(e: MouseDownEvent) { + for (const entity of this.entities) { + if (entity instanceof Button) { + if ( + e.x >= entity.x && + e.x <= entity.child.x + entity.child.width && + e.y >= entity.child.y && + e.y <= entity.child.y + entity.child.height + ) { + entity.onClick(); + } + } + } + this.mouseDown(e); + } public mouseDown(_e: MouseDownEvent): void { return; } diff --git a/src/entities/containers/Button.ts b/src/entities/containers/Button.ts new file mode 100644 index 0000000..f5236ab --- /dev/null +++ b/src/entities/containers/Button.ts @@ -0,0 +1,22 @@ +import { Entity, Sprite, World } from '../../../mod.ts'; + +export class Button extends Entity { + public child: Sprite; + public world: World; + + constructor(world: World, child: Sprite) { + super(child.x,child.y); + this.child = child; + this.world = world; + } + + public setX(x: number) { + this.child.x = x; + } + public setY(y: number) { + this.child.y = y; + } + public onClick() { + + } +} \ No newline at end of file diff --git a/src/entities/containers/mod.ts b/src/entities/containers/mod.ts index 60e0576..142c53b 100644 --- a/src/entities/containers/mod.ts +++ b/src/entities/containers/mod.ts @@ -1 +1,2 @@ -export { Group } from './Group.ts'; \ No newline at end of file +export { Group } from './Group.ts'; +export { Button } from './Button.ts'; \ No newline at end of file diff --git a/src/entities/mod.ts b/src/entities/mod.ts index 6557e08..12a9dfe 100644 --- a/src/entities/mod.ts +++ b/src/entities/mod.ts @@ -2,5 +2,5 @@ export { Entity } from "./Entity.ts"; export { Line, Point, Rectangle } from "./geometry/mod.ts"; export { Animation, Atlas, AtlasSprite, Image, Sprite } from "./sprites/mod.ts"; export { Text } from "./text/mod.ts"; -export { Group } from "./containers/mod.ts"; +export { Group, Button } from "./containers/mod.ts"; export * from "./textures/mod.ts";