From 011c16a2dc87f0cab8062cd53e73bea465b76ce1 Mon Sep 17 00:00:00 2001 From: Sai Vineet Date: Sat, 14 Dec 2013 16:40:44 +0000 Subject: Add capability to remove pens with Destroy Tool, pen tracking to journal entry. --- diff --git a/elements/elements.py b/elements/elements.py index 2b6fb71..4d9a361 100644 --- a/elements/elements.py +++ b/elements/elements.py @@ -430,7 +430,7 @@ class Elements: return variables - def json_save(self, path, additional_vars = {}): + def json_save(self, path, additional_vars = {}, serialize=False): import json worldmodel = {} @@ -501,8 +501,21 @@ class Elements: controllerlist = [] worldmodel['controllerlist'] = controllerlist - worldmodel['additional_vars'] = additional_vars + if serialize: + addvars = additional_vars + trackinfo = addvars['trackinfo'] + backup = trackinfo + for key, info in backup.iteritems(): + if not info[3]: + trackinfo[key][0] = info[0].userData['saveid'] + trackinfo[key][1] = info[1].userData['saveid'] + else: + addvars['trackinfo'][key][0] = None + addvars['trackinfo'][key][1] = None + + additional_vars['trackinfo'] = trackinfo + worldmodel['additional_vars'] = additional_vars f = open(path,'w') f.write(json.dumps(worldmodel)) f.close() @@ -510,7 +523,7 @@ class Elements: for body in self.world.GetBodyList(): del body.userData['saveid'] #remove temporary data - def json_load(self, path, additional_vars = {}): + def json_load(self, path, serialized=False): import json self.world.GetGroundBody().userData = {"saveid" : 0} @@ -577,8 +590,24 @@ class Elements: jointDef.maxMotorTorque = joint['maxMotorTorque'] self.world.CreateJoint(jointDef) + self.additional_vars = {} + addvars = {} for (k,v) in worldmodel['additional_vars'].items(): - additional_vars[k] = v + addvars[k] = v + + if serialized: + trackinfo = addvars['trackinfo'] + for key, info in trackinfo.iteritems(): + if not info[3]: + addvars['trackinfo'][key][0] = \ + self.getBodyWithSaveId(info[0]) + addvars['trackinfo'][key][1] = \ + self.getBodyWithSaveId(info[1]) + else: + addvars['trackinfo'][key][0] = None + addvars['trackinfo'][key][1] = None + + self.additional_vars = addvars for body in self.world.GetBodyList(): del body.userData['saveid'] #remove temporary data diff --git a/physics.py b/physics.py index c5f161e..6ce600a 100644 --- a/physics.py +++ b/physics.py @@ -83,7 +83,11 @@ class PhysicsGame: def write_file(self, path): #Saving to journal self.world.add.remove_mouseJoint() - self.world.json_save(path) + additional_data = { + "trackinfo" : self.trackinfo, + "full_pos_list" : self.full_pos_list + } + self.world.json_save(path, additional_data, serialize=True) def read_file(self, path): #Loading from journal @@ -117,7 +121,9 @@ class PhysicsGame: if self.opening_queue: path = self.opening_queue.encode('ascii', 'convert') if os.path.exists(path): - self.world.json_load(path) + self.world.json_load(path, serialized=True) + self.full_pos_list = self.world.additional_vars['full_pos_list'] + self.trackinfo = self.world.additional_vars['trackinfo'] while self.loop: while gtk.events_pending(): @@ -157,27 +163,28 @@ class PhysicsGame: for key, info in self.trackinfo.iteritems(): - body = info[1] # Format - [host_body, tracker, color] - trackdex = body.userData['track_index'] - def to_screen(pos): - px = self.world.meter_to_screen( - pos[0]) - py = self.world.meter_to_screen( - pos[1]) - py = self.world.renderer.get_surface() \ - .get_height() - py - return (px, py) - - x = body.position.x - y = body.position.y - tupled_pos = to_screen((x, y)) - posx = tupled_pos[0] - posy = tupled_pos[1] - try: - self.full_pos_list[trackdex].append(posx) - self.full_pos_list[trackdex].append(posy) - except IndexError: - self.full_pos_list.append([posx, posy]) + body = info[1] # [host_body, tracker, color, destroyed?] + if info[3] is False: # Not destroyed the pen + trackdex = info[4] + def to_screen(pos): + px = self.world.meter_to_screen( + pos[0]) + py = self.world.meter_to_screen( + pos[1]) + py = self.world.renderer.get_surface() \ + .get_height() - py + return (px, py) + + x = body.position.x + y = body.position.y + tupled_pos = to_screen((x, y)) + posx = tupled_pos[0] + posy = tupled_pos[1] + try: + self.full_pos_list[trackdex].append(posx) + self.full_pos_list[trackdex].append(posy) + except IndexError: + self.full_pos_list.append([posx, posy]) for body in self.world.world.GetBodyList(): if type(body.userData) == type({}): diff --git a/tools.py b/tools.py index 4861485..190b558 100644 --- a/tools.py +++ b/tools.py @@ -87,14 +87,17 @@ class Tool(object): # Default drawing method is draw the pen points. full_pos_list = self.game.full_pos_list surface = self.game.world.renderer.get_surface() - for i, pos_list in enumerate(full_pos_list): - dictkey = "pen{0}".format(i) - color = self.game.trackinfo[dictkey][2] - - for i in range(0, len(pos_list), 2): - posx = int(pos_list[i]) - posy = int(pos_list[i+1]) - pygame.draw.circle(surface, color, (posx, posy), 2) + for key, info in self.game.trackinfo.iteritems(): + color = info[2] + trackdex = info[4] + try: + pos_list = self.game.full_pos_list[trackdex] + for i in range(0, len(pos_list), 2): + posx = int(pos_list[i]) + posy = int(pos_list[i+1]) + pygame.draw.circle(surface, color, (posx, posy), 2) + except IndexError: + pass def cancel(self): # Default cancel doesn't do anything @@ -596,11 +599,22 @@ class DestroyTool(Tool): tokill = self.game.world.get_bodies_at_pos(tuple_to_int(event.pos)) if tokill: + tracklist = self.game.trackinfo.items() + destroyed_body = False + for key, info in tracklist: + trackdex = info[4] + if trackdex in tokill[0].userData['track_indices'] and \ + info[3] is False: + self.game.world.world.DestroyBody(info[1]) + self.game.trackinfo[key][3] = True + destroyed_body = True + break + jointnode = tokill[0].GetJointList() - if jointnode: + if jointnode and not destroyed_body: joint = jointnode.joint self.game.world.world.DestroyJoint(joint) - else: + elif not destroyed_body: self.game.world.world.DestroyBody(tokill[0]) elif event.type == MOUSEBUTTONUP and event.button == 1: self.cancel() @@ -695,10 +709,17 @@ class TrackTool(Tool): self.game.world.add.joint( track_circle, current_body, point_pos, point_pos, False) - self.game.trackinfo[dictkey] = [0, 1, 2] + if 'track_indices' in current_body.userData: + current_body.userData['track_indices'].append(trackdex) + else: + current_body.userData['track_indices'] = [trackdex] + + self.game.trackinfo[dictkey] = [0, 1, 2, 4, 5] self.game.trackinfo[dictkey][0] = current_body self.game.trackinfo[dictkey][1] = track_circle self.game.trackinfo[dictkey][2] = color + self.game.trackinfo[dictkey][3] = False # Pen destroyed or not. + self.game.trackinfo[dictkey][4] = trackdex # Tracking index. self.game.tracked_bodies += 1 # counter of tracked bodies. -- cgit v0.9.1