Method pointers for button events

physics
E. Almqvist 3 years ago
parent 3d23e1d3cb
commit 84c3d6ad26
  1. 2
      src/config.rb
  2. 15
      src/lib/ui.rb

@ -4,7 +4,7 @@ WORLD_SEED = 123456789
# UI
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 900
WINDOW_FULLSCREEN = true
WINDOW_FULLSCREEN = false
MAIN_FONT = "monospace"
# Keybinds

@ -42,7 +42,7 @@ class UI
end
class Button < UI
attr_accessor :selected, :menu, :colors
attr_accessor :selected, :menu, :colors, :events
attr_reader :text, :width, :height, :text_width, :text_height, :font, :zindex, :padding
def initialize(window, menu, text, font, x=0, y=0, padding={x:32, y:2}, zindex=0)
super window, x, y, width, height, zindex, menu.uiscale
@ -80,12 +80,13 @@ class Button < UI
self.selected = inx && iny
end
def method_ptr=(new_ptr)
# TODO: make a method pointer for button presses etc
def add_event(event_sym, method_ptr)
self.events[event_sym] = method_ptr
end
def onclick(id, *args, **kwargs)
self.method_ptr.call(id, *args, **kwargs)
def onevent(event_sym, *args, **kwargs)
# self.method_ptr.call(id, *args, **kwargs)
self.events[event_sym].(*args, **kwargs)
end
def render
@ -103,6 +104,10 @@ class MainMenu < UI
@show = show
end
def quit_game
self.window.close!
end
def render
if( @show ) then
self.draw_rect(0, 0, self.width, self.height, 0xaa_111015)

Loading…
Cancel
Save