Class: Zif::UI::TwoStageButton

Inherits:
CompoundSprite show all
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

ExampleApp::TallButton

Constant Summary

Constants inherited from Sprite

Sprite::BLENDMODE

Instance Attribute Summary collapse

Attributes inherited from CompoundSprite

#labels, #sprites

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

#actions, #dirty

Instance Method Summary collapse

Methods inherited from CompoundSprite

#draw_override, #source_rect

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

#absorb_click?, #clicked?

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

#assign

Constructor Details

#initialize(name = Zif.unique_name('two_stage_button'), &block) ⇒ TwoStageButton

Returns a new instance of TwoStageButton.

Parameters:

  • name (String) (defaults to: Zif.unique_name('two_stage_button'))

    The name of the button, used for debugging

  • block (Proc)

    A block to execute when the button is pressed (Clickable#on_mouse_up)



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_pressedBoolean

Returns Is the button in the pressed state?.

Returns:

  • (Boolean)

    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_offsetInteger

Returns A Y offset to subtract from the label in the pressed state.

Returns:

  • (Integer)

    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

#normalZif::Sprite

Returns The sprite to use for the normal (unpressed) state.

Returns:

  • (Zif::Sprite)

    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

#pressedZif::Sprite

Returns The sprite to use for the pressed state.

Returns:

  • (Zif::Sprite)

    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_heightInteger

Returns The height of the pressed state sprite, used to calculate center for label.

Returns:

  • (Integer)

    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

#labelZif::UI::Label

Returns The label of the button.

Returns:



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

#pressObject

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_labelObject

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

Parameters:

  • padding (Integer) (defaults to: 0)

    Some padding to subtract from the width of the sprite



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.

Parameters:

  • point (Array<Integer>)

    [x, y] point



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_pressedObject

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

#unpressObject

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