class MenuItem

Attributes

name[RW]
x[RW]
y[RW]

Public Class Methods

new(name, window, font, &action) click to toggle source
# File lib/game_2d/menu.rb, line 39
def initialize(name, window, font, &action)
  @name, @window, @font, @action = name, window, font, action
  @main_color, @select_color, @highlight_color =
    Gosu::Color::YELLOW, Gosu::Color::BLACK, Gosu::Color::CYAN

  # Default position: Upper-right corner
  @x, @y = @window.width - 1, 0
end

Public Instance Methods

bottom() click to toggle source
# File lib/game_2d/menu.rb, line 56
def bottom; @y + @font.height; end
choose_color(selected) click to toggle source
# File lib/game_2d/menu.rb, line 67
def choose_color(selected)
  selected ? @select_color : @main_color
end
draw() click to toggle source
# File lib/game_2d/menu.rb, line 58
def draw
  selected = mouse_over?
  color = choose_color(selected)
  @font.draw_rel(to_s, @x, @y, ZOrder::Text, 1.0, 0.0, 1.0, 1.0, color)
  if selected
    @window.draw_box_at(left, top, right, bottom, @highlight_color)
  end
end
handle_click() click to toggle source

Returns a true value if it handled the click May return a Menu or MenuItem to be set as the new menu to display May return simply ‘true’ if we should redisplay the top-level menu

# File lib/game_2d/menu.rb, line 74
def handle_click
  return unless mouse_over?
  @action.call(self) || true
end
left() click to toggle source
# File lib/game_2d/menu.rb, line 53
def left; @x - @font.text_width(to_s); end
mouse_over?() click to toggle source
# File lib/game_2d/menu.rb, line 48
def mouse_over?
  x, y = @window.mouse_x, @window.mouse_y
  (y >= top) && (y < bottom) && (x > left)
end
right() click to toggle source
# File lib/game_2d/menu.rb, line 54
def right; @x; end
to_s() click to toggle source
# File lib/game_2d/menu.rb, line 79
def to_s
  @name.respond_to?(:call) ? @name.call(self) : @name.to_s
end
top() click to toggle source
# File lib/game_2d/menu.rb, line 55
def top; @y; end