Class: Fidgit::Label

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

Direct Known Subclasses

Button, MenuPane::Separator

Constant Summary collapse

ICON_POSITIONS =
[:top, :bottom, :left, :right]

Constants inherited from Composite

Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

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

Instance Attribute Summary collapse

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

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

Instance Method Summary collapse

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

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

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

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 (Gosu::Image, nil) — default: nil
  • :justify (:left, :right, :center) — default: :left

    Text justification.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fidgit/elements/label.rb', line 57

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

  super(options)

  # Bit of a fudge since font info is managed circularly here!
  # By using a grid, we'll be able to turn it around easily (in theory).
  @contents = grid num_rows: 1, padding: 0, spacing_h: spacing_h, spacing_v: spacing_v, width: options[:width], height: options[:height], z: z do |contents|
    @text = TextLine.new(text, parent: contents, justify: options[:justify], color: options[:color], padding: 0, z: z,
                           font_name: options[:font_name], font_height: options[:font_height], align_h: :fill, align_v: :center)
  end

  # Create an image frame, but don't show it unless there is an image in it.
  @icon = ImageFrame.new(nil, options[:icon_options].merge(z: z, align: :center))
  @icon.image = options[:icon]

  self.icon_position = options[:icon_position]
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



9
10
11
# File 'lib/fidgit/elements/label.rb', line 9

def background_color
  @background_color
end

#border_colorObject

Returns the value of attribute border_color.



9
10
11
# File 'lib/fidgit/elements/label.rb', line 9

def border_color
  @border_color
end

#icon_positionObject

Returns the value of attribute icon_position.



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

def icon_position
  @icon_position
end

Instance Method Details

#hit_element(x, y) ⇒ Object



15
16
17
18
# File 'lib/fidgit/elements/label.rb', line 15

def hit_element(x, y)
  # The sub-elements should never get events.
  hit?(x, y) ? self : nil
end

#iconObject



13
# File 'lib/fidgit/elements/label.rb', line 13

def icon; @icon ? @icon.image : nil; end

#icon=(icon) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
# File 'lib/fidgit/elements/label.rb', line 20

def icon=(icon)
  raise ArgumentError.new("Icon must be a Gosu::Image") unless icon.is_a? Gosu::Image or icon.nil?

  @contents.remove(@icon) if @icon.image
  @icon.image = icon
  position = [:left, :top].include?(icon_position) ? 0 : 1
  @contents.insert(position, @icon) if @icon.image

  icon
end