Class: Fidgit::ToggleButton

Inherits:
Button show all
Defined in:
lib/fidgit/elements/toggle_button.rb

Overview

A button that toggles its value from false<->true when clicked.

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 collapse

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 Button

#activate, #clicked_left_mouse_button, #enabled=, #enter, #leave, #parent=, #text=

Methods inherited from Label

#draw_foreground

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #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) ⇒ ToggleButton

Returns a new instance of ToggleButton.

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fidgit/elements/toggle_button.rb', line 14

def initialize(text, options = {}, &block)
  options = {
    value: false
  }.merge! options

  @value = options[:value]

  super(text, options)

  @text_on = (options[:text_on] || text).dup
  @icon_on = options[:icon_on] || icon
  @tip_on = (options[:tip_on] || tip).dup
  @border_color_on = (options[:border_color_on] || options[:border_color] || default(:toggled, :border_color)).dup

  @text_off = (options[:text_off] || text).dup
  @icon_off = options[:icon_off] || icon
  @tip_off = (options[:tip_off] || tip).dup
  @border_color_off = (options[:border_color_off] || options[:border_color] || default(:border_color)).dup

  update_status

  subscribe :clicked_left_mouse_button do |sender, x, y|
    @value = (not @value)
    update_status
    publish :changed, @value
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/fidgit/elements/toggle_button.rb', line 8

def value
  @value
end