Class: MiniGL::Panel
- Inherits:
-
Object
- Object
- MiniGL::Panel
- Defined in:
- lib/minigl/forms.rb
Overview
Represents a container of form components.
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Whether the components inside this panel are enabled.
-
#visible ⇒ Object
Gets or sets whether the panel (and thus all components inside it) are visible.
Instance Method Summary collapse
-
#add_component(c) ⇒ Object
Adds a component to this panel.
-
#draw(alpha = 255, z_index = 0, color = 0xffffff) ⇒ Object
Draws the panel and all its child components.
-
#initialize(x, y, w, h, controls = [], img = nil, img_mode = :normal, retro = nil, scale_x = 1, scale_y = 1, anchor = nil) ⇒ Panel
constructor
Creates a new Panel.
-
#update ⇒ Object
Updates all child components of this panel.
Constructor Details
#initialize(x, y, w, h, controls = [], img = nil, img_mode = :normal, retro = nil, scale_x = 1, scale_y = 1, anchor = nil) ⇒ Panel
Creates a new Panel. Parameters:
- x
-
The horizontal coordinate of the top-left corner of the panel, or the horizontal offset from the anchor, if provided.
- y
-
The vertical coordinate of the top-left corner of the panel, or the vertical offset from the anchor, if provided.
- w
-
The width of the panel, in pixels.
- h
-
The height of the panel, in pixels.
- controls
-
An array of
Components that will be initially inside this panel. - img
-
Identifier of the image for the panel (see details in
Res::img). - img_mode
-
Mode to scale the image. If
:normal(default), the image will be loaded as a single image and scaled to fit the entire size of the panel; if:tiled, the image will be loaded as a 3x3 spritesheet, where the “corner” images (i.e., indices 0, 2, 6 and 8) will be scaled by thescale_xandscale_yparameters, the “border” images (indices 1, 3, 5 and 7) will be stretched in the corresponding direction (indices 1 and 7 will be horizontally stretched and indices 3 and 5, vertically), and the “center” image (index 4) will be stretched in both directions, as needed, to fill the width and height of the panel. - retro
-
Whether the image should be loaded in retro mode.
- scale_x
-
The fixed horizontal scale for “corner” and left and right “border” images (if
img_modeis:tiled). - scale_y
-
The fixed vertical scale for “corner” and top and bottom “border” images (if
img_modeis:tiled). - anchor
-
An alias for a predefined position of the window to be used as “anchor”, i.e., reference for the positioning of the panel. Following are the valid values and a description of the corresponding position if
xandyare 0 (these will be offsets from the reference position):-
:northor:topor:top_center: the panel will be horizontally centered and its top will be at the top of the window. -
:northeastor:top_right: the top-right corner of the panel will meet the top-right corner of the window. -
:westor:leftor:center_left: the panel will be vertically centered and its left edge will be at the left edge of the window. -
:center: the panel will be horizontally and vertically centered on the window. -
:eastor:rightor:center_right: the panel will be vertically centered and its right edge will be at the right edge of the window. -
:southwestor:bottom_left: the bottom-left corner of the panel will meet the bottom-left corner of the window. -
:southor:bottomor:bottom_center: the panel will be horizontally centered and its bottom will be at the bottom of the window. -
:southeastor:bottom_right: the bottom-right corner of the panel will meet the bottom-right corner of the window.
If a value is not provided, the reference is the top-left corner of the screen. Components added as children of
Panels use the panel’s coordinates as reference instead of the window. -
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 |
# File 'lib/minigl/forms.rb', line 111 def initialize(x, y, w, h, controls = [], img = nil, img_mode = :normal, retro = nil, scale_x = 1, scale_y = 1, anchor = nil) _, x, y = FormUtils.check_anchor(anchor, x, y, w, h) @x = x; @y = y; @w = w; @h = h @controls = controls controls.each do |c| _, x, y = FormUtils.check_anchor(c.anchor, c.anchor_offset_x, c.anchor_offset_y, c.w, c.h, @w, @h) c.set_position(@x + x, @y + y) end if img retro = Res.retro_images if retro.nil? if img_mode == :tiled @img = Res.imgs(img, 3, 3, true, '.png', retro, true) @scale_x = scale_x @scale_y = scale_y @tile_w = @img[0].width * @scale_x @tile_h = @img[0].height * @scale_y @draw_center_x = @w > 2 * @tile_w @draw_center_y = @h > 2 * @tile_h @center_scale_x = (@w - 2 * @tile_w).to_f / @tile_w * @scale_x @center_scale_y = (@h - 2 * @tile_h).to_f / @tile_h * @scale_y else @img = Res.img(img, true, false, '.png', retro) end end @visible = @enabled = true end |
Instance Attribute Details
#enabled ⇒ Object
Whether the components inside this panel are enabled.
79 80 81 |
# File 'lib/minigl/forms.rb', line 79 def enabled @enabled end |
#visible ⇒ Object
Gets or sets whether the panel (and thus all components inside it) are visible.
82 83 84 |
# File 'lib/minigl/forms.rb', line 82 def visible @visible end |
Instance Method Details
#add_component(c) ⇒ Object
Adds a component to this panel. Parameters:
- c
-
The component to add.
156 157 158 159 160 |
# File 'lib/minigl/forms.rb', line 156 def add_component(c) _, x, y = FormUtils.check_anchor(c.anchor, c.anchor_offset_x, c.anchor_offset_y, c.w, c.h, @w, @h) c.set_position(@x + x, @y + y) @controls << c end |
#draw(alpha = 255, z_index = 0, color = 0xffffff) ⇒ Object
Draws the panel and all its child components. Parameters:
- alpha
-
The opacity of the panel (0 = fully transparent, 255 = fully opaque).
- z_index
-
The z-index to draw the panel.
- color
-
The color to apply as filter to the panel image and to all child components’ images as well.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/minigl/forms.rb', line 167 def draw(alpha = 255, z_index = 0, color = 0xffffff) return unless @visible c = (alpha << 24) | color if @img if @img.is_a?(Array) @img[0].draw(@x, @y, z_index, @scale_x, @scale_y, c) @img[1].draw(@x + @tile_w, @y, z_index, @center_scale_x, @scale_y, c) if @draw_center_x @img[2].draw(@x + @w - @tile_w, @y, z_index, @scale_x, @scale_y, c) @img[3].draw(@x, @y + @tile_h, z_index, @scale_x, @center_scale_y, c) if @draw_center_y @img[4].draw(@x + @tile_w, @y + @tile_h, z_index, @center_scale_x, @center_scale_y, c) if @draw_center_x && @draw_center_y @img[5].draw(@x + @w - @tile_w, @y + @tile_h, z_index, @scale_x, @center_scale_y, c) if @draw_center_y @img[6].draw(@x, @y + @h - @tile_h, z_index, @scale_x, @scale_y, c) @img[7].draw(@x + @tile_w, @y + @h - @tile_h, z_index, @center_scale_x, @scale_y, c) if @draw_center_x @img[8].draw(@x + @w - @tile_w, @y + @h - @tile_h, z_index, @scale_x, @scale_y, c) else @img.draw(@x, @y, z_index, @w.to_f / @img.width, @h.to_f / @img.height) end end @controls.each { |k| k.draw(alpha, z_index, color) } end |
#update ⇒ Object
Updates all child components of this panel.
141 142 143 |
# File 'lib/minigl/forms.rb', line 141 def update @controls.each(&:update) end |