#!/usr/bin/env python # Copyright (C) 2011, One Laptop Per Child # Author, Manuel Kaufmann import moosegesture from gi.repository import Gtk from gi.repository import Gdk LENGHT = 100 DIRECTIONS = { 1: (-1, 1), 2: (0, 1), 3: (1, 1), 4: (-1, 0), 7: (-1, -1), 8: (0, -1), 9: (1, -1), 6: (1, 0), } class TouchWidget(Gtk.DrawingArea): def __init__(self): self.touches = {} self.gesture = [] super(TouchWidget, self).__init__() self.set_events(Gdk.EventMask.TOUCH_MASK) self.connect('draw', self.__draw_cb) self.connect('event', self.__event_cb) def __event_cb(self, widget, event): if event.type in (Gdk.EventType.TOUCH_BEGIN, Gdk.EventType.TOUCH_CANCEL, Gdk.EventType.TOUCH_END, Gdk.EventType.TOUCH_UPDATE): x = event.touch.x y = event.touch.y seq = str(event.touch.sequence) if event.type in (Gdk.EventType.TOUCH_BEGIN, Gdk.EventType.TOUCH_UPDATE): if seq in self.touches: self.touches[seq].append((x, y)) else: self.touches[seq] = [(x, y)] elif event.type == Gdk.EventType.TOUCH_END: self.gesture = moosegesture.getGesture(self.touches[seq]) del self.touches[seq] self.queue_draw() def __draw_cb(self, widget, ctx): ctx.set_source_rgba(0.3, 0.3, 0.3, 0.7) allocation = self.get_allocation() ctx.save() ctx.set_line_width(10) ctx.move_to(allocation.width / 2, allocation.height / 2) for direction in self.gesture: x, y = DIRECTIONS[direction] ctx.rel_line_to(x * LENGHT, y * LENGHT) ctx.stroke() ctx.restore() self.gesture = [] def main(): window = Gtk.Window() test_touch = TouchWidget() window.add(test_touch) window.connect("destroy", Gtk.main_quit) window.show_all() window.maximize() Gtk.main() if __name__ == "__main__": main()