Commit 1fb84754 authored by UVA Multi-touch's avatar UVA Multi-touch

Changed occurences of 'widget' to 'area' in code.

parent c476b2ba
from logger import Logger from logger import Logger
from tracker import GestureTracker, Gesture from tracker import GestureTracker, Gesture
from event_server import EventServer from event_server import EventServer
from widgets import * from areas import *
...@@ -6,11 +6,11 @@ from trackers import create_tracker ...@@ -6,11 +6,11 @@ from trackers import create_tracker
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
class Widget(Positionable, Logger): class Area(Positionable, Logger):
""" """
Abstract class for widget implementations. A widget represents a 2D object Abstract class for area implementations. A area represents a 2D object
on the screen in which gestures can occur. Handlers for a specific gesture on the screen in which gestures can occur. Handlers for a specific gesture
type can be bound to a widget. type can be bound to a area.
""" """
__metaclass__ = ABCMeta __metaclass__ = ABCMeta
...@@ -23,16 +23,16 @@ class Widget(Positionable, Logger): ...@@ -23,16 +23,16 @@ class Widget(Positionable, Logger):
# Map of gesture types to a list of handlers for that type # Map of gesture types to a list of handlers for that type
self.handlers = {} self.handlers = {}
# Widget tree references # Area tree references
self.parent = None self.parent = None
self.children = [] self.children = []
def get_root_widget(self): def get_root_area(self):
""" """
Traverse up in the widget tree to find the root widget. Traverse up in the area tree to find the root area.
""" """
if self.parent: if self.parent:
return self.parent.get_root_widget() return self.parent.get_root_area()
return self return self
...@@ -40,17 +40,17 @@ class Widget(Positionable, Logger): ...@@ -40,17 +40,17 @@ class Widget(Positionable, Logger):
""" """
Get the position relative to the screen. Get the position relative to the screen.
""" """
root = self.get_root_widget() root = self.get_root_area()
return root + self.get_offset(root) return root + self.get_offset(root)
def get_offset(self, offset_parent=None): def get_offset(self, offset_parent=None):
""" """
Get the position relative to an offset parent. If no offset parent is Get the position relative to an offset parent. If no offset parent is
specified, the position relative to the root widget is returned. The specified, the position relative to the root area is returned. The
position of the root widget itself is (0, 0). position of the root area itself is (0, 0).
""" """
if not offset_parent: if not offset_parent:
offset_parent = self.get_root_widget() offset_parent = self.get_root_area()
if not self.parent: if not self.parent:
if offset_parent is self: if offset_parent is self:
...@@ -65,29 +65,29 @@ class Widget(Positionable, Logger): ...@@ -65,29 +65,29 @@ class Widget(Positionable, Logger):
return x - ox, y - oy return x - ox, y - oy
def add_widget(self, widget): def add_area(self, area):
""" """
Add a new child widget. Add a new child area.
""" """
self.children.append(widget) self.children.append(area)
widget.set_parent(self) area.set_parent(self)
def remove_widget(self, widget): def remove_area(self, area):
""" """
Remove a child widget. Remove a child area.
""" """
self.children.remove(widget) self.children.remove(area)
widget.set_parent(None) area.set_parent(None)
def set_parent(self, widget): def set_parent(self, area):
""" """
Set a new parent widget. If a parent widget has already been assigned, Set a new parent area. If a parent area has already been assigned,
remove the widget from that parent first. remove the area from that parent first.
""" """
if widget and self.parent: if area and self.parent:
self.parent.remove_widget(self) self.parent.remove_area(self)
self.parent = widget self.parent = area
def unbind(self, gesture_type, handler=None): def unbind(self, gesture_type, handler=None):
""" """
...@@ -154,9 +154,9 @@ class Widget(Positionable, Logger): ...@@ -154,9 +154,9 @@ class Widget(Positionable, Logger):
def __getattr__(self, name): def __getattr__(self, name):
""" """
Allow calls like: Allow calls like:
widget.on_gesture(...) area.on_gesture(...)
instead of: instead of:
widget.bind('gesture', ...) area.bind('gesture', ...)
""" """
if len(name) < 4 or name[:3] != 'on_': if len(name) < 4 or name[:3] != 'on_':
raise AttributeError("'%s' has no attribute '%s'" raise AttributeError("'%s' has no attribute '%s'"
...@@ -167,14 +167,14 @@ class Widget(Positionable, Logger): ...@@ -167,14 +167,14 @@ class Widget(Positionable, Logger):
@abstractmethod @abstractmethod
def contains_event(self, event): def contains_event(self, event):
""" """
Check if the coordinates of an event are contained within this widget. Check if the coordinates of an event are contained within this area.
""" """
raise NotImplementedError raise NotImplementedError
def delegate_event(self, event): def delegate_event(self, event):
""" """
Delegate a triggered event to all child widgets. If a child stops Delegate a triggered event to all child areas. If a child stops
propagation, return so that its siblings and the parent widget will not propagation, return so that its siblings and the parent area will not
delegate the event to their trackers. delegate the event to their trackers.
""" """
child_found = False child_found = False
...@@ -182,7 +182,7 @@ class Widget(Positionable, Logger): ...@@ -182,7 +182,7 @@ class Widget(Positionable, Logger):
if self.children: if self.children:
event.set_offset(self.get_offset()) event.set_offset(self.get_offset())
# Delegate to children in reverse order because widgets that are # Delegate to children in reverse order because areas that are
# added later, should be placed over previously added siblings # added later, should be placed over previously added siblings
for child in reversed(self.children): for child in reversed(self.children):
if child.contains_event(event): if child.contains_event(event):
......
from widget import Widget from area import Area
from screen import screen_size from screen import screen_size
__all__ = ['RectangularWidget', 'CircularWidget', 'FullscreenWidget'] __all__ = ['RectangularArea', 'CircularArea', 'FullscreenArea']
class RectangularWidget(Widget): class RectangularArea(Area):
""" """
Rectangular widget, has a position and a size. Rectangular area, has a position and a size.
""" """
def __init__(self, x, y, width, height): def __init__(self, x, y, width, height):
super(RectangularWidget, self).__init__(x, y) super(RectangularArea, self).__init__(x, y)
self.set_size(width, height) self.set_size(width, height)
def __str__(self): def __str__(self):
...@@ -31,12 +31,12 @@ class RectangularWidget(Widget): ...@@ -31,12 +31,12 @@ class RectangularWidget(Widget):
and self.y <= y <= self.y + self.height and self.y <= y <= self.y + self.height
class CircularWidget(Widget): class CircularArea(Area):
""" """
Circular widget, has a position and a radius. Circular area, has a position and a radius.
""" """
def __init__(self, x, y, radius): def __init__(self, x, y, radius):
super(CircularWidget, self).__init__(x, y) super(CircularArea, self).__init__(x, y)
self.set_radius(radius) self.set_radius(radius)
def __str__(self): def __str__(self):
...@@ -54,16 +54,16 @@ class CircularWidget(Widget): ...@@ -54,16 +54,16 @@ class CircularWidget(Widget):
return event.distance_to(self) <= self.radius return event.distance_to(self) <= self.radius
class FullscreenWidget(RectangularWidget): class FullscreenArea(RectangularArea):
""" """
Widget representation for the entire screen. This class provides an easy Area representation for the entire screen. This class provides an easy
way to create a single rectangular widget that catches all gestures. way to create a single rectangular area that catches all gestures.
""" """
def __init__(self): def __init__(self):
super(FullscreenWidget, self).__init__(0, 0, *screen_size) super(FullscreenArea, self).__init__(0, 0, *screen_size)
def __str__(self): def __str__(self):
return '<FullscreenWidget size=(%d, %d)>' % self.get_size() return '<FullscreenArea size=(%d, %d)>' % self.get_size()
def contains_event(self, event): def contains_event(self, event):
return True return True
...@@ -6,7 +6,7 @@ class Event(Positionable): ...@@ -6,7 +6,7 @@ class Event(Positionable):
""" """
Abstract base class for events triggered by an event driver. These events Abstract base class for events triggered by an event driver. These events
are delegated to gesture trackers, to be translated to gestures. To be able are delegated to gesture trackers, to be translated to gestures. To be able
to check whether an event is located within a widget, a position is to check whether an event is located within a area, a position is
required. Therefore, the touch object that triggers the event is is linked required. Therefore, the touch object that triggers the event is is linked
to the event object. to the event object.
""" """
...@@ -32,8 +32,8 @@ class Event(Positionable): ...@@ -32,8 +32,8 @@ class Event(Positionable):
def set_offset(self, offset): def set_offset(self, offset):
self.offset.set_position(*offset) self.offset.set_position(*offset)
def set_root_widget(self, widget): def set_root_area(self, area):
x, y = widget x, y = area
self.x -= x self.x -= x
self.y -= y self.y -= y
......
...@@ -7,7 +7,7 @@ class EventDriver(Logger): ...@@ -7,7 +7,7 @@ class EventDriver(Logger):
messages to a common set of events. The minimal set is {point_down, messages to a common set of events. The minimal set is {point_down,
point_move, point_up}. A driver implementation should define the methods point_move, point_up}. A driver implementation should define the methods
'start' and 'stop', which starts/stops some event loop that triggers the 'start' and 'stop', which starts/stops some event loop that triggers the
'delegate_event' method of a widget. 'delegate_event' method of a area.
""" """
def __init__(self, event_server): def __init__(self, event_server):
self.event_server = event_server self.event_server = event_server
......
...@@ -5,37 +5,37 @@ from drivers import select_driver ...@@ -5,37 +5,37 @@ from drivers import select_driver
class EventServer(Logger): class EventServer(Logger):
""" """
The event server uses an event driver to receive events, which are The event server uses an event driver to receive events, which are
delegated to a widget tree (and eventually to gesture trackers). delegated to a area tree (and eventually to gesture trackers).
""" """
def __init__(self, root_widget=None): def __init__(self, root_area=None):
# Root widget to which events are delegated # Root area to which events are delegated
self.root_widget = root_widget self.root_area = root_area
# Driver implementation that will be serving events # Driver implementation that will be serving events
self.event_driver = select_driver(self) self.event_driver = select_driver(self)
def get_root_widget(self): def get_root_area(self):
return self.root_widget return self.root_area
def set_root_widget(self, widget): def set_root_area(self, area):
self.root_widget = widget self.root_area = area
def delegate_event(self, event): def delegate_event(self, event):
""" """
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. area tree.
""" """
if self.root_widget.contains_event(event): if self.root_area.contains_event(event):
event.set_root_widget(self.root_widget) event.set_root_area(self.root_area)
self.root_widget.delegate_event(event) self.root_area.delegate_event(event)
def start(self): def start(self):
""" """
Start the event loop. A root widget is needed to be able to delegate Start the event loop. A root area is needed to be able to delegate
events, so check if it exists first. events, so check if it exists first.
""" """
if not self.root_widget: if not self.root_area:
raise ValueError('Cannot start event server without root widget.') raise ValueError('Cannot start event server without root area.')
self.event_driver.start() self.event_driver.start()
......
...@@ -15,12 +15,12 @@ class GestureTracker(Logger): ...@@ -15,12 +15,12 @@ class GestureTracker(Logger):
# Configurable properties (see configure() method) # Configurable properties (see configure() method)
configurable = [] configurable = []
def __init__(self, widget): def __init__(self, area):
self.widget = widget self.area = area
def handle_event(self, event): def handle_event(self, event):
""" """
Handle an event that was delegated by a widget. The tracker Handle an event that was delegated by a area. The tracker
implementation should define a handler function for the event. implementation should define a handler function for the event.
Otherwise, the event will be ignored. Otherwise, the event will be ignored.
""" """
...@@ -32,7 +32,7 @@ class GestureTracker(Logger): ...@@ -32,7 +32,7 @@ class GestureTracker(Logger):
def trigger(self, gesture): def trigger(self, gesture):
gesture.set_tracker(self) gesture.set_tracker(self)
self.info('Triggered %s.' % gesture) self.info('Triggered %s.' % gesture)
self.widget.handle_gesture(gesture) self.area.handle_gesture(gesture)
def configure(self, **kwargs): def configure(self, **kwargs):
for name, value in kwargs.iteritems(): for name, value in kwargs.iteritems():
......
...@@ -36,8 +36,8 @@ class TapTracker(GestureTracker): ...@@ -36,8 +36,8 @@ class TapTracker(GestureTracker):
configurable = ['tap_distance', 'tap_time', 'double_tap_time', configurable = ['tap_distance', 'tap_time', 'double_tap_time',
'double_tap_distance', 'update_rate', 'propagate_up_event'] 'double_tap_distance', 'update_rate', 'propagate_up_event']
def __init__(self, widget): def __init__(self, area):
super(TapTracker, self).__init__(widget) super(TapTracker, self).__init__(area)
# Map of touch object id to tuple (timestamp, position) of point down # Map of touch object id to tuple (timestamp, position) of point down
self.reg = {} self.reg = {}
...@@ -57,8 +57,8 @@ class TapTracker(GestureTracker): ...@@ -57,8 +57,8 @@ class TapTracker(GestureTracker):
# Times per second to detect single taps # Times per second to detect single taps
self.update_rate = 30 self.update_rate = 30
# Whether to stop propagation of the 'point_up' event to parent widgets # Whether to stop propagation of the 'point_up' event to parent areas
# If False, this reserves tap events to child widgets # If False, this reserves tap events to child areas
self.propagate_up_event = True self.propagate_up_event = True
self.reset_last_tap() self.reset_last_tap()
......
...@@ -73,8 +73,8 @@ class TransformationTracker(GestureTracker): ...@@ -73,8 +73,8 @@ class TransformationTracker(GestureTracker):
""" """
supported_gestures = [RotationGesture, PinchGesture, DragGesture] supported_gestures = [RotationGesture, PinchGesture, DragGesture]
def __init__(self, widget): def __init__(self, area):
super(TransformationTracker, self).__init__(widget) super(TransformationTracker, self).__init__(area)
# All touch points performing the transformation # All touch points performing the transformation
self.points = [] self.points = []
...@@ -102,7 +102,7 @@ class TransformationTracker(GestureTracker): ...@@ -102,7 +102,7 @@ class TransformationTracker(GestureTracker):
else: else:
self.centroid = MovingPositionable(x, y) self.centroid = MovingPositionable(x, y)
#wx, wy = self.widget.get_screen_offset() #wx, wy = self.area.get_screen_offset()
#self.centroid.translate(-wx, -wy) #self.centroid.translate(-wx, -wy)
def on_point_down(self, event): def on_point_down(self, event):
......
from src.event_server import EventServer from src.event_server import EventServer
from src.widgets import FullscreenWidget from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen widget # Create server and fullscreen area
screen = FullscreenWidget() screen = FullscreenArea()
server = EventServer(screen) server = EventServer(screen)
# Bind handlers # Bind handlers
......
...@@ -12,7 +12,7 @@ def quit(*args): ...@@ -12,7 +12,7 @@ def quit(*args):
gtk.main_quit() gtk.main_quit()
class Rectangle(mt.RectangularWidget): class Rectangle(mt.RectangularArea):
def __init__(self, x, y, width, height, color=(1, 0, 0)): def __init__(self, x, y, width, height, color=(1, 0, 0)):
super(Rectangle, self).__init__(x, y, width, height) super(Rectangle, self).__init__(x, y, width, height)
self.angle = 1.0 self.angle = 1.0
...@@ -72,7 +72,7 @@ def create_context_window(w, h, callback): ...@@ -72,7 +72,7 @@ def create_context_window(w, h, callback):
draw() draw()
def move_window(win, event): def move_window(win, event):
"""Synchronize root multi-touch widget with GTK window.""" """Synchronize root multi-touch area with GTK window."""
root.set_position(*event.get_coords()) root.set_position(*event.get_coords())
root.set_size(event.width, event.height) root.set_size(event.width, event.height)
draw() draw()
...@@ -91,9 +91,9 @@ def create_context_window(w, h, callback): ...@@ -91,9 +91,9 @@ def create_context_window(w, h, callback):
elif key == 'q': elif key == 'q':
quit() quit()
# Root widget (will be synchronized with GTK window) # Root area (will be synchronized with GTK window)
global root global root
root = mt.RectangularWidget(0, 0, w, h) root = mt.RectangularArea(0, 0, w, h)
# GTK window # GTK window
global window global window
...@@ -146,7 +146,7 @@ def on_show(window): ...@@ -146,7 +146,7 @@ def on_show(window):
# Create blue rectangle # Create blue rectangle
rect = Rectangle(300, 200, 250, 150, color=(0, 0, 1)) rect = Rectangle(300, 200, 250, 150, color=(0, 0, 1))
draw_objects.append(rect) draw_objects.append(rect)
root.add_widget(rect) root.add_area(rect)
def rect_tap(g): print 'tapped on rectangle' def rect_tap(g): print 'tapped on rectangle'
rect.on_tap(rect_tap, propagate_up_event=False) rect.on_tap(rect_tap, propagate_up_event=False)
...@@ -157,7 +157,7 @@ if __name__ == '__main__': ...@@ -157,7 +157,7 @@ if __name__ == '__main__':
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
parse_args(create_parser()) parse_args(create_parser())
# Create a window with a Cairo context in it and a multi-touch widget # Create a window with a Cairo context in it and a multi-touch area
# syncronized with it # syncronized with it
create_context_window(640, 460, on_show) create_context_window(640, 460, on_show)
......
...@@ -5,7 +5,7 @@ from threading import Thread ...@@ -5,7 +5,7 @@ from threading import Thread
from math import degrees from math import degrees
from src.event_server import EventServer from src.event_server import EventServer
from src.widgets import FullscreenWidget from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
from src.screen import screen_size from src.screen import screen_size
...@@ -146,17 +146,17 @@ def pinch(gesture): ...@@ -146,17 +146,17 @@ def pinch(gesture):
save_tracker(gesture) save_tracker(gesture)
widget = FullscreenWidget() area = FullscreenArea()
server = EventServer(widget) server = EventServer(area)
widget.on_rotate(rotate) area.on_rotate(rotate)
widget.on_pinch(pinch) area.on_pinch(pinch)
widget.on_tap(lambda g: taps.append([coord(*g), FINGER_RADIUS])) area.on_tap(lambda g: taps.append([coord(*g), FINGER_RADIUS]))
widget.on_single_tap(lambda g: dtaps.append(list(coord(*g)) + [1])) area.on_single_tap(lambda g: dtaps.append(list(coord(*g)) + [1]))
widget.on_double_tap(lambda g: dtaps.append(list(coord(*g)) + [0])) area.on_double_tap(lambda g: dtaps.append(list(coord(*g)) + [0]))
widget.on_point_down(lambda g: points.append(g.get_event().point)) area.on_point_down(lambda g: points.append(g.get_event().point))
widget.on_point_up(lambda g: points.remove(g.get_event().point)) area.on_point_up(lambda g: points.remove(g.get_event().point))
try: try:
......
from src.event_server import EventServer from src.event_server import EventServer
from src.widgets import FullscreenWidget from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen widget # Create server and fullscreen area
screen = FullscreenWidget() screen = FullscreenArea()
server = EventServer(screen) server = EventServer(screen)
# Bind handlers # Bind handlers
......
from src.event_server import EventServer from src.event_server import EventServer
from src.widgets import FullscreenWidget from src.areas import FullscreenArea
from tests.parse_arguments import create_parser, parse_args from tests.parse_arguments import create_parser, parse_args
parse_args(create_parser()) parse_args(create_parser())
# Create server and fullscreen widget # Create server and fullscreen area
screen = FullscreenWidget() screen = FullscreenArea()
server = EventServer(screen) server = EventServer(screen)
# Bind handlers # Bind handlers
......
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