Method: Fidgit::Button#initialize

Defined in:
lib/fidgit/elements/button.rb

#initialize(text, options = {}, &block) ⇒ Button

Returns a new instance of Button.

Parameters:

  • text (String)

    The string to display in the label.

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

    a customizable set of options

Options Hash (options):

  • :shortcut (Symbol) — default: nil

    Adds a shortcut key for this element, that activates it. :auto takes the first letter of the text.

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

    Text justification.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fidgit/elements/button.rb', line 8

def initialize(text, options = {}, &block)
  options = {
    color: default(:color),
    background_color: default(:background_color),
    border_color: default(:border_color),
    shortcut_color: default(:shortcut_color),
    shortcut: nil,
  }.merge! options

  @shortcut_color = options[:shortcut_color].dup

  @shortcut = if options[:shortcut] == :auto
                raise ArgumentError.new("Can't use :auto for :shortcut without text") if text.empty?
                text[0].downcase.to_sym
              else
                options[:shortcut]
              end

  raise ArgumentError.new(":shortcut must be a symbol") unless @shortcut.nil? or @shortcut.is_a? Symbol

  super(text, options)

  self.text = text # Force shortcut to be written out properly.

  update_colors
end