From 1e7668e6a1846cd5a0182fef114013f84eb089f5 Mon Sep 17 00:00:00 2001 From: Ronja Date: Fri, 24 Apr 2020 15:39:39 +0200 Subject: [PATCH] funky new button class --- Program/src/Systems/ClickableSystem.ts | 1 + Program/src/UI/Button.ts | 66 +++++++++++++++++++++++++- Program/src/UI/InfoMenu.ts | 8 ++-- Program/src/UI/PlanningMenu.ts | 14 ++---- Program/src/globals.ts | 8 ++-- 5 files changed, 77 insertions(+), 20 deletions(-) diff --git a/Program/src/Systems/ClickableSystem.ts b/Program/src/Systems/ClickableSystem.ts index c75480c..c957a7f 100644 --- a/Program/src/Systems/ClickableSystem.ts +++ b/Program/src/Systems/ClickableSystem.ts @@ -9,6 +9,7 @@ export class ClickableSystem extends System { priority = 80 init() { + //the great deselector globals.app.stage.interactive = true globals.app.stage.on("click", (event: interaction.InteractionEvent) => { if (event.target == event.currentTarget) globals.selected.set(null) diff --git a/Program/src/UI/Button.ts b/Program/src/UI/Button.ts index c91d4b2..69bb894 100644 --- a/Program/src/UI/Button.ts +++ b/Program/src/UI/Button.ts @@ -1,3 +1,65 @@ -import { Sprite } from "pixi.js" +import { Sprite, Texture, interaction } from "pixi.js" -export class Button extends Sprite {} +export class Button extends Sprite { + //TODO: sound utility + + defaultSprite: Texture + hoverSprite: Texture + clickSprite: Texture + + hovered = false + clicked = false + + constructor(texture?: Texture) { + super() + + this.defaultSprite = texture + + this.texture = texture + this.interactive = true + + this.on("pointerover", this.startHover) + this.on("pointerout", this.endHover) + this.on("pointercancel", this.endHover) + + this.on("pointerdown", this.startClick) + this.on("pointerup", this.endClick) + this.on("pointerupoutside", this.endClick) + } + + startHover() { + this.hovered = true + this.updateSprite() + } + + endHover() { + this.hovered = false + this.updateSprite() + } + + startClick() { + this.clicked = true + this.updateSprite() + } + + endClick() { + this.clicked = false + this.updateSprite() + } + + updateSprite() { + if (this.clicked && this.clickSprite) { + this.texture = this.clickSprite + return + } + if (this.hovered && this.hoverSprite) { + this.texture = this.hoverSprite + return + } + this.texture = this.defaultSprite + } + + onClick(action: (event: interaction.InteractionEvent) => void, context: any) { + this.on("click", action, context) + } +} diff --git a/Program/src/UI/InfoMenu.ts b/Program/src/UI/InfoMenu.ts index c210326..42c7d0e 100644 --- a/Program/src/UI/InfoMenu.ts +++ b/Program/src/UI/InfoMenu.ts @@ -4,6 +4,7 @@ import { Point } from "../Datatypes/MathTypes/point" import { InfoSystem } from "../Systems/InfoSystem" import { Name } from "../Components/name" import { Entity } from "ecsy" +import { Button } from "./Button" export class InfoMenu extends Container { system: InfoSystem @@ -61,12 +62,9 @@ export class InfoMenu extends Container { for (let i = 0; i < buttonDef.length; i++) { let def = buttonDef[i] - let button = new Sprite(def.texture) + let button = new Button(def.texture) - if (def.action) { - button.on("click", def.action, def.context) - button.interactive = true - } + if (def.action) button.onClick(def.action, def.context) container.addChild(button) button.position = new Point(i * 18, 0) diff --git a/Program/src/UI/PlanningMenu.ts b/Program/src/UI/PlanningMenu.ts index 50b64f3..6a4d0e5 100644 --- a/Program/src/UI/PlanningMenu.ts +++ b/Program/src/UI/PlanningMenu.ts @@ -1,14 +1,12 @@ -import { Container, Text, Sprite, Loader, Texture, interaction } from "pixi.js" +import { Container, Loader, Texture, interaction } from "pixi.js" import { Point } from "../Datatypes/MathTypes/point" import { InfoSystem } from "../Systems/InfoSystem" +import { Button } from "./Button" export class PlanningMenu extends Container { system: InfoSystem - nameField: Text - portrait: Sprite - constructor(system: InfoSystem) { super() @@ -44,11 +42,9 @@ export class PlanningMenu extends Container { for (let i = 0; i < buttonDef.length; i++) { let def = buttonDef[i] - let button = new Sprite(def.texture) - if (def.action) { - button.on("click", def.action, def.context) - button.interactive = true - } + let button = new Button(def.texture) + if (def.action) button.onClick(def.action, def.context) + container.addChild(button) button.position = new Point(i * 18, 0) } diff --git a/Program/src/globals.ts b/Program/src/globals.ts index ef5dde7..242ca12 100644 --- a/Program/src/globals.ts +++ b/Program/src/globals.ts @@ -1,9 +1,9 @@ -import { World, Entity } from "ecsy"; -import { Application } from "pixi.js"; -import { Observable } from "./Datatypes/observable"; +import { World, Entity } from "ecsy" +import { Application } from "pixi.js" +import { Observable } from "./Datatypes/observable" export default { world: null, app: null, selected: new Observable(), -} \ No newline at end of file +}