Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnacio Rodríguez <ignaciorodriguez@sugarlabs.org>2013-11-20 16:41:20 (GMT)
committer Ignacio Rodríguez <ignaciorodriguez@sugarlabs.org>2013-11-20 16:41:20 (GMT)
commit59b43fe933be7ecff389331fdf735107fb787f2d (patch)
treeb4fe018a0e9246c898214b0cba76dc8e96d609e7
parent48d87969bc0685f9e4de6c198dcc0d42008c4d5f (diff)
Touch Skeleton - TestMultiTouch activity
-rw-r--r--Touch/activity.py188
-rw-r--r--Touch/activity/activity-testmultitouch.svg9
-rw-r--r--Touch/activity/activity.info7
-rwxr-xr-xTouch/setup.py3
4 files changed, 207 insertions, 0 deletions
diff --git a/Touch/activity.py b/Touch/activity.py
new file mode 100644
index 0000000..1245451
--- /dev/null
+++ b/Touch/activity.py
@@ -0,0 +1,188 @@
+# Copyright (c) 2012 Bert Freudenberg
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+"""
+Activity: TestMultiTouch
+Shows multi-touch events
+"""
+
+from gi.repository import Gtk
+from gi.repository import Gdk
+from random import random
+from math import pi as M_PI
+
+from sugar3.activity import activity
+from sugar3.graphics.toolbarbox import ToolbarBox
+from sugar3.activity.widgets import ActivityButton
+from sugar3.activity.widgets import TitleEntry
+from sugar3.activity.widgets import StopButton
+from sugar3.activity.widgets import ShareButton
+from sugar3.activity.widgets import DescriptionItem
+
+class TestMultiTouchActivity(activity.Activity):
+
+ def __init__(self, handle):
+ activity.Activity.__init__(self, handle)
+ self.max_participants = 1
+
+ toolbar_box = ToolbarBox()
+
+ activity_button = ActivityButton(self)
+ toolbar_box.toolbar.insert(activity_button, 0)
+ activity_button.show()
+
+ title_entry = TitleEntry(self)
+ toolbar_box.toolbar.insert(title_entry, -1)
+ title_entry.show()
+
+ description_item = DescriptionItem(self)
+ toolbar_box.toolbar.insert(description_item, -1)
+ description_item.show()
+
+ share_button = ShareButton(self)
+ toolbar_box.toolbar.insert(share_button, -1)
+ share_button.show()
+
+ separator = Gtk.SeparatorToolItem()
+ separator.props.draw = False
+ separator.set_expand(True)
+ toolbar_box.toolbar.insert(separator, -1)
+ separator.show()
+
+ stop_button = StopButton(self)
+ toolbar_box.toolbar.insert(stop_button, -1)
+ stop_button.show()
+
+ self.set_toolbar_box(toolbar_box)
+ toolbar_box.show()
+
+ # main view
+ touch_area = TouchArea()
+ self.set_canvas(touch_area)
+ touch_area.show()
+
+
+class TouchArea(Gtk.DrawingArea):
+
+ def __init__(self):
+ Gtk.DrawingArea.__init__(self)
+ self.fingers = {}
+ 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_UPDATE,
+ Gdk.EventType.TOUCH_CANCEL,
+ Gdk.EventType.TOUCH_END):
+ # sequence is a void ptr object identifying the finger
+ # we make it a string for use as dict key
+ seq = str(event.touch.sequence)
+ if event.type == Gdk.EventType.TOUCH_BEGIN:
+ new_finger = Finger(event)
+ self.fingers[seq] = new_finger
+ self.remove_inactive_fingers(new_finger)
+ else:
+ self.fingers[seq].update(event)
+ self.queue_draw()
+
+ def __draw_cb(self, widget, ctx):
+ alloc = self.get_allocation()
+ ctx.set_source_rgb(1, 1, 1)
+ ctx.paint()
+ for f in self.fingers:
+ self.fingers[f].draw(ctx, alloc.width, alloc.height)
+
+ def remove_inactive_fingers(self, new_finger):
+ for f,old_finger in list(self.fingers.items()):
+ if not old_finger.active:
+ del self.fingers[f]
+ if new_finger.time - old_finger.time < 300:
+ new_finger.remember(old_finger)
+
+
+class Finger:
+
+ def __init__(self, event):
+ self.trail = [ Point(event.touch.x, event.touch.y) ]
+ self.color = (random(), random(), random())
+ self.time = event.touch.time
+ self.active = True
+ self.other = None
+
+ def update(self, event):
+ self.time = event.touch.time
+ if event.type == Gdk.EventType.TOUCH_UPDATE:
+ self.trail.append( Point(event.touch.x, event.touch.y) )
+ elif event.type == Gdk.EventType.TOUCH_END:
+ self.active = False
+ elif event.type == Gdk.EventType.TOUCH_CANCEL:
+ self.active = False
+ self.color = (1, 0, 0)
+
+ def remember(self, finger):
+ if self.other:
+ finger.remember(self.other)
+ self.other = finger
+
+ def draw(self, ctx, width, height):
+ if self.other:
+ self.other.draw(ctx, width, height)
+ (r, g, b) = self.color
+ ctx.set_source_rgb(r, g, b)
+ self.draw_trail(ctx)
+ if self.active:
+ self.draw_touch(ctx)
+ ctx.set_source_rgb(0.5, 0.5, 0.5)
+ self.draw_crosshair(ctx, width, height)
+
+ def draw_crosshair(self, ctx, width, height):
+ p = self.trail[-1]
+ ctx.set_line_width(1)
+ ctx.move_to(p.x, 0)
+ ctx.line_to(p.x, height)
+ ctx.move_to(0, p.y)
+ ctx.line_to(width, p.y)
+ ctx.stroke()
+
+ def draw_touch(self, ctx):
+ p = self.trail[-1]
+ ctx.set_line_width(10)
+ ctx.arc(p.x, p.y, 30, 0, 2 * M_PI)
+ ctx.stroke()
+
+ def draw_trail(self, ctx):
+ ctx.set_line_width(5)
+ p = self.trail[0]
+ ctx.move_to(p.x, p.y)
+ for p in self.trail:
+ ctx.line_to(p.x, p.y)
+ ctx.stroke()
+
+
+class Point:
+
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
diff --git a/Touch/activity/activity-testmultitouch.svg b/Touch/activity/activity-testmultitouch.svg
new file mode 100644
index 0000000..c74ac2b
--- /dev/null
+++ b/Touch/activity/activity-testmultitouch.svg
@@ -0,0 +1,9 @@
+<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' [
+ <!ENTITY stroke_color "#010101">
+ <!ENTITY fill_color "#FFFFFF">
+]>
+<svg viewBox="0 0 55 55" xmlns="http://www.w3.org/2000/svg">
+ <path d="M9.263,48.396c0.682,1.152,6.027,0.059,8.246-1.463 c2.102-1.432,3.207-2.596,4.336-2.596c1.133,0,12.54,0.92,20.935-5.715c7.225-5.707,9.773-13.788,4.52-21.437 c-5.252-7.644-13.832-9.08-20.878-8.56C16.806,9.342,4.224,16.91,4.677,28.313c0.264,6.711,3.357,9.143,4.922,10.703 c1.562,1.566,4.545,1.566,2.992,5.588C11.981,46.183,8.753,47.522,9.263,48.396z" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5"/>
+ <circle cx="20" cy="25" r="5" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <circle cx="35" cy="30" r="5" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+</svg>
diff --git a/Touch/activity/activity.info b/Touch/activity/activity.info
new file mode 100644
index 0000000..025c591
--- /dev/null
+++ b/Touch/activity/activity.info
@@ -0,0 +1,7 @@
+[Activity]
+name = TestMultiTouch
+activity_version = 5
+bundle_id = org.sugarlabs.TestMultiTouch
+exec = sugar-activity activity.TestMultiTouchActivity
+icon = activity-testmultitouch
+license = MIT
diff --git a/Touch/setup.py b/Touch/setup.py
new file mode 100755
index 0000000..9a141b3
--- /dev/null
+++ b/Touch/setup.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+from sugar3.activity import bundlebuilder
+bundlebuilder.start()