Class: Chingu::SimpleMenu
- Inherits:
-
BasicGameObject
- Object
- BasicGameObject
- Chingu::SimpleMenu
- Includes:
- Helpers::InputClient
- Defined in:
- lib/chingu/simple_menu.rb
Overview
When you want a quick 'n dirty menu, SimpleMenu might help. Basic usage:
SimpleMenu.create(:menu_items => {"StartGame" => PlayState, "Edit" => :exit})
It will catch keys :up, :down, :return and :space and navigate through the menu
Instance Attribute Summary (collapse)
-
- (Object) menu_items
Returns the value of attribute menu_items.
-
- (Object) visible
Returns the value of attribute visible.
Attributes inherited from BasicGameObject
Instance Method Summary (collapse)
- - (Object) draw
-
- (SimpleMenu) initialize(options = {})
constructor
A new instance of SimpleMenu.
- - (Object) on_deselect(object)
- - (Object) on_select(object)
- - (Object) select
- - (Object) selected
-
- (Object) step(value)
Moves selection within the menu.
Methods included from Helpers::InputClient
#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input
Methods inherited from BasicGameObject
all, create, #destroy, destroy_all, destroy_if, #draw_trait, each, each_with_index, #filename, initialize_trait, #pause!, #paused?, select, #setup, #setup_trait, size, trait, #trait_options, traits, #unpause!, #update, #update_trait
Methods included from Helpers::ClassInheritableAccessor
Constructor Details
- (SimpleMenu) initialize(options = {})
A new instance of SimpleMenu
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chingu/simple_menu.rb', line 34 def initialize( = {}) super # @font_size = options.delete(:font_size) || 30 @menu_items = .delete(:menu_items) @x = .delete(:x) || $window.width/2 @y = .delete(:y) || 0 @spacing = .delete(:spacing) || 100 @items = [] @visible = true y = @y .each do |key, value| item = if key.is_a? String Text.new(key, .dup) elsif key.is_a? Image GameObject.new(.merge!(:image => key)) elsif key.is_a? GameObject key..merge!(.dup) key end item.[:on_select] = method(:on_select) item.[:on_deselect] = method(:on_deselect) item.[:action] = value item.rotation_center = :center_top item.x = @x item.y = y y += item.height + @spacing @items << item end @selected = [:selected] || 0 step(0) self.input = {:up => lambda{step(-1)}, :down => lambda{step(1)}, [:return, :space] => :select} end |
Instance Attribute Details
- (Object) menu_items
Returns the value of attribute menu_items
32 33 34 |
# File 'lib/chingu/simple_menu.rb', line 32 def @menu_items end |
- (Object) visible
Returns the value of attribute visible
32 33 34 |
# File 'lib/chingu/simple_menu.rb', line 32 def visible @visible end |
Instance Method Details
- (Object) draw
99 100 101 |
# File 'lib/chingu/simple_menu.rb', line 99 def draw @items.each { |item| item.draw } end |
- (Object) on_deselect(object)
91 92 93 |
# File 'lib/chingu/simple_menu.rb', line 91 def on_deselect(object) object.color = ::Gosu::Color::WHITE end |
- (Object) on_select(object)
95 96 97 |
# File 'lib/chingu/simple_menu.rb', line 95 def on_select(object) object.color = ::Gosu::Color::RED end |
- (Object) select
83 84 85 |
# File 'lib/chingu/simple_menu.rb', line 83 def select dispatch_action(selected.[:action], self.parent) end |
- (Object) selected
87 88 89 |
# File 'lib/chingu/simple_menu.rb', line 87 def selected @items[@selected] end |
- (Object) step(value)
Moves selection within the menu. Can be called with negative or positive values. -1 and 1 makes most sense.
75 76 77 78 79 80 81 |
# File 'lib/chingu/simple_menu.rb', line 75 def step(value) selected.[:on_deselect].call(selected) @selected += value @selected = @items.count-1 if @selected < 0 @selected = 0 if @selected == @items.count selected.[:on_select].call(selected) end |