From ea2237fa3166321f06c4aea2586de315fab62282 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Fri, 5 Jan 2024 17:05:18 -0300 Subject: feat Add event to menu selection --- lib/menu.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/menu.rb b/lib/menu.rb index 3921147..4ed44e9 100644 --- a/lib/menu.rb +++ b/lib/menu.rb @@ -1,4 +1,4 @@ -# Copyright 2022-2023 Frederico de Oliveira Linhares +# Copyright 2022-2024 Frederico de Oliveira Linhares # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ module CandyGear @stack << menu; end + def current_menu() = @stack[-1]; + def push(menu) = @stack << menu; def pop() @@ -134,13 +136,14 @@ module CandyGear end class Option - attr_reader(:action, :text, :width, :height); + attr_reader(:action, :selection, :text, :width, :height); - def initialize(text, action, width, height); + def initialize(text, width, height, action, selection = nil); @text = text; @action = action; @width = width; @height = height; + @selection = selection; end end @@ -157,9 +160,10 @@ module CandyGear texture = Texture.from_text(menu_view.font, opt[:text]) Option.new( Sprite.new(texture, 0, 0, 1.0, 1.0), - opt[:action], texture.width, - texture.height); + texture.height, + opt[:action], + opt[:selection]); end @current_option = 0; @option_max_width = 0; @@ -181,6 +185,7 @@ module CandyGear def next_opt() @current_option += 1; @current_option = 0 if @current_option >= @options.size(); + self.call_selection(); end def pred_opt() @@ -189,6 +194,7 @@ module CandyGear else @current_option -= 1; end + self.call_selection(); end def activate() @@ -215,5 +221,10 @@ module CandyGear @option_max_height * @current_option, @menu_view.border_width, @menu_view.border_height); end + + protected + def call_selection + @options[@current_option].selection&.call(); + end end end -- cgit v1.2.3