Class: Fidgit::Button

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

Direct Known Subclasses

ComboBox, MenuPane::Item, RadioButton, ToggleButton

Constant Summary

Constant Summary

Constants inherited from Label

Label::ICON_POSITIONS

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

Attributes inherited from Label

#background_color, #border_color, #icon_position

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

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

Instance Method Summary collapse

Methods inherited from Label

#hit_element, #icon, #icon=

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #hit_element, #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?, #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 = {}, &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

Instance Method Details

#activateObject

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



109
110
111
# 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) ⇒ Object



57
58
59
60
# File 'lib/fidgit/elements/button.rb', line 57

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

#enabled=(value) ⇒ Object



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

def enabled=(value)
  super(value)
  update_colors

  value
end

#enter(sender) ⇒ Object



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

def enter(sender)
  @mouse_over = true
  update_colors

  nil
end

#leave(sender) ⇒ Object



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

def leave(sender)
  @mouse_over = false
  update_colors

  nil
end

#parent=(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



35
36
37
38
39
40
41
# 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