Class: RedBird::UIBox

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

Overview

User Interface Box works as a visual background for texts, menus, or anything else.

Author:

  • Frederico Linhares

Defined Under Namespace

Classes: Style

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, num_vertical_sprites, num_horizontal_sprites) ⇒ UIBox

Returns a new instance of UIBox.

Parameters:

  • style (RedBird::Style)
  • pos_x (Integer)
  • pos_y (Integer)
  • num_vertical_sprites (Integer)

    number of sprites rows.

  • num_horizontal_sprites (Integer)

    number of sprites columns.

Author:

  • Frederico Linhares



76
77
78
79
80
81
82
83
# File 'lib/red_bird/uibox.rb', line 76

def initialize(style, pos_x, pos_y, num_vertical_sprites,
               num_horizontal_sprites)
  @style = style
  @pos_x = pos_x
  @pos_y = pos_y
  @num_vertical_sprites = num_vertical_sprites
  @num_horizontal_sprites = num_horizontal_sprites
end

Instance Method Details

#heightInteger

The height is defined by num_vertical_sprites plus two (the borders). The height of each sprite is defined by the style used.

Returns:

  • (Integer)

Author:

  • Frederico Linhares



99
100
101
# File 'lib/red_bird/uibox.rb', line 99

def height
  @style.sprite_height * (@num_vertical_sprites + 2)
end

#renderObject

Renders to the screen.

Author:

  • Frederico Linhares



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/red_bird/uibox.rb', line 106

def render
  Render.set_color(@style.bg_color)
  Render.fill_rect(@pos_x, @pos_y, self.width, self.height)

  # Draw the corners.
  @style.sprites[:box_top_left].render_to_screen(@pos_x, @pos_y)
  @style.sprites[:box_top_right].render_to_screen(
    @style.sprite_width * (@num_horizontal_sprites + 1) + @pos_x,
    @pos_y)
  @style.sprites[:box_bottom_left].render_to_screen(
    @pos_x,
    @style.sprite_height * (@num_vertical_sprites + 1) + @pos_y)
  @style.sprites[:box_bottom_right].render_to_screen(
    @style.sprite_width * (@num_horizontal_sprites + 1) + @pos_x,
    @style.sprite_height * (@num_vertical_sprites + 1) + @pos_y)

  # Draw the edges.
  @num_horizontal_sprites.times do |i|
    # Top
    @style.sprites[:box_top].render_to_screen(
      @style.sprite_width * (i + 1) + @pos_x, @pos_y)
    # Bottom
    @style.sprites[:box_bottom].render_to_screen(
      @style.sprite_width * (i + 1) + @pos_x,
      @style.sprite_height * (@num_vertical_sprites + 1) + @pos_y)
  end
  @num_vertical_sprites.times do |i|
    # Left
    @style.sprites[:box_left].render_to_screen(
      @pos_x, @style.sprite_height * (i + 1) + @pos_y)
    # Right
    @style.sprites[:box_right].render_to_screen(
      @style.sprite_width * (@num_horizontal_sprites + 1) + @pos_x,
      @style.sprite_height * (i + 1) + @pos_y)
  end
end

#widthInteger

The height is defined by num_horizontal_sprites plus two (the borders). The width of each sprite is defined by the style used.

Returns:

  • (Integer)

Author:

  • Frederico Linhares



90
91
92
# File 'lib/red_bird/uibox.rb', line 90

def width
  @style.sprite_width * (@num_horizontal_sprites + 2)
end