123 lines
3.8 KiB
Text
123 lines
3.8 KiB
Text
import "luxe: ui/image" for UIImage
|
|
import "luxe: world" for UIEvent, UI
|
|
import "luxe: ui/control" for Control
|
|
import "luxe: settings" for Settings
|
|
import "luxe: io" for IO
|
|
import "luxe: math" for Math
|
|
|
|
import "globals" for Globals
|
|
import "math/util" for Util
|
|
|
|
class Clickable{
|
|
static make_clickable(button){
|
|
var ent = Control.get_entity(button)
|
|
var data = Control.get_state_data(button)
|
|
if(data == null) data = {}
|
|
var newData = {
|
|
"hover": false,
|
|
"press": false,
|
|
"state_change": null,
|
|
"tooltip": null,
|
|
"allow_cancelled": false,
|
|
"click_time": Settings.get("game.ui.button.click_time", 10),
|
|
"click_area": Settings.get("game.ui.button.click_area", 2),
|
|
"on_click": null,
|
|
"press_time": -1,
|
|
"press_location": [0, 0],
|
|
}
|
|
newData.each{|val|
|
|
if(data.containsKey(val.key)) Log.print("[warning] overriding key %(val.key)")
|
|
data[val.key] = val.value
|
|
}
|
|
Control.set_state_data(button, data)
|
|
Control.set_allow_input(button, true)
|
|
|
|
Control.set_events(button) {|event|
|
|
var data = Control.get_state_data(button)
|
|
if(!Util.valid_event(event, data["allow_cancelled"])) return
|
|
var changed = false
|
|
if(event.type == UIEvent.enter){
|
|
changed = true
|
|
data["hover"] = true
|
|
} else if(event.type == UIEvent.exit) {
|
|
changed = true
|
|
data["hover"] = false
|
|
data["press"] = false
|
|
} else if(event.type == UIEvent.press) {
|
|
changed = true
|
|
data["press"] = true
|
|
if(data["click_time"] < 0){
|
|
if(data["on_click"])data["on_click"].call() //you are valid, you are loved, you have been clicked
|
|
} else {
|
|
data["press_time"] = IO.timestamp()
|
|
data["press_location"] = [event.x, event.y]
|
|
}
|
|
} else if(event.type == UIEvent.release) {
|
|
changed = true
|
|
data["press"] = false
|
|
|
|
var press_time = data["press_time"]
|
|
var click_time = data["click_time"]
|
|
if(click_time >= 0 && press_time >= 0 &&
|
|
press_time + click_time >= IO.timestamp()){
|
|
if(data["on_click"])data["on_click"].call()
|
|
}
|
|
} else if(event.type == UIEvent.move) {
|
|
var press_location = data["press_location"]
|
|
//if mouse moved too much
|
|
if(Math.length(press_location.x - event.x, press_location.y - event.y) > data["click_area"]) {
|
|
//abort click
|
|
data["press_time"] = -1
|
|
}
|
|
}
|
|
|
|
if(changed){
|
|
//Log.print(UIEvent.name(string_type) + " " + data["tooltip"])
|
|
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)
|
|
}
|
|
}
|
|
UI.event_cancel(ent, event.id)
|
|
}
|
|
|
|
}
|
|
|
|
return button
|
|
}
|
|
|
|
static set_tooltip(control, tooltip){
|
|
var data = Control.get_state_data(control)
|
|
data["tooltip"] = tooltip
|
|
}
|
|
|
|
static set_allow_cancelled(control, allow){
|
|
var data = Control.get_state_data(control)
|
|
data["allow_cancelled"] = allow
|
|
}
|
|
|
|
static set_state_change(control, fn){
|
|
var data = Control.get_state_data(control)
|
|
data["state_change"] = fn
|
|
}
|
|
|
|
static set_on_click(control, fn){
|
|
var data = Control.get_state_data(control)
|
|
data["on_click"] = fn
|
|
}
|
|
|
|
static get_data(control){
|
|
return Control.get_state_data(control)
|
|
}
|
|
}
|
|
|