Class: RedBird::VerticalMenu

Inherits:
Entity
  • Object
show all
Defined in:
lib/red_bird/vertical_menu.rb

Overview

VerticalMenu is an interactive menu that receives input from players. It consists of a vertical list of Option.

Author:

  • Frederico Linhares

Defined Under Namespace

Classes: Option

Instance Attribute Summary

Attributes inherited from Entity

#pos_x, #pos_y, #priority

Instance Method Summary collapse

Methods inherited from Entity

#cursor_down, #cursor_hover, #cursor_up, #tick

Constructor Details

#initialize(style, pos_x, pos_y, options) ⇒ VerticalMenu

Returns a new instance of VerticalMenu.

Parameters:

Author:

  • Frederico Linhares



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/red_bird/vertical_menu.rb', line 36

def initialize(style, pos_x, pos_y, options)
  @style = style
  @pos_x = pos_x
  @pos_y = pos_y

  @options = options
  @current_option = 0
  @option_max_width = 0
  @option_max_height = 0

  @options.each do |i|
    if @option_max_width < i.text.width then
      @option_max_width = i.text.width
    end
    if @option_max_height < i.text.height then
      @option_max_height = i.text.height
    end
  end
end

Instance Method Details

#activateObject

Executes the action in the selected option.

Author:

  • Frederico Linhares



71
72
73
# File 'lib/red_bird/vertical_menu.rb', line 71

def activate
  @options[@current_option].action.call
end

#heightInteger

Returns:

  • (Integer)

Author:

  • Frederico Linhares



64
65
66
# File 'lib/red_bird/vertical_menu.rb', line 64

def height
  @option_max_height * @options.size
end

#next_optObject

Changes the selected Option to the next one.

Author:

  • Frederico Linhares



78
79
80
81
# File 'lib/red_bird/vertical_menu.rb', line 78

def next_opt
  @current_option += 1
  @current_option = 0 if @current_option >= @options.size
end

#pred_optObject

Changes the selected Option to the previous one.

Author:

  • Frederico Linhares



86
87
88
89
90
91
92
# File 'lib/red_bird/vertical_menu.rb', line 86

def pred_opt
  if @current_option <= 0 then
    @current_option = @options.size - 1
  else
    @current_option -= 1
  end
end

#renderObject

Renders to the screen.

Author:

  • Frederico Linhares



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/red_bird/vertical_menu.rb', line 97

def render
  # Render texts.
  @options.each_with_index do |o, i|
    o.text.render_to_screen(
      @style.sprite_width * 2 + @pos_x,
      @style.sprite_height * (i + 1) + @pos_y)
  end

  @style.sprites[:arrow_select].render_to_screen(
    @style.sprite_width + @pos_x,
    @style.sprite_height * (@current_option + 1) + @pos_y)
end

#widthInteger

Returns:

  • (Integer)

Author:

  • Frederico Linhares



58
59
60
# File 'lib/red_bird/vertical_menu.rb', line 58

def width
  @option_max_width
end