Class: Chingu::SimpleMenu

Inherits:
BasicGameObject show all
Includes:
Helpers::InputClient
Defined in:
lib/chingu/simple_menu.rb

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

#initialize(options = {}) ⇒ SimpleMenu

Returns a new instance of SimpleMenu.



30
31
32
33
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
# File 'lib/chingu/simple_menu.rb', line 30

def initialize(options = {})
  super
  
  @menu_items = options.delete(:menu_items)     # || {"Exit" => :exit}
  @x = options.delete(:x) || $window.width/2
  @y = options.delete(:y) || 100
  @spacing = options.delete(:spacing) || 100
  @items = []
  @visible = true
  
  y = @spacing
  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
      menu_item.options.merge!(options.dup)
      menu_item
    end
    
    item.options[:on_select] = method(:on_select)
    item.options[:on_deselect] = method(:on_deselect)
    item.options[:action] = value
    
    item.rotation_center = :center_center
    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.



28
29
30
# File 'lib/chingu/simple_menu.rb', line 28

def menu_items
  @menu_items
end

#visibleObject

Returns the value of attribute visible.



28
29
30
# File 'lib/chingu/simple_menu.rb', line 28

def visible
  @visible
end

Instance Method Details

#drawObject



91
92
93
# File 'lib/chingu/simple_menu.rb', line 91

def draw
  @items.each { |item| item.draw }
end

#on_deselect(object) ⇒ Object



83
84
85
# File 'lib/chingu/simple_menu.rb', line 83

def on_deselect(object)
  object.color = Color::WHITE
end

#on_select(object) ⇒ Object



87
88
89
# File 'lib/chingu/simple_menu.rb', line 87

def on_select(object)
  object.color = Color::RED
end

#selectObject



75
76
77
# File 'lib/chingu/simple_menu.rb', line 75

def select
  dispatch_action(selected.options[:action], self.parent)
end

#selectedObject



79
80
81
# File 'lib/chingu/simple_menu.rb', line 79

def selected
  @items[@selected]
end

#step(value) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/chingu/simple_menu.rb', line 67

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