From 2e0c2bbc9a6544c39c4c6b31ced96aa658a87e4f Mon Sep 17 00:00:00 2001 From: Sai Vineet Date: Tue, 03 Dec 2013 14:02:57 +0000 Subject: Export body positions in Physics Enhancement: Physics should allow exporting position of body to the journal for usage in other activities like Chart, etc. Fixes #3686 --- diff --git a/activity.py b/activity.py index 9994dea..6539119 100644 --- a/activity.py +++ b/activity.py @@ -22,6 +22,8 @@ import os import gtk from gettext import gettext as _ import logging +import json +import tempfile import pygame import sugargame @@ -36,6 +38,7 @@ from sugar.graphics.alert import ConfirmationAlert from sugar.graphics.toolbarbox import ToolbarBox from sugar.graphics.toolbarbox import ToolbarButton from sugar.graphics.style import GRID_CELL_SIZE +from sugar.datastore import datastore import tools import physics @@ -120,6 +123,12 @@ class PhysicsActivity(activity.Activity): self._insert_stop_play_button(toolbar_box.toolbar) + export_json = ToolButton("document-save") + export_json.set_tooltip(_("Export tracked objects to journal")) + export_json.connect('clicked', self._export_json_cb) + toolbar_box.toolbar.insert(export_json, -1) + export_json.show() + separator = gtk.SeparatorToolItem() toolbar_box.toolbar.insert(separator, -1) separator.show() @@ -216,6 +225,16 @@ class PhysicsActivity(activity.Activity): else: pass + def _export_json_cb(self, button): + jobject = datastore.create() + jobject.metadata['title'] = _("Physics export") + jobject.metadata['mime_type'] = "application/json" + tmp = tempfile.TemporaryFile() + data = self.game.full_pos_list + json.dump(data, tmp) + jobject.file_path = os.path.abspath(tmp.name) + datastore.write(jobject) + def radioClicked(self, button): pygame.event.post(pygame.event.Event(pygame.USEREVENT, action=self.radioList[button])) diff --git a/icons/track.svg b/icons/track.svg new file mode 100644 index 0000000..16b2afc --- /dev/null +++ b/icons/track.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + diff --git a/physics.py b/physics.py index 253ff1b..bb7f066 100644 --- a/physics.py +++ b/physics.py @@ -66,6 +66,9 @@ class PhysicsGame: self.loop = True self.pygame_started = False + self.full_pos_list = [] + self.tracked_bodies = [] + def switch_off_fake_pygame_cursor_cb(self, panel, event): self.show_fake_cursor = False @@ -127,6 +130,12 @@ class PhysicsGame: self.show_fake_cursor = True if self.in_focus: + # Tracking object positions which have pens on them + if self.tracked_bodies is not []: + temp_zipped = zip(self.full_pos_list, self.tracked_bodies) + for single_body_list, body in temp_zipped: + self.single_body_list.append(body.position) + # Drive motors if self.world.run_physics: for body in self.world.world.GetBodyList(): diff --git a/tools.py b/tools.py index 62aba05..f5b971f 100644 --- a/tools.py +++ b/tools.py @@ -594,6 +594,34 @@ class DestroyTool(Tool): def cancel(self): self.vertices = None +class TrackTool(Tool): + name = 'Track' + icon = 'track' + toolTip = _('Track Object') + toolAccelerator = _("r") + + + def __init__(self, game): + Tool.__init__(self, game) + self.radius = 1 + + def handleToolEvent(self, event): + Tool.handleToolEvent(self, event) + + if pygame.mouse.get_pressed()[0]: + current_body = self.game.world.get_bodies_at_pos( + tuple_to_int(event.pos))[0] + if current_body: + point_pos = tuple_to_int(event.pos) + track_circle = self.game.world.add.ball(point_pos, self.radius, + dynamic=True, density=0.001, + restitution=0.16, friction=0.1) + + mod_pos = (point_pos[0], point_pos[1]) + self.game.world.add.joint( + track_circle, current_body, point_pos, mod_pos) + self.game.tracked_bodies.append(track_circle) + def getAllTools(): return [MagicPenTool, @@ -605,6 +633,7 @@ def getAllTools(): MotorTool, PinTool, JointTool, + TrackTool, DestroyTool] allTools = getAllTools() -- cgit v0.9.1