Class: Zif::UI::TwoStageButton
- Inherits:
-
CompoundSprite
- Object
- Sprite
- CompoundSprite
- Zif::UI::TwoStageButton
- Defined in:
- lib/zif/ui/two_stage_button.rb
Overview
This is a button which has pressed and unpressed states defined using two different sprites.
If you give it a label by mybutton.labels << Zif::UI::Label.new …, there are some convenience methods available #retruncate_label #recenter_label #label_text=.
It is able to have multiple sprites and a label because it inherits from CompoundSprite.
Because this inherits from CompoundSprite and Sprite, it obtains a lot of extra functionality. An important aspect is that it is a Clickable and can be registered as a clickable with the Services::InputService.
By default it will automatically set up the click handlers to handle switching states based on clicks. You can pass a block to #initialize, this will be executed if the button is clicked and the mouse click goes up within the rectangle of the button. (You can click down on the button, move the mouse outside, and let go - it will not trigger the callback in this case.)
Use #toggle_pressed to switch states manually.
Direct Known Subclasses
Constant Summary
Constants inherited from Sprite
Instance Attribute Summary collapse
-
#is_pressed ⇒ Boolean
Is the button in the pressed state?.
-
#label_y_offset ⇒ Integer
A Y offset to subtract from the label in the pressed state.
-
#normal ⇒ Zif::Sprite
The sprite to use for the normal (unpressed) state.
-
#pressed ⇒ Zif::Sprite
The sprite to use for the pressed state.
-
#pressed_height ⇒ Integer
The height of the pressed state sprite, used to calculate center for label.
Attributes inherited from CompoundSprite
Attributes inherited from Sprite
#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index
Attributes included from Clickable
#on_mouse_changed, #on_mouse_down, #on_mouse_up
Attributes included from Actions::Animatable
#animation_sequences, #cur_animation
Attributes included from Actions::Actionable
Instance Method Summary collapse
-
#initialize(name = Zif.unique_name('two_stage_button'), &block) ⇒ TwoStageButton
constructor
A new instance of TwoStageButton.
-
#label ⇒ Zif::UI::Label
The label of the button.
-
#label_text=(text) ⇒ Object
Changes the Label#full_text of the label.
-
#press ⇒ Object
Sets the button to the pressed state.
-
#recenter_label ⇒ Object
Moves the label to the center of the button Applies the #label_y_offset and #pressed_height.
-
#retruncate_label(padding = 0) ⇒ Object
Calls Label#retruncate on the label.
-
#toggle_on_change(point) ⇒ Object
Toggles pressed state if the point is inside the button.
-
#toggle_pressed ⇒ Object
Switches the pressed state.
-
#unpress ⇒ Object
Sets the button to the unpressed state.
Methods inherited from CompoundSprite
Methods inherited from Sprite
#blend, #blend=, #center, #center_x, #center_y, #clicked?, #color, #color=, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor
Methods included from Clickable
Methods included from Actions::Animatable
#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating
Methods included from Actions::Actionable
#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action
Methods included from Serializable
#exclude_from_serialize, #inspect, #serialize, #to_s
Methods included from Assignable
Constructor Details
#initialize(name = Zif.unique_name('two_stage_button'), &block) ⇒ TwoStageButton
Returns a new instance of TwoStageButton.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/zif/ui/two_stage_button.rb', line 34 def initialize(name=Zif.unique_name('two_stage_button'), &block) super(name) @normal = [] @pressed = [] @is_pressed = false @on_mouse_up = lambda { |_sprite, point| block&.call(point) toggle_pressed if @is_pressed } @on_mouse_changed = ->(_sprite, point) { toggle_on_change(point) } @on_mouse_down = ->(_sprite, _point) { toggle_pressed } @label_y_offset = 0 end |
Instance Attribute Details
#is_pressed ⇒ Boolean
Returns Is the button in the pressed state?.
26 27 28 |
# File 'lib/zif/ui/two_stage_button.rb', line 26 def is_pressed @is_pressed end |
#label_y_offset ⇒ Integer
Returns A Y offset to subtract from the label in the pressed state.
28 29 30 |
# File 'lib/zif/ui/two_stage_button.rb', line 28 def label_y_offset @label_y_offset end |
#normal ⇒ Zif::Sprite
Returns The sprite to use for the normal (unpressed) state.
22 23 24 |
# File 'lib/zif/ui/two_stage_button.rb', line 22 def normal @normal end |
#pressed ⇒ Zif::Sprite
Returns The sprite to use for the pressed state.
24 25 26 |
# File 'lib/zif/ui/two_stage_button.rb', line 24 def pressed @pressed end |
#pressed_height ⇒ Integer
Returns The height of the pressed state sprite, used to calculate center for label.
30 31 32 |
# File 'lib/zif/ui/two_stage_button.rb', line 30 def pressed_height @pressed_height end |
Instance Method Details
#label ⇒ Zif::UI::Label
Returns The label of the button.
78 79 80 |
# File 'lib/zif/ui/two_stage_button.rb', line 78 def label @labels.first end |
#label_text=(text) ⇒ Object
Changes the Label#full_text of the label
100 101 102 103 104 105 |
# File 'lib/zif/ui/two_stage_button.rb', line 100 def label_text=(text) return unless label label.full_text = text retruncate_label end |
#press ⇒ Object
Sets the button to the pressed state
55 56 57 58 59 |
# File 'lib/zif/ui/two_stage_button.rb', line 55 def press @is_pressed = true recenter_label @sprites = @pressed end |
#recenter_label ⇒ Object
Moves the label to the center of the button Applies the #label_y_offset and #pressed_height
84 85 86 87 88 89 |
# File 'lib/zif/ui/two_stage_button.rb', line 84 def recenter_label return unless label cur_h = @is_pressed ? (@pressed_height || @h) : @h label.recenter_in(@w, cur_h, offset: @label_y_offset) end |
#retruncate_label(padding = 0) ⇒ Object
Calls Label#retruncate on the label
93 94 95 96 97 |
# File 'lib/zif/ui/two_stage_button.rb', line 93 def retruncate_label(padding=0) return unless label label.retruncate(@w - padding) end |
#toggle_on_change(point) ⇒ Object
Toggles pressed state if the point is inside the button.
50 51 52 |
# File 'lib/zif/ui/two_stage_button.rb', line 50 def toggle_on_change(point) toggle_pressed if point.inside_rect?(self) != @is_pressed end |
#toggle_pressed ⇒ Object
Switches the pressed state
69 70 71 72 73 74 75 |
# File 'lib/zif/ui/two_stage_button.rb', line 69 def toggle_pressed if @is_pressed unpress else press end end |
#unpress ⇒ Object
Sets the button to the unpressed state
62 63 64 65 66 |
# File 'lib/zif/ui/two_stage_button.rb', line 62 def unpress @is_pressed = false recenter_label @sprites = @normal end |