Class: MiniGL::Label

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

Overview

This class represents a label.

Instance Attribute Summary

Attributes inherited from Component

#anchor, #anchor_offset_x, #anchor_offset_y, #enabled, #h, #params, #visible, #w, #x, #y

Instance Method Summary collapse

Methods inherited from Component

#set_position, #update

Constructor Details

#initialize(x, y = nil, font = nil, text = nil, text_color = 0, disabled_text_color = 0, scale_x = 1, scale_y = 1, anchor = nil) ⇒ Label

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.



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
# File 'lib/minigl/forms.rb', line 1440

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

Instance Method Details

#draw(alpha = 255, z_index = 0, color = 0xffffff) ⇒ Object

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.



1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
# File 'lib/minigl/forms.rb', line 1470

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