CabinGame/Luxe/blocks/ui/image_button.wren
2020-09-03 17:31:23 +02:00

75 lines
2.1 KiB
Text

import "luxe: ui/image" for UIImage
import "luxe: world" for UIEvent, UI
import "luxe: ui/control" for Control
import "globals" for Globals
class ImageButton{
static create(ent){
var button = UIImage.create(ent)
var data = {
"hover": false,
"press": false,
"state_change": null,
"tooltip": null
}
Control.set_state_data(button, data)
Control.set_events(button) {|event|
var data = null
if(event.type == UIEvent.enter){
data = Control.get_state_data(button)
data["hover"] = true
} else if(event.type == UIEvent.exit) {
data = Control.get_state_data(button)
data["hover"] = false
data["press"] = false
} else if(event.type == UIEvent.press) {
data = Control.get_state_data(button)
data["press"] = true
} else if(event.type == UIEvent.release) {
data = Control.get_state_data(button)
data["press"] = false
}
if(data){
var func = data["state_change"]
if(func){
func.call(data, button)
}
var tooltip = data["tooltip"]
if(tooltip){
if(data["hover"]) {
Globals["Tooltip"].set(tooltip, button)
//this only works when the UI is at the very bottom
//which it is so whatever :)
Globals["Tooltip"].fix_y(Globals["UiRect"].height-Control.get_pos_y_abs(button))
} else {
Globals["Tooltip"].clear(button)
}
}
}
}
return button
}
static set_tooltip(control, tooltip){
var data = Control.get_state_data(control)
data["tooltip"] = tooltip
}
static set_state_change(control, fn){
var data = Control.get_state_data(control)
data["state_change"] = fn
}
static get_data(control){
//System.print(Control.get_state_data(control))
}
static set_tile_uv(control, tiles, index){
UIImage.set_uv(control, index.x/tiles.x, index.y/tiles.y, (index.x+1)/tiles.x, (index.y+1)/tiles.y)
}
}