Class: Metro::HitList
- Inherits:
-
Object
- Object
- Metro::HitList
- 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
-
#drawers ⇒ Object
readonly
Returns the value of attribute drawers.
Instance Method Summary collapse
- #add(hits) ⇒ Object
- #clear ⇒ Object
- #drawers_at(point) ⇒ Object
- #hit(event) ⇒ Object
-
#initialize(drawers) ⇒ HitList
constructor
A new instance of HitList.
- #list ⇒ Object
- #offset_from_last_event(event) ⇒ Object
- #release(event) ⇒ Object
- #save_event(event) ⇒ Object
- #update(event) ⇒ Object
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
#drawers ⇒ Object (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 |
#clear ⇒ Object
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 |
#list ⇒ Object
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 |