class Rcade::Menu::Menu

Attributes

alignment[RW]
height[RW]
option_styles[RW]
options[RW]
width[RW]

Public Class Methods

new() click to toggle source
# File lib/rcade/menu/menu.rb, line 11
def initialize
  @options = []
  @option_styles = OptionStyles.new
  @selected_option = 0
end

Public Instance Methods

add_option(label_text, &callback) click to toggle source
# File lib/rcade/menu/menu.rb, line 17
def add_option(label_text, &callback)
  @options << Option.new(label_text, &callback)
end
decrement_selection() click to toggle source
# File lib/rcade/menu/menu.rb, line 21
def decrement_selection
  @selected_option -= 1 unless @selected_option == 0
end
increment_selection() click to toggle source
# File lib/rcade/menu/menu.rb, line 25
def increment_selection
  @selected_option += 1 unless @selected_option == @options.count - 1
end
offset_left(window) click to toggle source
# File lib/rcade/menu/menu.rb, line 67
def offset_left(window)
  @offset_left ||= case @x
    when :left   then 0
    when :center then window.width / 2 - width
    when :right  then window.width - width
    else @x < 0 ? @x + window.width - width : @x
  end
end
offset_top(window) click to toggle source
# File lib/rcade/menu/menu.rb, line 76
def offset_top(window)
  @offset_top ||= case @y
    when :top    then 0
    when :center then window.height / 2 - height
    when :bottom then window.height - height
    else @y < 0 ? @y + window.height - height : @y
  end
  @offset_top
end
position(x: nil, y: nil, top: nil, right: nil, bottom: nil, left: nil) click to toggle source
# File lib/rcade/menu/menu.rb, line 33
def position(x: nil, y: nil, top: nil, right: nil, bottom: nil, left: nil)
  if x.nil?
    x = left if left
    x = -(right) if right
    x = 0 unless x
  end
  @x = x

  if y.nil?
    y = top if top
    y = -(bottom) if bottom
    y = 0 unless y
  end
  @y = y
end
render(window) click to toggle source
# File lib/rcade/menu/menu.rb, line 86
def render(window)
  options.each_with_index do |option, i|
    option.window = window
    option.styles = @option_styles
    x = offset_left(window)
    y = offset_top(window)

    x += i * option.width if alignment == :horizontal
    y += i * option.height if alignment == :vertical
    z = 0 # TODO
    selected = @selected_option == i

    option.render(x, y, z, selected)
  end
end
select_current_option() click to toggle source
# File lib/rcade/menu/menu.rb, line 29
def select_current_option
  @options[@selected_option].callback.call
end