Module: Zif::Clickable

Included in:
Sprite
Defined in:
lib/zif/clickable.rb

Overview

A mixin to allow compatibility with Services::InputService

Set click handler attributes #on_mouse_up, #on_mouse_down, #on_mouse_changed to Lambdas accepting a point argument to do something with this object when clicked.

This also works for simple single touch responses on mobile targets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#on_mouse_changedLambda

Returns Called when the mouse moves while click is down. Called with point [x, y] position Array arg.

Returns:

  • (Lambda)

    Called when the mouse moves while click is down. Called with point [x, y] position Array arg.



16
17
18
# File 'lib/zif/clickable.rb', line 16

def on_mouse_changed
  @on_mouse_changed
end

#on_mouse_downLambda

Returns Called when the mouse click begins. Called with point [x, y] position Array arg.

Returns:

  • (Lambda)

    Called when the mouse click begins. Called with point [x, y] position Array arg.



10
11
12
# File 'lib/zif/clickable.rb', line 10

def on_mouse_down
  @on_mouse_down
end

#on_mouse_upLambda

Returns Called when the mouse click ends. Called with point [x, y] position Array arg.

Returns:

  • (Lambda)

    Called when the mouse click ends. Called with point [x, y] position Array arg.



13
14
15
# File 'lib/zif/clickable.rb', line 13

def on_mouse_up
  @on_mouse_up
end

Instance Method Details

#absorb_click?Boolean

The return value of this method informs Services::InputService#process_click that it should stop checking subsequent eligible clickables for a response. Imagine two overlapping clickables, if you want the one underneath this one to respond to a click, this should return false. If you only want the top clickable to respond, this should return true. The default behavior is that it returns true if any standard click handlers are defined (#on_mouse_up, #on_mouse_down, #on_mouse_changed). To customize this behavior, override this method in your class.

Returns:

  • (Boolean)

    Should clicks propagate through this object?



25
26
27
# File 'lib/zif/clickable.rb', line 25

def absorb_click?
  on_mouse_up || on_mouse_down || on_mouse_changed
end

#clicked?(point, kind = :up) ⇒ Object?

Returns If the click is within the rectangle of this object, return this object. Otherwise return nil.

Parameters:

  • point (Array<Integer>)

    [x, y] position Array of the current mouse click.

  • kind (Symbol) (defaults to: :up)

    The kind of click coming through, one of [:up, :down, :changed]

Returns:

  • (Object, nil)

    If the click is within the rectangle of this object, return this object. Otherwise return nil.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zif/clickable.rb', line 34

def clicked?(point, kind=:up)
  # puts "Clickable:#{@name}: clicked? #{kind} #{kind.class} #{point} -> #{rect} = #{point.inside_rect?(self)}"
  return nil if (kind == :down) && !point.inside_rect?(self)

  click_handler = case kind
                  when :up
                    on_mouse_up
                  when :down
                    on_mouse_down
                  when :changed
                    on_mouse_changed
                  end

  # puts "Clickable:#{@name}: click handler: #{kind} #{click_handler}"

  click_handler&.call(self, point)
  self
end