Class: Metro::UI::Label

Inherits:
Model
  • Object
show all
Defined in:
lib/metro/models/ui/label.rb

Overview

The label will draw the specified text with the font, position, color, and alignment provided.

Examples:

Drawing a label


class TitleScene < GameScene
  draw :game_title, model: "metro::ui::label", position: "320,240",
    vertical_align: "center", align: "center", color: "rgba(255,255,255,1.0)",
    text: "Super Awesome Game\n2"
end

Constant Summary

Constants included from Metro::Units

Metro::Units::Bounds

Instance Attribute Summary collapse

Attributes inherited from Model

#scene, #window

Instance Method Summary collapse

Methods inherited from Model

#_load, #_save, #after_initialize, #create, #draw_completed?, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #show, #to_hash, #update, #update_completed?

Methods included from HasEvents

included

Methods included from KeyValueCoding

#get, #set

Methods included from PropertyOwner

included, #properties

Constructor Details

This class inherits a constructor from Metro::Model

Instance Attribute Details

#alignObject

The alignment for the label. This influences where the text draws in relation to the specified position. Your choices are ‘left’, ‘center’, and ‘right’. The default is ‘left’.



42
# File 'lib/metro/models/ui/label.rb', line 42

property :align, type: :text, default: "left"

#colorObject

The text color



28
# File 'lib/metro/models/ui/label.rb', line 28

property :color, default: "rgba(255,255,255,1.0)"

#dimensionsObject

The dimensions of label. If the label text is blank, then the dimensions of the label will return a minimum dimension.



59
60
61
62
# File 'lib/metro/models/ui/label.rb', line 59

property :dimensions do
  calculated = Dimensions.of (longest_line * x_factor), (line_height * line_count * y_factor)
  [ calculated, minimum_dimensions ].max
end

#fontObject

The font to use for the label text



32
# File 'lib/metro/models/ui/label.rb', line 32

property :font, default: { size: 20 }

#minimum_dimensionsObject

When calculating the dimensions of the label, this is the minimum dimensions. This is necessary in cases when the text is blank. This allows for the label to have size when in the edit mode.



54
# File 'lib/metro/models/ui/label.rb', line 54

property :minimum_dimensions, type: :dimensions, default: "16,16"

#positionObject

The position of the label



20
# File 'lib/metro/models/ui/label.rb', line 20

property :position

#scaleObject

The scale of the text



24
# File 'lib/metro/models/ui/label.rb', line 24

property :scale, default: Scale.one

#textObject

The text to appear on the label



36
# File 'lib/metro/models/ui/label.rb', line 36

property :text

#vertical_alignObject

The vertical alignment for the label. This influences where the text draws in relation to the specified position. Your choices are ‘top’, ‘center’, and ‘bottom’. This default is ‘top’



48
# File 'lib/metro/models/ui/label.rb', line 48

property :vertical_align, type: :text, default: "top"

Instance Method Details

#boundsObject



64
65
66
# File 'lib/metro/models/ui/label.rb', line 64

def bounds
  Bounds.new left: left_x, right: right_x, top: top_y, bottom: bottom_y
end

#drawObject



68
69
70
71
72
# File 'lib/metro/models/ui/label.rb', line 68

def draw
  parsed_text.each_with_index do |line,index|
    font.draw line, x_position(index), y_position(index), z_order, x_factor, y_factor, color
  end
end