Class: Chingu::SimpleMenu

Inherits:
BasicGameObject show all
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)

Attributes inherited from BasicGameObject

#options, #parent, #paused

Instance Method Summary (collapse)

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

included

Constructor Details

- (SimpleMenu) initialize(options = {})



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(options = {})
  super
  
  # @font_size = options.delete(:font_size) || 30
  @menu_items = options.delete(:menu_items)
  @x = options.delete(:x) || $window.width/2
  @y = options.delete(:y) || 0
  @spacing = options.delete(:spacing) || 100
  @items = []
  @visible = true
  
  y = @y
  menu_items.each do |key, value|
    item = if key.is_a? String
      Text.new(key, options.dup)
    elsif key.is_a? Image
      GameObject.new(options.merge!(:image => key))
    elsif key.is_a? GameObject
      key.options.merge!(options.dup)
      key
    end
    
    item.options[:on_select] = method(:on_select)
    item.options[:on_deselect] = method(:on_deselect)
    item.options[:action] = value
    
    item.rotation_center = :center_top
    item.x = @x
    item.y = y
    y += item.height + @spacing
    @items << item
  end      
  @selected = options[:selected] || 0
  step(0)
  
  self.input = {:up => lambda{step(-1)}, :down => lambda{step(1)}, [:return, :space] => :select}
end

Instance Attribute Details

Returns the value of attribute menu_items



32
33
34
# File 'lib/chingu/simple_menu.rb', line 32

def menu_items
  @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.options[: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.options[:on_deselect].call(selected)
  @selected += value
  @selected = @items.count-1  if @selected < 0
  @selected = 0               if @selected == @items.count
  selected.options[:on_select].call(selected)
end