Class: Metro::HitList

Inherits:
Object
  • Object
show all
Defined in:
lib/metro/events/hit_list.rb

Overview

The HitList is an object that maintains when an object is touched/clicked and then moved and finally released. The object attempts to work through the process:

hit_list.hit(first_event)
hit_list.update(next_event)
hit_list.update(next_event)
hit_list.release(last_event)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(drawers) ⇒ HitList

Returns a new instance of HitList.



17
18
19
# File 'lib/metro/events/hit_list.rb', line 17

def initialize(drawers)
  @drawers = drawers
end

Instance Attribute Details

#drawersObject (readonly)

Returns the value of attribute drawers.



21
22
23
# File 'lib/metro/events/hit_list.rb', line 21

def drawers
  @drawers
end

Instance Method Details

#add(hits) ⇒ Object



65
66
67
# File 'lib/metro/events/hit_list.rb', line 65

def add(hits)
  Array(hits).each {|hit| list.push hit }
end

#clearObject



69
70
71
72
# File 'lib/metro/events/hit_list.rb', line 69

def clear
  list.clear
  @first_event = nil
end

#drawers_at(point) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/metro/events/hit_list.rb', line 43

def drawers_at(point)
  hit_drawers = drawers.find_all { |drawer| drawer.bounds.contains?(point) }

  # assumed that we only want one item
  top_drawer = hit_drawers.inject(hit_drawers.first) {|top,drawer| drawer.z_order > top.z_order ? drawer : top }
  [ top_drawer ].compact
end

#hit(event) ⇒ Object



23
24
25
26
# File 'lib/metro/events/hit_list.rb', line 23

def hit(event)
  add drawers_at(event.mouse_point)
  save_event event
end

#listObject



61
62
63
# File 'lib/metro/events/hit_list.rb', line 61

def list
  @list ||= []
end

#offset_from_last_event(event) ⇒ Object



51
52
53
54
# File 'lib/metro/events/hit_list.rb', line 51

def offset_from_last_event(event)
  return Point.zero unless @last_event
  event.mouse_point - @last_event.mouse_point
end

#release(event) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/metro/events/hit_list.rb', line 35

def release(event)
  offset = offset_from_last_event(event)
  list.each { |d| d.position = d.position + offset }

  save_event event
  clear
end

#save_event(event) ⇒ Object



56
57
58
59
# File 'lib/metro/events/hit_list.rb', line 56

def save_event(event)
  @first_event = event unless @first_event
  @last_event = event
end

#update(event) ⇒ Object



28
29
30
31
32
33
# File 'lib/metro/events/hit_list.rb', line 28

def update(event)
  offset = offset_from_last_event(event)
  list.each { |d| d.position = d.position + offset }

  save_event event
end