Commit 7433abd3 authored by Taddeus Kroes's avatar Taddeus Kroes

Moved propagation from gesture to event.

parent fa4f154f
...@@ -13,7 +13,7 @@ class Event(object): ...@@ -13,7 +13,7 @@ class Event(object):
def __init__(self, touch_object): def __init__(self, touch_object):
self.touch_object = touch_object self.touch_object = touch_object
self.stop = False self.stopped = self.stopped_immidiate = False
def __getattr__(self, name): def __getattr__(self, name):
if name in OBJECT_NAMES \ if name in OBJECT_NAMES \
...@@ -32,8 +32,15 @@ class Event(object): ...@@ -32,8 +32,15 @@ class Event(object):
def get_position(self): def get_position(self):
return self.touch_object.get_position() return self.touch_object.get_position()
def stop_delegation(self): def stop_propagation(self):
self.stop = True self.stopped = True
def is_delegation_stopped(self): def is_propagation_stopped(self):
return self.stop return self.stopped
def stop_immidiate_propagation(self):
self.stop_propagation()
self.stopped_immidiate = True
def is_immediate_propagation_stopped(self):
return self.stopped_immidiate
...@@ -25,7 +25,7 @@ class EventServer(Logger): ...@@ -25,7 +25,7 @@ class EventServer(Logger):
Delegate an event that has been triggered by the event driver to the Delegate an event that has been triggered by the event driver to the
widget tree. widget tree.
""" """
self.root_widget.handle_event(event) self.root_widget.delegate_event(event)
def start(self): def start(self):
""" """
......
...@@ -50,7 +50,6 @@ class Gesture(object): ...@@ -50,7 +50,6 @@ class Gesture(object):
_type = NotImplemented _type = NotImplemented
def __init__(self, originating_event=None): def __init__(self, originating_event=None):
self.stop = False
self.tracker = None self.tracker = None
self.originating_event = originating_event self.originating_event = originating_event
...@@ -68,9 +67,3 @@ class Gesture(object): ...@@ -68,9 +67,3 @@ class Gesture(object):
def set_tracker(self, tracker): def set_tracker(self, tracker):
self.tracker = tracker self.tracker = tracker
def stop_propagation(self):
self.stop = True
def is_propagation_stopped(self):
return self.stop
...@@ -22,6 +22,24 @@ class Widget(Positionable, Logger): ...@@ -22,6 +22,24 @@ class Widget(Positionable, Logger):
self.parent = None self.parent = None
self.children = [] self.children = []
def get_offset(self, offset_parent=None):
"""
Get the offset position relative to an offset parent. If no offset
parent is specified, the parent widget is used. If no parent widget is
assigned, return absolute coordinates.
"""
x, y = self.get_position()
if not offset_parent:
if not self.parent:
return x, y
offset_parent = self.parent
ox, oy = offset_parent.get_position()
return x - ox, y - oy
def add_widget(self, widget): def add_widget(self, widget):
""" """
Add a new child widget. Add a new child widget.
...@@ -114,19 +132,30 @@ class Widget(Positionable, Logger): ...@@ -114,19 +132,30 @@ class Widget(Positionable, Logger):
""" """
raise NotImplementedError raise NotImplementedError
def handle_event(self, event): def delegate_event(self, event):
""" """
Delegate a triggered event to gesture trackers and child widgets. A Delegate a triggered event to child widgets. If a child stops
handler can stop the delegation of the event, preventing it from being propagation
delegated to child widgets.
""" """
if not self.children:
self.propagate_event(event)
else:
for child in self.children:
if child.contains_event(event):
child.delegate_event(event)
if event.is_propagation_stopped():
break
def propagate_event(self, event):
for tracker in set(self.trackers.itervalues()): for tracker in set(self.trackers.itervalues()):
tracker.handle_event(event) tracker.handle_event(event)
if not event.is_delegation_stopped(): if event.is_immediate_propagation_stopped():
for child in self.children: break
if child.contains_event(event):
child.handle_event(event) if self.parent and not event.is_propagation_stopped():
self.parent.propagate_event(event)
def handle_gesture(self, gesture): def handle_gesture(self, gesture):
""" """
...@@ -138,5 +167,5 @@ class Widget(Positionable, Logger): ...@@ -138,5 +167,5 @@ class Widget(Positionable, Logger):
for handler in self.handlers.get(gesture.get_type(), ()): for handler in self.handlers.get(gesture.get_type(), ()):
handler(gesture) handler(gesture)
if self.parent and not gesture.is_propagation_stopped(): #if self.parent and not gesture.is_propagation_stopped():
self.parent.handle_gesture(gesture) # self.parent.handle_gesture(gesture)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment