From d44edb28f684caef741442e83bda63fbbbe5e44f Mon Sep 17 00:00:00 2001 From: Sai Vineet Date: Sun, 05 Jan 2014 11:13:53 +0000 Subject: Add Rope tool Also added previous commits, - * Pen badge demo * Box2d 2.02b port, fixes memory leak --- diff --git a/icons/chain.svg b/icons/chain.svg new file mode 100644 index 0000000..0506c3d --- /dev/null +++ b/icons/chain.svg @@ -0,0 +1,908 @@ + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/icons/trophy-icon-physics.svg b/icons/trophy-icon-physics.svg new file mode 100644 index 0000000..70543cb --- /dev/null +++ b/icons/trophy-icon-physics.svg @@ -0,0 +1,66 @@ + + + +image/svg+xml + + + + + + \ No newline at end of file diff --git a/lib/Box2D-2.0.2b2-py2.7-linux-i686.egg b/lib/Box2D-2.0.2b2-py2.7-linux-i686.egg new file mode 100644 index 0000000..7477359 --- /dev/null +++ b/lib/Box2D-2.0.2b2-py2.7-linux-i686.egg Binary files differ diff --git a/tools.py b/tools.py index 0c925a4..7911168 100644 --- a/tools.py +++ b/tools.py @@ -20,12 +20,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os +from shutil import copy import pygame +import json from pygame.locals import * from helpers import * from gettext import gettext as _ -from sugar.graphics.alert import ConfirmationAlert +from sugar.activity import activity import gtk +import math # Tools that can be superlcassed @@ -98,6 +102,28 @@ class Tool(object): # Default cancel doesn't do anything pass + def add_badge(self, message, + icon="trophy-icon-physics", from_="Physics"): + badge = { + 'icon': icon, + 'from': from_, + 'message': message + } + icon_path = os.path.join(activity.get_bundle_path(), + "icons", + (icon+".svg")) + sugar_icons = os.path.join( + os.path.expanduser('~'), + ".icons") + copy(icon_path, sugar_icons) + + if 'comments' in self.game.activity.metadata: + comments = json.loads(self.game.activity.metadata['comments']) + comments.append(badge) + self.game.activity.metadata['comments'] = json.dumps(comments) + else: + self.game.activity.metadata['comments'] = json.dumps([badge]) + # The circle creation tool class CircleTool(Tool): @@ -689,6 +715,7 @@ class TrackTool(Tool): def __init__(self, game): Tool.__init__(self, game) self.radius = 1 + self.added_badge = False def handleToolEvent(self, event): Tool.handleToolEvent(self, event) @@ -722,6 +749,85 @@ class TrackTool(Tool): self.game.trackinfo[dictkey][4] = trackdex # Tracking index. self.game.tracked_bodies += 1 # counter of tracked bodies + if not self.added_badge: + self.add_badge(message="Congratulations! You just added a" + " Pen to your machine!", + from_="Isacc Newton") + self.added_badge = True + + +class ChainTool(Tool): + name = 'Chain' + icon = 'chain' + toolTip = _("Chain") + toolAccelerator = "i" + + def __init__(self, gameInstance): + Tool.__init__(self, gameInstance) + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None + self.circle_distance = 25 + self.circle_radius = 5 + + def handleToolEvent(self, event): + Tool.handleToolEvent(self, event) + if event.type == MOUSEBUTTONDOWN: + if event.button >= 1: + # Grab the first body + self.jb1pos = tuple_to_int(event.pos) + self.jb1 = self.game.world.get_bodies_at_pos( + tuple_to_int(event.pos)) + self.jb2 = self.jb2pos = None + elif event.type == MOUSEBUTTONUP: + if event.button == 1: + # Grab the second body + self.jb2pos = tuple_to_int(event.pos) + self.jb2 = self.game.world.get_bodies_at_pos( + tuple_to_int(event.pos)) + # If we have two distinct bodies, make the chain. + if self.jb1 and self.jb2 and str(self.jb1) != str(self.jb2): + self.make_chain( + self.jb1[0], self.jb2[0], self.jb1pos, self.jb2pos) + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None + + def draw(self): + Tool.draw(self) + if self.jb1: + pygame.draw.line(self.game.screen, (100, 180, 255), self.jb1pos, + tuple_to_int(pygame.mouse.get_pos()), 3) + + def cancel(self): + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None + + def make_chain(self, bodyA, bodyB, pos1, pos2): + d = int(distance(pos1, pos2)) + x1, y1 = pos1 + x2, y2 = pos2 + bearing = math.atan2((y2-y1), (x2-x1)) + first_iter = True + prevcircle = None + prevpos = None + for p in range(5, d, self.circle_distance): + x = x1 + p * math.cos(bearing) + y = y1 + p * math.sin(bearing) + circle = self.game.world.add.ball( + (x, y), self.circle_radius, dynamic=True) + circle.userData['color'] = (0, 0, 0) + circle = self.game.world.get_bodies_at_pos( + tuple_to_int((x, y)))[0] + if first_iter: + self.game.world.add.joint(circle, bodyA, (x, y), pos1, False) + first_iter = False + oldcircle = circle + prevpos = (x, y) + elif (p+25) > d: + self.game.world.add.joint(circle, oldcircle, (x, y), prevpos) + self.game.world.add.joint(circle, bodyB, (x, y), pos2, False) + else: + self.game.world.add.joint(circle, oldcircle, (x, y), prevpos, + False) + oldcircle = circle + prevpos = (x, y) + def getAllTools(): return [MagicPenTool, @@ -733,9 +839,8 @@ def getAllTools(): MotorTool, PinTool, JointTool, + ChainTool, TrackTool, - DestroyTool, - # EraseAllTool, # moved to main toolbar - ] + DestroyTool] allTools = getAllTools() -- cgit v0.9.1