Class: MiniGL::Panel

Inherits:
Object
  • Object
show all
Defined in:
lib/minigl/forms.rb

Overview

Represents a container of form components.

Instance Attribute Summary collapse

Instance Method Summary collapse

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 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.



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
157
158
159
# 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

Instance Attribute Details

#controlsObject (readonly)

The components contained in this panel.



102
103
104
# File 'lib/minigl/forms.rb', line 102

def controls
  @controls
end

#enabledObject

Whether the components inside this panel are enabled.



96
97
98
# File 'lib/minigl/forms.rb', line 96

def enabled
  @enabled
end

#hObject (readonly)

The height of the panel in pixels.



93
94
95
# File 'lib/minigl/forms.rb', line 93

def h
  @h
end

#visibleObject

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



99
100
101
# File 'lib/minigl/forms.rb', line 99

def visible
  @visible
end

#wObject (readonly)

The width of the panel in pixels.



90
91
92
# File 'lib/minigl/forms.rb', line 90

def w
  @w
end

#xObject (readonly)

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



84
85
86
# File 'lib/minigl/forms.rb', line 84

def x
  @x
end

#yObject (readonly)

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



87
88
89
# File 'lib/minigl/forms.rb', line 87

def y
  @y
end

Instance Method Details

#add_component(c) ⇒ Object

Adds a component to this panel. Parameters:

c

The component to add.



178
179
180
181
182
# 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) ⇒ 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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# 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

#updateObject

Updates all child components of this panel.



162
163
164
165
# File 'lib/minigl/forms.rb', line 162

def update
  return unless @visible
  @controls.each(&:update)
end