Class: Metro::UI::Menu
Overview
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.
Constant Summary
Constants included from Metro::Units
Instance Attribute Summary collapse
-
#alpha ⇒ Object
The alpha level of the menu from 0 to 255.
-
#dimensions ⇒ Object
The dimensions of the menu.
-
#enabled ⇒ Object
Determines whether the menu is currently enabled or disabled.
-
#layout ⇒ Object
Allows the menu to be layouted out in a ‘horizontal’ or ‘vertical’ fashion.
-
#movement_sample ⇒ Object
The sample sound when moving between the different options in the menu.
-
#options ⇒ Object
The options that are displayed in the menu.
-
#padding ⇒ Object
The distance between each of the menu items.
-
#position ⇒ Object
The position of the menu.
-
#scale ⇒ Object
The scale at which the menu should be drawn.
-
#selected_color ⇒ Object
The color of the item that currently has focus.
-
#selection_sample ⇒ Object
The sample sound that plays when a selection has been made.
-
#unselected_color ⇒ Object
The color for all the currently unselected items.
Attributes inherited from Model
Instance Method Summary collapse
- #adjust_alpha_on_colors(alpha) ⇒ Object
- #alpha_changed(alpha) ⇒ Object
- #bottom_y ⇒ Object
- #bounds ⇒ Object
- #draw ⇒ Object
- #left_x ⇒ Object
-
#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.
- #right_x ⇒ Object
- #show ⇒ Object
- #top_y ⇒ Object
- #update_options ⇒ Object
Methods inherited from Model
#_load, #_save, #after_initialize, #completed?, #create, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #to_hash, #update
Methods included from HasEvents
Methods included from KeyValueCoding
Methods included from PropertyOwner
Constructor Details
This class inherits a constructor from Metro::Model
Instance Attribute Details
#alpha ⇒ Object
The alpha level of the menu from 0 to 255.
65 |
# File 'lib/metro/models/ui/menu.rb', line 65 property :alpha, default: 255 |
#dimensions ⇒ Object
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 |
#enabled ⇒ Object
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 |
#layout ⇒ Object
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_sample ⇒ Object
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" |
#options ⇒ Object
The options that are displayed in the menu. These are by default ‘metro::ui::labels’ But can be defined more dynamically as neeeded.
87 |
# File 'lib/metro/models/ui/menu.rb', line 87 property :options |
#padding ⇒ Object
The distance between each of the menu items
73 |
# File 'lib/metro/models/ui/menu.rb', line 73 property :padding, default: 20 |
#position ⇒ Object
The position of the menu
61 |
# File 'lib/metro/models/ui/menu.rb', line 61 property :position, default: Game.center |
#scale ⇒ Object
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_color ⇒ Object
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_sample ⇒ Object
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_color ⇒ Object
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) .each { |option| option.alpha = alpha.floor } end |
#bottom_y ⇒ Object
150 151 152 |
# File 'lib/metro/models/ui/menu.rb', line 150 def bottom_y .map {|option| option.bounds.bottom }.max end |
#bounds ⇒ Object
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 |
#draw ⇒ Object
208 209 210 |
# File 'lib/metro/models/ui/menu.rb', line 208 def draw .each { |label| label.draw } end |
#left_x ⇒ Object
138 139 140 |
# File 'lib/metro/models/ui/menu.rb', line 138 def left_x .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]) .each { |option| option.position += difference } end |
#right_x ⇒ Object
142 143 144 |
# File 'lib/metro/models/ui/menu.rb', line 142 def right_x .map {|option| option.bounds.right }.max end |
#show ⇒ Object
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 .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 .selected.color = selected_color end |
#top_y ⇒ Object
146 147 148 |
# File 'lib/metro/models/ui/menu.rb', line 146 def top_y .map {|option| option.bounds.top }.min end |
#update_options ⇒ Object
203 204 205 206 |
# File 'lib/metro/models/ui/menu.rb', line 203 def .unselected.each { |option| option.color = unselected_color } .selected.color = selected_color end |