Class: RGame::Engine::UI::MenuItem
- Defined in:
- lib/rgame/engine/ui/menu_item.rb
Overview
One entry in a Menu: a label on a nine-slice, and a signal for when it is chosen.
resume = .add_item('Resume')
resume.on_activated { cutscene.close }
It draws itself from its state — focused, pressed, disabled or idle — which is why the shipped atlas has an element for each. There is no hover, because there is no pointer: what a mouse-driven control would get from the cursor being over it, this gets from the Menu telling it it is the focused one.
Its position is its own, resolved through the tree like any node's, so a Menu inside a PlayerLayer puts its items inside that player's region without either of them arranging it.
Constant Summary collapse
- STYLE =
Atlas element per state. Replaceable per menu, so a game with its own art is not obliged to name it the way the shipped atlas does.
{ idle: :button_idle, focus: :button_focus, pressed: :button_pressed, disabled: :button_disabled }.freeze
- LABEL_COLOR =
[46, 34, 24].freeze
- DISABLED_LABEL_COLOR =
[120, 110, 100].freeze
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#focused ⇒ Object
writeonly
Sets the attribute focused.
-
#label ⇒ Object
Returns the value of attribute label.
-
#pressed ⇒ Object
writeonly
Sets the attribute pressed.
Attributes inherited from Node2D
#abs_angle, #abs_band, #abs_input_owner, #abs_x, #abs_y, #angle, #band, #children, #components, #context, #height, #input_owner, #parent, #paused, #scene, #sibling_order, #width, #x, #y, #z
Instance Method Summary collapse
-
#activate ⇒ Object
Fires the signal and returns the item, or nil if it is disabled — so a caller never has to check first, and a disabled item cannot be activated by any route.
- #enabled? ⇒ Boolean
- #focused? ⇒ Boolean
-
#initialize(label:, style: STYLE, enabled: true) ⇒ MenuItem
constructor
A new instance of MenuItem.
-
#on_draw(renderer, _view) ⇒ Object
The panel and its label share this node's slot, so the only ordering question is which of the two goes on top — and
z: 1says exactly that, about this item and nothing else in the frame.
Methods inherited from Node2D
#add_component, #add_node, #children_unsorted!, #control, #draw, #enter_tree, #exit_tree, #freed?, #get_component, #in_tree?, #on_add, #on_control, #on_remove, #on_update, #queue_free, #remove_component, #remove_node, #root, #sweep_freed, #system, #update
Methods included from Signal::DSL
Constructor Details
#initialize(label:, style: STYLE, enabled: true) ⇒ MenuItem
Returns a new instance of MenuItem.
39 40 41 42 43 44 45 46 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 39 def initialize(label:, style: STYLE, enabled: true, **) super(**) @label = label @style = style @enabled = enabled @focused = false @pressed = false end |
Instance Attribute Details
#enabled ⇒ Object
Returns the value of attribute enabled.
36 37 38 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 36 def enabled @enabled end |
#focused=(value) ⇒ Object (writeonly)
Sets the attribute focused
37 38 39 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 37 def focused=(value) @focused = value end |
#label ⇒ Object
Returns the value of attribute label.
36 37 38 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 36 def label @label end |
#pressed=(value) ⇒ Object (writeonly)
Sets the attribute pressed
37 38 39 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 37 def pressed=(value) @pressed = value end |
Instance Method Details
#activate ⇒ Object
Fires the signal and returns the item, or nil if it is disabled — so a caller never has to check first, and a disabled item cannot be activated by any route.
54 55 56 57 58 59 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 54 def activate return nil unless @enabled on_activated_signal.emit self end |
#enabled? ⇒ Boolean
48 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 48 def enabled? = @enabled |
#focused? ⇒ Boolean
49 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 49 def focused? = @focused |
#on_draw(renderer, _view) ⇒ Object
The panel and its label share this node's slot, so the only ordering
question is which of the two goes on top — and z: 1 says exactly
that, about this item and nothing else in the frame.
64 65 66 67 68 |
# File 'lib/rgame/engine/ui/menu_item.rb', line 64 def on_draw(renderer, _view) renderer.nine_slice(@style.fetch(state), abs_x, abs_y, width, height) renderer.text(@label, label_x(renderer), label_y(renderer), z: 1, color: @enabled ? LABEL_COLOR : DISABLED_LABEL_COLOR) end |