Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class button{
- attr widget
- attr on_trigger
- attr mouse_down
- def button(name, text, origin, size, on_trigger){
- this.widget := stage.create_actor()
- this.widget.set_origin(origin)
- this.widget.set_size(size)
- this.widget.add_model("button", origin, size, "green_button.json")
- var text_color := vec3(1.0f, 1.0f, 1.0f)
- this.widget.add_text(name, text, origin, text_color, 24, "ui_font.ttf")
- var trigger = on_trigger
- trigger() // triggers
- this.on_trigger := on_trigger // does not trigger with this.on_trigger = on_trigger either
- this.mouse_down = false
- on_trigger() // triggers
- this.on_trigger() // this does not trigger
- this.widget.construct(fun(){}, fun(){}, fun[this](delta){this.logic(delta)})
- }
- def logic(delta){
- var current_camera := stage.get_scene("current").get_camera("current")
- var world_coordinates := current_camera.convert_screen_to_world(core.input.get_mouse_coordinates())
- var mouse_x = world_coordinates.x
- var mouse_y = world_coordinates.y
- var x = this.widget.get_origin().x
- var y = this.widget.get_origin().y
- var w = this.widget.get_size().x
- var h = this.widget.get_size().y
- if(mouse_x >= x - w / 2.0f && mouse_x <= x + w / 2.0f && mouse_y >= y - h / 2.0f && mouse_y <= y + h / 2.0f){
- if(core.input.is_key_down("mouse_left")){
- if(!this.mouse_down){
- this.widget.get_model("button").change_animation("button_on")
- this.mouse_down = true
- }
- } else {
- if(this.mouse_down){
- this.widget.get_model("button").change_animation("button_off")
- this.mouse_down = false
- this.on_trigger()
- }
- }
- } else {
- this.mouse_down = false
- this.widget.get_model("button").change_animation("button_off")
- }
- }
- def get(){
- return this.widget
- }
- }
- return fun(name, text, origin, size, on_trigger){
- return button(name, text, origin, size, on_trigger)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement