Class: Bestguigui::Menu
- Inherits:
-
Object
- Object
- Bestguigui::Menu
- Defined in:
- lib/bestguigui/menu.rb
Overview
Liste de choix avec un index sélectionné. Gère l'input (haut/bas/valider) et son propre rendu (texte + fond + bordure optionnels) — un state n'a en général rien d'autre à écrire que la construction du menu.
@menu = Bestguigui::Menu.new(
{
"New Game" => lambda { app.switch_state(GameplayState.new) },
"Load" => lambda { app.switch_state(LoadState.new) },
"Exit" => lambda { app.close! }
},
x: 300, y: 220,
font_size: 32,
color: Gosu::Color::WHITE,
selected_color: Gosu::Color::YELLOW,
background: Gosu::Color.new(200, 20, 20, 20),
border_color: Gosu::Color::WHITE,
border_width: 2,
padding: 16,
line_height: 40,
mouse: true
)
def update(dt) = @menu.hover(app.mouse_x, app.mouse_y) # seulement si mouse: true
def (id) = @menu.(id)
def draw = @menu.draw
mouse: true active le survol (met à jour la sélection via #hover, à
appeler chaque frame avec la position souris) et le clic (Gosu::MsLeft,
déjà géré par #button_down). Off par défaut : sans #hover appelé, rien ne
change au comportement clavier seul.
Rendu custom possible via each_item si le style intégré ne convient pas :
@menu.each_item { |label, selected| ... }
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
Mapping clavier par défaut : flèches ou Z/S pour naviguer, Entrée/Espace pour valider.
- #confirm ⇒ Object
- #down ⇒ Object
- #draw ⇒ Object
-
#each_item ⇒ Object
yield(label, selected?) pour chaque item, dans l'ordre.
- #font ⇒ Object
-
#hover(mouse_x, mouse_y) ⇒ Object
À appeler chaque frame (ex: dans #update) avec la position souris — ne fait rien si mouse: false.
-
#initialize(items, x: 0, y: 0, font_size: 28, color: Gosu::Color::WHITE, selected_color: Gosu::Color::YELLOW, background: nil, border_color: nil, border_width: 2, padding: 12, line_height: 40, mouse: false) ⇒ Menu
constructor
A new instance of Menu.
- #selected_label ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(items, x: 0, y: 0, font_size: 28, color: Gosu::Color::WHITE, selected_color: Gosu::Color::YELLOW, background: nil, border_color: nil, border_width: 2, padding: 12, line_height: 40, mouse: false) ⇒ Menu
Returns a new instance of Menu.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bestguigui/menu.rb', line 36 def initialize(items, x: 0, y: 0, font_size: 28, color: Gosu::Color::WHITE, selected_color: Gosu::Color::YELLOW, background: nil, border_color: nil, border_width: 2, padding: 12, line_height: 40, mouse: false) @labels = items.keys @actions = items.values @index = 0 @x = x @y = y @font_size = font_size @color = color @selected_color = selected_color @background = background @border_color = border_color @border_width = border_width @padding = padding @line_height = line_height @mouse = mouse end |
Instance Method Details
#button_down(id) ⇒ Object
Mapping clavier par défaut : flèches ou Z/S pour naviguer, Entrée/Espace pour valider. Clic gauche valide aussi si mouse: true (l'item sélectionné est celui sous le curseur, via #hover).
72 73 74 75 76 77 78 79 |
# File 'lib/bestguigui/menu.rb', line 72 def (id) case id when Gosu::KB_UP, Gosu::KB_W then up when Gosu::KB_DOWN, Gosu::KB_S then down when Gosu::KB_RETURN, Gosu::KB_SPACE then confirm when Gosu::MsLeft then confirm if @mouse end end |
#confirm ⇒ Object
65 66 67 |
# File 'lib/bestguigui/menu.rb', line 65 def confirm @actions[@index].call end |
#down ⇒ Object
61 62 63 |
# File 'lib/bestguigui/menu.rb', line 61 def down @index = (@index + 1) % @labels.size end |
#draw ⇒ Object
104 105 106 107 108 |
# File 'lib/bestguigui/menu.rb', line 104 def draw draw_background draw_border draw_items end |
#each_item ⇒ Object
yield(label, selected?) pour chaque item, dans l'ordre.
96 97 98 |
# File 'lib/bestguigui/menu.rb', line 96 def each_item @labels.each_with_index { |label, i| yield label, i == @index } end |
#font ⇒ Object
100 101 102 |
# File 'lib/bestguigui/menu.rb', line 100 def font @font ||= Gosu::Font.new(@font_size) end |
#hover(mouse_x, mouse_y) ⇒ Object
À appeler chaque frame (ex: dans #update) avec la position souris — ne fait rien si mouse: false. Met à jour l'item survolé.
83 84 85 86 87 88 89 |
# File 'lib/bestguigui/menu.rb', line 83 def hover(mouse_x, mouse_y) return unless @mouse @labels.each_index do |i| @index = i if item_hit?(i, mouse_x, mouse_y) end end |
#selected_label ⇒ Object
91 92 93 |
# File 'lib/bestguigui/menu.rb', line 91 def selected_label @labels[@index] end |
#up ⇒ Object
57 58 59 |
# File 'lib/bestguigui/menu.rb', line 57 def up @index = (@index - 1) % @labels.size end |