Class: MiniGL::ToggleButton
- Defined in:
- lib/minigl/forms.rb
Overview
This class represents a toggle button, which can be also interpreted as a check box. It is always in one of two states, given as true or false by its property checked.
Instance Attribute Summary collapse
-
#checked ⇒ Object
Defines the state of the button (returns
trueorfalse).
Attributes inherited from Button
Attributes inherited from Component
#enabled, #h, #params, #visible, #w, #x, #y
Instance Method Summary collapse
-
#click ⇒ Object
Executes the button click action, and toggles its state.
-
#enabled=(value) ⇒ Object
:nodoc:.
-
#initialize(x, y, font, text, img, checked = false, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) ⇒ ToggleButton
constructor
Creates a ToggleButton.
-
#update ⇒ Object
Updates the button, checking the mouse movement and buttons to define the button state.
Methods inherited from Button
Constructor Details
#initialize(x, y, font, text, img, checked = false, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) ⇒ ToggleButton
Creates a ToggleButton. All parameters work the same as in Button, except for the image, img, which now has to be composed of two columns and four rows, the first column with images for the unchecked state, and the second with images for the checked state, and for checked, which defines the initial state of the ToggleButton.
The action block now will always receive a first boolean parameter corresponding to the value of checked. So, if you want to pass parameters to the block, you should declare it like this:
b = ToggleButton.new ... { |checked, params|
puts "button was checked" if checked
# do something with params
}
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/minigl/forms.rb', line 252 def initialize(x, y, font, text, img, checked = false, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action) super x, y, font, text, nil, text_color, disabled_text_color, over_text_color, down_text_color, center_x, center_y, margin_x, margin_y, width, height, params, &action @img = if img; Res.imgs img, 2, 4, true else; nil; end @w = if img; @img[0].width else; width; end @h = if img; @img[0].height else; height; end @text_x = x + @w / 2 if center_x @text_y = y + @h / 2 if center_y @checked = checked end |
Instance Attribute Details
#checked ⇒ Object
Defines the state of the button (returns true or false).
237 238 239 |
# File 'lib/minigl/forms.rb', line 237 def checked @checked end |
Instance Method Details
#click ⇒ Object
Executes the button click action, and toggles its state. The action block always receives as a first parameter true, if the button has been changed to checked, or false, otherwise.
283 284 285 286 |
# File 'lib/minigl/forms.rb', line 283 def click @checked = !@checked @action.call @checked, @params end |
#enabled=(value) ⇒ Object
:nodoc:
297 298 299 300 301 |
# File 'lib/minigl/forms.rb', line 297 def enabled=(value) # :nodoc: @enabled = value @state = :up @img_index = @checked ? 7 : 6 end |
#update ⇒ Object
Updates the button, checking the mouse movement and buttons to define the button state.
272 273 274 275 276 277 278 |
# File 'lib/minigl/forms.rb', line 272 def update return unless @enabled and @visible super @img_index *= 2 @img_index += 1 if @checked end |