Class: Fidgit::Button

Inherits:
Label show all
Defined in:
lib/fidgit/elements/button.rb

Direct Known Subclasses

ComboBox, MenuPane::Item, RadioButton, ToggleButton

Constant Summary

Constants inherited from Label

Label::VALID_JUSTIFICATION

Constants inherited from Element

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

Instance Attribute Summary

Attributes inherited from Label

#background_color, #border_color, #color, #icon, #text

Attributes inherited from Element

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

Instance Method Summary collapse

Methods inherited from Label

#draw_foreground

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #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 = {}, &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 (Fidgit::Thumbnail, 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
# 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)

  update_colors
end

Instance Method Details

#activateObject

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



107
108
109
# File 'lib/fidgit/elements/button.rb', line 107

def activate
  publish(:clicked_left_mouse_button, x + width / 2, y + height / 2) if enabled?
end

#clicked_left_mouse_button(sender, x, y) ⇒ Object



55
56
57
58
# File 'lib/fidgit/elements/button.rb', line 55

def clicked_left_mouse_button(sender, x, y)
  # TODO: Play click sound?
  nil
end

#enabled=(value) ⇒ Object



60
61
62
63
64
65
# File 'lib/fidgit/elements/button.rb', line 60

def enabled=(value)
  super(value)
  update_colors

  value
end

#enter(sender) ⇒ Object



67
68
69
70
71
72
# File 'lib/fidgit/elements/button.rb', line 67

def enter(sender)
  @mouse_over = true
  update_colors

  nil
end

#leave(sender) ⇒ Object



74
75
76
77
78
79
# File 'lib/fidgit/elements/button.rb', line 74

def leave(sender)
  @mouse_over = false
  update_colors

  nil
end

#parent=(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fidgit/elements/button.rb', line 41

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) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fidgit/elements/button.rb', line 33

def text=(value)
  if @shortcut
    super value.sub(/#{@shortcut}/i) {|char| "<c=#{@shortcut_color.to_hex}>#{char}</c>" }
  else
    super value
  end
end