class Fidgit::TextLine

Used internally by the label.

Constants

VALID_JUSTIFICATION

Attributes

color[R]
justify[R]

Public Class Methods

new(text, options = {}) click to toggle source

@param (see Element#initialize) @param [String] text The string to display in the line of text.

@option (see Element#initialize) @option options [:left, :right, :center] :justify (:left) Text justification.

Calls superclass method Fidgit::Element::new
# File lib/fidgit/elements/text_line.rb, line 37
def initialize(text, options = {})
  options = {
      color: default(:color),
      justify: default(:justify),
  }.merge! options

  super(options)

  self.justify = options[:justify]
  self.color = options[:color]
  self.text = text
end

Public Instance Methods

color=(color) click to toggle source
# File lib/fidgit/elements/text_line.rb, line 8
def color=(color)
  raise ArgumentError.new("Text must be a Gosu::Color") unless color.is_a? Gosu::Color

  @color = color.dup

  color
end
draw_foreground() click to toggle source
# File lib/fidgit/elements/text_line.rb, line 50
def draw_foreground
  case @justify
    when :left
      rel_x = 0.0
      draw_x = x + padding_left

    when :right
      rel_x = 1.0
      draw_x = x + rect.width - padding_right

    when :center
      rel_x = 0.5
      draw_x = (x + padding_left) + (rect.width - padding_right - padding_left) / 2.0
  end

  font.draw_rel(@text, draw_x, y + padding_top, z, rel_x, 0, 1, 1, color)
end
justify=(justify) click to toggle source
# File lib/fidgit/elements/text_line.rb, line 27
def justify=(justify)
  raise ArgumentError.new("Justify must be one of #{VALID_JUSTIFICATION.inspect}") unless VALID_JUSTIFICATION.include? justify
  @justify = justify
end
min_width() click to toggle source
Calls superclass method Fidgit::Element#min_width
# File lib/fidgit/elements/text_line.rb, line 68
def min_width
  if @text.empty?
    [padding_left + padding_right, super].max
  else
    [padding_left + font.text_width(@text) + padding_right, super].max
  end
end
text() click to toggle source
# File lib/fidgit/elements/text_line.rb, line 16
def text; @text.dup; end
text=(text) click to toggle source
# File lib/fidgit/elements/text_line.rb, line 18
def text=(text)
  raise ArgumentError.new("Text must be a String") unless text.respond_to? :to_s

  @text = text.to_s.dup

  recalc
  text
end
to_s() click to toggle source
# File lib/fidgit/elements/text_line.rb, line 88
def to_s
  "#{super} '#{@text}'"
end

Protected Instance Methods

layout() click to toggle source
# File lib/fidgit/elements/text_line.rb, line 77
def layout
  rect.width = [min_width, max_width].min

  if @text.empty?
    rect.height = [[padding_top + padding_bottom, min_height].max, max_height].min
  else
    rect.height = [[padding_top + font.height + padding_bottom, min_height].max, max_height].min
  end
end