diff --git a/src/lib/keyhook.rb b/src/lib/keyhook.rb index e30b077..81b0687 100644 --- a/src/lib/keyhook.rb +++ b/src/lib/keyhook.rb @@ -3,6 +3,8 @@ def nullmethod return end +# TODO: use Object.create_method +# https://apidock.com/ruby/Module/define_method # Handler for key events class KeyHook diff --git a/src/lib/ui.rb b/src/lib/ui.rb index bd0ec1b..0057cfb 100644 --- a/src/lib/ui.rb +++ b/src/lib/ui.rb @@ -73,6 +73,15 @@ class Button < UI @events = {} end + def onclick + end + + # TODO: make this work + # TODO: make this also work for KeyHooks + def set_onclick(name, &block) + self.class.send(:define_method, name, &block) + end + def hover? inx = window.mouse_x >= self.x && window.mouse_x <= self.x + self.width iny = window.mouse_y >= self.y - self.padding[:y] && window.mouse_y <= self.y + self.height + self.padding[:y] @@ -82,14 +91,6 @@ class Button < UI self.selected = inx && iny end - def add_event(event_sym, method_ptr) - self.events[event_sym] = method_ptr - end - - def onevent(event_sym, *args, **kwargs) - self.events[event_sym].(*args, **kwargs) - end - def render sel = self.hover? ? :selected : :default self.draw_rect(0, 0, self.width, self.height, self.colors[:background][sel]) diff --git a/src/ui/mainmenu.rb b/src/ui/mainmenu.rb index ec158c6..21063ac 100644 --- a/src/ui/mainmenu.rb +++ b/src/ui/mainmenu.rb @@ -19,6 +19,9 @@ class MainMenu < UI @quitbtn = Button.new(self.window, self, "Quit", self.window.fonts[:button]) @quitbtn.x, @quitbtn.y = self.width/2 - @quitbtn.width/2, @quitbtn.height + @playbtn.y + 16 + @quitbtn.create_method(:onclick) { + @window.close + } end