Method: MiniGL::Panel#initialize
- Defined in:
- lib/minigl/forms.rb
#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. -
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/minigl/forms.rb', line 128 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) c.panel = self 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 |