class MiniGL::Panel

Represents a container of form components.

Attributes

controls[R]

The components contained in this panel.

enabled[R]

Whether the components inside this panel are enabled.

h[R]

The height of the panel in pixels.

visible[RW]

Gets or sets whether the panel (and thus all components inside it) are visible.

w[R]

The width of the panel in pixels.

x[R]

The horizontal position of the panel from the top left corner of the window.

y[R]

The vertical position of the panel from the top left corner of the window.

Public Class Methods

new(x, y, w, h, controls = [], img = nil, img_mode = :normal, retro = nil, scale_x = 1, scale_y = 1, anchor = nil) click to toggle source

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 the scale_x and scale_y parameters, 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_mode is :tiled).

scale_y

The fixed vertical scale for “corner” and top and bottom “border” images (if img_mode is :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 x and y are 0 (these will be offsets from the reference position):

  • :north or :top or :top_center: the panel will be horizontally centered and its top will be at the top of the window.

  • :northeast or :top_right: the top-right corner of the panel will meet the top-right corner of the window.

  • :west or :left or :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.

  • :east or :right or :center_right: the panel will be vertically centered and its right edge will be at the right edge of the window.

  • :southwest or :bottom_left: the bottom-left corner of the panel will meet the bottom-left corner of the window.

  • :south or :bottom or :bottom_center: the panel will be horizontally centered and its bottom will be at the bottom of the window.

  • :southeast or :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.

# File lib/minigl/forms.rb, line 131
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

Public Instance Methods

add_component(c) click to toggle source

Adds a component to this panel. Parameters:

c

The component to add.

# File lib/minigl/forms.rb, line 178
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) click to toggle source

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.

# File lib/minigl/forms.rb, line 189
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) if k.visible }
end
enabled=(value) click to toggle source

Enables or disables all child components of this panel. Parameters:

value

Whether the components should be enabled.

# File lib/minigl/forms.rb, line 170
def enabled=(value)
  @enabled = value
  @controls.each { |c| c.enabled = value }
end
update() click to toggle source

Updates all child components of this panel.

# File lib/minigl/forms.rb, line 162
def update
  return unless @visible
  @controls.each(&:update)
end