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
true
orfalse
).
Attributes inherited from Button
Attributes inherited from Component
#anchor, #anchor_offset_x, #anchor_offset_y, #enabled, #h, #panel, #params, #text, #visible, #w, #x, #y
Instance Method Summary collapse
-
#enabled=(value) ⇒ Object
:nodoc:.
-
#initialize(x, y = nil, font = nil, text = nil, img = nil, 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, retro = nil, scale_x = 1, scale_y = 1, anchor = 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
Methods inherited from Component
Constructor Details
#initialize(x, y = nil, font = nil, text = nil, img = nil, 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, retro = nil, scale_x = 1, scale_y = 1, anchor = 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
}
Obs.: This method accepts named parameters, but x
and y
are mandatory (also, img
is mandatory when width
and height
are not provided, and vice-versa).
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/minigl/forms.rb', line 480 def initialize(x, y = nil, font = nil, text = nil, img = nil, 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, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &action) if x.is_a? Hash y = x[:y] font = x[:font] text = x[:text] img = x[:img] checked = x.fetch(:checked, false) text_color = x.fetch(:text_color, 0) disabled_text_color = x.fetch(:disabled_text_color, 0) over_text_color = x.fetch(:over_text_color, 0) down_text_color = x.fetch(:down_text_color, 0) center_x = x.fetch(:center_x, true) center_y = x.fetch(:center_y, true) margin_x = x.fetch(:margin_x, 0) margin_y = x.fetch(:margin_y, 0) width = x.fetch(:width, nil) height = x.fetch(:height, nil) params = x.fetch(:params, nil) retro = x.fetch(:retro, nil) scale_x = x.fetch(:scale_x, 1) scale_y = x.fetch(:scale_y, 1) anchor = x.fetch(:anchor, nil) x = x[:x] end 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, 0, 0, params, retro, scale_x, scale_y, anchor, &action @img = if img; Res.imgs img, 2, 4, true, '.png', retro else; nil; end @w = if img; @img[0].width * @scale_x else; width * @scale_x; end @h = if img; @img[0].height * @scale_y else; height * @scale_y; end _, x, y = FormUtils.check_anchor(anchor, @anchor_offset_x, @anchor_offset_y, @w, @h) set_position(x, y) @checked = checked end |
Instance Attribute Details
#checked ⇒ Object
Defines the state of the button (returns true
or false
).
461 462 463 |
# File 'lib/minigl/forms.rb', line 461 def checked @checked end |
Instance Method Details
#enabled=(value) ⇒ Object
:nodoc:
543 544 545 546 547 |
# File 'lib/minigl/forms.rb', line 543 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.
526 527 528 529 530 531 532 |
# File 'lib/minigl/forms.rb', line 526 def update return unless @enabled and @visible super @img_index *= 2 @img_index += 1 if @checked && !@toggling || !@checked && @toggling end |