Class: RGame::Engine::UI::MenuItem

Inherits:
Node2D
  • Object
show all
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 = menu.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

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

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

#signal

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

#enabledObject

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

Parameters:

  • value

    the value to set the attribute focused to.



37
38
39
# File 'lib/rgame/engine/ui/menu_item.rb', line 37

def focused=(value)
  @focused = value
end

#labelObject

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

Parameters:

  • value

    the value to set the attribute pressed to.



37
38
39
# File 'lib/rgame/engine/ui/menu_item.rb', line 37

def pressed=(value)
  @pressed = value
end

Instance Method Details

#activateObject

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

Returns:

  • (Boolean)


48
# File 'lib/rgame/engine/ui/menu_item.rb', line 48

def enabled? = @enabled

#focused?Boolean

Returns:

  • (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