Class: Fidgit::Label

Inherits:
Element show all
Defined in:
lib/fidgit/elements/label.rb

Direct Known Subclasses

Button, MenuPane::Separator, ToolTip

Constant Summary collapse

VALID_JUSTIFICATION =
[:left, :right, :center]

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary collapse

Attributes inherited from Element

#align_h, #align_v, #border_thickness, #font_size, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary collapse

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #font, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, #publish, #subscribe

Constructor Details

#initialize(text, options = {}) ⇒ Label

Returns a new instance of Label.

Parameters:

  • text (String)

    The string to display in the label.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :icon (Fidgit::Thumbnail, Gosu::Image, nil) — default: nil
  • :justify (:left, :right, :center) — default: :left

    Text justification.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fidgit/elements/label.rb', line 28

def initialize(text, options = {})
  options = {
    color: default(:color),
    justify: default(:justify),
    background_color: default(:background_color),
    border_color: default(:border_color),
  }.merge! options

  @text = text.dup
  @icon = options[:icon]
  @color = options[:color].dup

  raise "Justification must be one of #{VALID_JUSTIFICATION.inspect}" unless VALID_JUSTIFICATION.include? options[:justify]
  @justify = options[:justify]

  super(options)

  self.text = text # Forces stuff that manipulates text to work.
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



5
6
7
# File 'lib/fidgit/elements/label.rb', line 5

def background_color
  @background_color
end

#border_colorObject

Returns the value of attribute border_color.



5
6
7
# File 'lib/fidgit/elements/label.rb', line 5

def border_color
  @border_color
end

#colorObject

Returns the value of attribute color.



5
6
7
# File 'lib/fidgit/elements/label.rb', line 5

def color
  @color
end

#iconObject

Returns the value of attribute icon.



6
7
8
# File 'lib/fidgit/elements/label.rb', line 6

def icon
  @icon
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/fidgit/elements/label.rb', line 6

def text
  @text
end

Instance Method Details

#draw_foregroundObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fidgit/elements/label.rb', line 48

def draw_foreground
  current_x = x + padding_left
  if @icon
    @icon.draw(current_x, y + padding_top, z)
    current_x += @icon.width + padding_left
  end

  unless @text.empty?
    case @justify
      when :left
        rel_x = 0.0
        center_x = current_x

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

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

    # Make text centered alongside the icon
    # TODO: Probably should have this as an option.
    center_y = y + padding_top + ((y + height - padding_bottom) - (y + padding_top)) / 2.0

    font.draw_rel(@text, center_x, center_y, z, rel_x, 0.5, 1, 1, @color)
  end

  nil
end