class MiniGL::Label

This class represents a label.

Public Class Methods

new(x, y = nil, font = nil, text = nil, text_color = 0, disabled_text_color = 0, scale_x = 1, scale_y = 1, anchor = nil) click to toggle source

Creates a new label.

Parameters:

x

The x-coordinate of the label.

y

The x-coordinate of the label.

font

Font that will be used to draw the label’s text.

text

The label’s text.

text_color

The default text color.

disabled_text_color

The text color when the label is disabled.

scale_x

The horizontal scale factor.

scale_y

The vertical scale factor.

anchor

See parameter with the same name in Panel#initialize for details.

Calls superclass method
# File lib/minigl/forms.rb, line 1475
def initialize(x, y = nil, font = nil, text = nil, text_color = 0, disabled_text_color = 0, scale_x = 1, scale_y = 1, anchor = nil)
  if x.is_a? Hash
    y = x[:y]
    font = x[:font]
    text = x[:text]
    text_color = x.fetch(:text_color, 0)
    disabled_text_color = x.fetch(:disabled_text_color, 0)
    scale_x = x.fetch(:scale_x, 1)
    scale_y = x.fetch(:scale_y, 1)
    anchor = x.fetch(:anchor, nil)
    x = x[:x]
  end

  @scale_x = scale_x
  @scale_y = scale_y
  @w = font.text_width(text) * scale_x
  @h = font.height * scale_y
  @anchor_offset_x = x; @anchor_offset_y = y
  @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)
  super(x, y, font, text, text_color, disabled_text_color)
end

Public Instance Methods

draw(alpha = 255, z_index = 0, color = 0xffffff) click to toggle source

Draws the label.

Parameters:

alpha

The opacity with which the label will be drawn. Allowed values vary between 0 (fully transparent) and 255 (fully opaque).

z_index

The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders.

color

Color to apply a filter to the text.

# File lib/minigl/forms.rb, line 1521
def draw(alpha = 255, z_index = 0, color = 0xffffff)
  c = @enabled ? @text_color : @disabled_text_color
  r1 = c >> 16
  g1 = (c & 0xff00) >> 8
  b1 = (c & 0xff)
  r2 = color >> 16
  g2 = (color & 0xff00) >> 8
  b2 = (color & 0xff)
  r1 *= r2; r1 /= 255
  g1 *= g2; g1 /= 255
  b1 *= b2; b1 /= 255
  color = (alpha << 24) | (r1 << 16) | (g1 << 8) | b1
  @font.draw_text(@text, @x, @y, z_index, @scale_x, @scale_y, color)
end
text=(new_text) click to toggle source

Changes the label’s text.

Parameters:

new_text

The new text to show in the label.

# File lib/minigl/forms.rb, line 1501
def text=(new_text)
  @text = new_text
  @w = @font.text_width(@text) * @scale_x
  x = @anchor_offset_x; y = @anchor_offset_y
  _, x, y = FormUtils.check_anchor(@anchor, x, y, @w, @h, panel ? panel.w : G.window.width, panel ? panel.h : G.window.height)
  if panel
    set_position(panel.x + x, panel.y + y)
  else
    set_position(x, y)
  end
end