class Fidgit::Button

Public Class Methods

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

@param (see Label#initialize) @option (see Label#initialize) @option options [Symbol] :shortcut (nil) Adds a shortcut key for this element, that activates it. :auto takes the first letter of the text.

Calls superclass method
# 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

Public Instance Methods

activate() click to toggle source

Activate the button, as though it had been clicked on. Does not do anything if the button is disabled.

# File lib/fidgit/elements/button.rb, line 109
def activate
  publish(:clicked_left_mouse_button, x + width / 2, y + height / 2) if enabled?
end
clicked_left_mouse_button(sender, x, y) click to toggle source
# File lib/fidgit/elements/button.rb, line 57
def clicked_left_mouse_button(sender, x, y)
  # TODO: Play click sound?
  nil
end
enabled=(value) click to toggle source
Calls superclass method
# File lib/fidgit/elements/button.rb, line 62
def enabled=(value)
  super(value)
  update_colors

  value
end
enter(sender) click to toggle source
# File lib/fidgit/elements/button.rb, line 69
def enter(sender)
  @mouse_over = true
  update_colors

  nil
end
leave(sender) click to toggle source
# File lib/fidgit/elements/button.rb, line 76
def leave(sender)
  @mouse_over = false
  update_colors

  nil
end
parent=(value) click to toggle source
Calls superclass method
# File lib/fidgit/elements/button.rb, line 43
def parent=(value)
  if @shortcut
    state = $window.game_state_manager.inside_state || $window.current_game_state
    if parent
      raise ArgumentError.new("Repeat of shortcut #{@shortcut.inspect}") if state.input.has_key? @shortcut
      state.on_input(@shortcut) { activate unless state.focus }
    else
      state.input.delete @shortcut
    end
  end

  super(value)
end
text=(value) click to toggle source
Calls superclass method
# File lib/fidgit/elements/button.rb, line 35
def text=(value)
  if @shortcut
    super value.sub(/#{Regexp.escape @shortcut}/i) {|char| "<c=#{@shortcut_color.to_hex}>#{char}</c>" }
  else
    super value
  end
end

Protected Instance Methods

post_init_block(&block) click to toggle source

A block added to any button subscribes to LMB click.

# File lib/fidgit/elements/button.rb, line 102
def post_init_block(&block)
  subscribe :clicked_left_mouse_button, &block
end
update_colors() click to toggle source
# File lib/fidgit/elements/button.rb, line 84
def update_colors
  [:color, :background_color].each do |attribute|
    send :"#{attribute.to_s}=", enabled? ? default(attribute) : default(:disabled, attribute)
  end

  self.background_color = if @mouse_over and enabled?
                            default(:hover, :background_color)
                          else
                            default(:background_color)
                          end

  @icon.enabled = enabled? if @icon

  nil
end