diff options
author | Sai Vineet <saivineet89@gmail.com> | 2014-01-05 11:13:53 (GMT) |
---|---|---|
committer | Sai Vineet <saivineet89@gmail.com> | 2014-01-05 11:13:53 (GMT) |
commit | d44edb28f684caef741442e83bda63fbbbe5e44f (patch) | |
tree | 39f2583935bc65b1d801ba9d8fc5d5c220799431 /tools.py | |
parent | 7613ba67e7c784dc38e0a56299b6874d5ef6c864 (diff) |
Add Rope tool
Also added previous commits, -
* Pen badge demo
* Box2d 2.02b port, fixes memory leak
Diffstat (limited to 'tools.py')
-rw-r--r-- | tools.py | 113 |
1 files changed, 109 insertions, 4 deletions
@@ -20,12 +20,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +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 = "<ctrl>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() |