Interactivity
package demos
import kotlin.browser.document
fun main(args: Array<String>) {
data class ButtonState(
var isDown: Boolean = false,
var isOver: Boolean = false
)
val app = PIXI.Application()
document.body!!.appendChild(app.view)
val background = PIXI.Sprite.fromImage("../assets/button_test_BG.jpg")
background.width = app.screen.width
background.height = app.screen.height
app.stage.addChild(background)
val textureButton = PIXI.Texture.fromImage("../assets/button.png")
val textureButtonDown = PIXI.Texture.fromImage("../assets/buttonDown.png")
val textureButtonOver = PIXI.Texture.fromImage("../assets/buttonOver.png")
fun onButtonDown(event: PIXI.interaction.InteractionEvent) {
val button = event.currentTarget as PIXI.Sprite
button.texture = textureButtonDown
button.alpha = 1
val buttonState = button.asDynamic().buttonState as ButtonState
buttonState.isDown = true
}
fun onButtonUp(event: PIXI.interaction.InteractionEvent) {
val button = event.currentTarget as PIXI.Sprite
val buttonState = button.asDynamic().buttonState as ButtonState
buttonState.isDown = false
if (buttonState.isOver) {
button.texture = textureButtonOver
} else {
button.texture = textureButton
}
}
fun onButtonOver(event: PIXI.interaction.InteractionEvent) {
val button = event.currentTarget as PIXI.Sprite
val buttonState = button.asDynamic().buttonState as ButtonState
buttonState.isOver = true
if (buttonState.isDown) {
return
}
button.texture = textureButtonOver
}
fun onButtonOut(event: PIXI.interaction.InteractionEvent) {
val button = event.currentTarget as PIXI.Sprite
val buttonState = button.asDynamic().buttonState as ButtonState
buttonState.isOver = false
if (buttonState.isDown) {
return
}
button.texture = textureButton
}
val buttons = ArrayList<PIXI.Sprite>()
val buttonPositions = arrayOf(
175, 75,
655, 75,
410, 325,
150, 465,
685, 445
)
for (i in 0 until 5) {
val button = PIXI.Sprite(textureButton)
button.asDynamic().buttonState = ButtonState()
button.buttonMode = true
button.anchor.set(0.5)
button.x = buttonPositions[i * 2]
button.y = buttonPositions[i * 2 + 1]
button.interactive = true
button.buttonMode = true
button
.on("pointerdown", ::onButtonDown)
.on("pointerup", ::onButtonUp)
.on("pointerupoutside", ::onButtonUp)
.on("pointerover", ::onButtonOver)
.on("pointerout", ::onButtonOut)
app.stage.addChild(button)
buttons.add(button)
}
buttons[0].scale.set(1.2)
buttons[2].rotation = kotlin.math.PI / 10
buttons[3].scale.set(0.8)
buttons[4].scale.set(0.8, 1.2)
buttons[4].rotation = kotlin.math.PI
}