Class: UnderOs::UI::Events::TouchListeners

Inherits:
Object
  • Object
show all
Defined in:
lib/under_os/ui/utils/events.rb

Defined Under Namespace

Classes: Touch

Class Method Summary collapse

Class Method Details

.add(eventname, view) ⇒ Object



40
41
42
# File 'lib/under_os/ui/utils/events.rb', line 40

def self.add(eventname, view)
  listeners[eventname] << view
end

.listenersObject



36
37
38
# File 'lib/under_os/ui/utils/events.rb', line 36

def self.listeners
  @listeners ||= Hash.new{ |h,k| h[k] = [] }
end

.notify(eventname, event) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/under_os/ui/utils/events.rb', line 44

def self.notify(eventname, event)
  listeners[eventname].each do |view|
    if eventname == :touchmove # being nice and throttling the touchmove events
      return if @__working
      @__working = true
    end

    touches = touches_for_view(view, event)

    view.emit(eventname, touches: touches) if touches.size > 0

    @__working = false
  end
end

.touch_inside_of(frame, touch) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/under_os/ui/utils/events.rb', line 72

def self.touch_inside_of(frame, touch)
  point = touch.locationInView(nil)
  point = nil if point.x < frame.origin.x ||
       point.y < frame.origin.y ||
       point.x > frame.origin.x + frame.size.width ||
       point.y > frame.origin.y + frame.size.height

  point
end

.touches_for_view(view, event) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/under_os/ui/utils/events.rb', line 59

def self.touches_for_view(view, event)
  frame   = view._.frame
  touches = []

  event.allTouches.each do |touch|
    if point = touch_inside_of(frame, touch)
      touches << Touch.new(view, point)
    end
  end

  touches
end