funky new button class
This commit is contained in:
parent
b83024ded6
commit
1e7668e6a1
5 changed files with 77 additions and 20 deletions
|
|
@ -9,6 +9,7 @@ export class ClickableSystem extends System {
|
||||||
priority = 80
|
priority = 80
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
//the great deselector
|
||||||
globals.app.stage.interactive = true
|
globals.app.stage.interactive = true
|
||||||
globals.app.stage.on("click", (event: interaction.InteractionEvent) => {
|
globals.app.stage.on("click", (event: interaction.InteractionEvent) => {
|
||||||
if (event.target == event.currentTarget) globals.selected.set(null)
|
if (event.target == event.currentTarget) globals.selected.set(null)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { Point } from "../Datatypes/MathTypes/point"
|
||||||
import { InfoSystem } from "../Systems/InfoSystem"
|
import { InfoSystem } from "../Systems/InfoSystem"
|
||||||
import { Name } from "../Components/name"
|
import { Name } from "../Components/name"
|
||||||
import { Entity } from "ecsy"
|
import { Entity } from "ecsy"
|
||||||
|
import { Button } from "./Button"
|
||||||
|
|
||||||
export class InfoMenu extends Container {
|
export class InfoMenu extends Container {
|
||||||
system: InfoSystem
|
system: InfoSystem
|
||||||
|
|
@ -61,12 +62,9 @@ export class InfoMenu extends Container {
|
||||||
|
|
||||||
for (let i = 0; i < buttonDef.length; i++) {
|
for (let i = 0; i < buttonDef.length; i++) {
|
||||||
let def = buttonDef[i]
|
let def = buttonDef[i]
|
||||||
let button = new Sprite(def.texture)
|
let button = new Button(def.texture)
|
||||||
|
|
||||||
if (def.action) {
|
if (def.action) button.onClick(def.action, def.context)
|
||||||
button.on("click", def.action, def.context)
|
|
||||||
button.interactive = true
|
|
||||||
}
|
|
||||||
|
|
||||||
container.addChild(button)
|
container.addChild(button)
|
||||||
button.position = new Point(i * 18, 0)
|
button.position = new Point(i * 18, 0)
|
||||||
|
|
|
||||||
|
|
@ -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 { Point } from "../Datatypes/MathTypes/point"
|
||||||
import { InfoSystem } from "../Systems/InfoSystem"
|
import { InfoSystem } from "../Systems/InfoSystem"
|
||||||
|
import { Button } from "./Button"
|
||||||
|
|
||||||
export class PlanningMenu extends Container {
|
export class PlanningMenu extends Container {
|
||||||
system: InfoSystem
|
system: InfoSystem
|
||||||
|
|
||||||
nameField: Text
|
|
||||||
portrait: Sprite
|
|
||||||
|
|
||||||
constructor(system: InfoSystem) {
|
constructor(system: InfoSystem) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
|
@ -44,11 +42,9 @@ export class PlanningMenu extends Container {
|
||||||
|
|
||||||
for (let i = 0; i < buttonDef.length; i++) {
|
for (let i = 0; i < buttonDef.length; i++) {
|
||||||
let def = buttonDef[i]
|
let def = buttonDef[i]
|
||||||
let button = new Sprite(def.texture)
|
let button = new Button(def.texture)
|
||||||
if (def.action) {
|
if (def.action) button.onClick(def.action, def.context)
|
||||||
button.on("click", def.action, def.context)
|
|
||||||
button.interactive = true
|
|
||||||
}
|
|
||||||
container.addChild(button)
|
container.addChild(button)
|
||||||
button.position = new Point(i * 18, 0)
|
button.position = new Point(i * 18, 0)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import { World, Entity } from "ecsy";
|
import { World, Entity } from "ecsy"
|
||||||
import { Application } from "pixi.js";
|
import { Application } from "pixi.js"
|
||||||
import { Observable } from "./Datatypes/observable";
|
import { Observable } from "./Datatypes/observable"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
world: <World>null,
|
world: <World>null,
|
||||||
app: <Application>null,
|
app: <Application>null,
|
||||||
selected: new Observable<Entity>(),
|
selected: new Observable<Entity>(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue