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, #panel, #params, #text, #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.



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
# 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

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.



1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
# 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) ⇒ Object

Changes the label’s text.

Parameters:

new_text

The new text to show in the label.



1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
# 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