Class: Metro::UI::Menu

Inherits:
Model
  • Object
show all
Defined in:
lib/metro/models/ui/menu.rb

Overview

Note:

Only one ‘menu’ can be defined for a given scene

Draws a a menu of options. A menu model inserts itself into the scene as an event target as it needs to maintain the state of the menu. When an option is selected an event is fired based on the name of the option.

Examples:

Creating a menu with basic options


menu:
  model: metro::ui::menu
  position: "472.0,353.0,5.0"
  alpha: 0
  unselected_color: "rgba(119,119,119,1.0)"
  selected_color: "rgba(255,255,255,1.0)"
  options: [ 'Start Game', 'Exit' ]

Creating a menu with a selected item


menu:
  model: metro::ui::menu
  position: "472.0,353.0,5.0"
  alpha: 0
  unselected_color: "rgba(119,119,119,1.0)"
  selected_color: "rgba(255,255,255,1.0)"
  options:
    selected: 0
    items: [ 'Start Game', 'Exit' ]

Creating a menu with complex options


menu:
  model: metro::ui::menu
  position: "472.0,353.0,5.0"
  alpha: 0
  layout: vertical
  # layout: horizontal
  unselected_color: "rgba(119,119,119,1.0)"
  selected_color: "rgba(255,255,255,1.0)"
  options:
    selected: 1
    items:
    -
      model: "metro::ui::label"
      text: "Start Game"
      action: start_game
    -
      model: metro::ui::label
      text: Exit
      action: exit_game

Constant Summary

Constants included from Metro::Units

Metro::Units::Bounds

Instance Attribute Summary collapse

Attributes inherited from Model

#scene, #window

Instance Method Summary collapse

Methods inherited from Model

#_load, #_save, #after_initialize, #create, #draw_completed?, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #to_hash, #update, #update_completed?

Methods included from HasEvents

included

Methods included from KeyValueCoding

#get, #set

Methods included from PropertyOwner

included, #properties

Constructor Details

This class inherits a constructor from Metro::Model

Instance Attribute Details

#alphaObject

The alpha level of the menu from 0 to 255.



65
# File 'lib/metro/models/ui/menu.rb', line 65

property :alpha, default: 255

#dimensionsObject

The dimensions of the menu. This is based on the items within the menu.



78
79
80
# File 'lib/metro/models/ui/menu.rb', line 78

property :dimensions do
  Dimensions.of (right_x - left_x), (bottom_y - top_y)
end

#enabledObject

Determines whether the menu is currently enabled or disabled.



117
# File 'lib/metro/models/ui/menu.rb', line 117

property :enabled, type: :boolean, default: true

#layoutObject

Allows the menu to be layouted out in a ‘horizontal’ or ‘vertical’ fashion. By default this is ‘vertical’



122
# File 'lib/metro/models/ui/menu.rb', line 122

property :layout, type: :text, default: "vertical"

#movement_sampleObject

The sample sound when moving between the different options in the menu.



113
# File 'lib/metro/models/ui/menu.rb', line 113

property :movement_sample, type: :sample, path: "menu-movement.wav"

#optionsObject

The options that are displayed in the menu. These are by default ‘metro::ui::labels’ But can be defined more dynamically as neeeded.

See Also:

  • Model::Property::OptionsProperty


87
# File 'lib/metro/models/ui/menu.rb', line 87

property :options

#paddingObject

The distance between each of the menu items



73
# File 'lib/metro/models/ui/menu.rb', line 73

property :padding, default: 20

#positionObject

The position of the menu



61
# File 'lib/metro/models/ui/menu.rb', line 61

property :position, default: Game.center

#scaleObject

The scale at which the menu should be drawn.



69
# File 'lib/metro/models/ui/menu.rb', line 69

property :scale, default: Scale.one

#selected_colorObject

The color of the item that currently has focus.



95
# File 'lib/metro/models/ui/menu.rb', line 95

property :selected_color, type: :color, default: "rgba(255,255,255,1.0)"

#selection_sampleObject

The sample sound that plays when a selection has been made.



109
# File 'lib/metro/models/ui/menu.rb', line 109

property :selection_sample, type: :sample, path: "menu-selection.wav"

#unselected_colorObject

The color for all the currently unselected items.



91
# File 'lib/metro/models/ui/menu.rb', line 91

property :unselected_color, type: :color, default: "rgba(119,119,119,1.0)"

Instance Method Details

#adjust_alpha_on_colors(alpha) ⇒ Object



102
103
104
105
# File 'lib/metro/models/ui/menu.rb', line 102

def adjust_alpha_on_colors(alpha)
  self.selected_color_alpha = alpha
  self.unselected_color_alpha = alpha
end

#alpha_changed(alpha) ⇒ Object



97
98
99
100
# File 'lib/metro/models/ui/menu.rb', line 97

def alpha_changed(alpha)
  adjust_alpha_on_colors(alpha)
  options.each { |option| option.alpha = alpha.floor }
end

#bottom_yObject



150
151
152
# File 'lib/metro/models/ui/menu.rb', line 150

def bottom_y
  options.map {|option| option.bounds.bottom }.max
end

#boundsObject



124
125
126
# File 'lib/metro/models/ui/menu.rb', line 124

def bounds
  Bounds.new left: left_x, right: right_x, top: top_y, bottom: bottom_y
end

#drawObject



208
209
210
# File 'lib/metro/models/ui/menu.rb', line 208

def draw
  options.each { |label| label.draw }
end

#left_xObject



138
139
140
# File 'lib/metro/models/ui/menu.rb', line 138

def left_x
  options.map {|option| option.bounds.left }.min
end

#position_changed(new_position) ⇒ Object

When the position has changed on every other time beside the first time we want to update the position of all the options defined in the menu.



132
133
134
135
136
# File 'lib/metro/models/ui/menu.rb', line 132

def position_changed(new_position)
  return unless properties[:position]
  difference = Point.parse(new_position) - Point.parse(properties[:position])
  options.each { |option| option.position += difference }
end

#right_xObject



142
143
144
# File 'lib/metro/models/ui/menu.rb', line 142

def right_x
  options.map {|option| option.bounds.right }.max
end

#showObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/metro/models/ui/menu.rb', line 182

def show
  adjust_alpha_on_colors(alpha)

  previous_width = 0

  options.each_with_index do |option,index|
    option.color = unselected_color
    option.scale = scale

    option_x = x + (layout == "horizontal" ? (previous_width + padding) * index : 0)
    previous_width = option.width
    option_y = y + (layout == "vertical" ? (option.height + padding) * index : 0)
    option_z = z

    option.position = option.position + Point.at(option_x,option_y,option_z)

  end

  options.selected.color = selected_color
end

#top_yObject



146
147
148
# File 'lib/metro/models/ui/menu.rb', line 146

def top_y
  options.map {|option| option.bounds.top }.min
end

#update_optionsObject



203
204
205
206
# File 'lib/metro/models/ui/menu.rb', line 203

def update_options
  options.unselected.each { |option| option.color = unselected_color }
  options.selected.color = selected_color
end