Class: Fidgit::Button
- Defined in:
- lib/fidgit/elements/button.rb
Direct Known Subclasses
Constant Summary
Constants inherited from Label
Constants inherited from Composite
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
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
-
#activate ⇒ Object
Activate the button, as though it had been clicked on.
- #clicked_left_mouse_button(sender, x, y) ⇒ Object
- #enabled=(value) ⇒ Object
- #enter(sender) ⇒ Object
-
#initialize(text, options = {}, &block) ⇒ Button
constructor
A new instance of Button.
- #leave(sender) ⇒ Object
- #parent=(value) ⇒ Object
- #text=(value) ⇒ Object
Methods inherited from Label
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.
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, = {}, &block) = { color: default(:color), background_color: default(:background_color), border_color: default(:border_color), shortcut_color: default(:shortcut_color), shortcut: nil, }.merge! @shortcut_color = [:shortcut_color].dup @shortcut = if [:shortcut] == :auto raise ArgumentError.new("Can't use :auto for :shortcut without text") if text.empty? text[0].downcase.to_sym else [:shortcut] end raise ArgumentError.new(":shortcut must be a symbol") unless @shortcut.nil? or @shortcut.is_a? Symbol super(text, ) self.text = text # Force shortcut to be written out properly. update_colors end |
Instance Method Details
#activate ⇒ Object
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 (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 |