Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/touch.py
blob: 069e522c928d01f95afb1d27f31ba89a7869b1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# Copyright (C) 2011, One Laptop Per Child
# Author, Manuel Kaufmann <humitos@gmail.com>

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()