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 # UI
WINDOW_WIDTH = 1600 WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 900 WINDOW_HEIGHT = 900
WINDOW_FULLSCREEN = true WINDOW_FULLSCREEN = false
MAIN_FONT = "monospace" MAIN_FONT = "monospace"
# Keybinds # Keybinds

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

Loading…
Cancel
Save