Created
December 20, 2012 09:42
-
-
Save chergert/4344205 to your computer and use it in GitHub Desktop.
basic touch test using gtk and touch events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cairo | |
from gi.repository import Gdk | |
from gi.repository import Gtk | |
colors = ['a40000', '75507b', '8ae234', '729fcf', 'f57900', | |
'c4a000', 'ce5c00', 'eeeeec', '4e9a06', '204a87'] | |
def next_idx(seq): | |
values = seq.values() | |
for i in xrange(0, 10): | |
if i not in values: | |
return i | |
return 0 | |
def on_touch_event(drawing_area, event, seq): | |
surface = drawing_area.surface | |
if surface: | |
if event.touch.type == Gdk.EventType.TOUCH_BEGIN: | |
seq[event.touch.sequence] = next_idx(seq) | |
cr = cairo.Context(surface) | |
cr.rectangle(event.touch.x - 5, event.touch.y - 5, 10, 10) | |
i = seq[event.touch.sequence] | |
c = colors[i] | |
t = int(c[0:2],16)/255.0, int(c[2:4],16)/255.0, int(c[4:6],16)/255.0 | |
cr.set_source_rgb(*t) | |
cr.fill() | |
drawing_area.queue_draw() | |
if event.touch.type == Gdk.EventType.TOUCH_END: | |
del seq[event.touch.sequence] | |
def on_draw(drawing_area, cr): | |
if not drawing_area.surface: | |
return | |
alloc = drawing_area.get_allocation() | |
cr.set_source_surface(drawing_area.surface, 0, 0) | |
cr.rectangle(0, 0, alloc.width, alloc.height) | |
cr.paint() | |
def on_size_allocate(drawing_area, alloc): | |
if hasattr(drawing_area, 'surface'): | |
del drawing_area.surface | |
drawing_area.surface = None | |
window = drawing_area.get_window() | |
if window: | |
drawing_area.surface = window.create_similar_surface(0x1000, alloc.width, alloc.height) | |
if __name__ == '__main__': | |
sequences = {} | |
w = Gtk.Window() | |
w.connect('delete-event', lambda *_: Gtk.main_quit()) | |
a = Gtk.DrawingArea(visible=True) | |
a.connect('draw', on_draw) | |
a.connect('touch-event', on_touch_event, sequences) | |
a.connect('size-allocate', on_size_allocate) | |
a.add_events(Gdk.EventMask.TOUCH_MASK) | |
w.add(a) | |
w.present() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment