Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
diff options
context:
space:
mode:
Diffstat (limited to 'activity.py')
-rw-r--r--activity.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/activity.py b/activity.py
new file mode 100644
index 0000000..b09d20f
--- /dev/null
+++ b/activity.py
@@ -0,0 +1,118 @@
+# 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.
+
+
+"""TestMultiTouch Activity: An example of using multi-touch in an activity."""
+
+from gi.repository import Gtk
+from gi.repository import Gdk
+from random import random
+
+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.points = {}
+ self.colors = {}
+ 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)
+ pos = (event.touch.x, event.touch.y)
+ if event.type == Gdk.EventType.TOUCH_BEGIN:
+ self.points[seq] = [pos]
+ self.colors[seq] = (random(), random(), random())
+ elif event.type == Gdk.EventType.TOUCH_UPDATE:
+ self.points[seq].append(pos)
+ else:
+ del self.points[seq]
+ del self.colors[seq]
+ self.queue_draw()
+
+ def __draw_cb(self, widget, ctx):
+ ctx.set_line_width(5)
+ for seq in self.points:
+ (r, g, b) = self.colors[seq]
+ ctx.set_source_rgb(r, g, b)
+ p = self.points[seq][0]
+ ctx.move_to(p[0], p[1])
+ for p in self.points[seq]:
+ ctx.line_to(p[0], p[1])
+ ctx.stroke()