Skip to content

Instantly share code, notes, and snippets.

@chergert
Created December 20, 2012 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chergert/4344205 to your computer and use it in GitHub Desktop.
Save chergert/4344205 to your computer and use it in GitHub Desktop.
basic touch test using gtk and touch events
#!/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