Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools.py
blob: 1302d8c15afce6da25b6b37309b8dd785b170009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#==================================================================
#                           Physics.activity
#                              Tool Classes
#                           By Alex Levenson
#==================================================================
import pygame
from elements import box2d
from pygame.locals import *
from helpers import *
from inspect import getmro
# tools that can be used superlcass
class Tool(object):
    name = "Tool"
    icon = "icon"
    toolTip = "Tool Tip"
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Tool"
    def handleEvents(self,event):
        handled = True
        # default event handling
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            # bye bye! Hope you had fun!
            self.game.running = False
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                #space pauses
                self.game.world.run_physics = not self.game.world.run_physics  
            elif event.key == K_t:
                self.game.setTool("triangle")
            elif event.key == K_b:
                self.game.setTool("box")
            elif event.key == K_c:
                self.game.setTool("circle")
            elif event.key == K_j:
                self.game.setTool("joint")
            elif event.key == K_p:
                self.game.setTool("polygon")
            elif event.key == K_g:
                self.game.setTool("grab")
            elif event.key == K_d:
                self.game.setTool("destroy")
            elif event.key == K_m:
                self.game.setTool("magicpen")
            #elif event.key == K_g:
            #    self.game.setTool("gear")
            # Game/joystick-related keys
            elif event.key == K_KP4: # Left gamepad, left arrow
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(9000) 
                elif self.game.debug: print "Left gamepad left arrow error: no joystick object selected"
            elif event.key == K_KP6: # Left gamepad, right arrow
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(9000) 
                elif self.game.debug: print "Left gamepad right arrow error: no joystick object selected"
            elif event.key == K_KP8: # Left gamepad, up arrow
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(9000) 
                elif self.game.debug: print "Left gamepad up arrow error: no joystick object selected"
            elif event.key == K_KP2: # Left gamepad, down arrow
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(9000) 
                elif self.game.debug: print "Left gamepad down arrow error: no joystick object selected"
            elif event.key == K_KP7: # Right gamepad, square
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(-9000) 
                elif self.game.debug: print "Right gamepad square button error: no joystick object selected"
            elif event.key == K_KP1: # Right gamepad, check
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(-9000) 
                elif self.game.debug: print "Right gamepad check button error: no joystick object selected"
            elif event.key == K_KP9: # Right gamepad, circle
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(-9000) 
                elif self.game.debug: print "Right gamepad circle button error: no joystick object selected"
            elif event.key == K_KP3: # Right gamepad, X
                if self.game.joystickobject:
                    self.game.joystickobject[0].ApplyTorque(-9000) 
                elif self.game.debug: print "Right gamepad X button error: no joystick object selected"
        elif event.type == USEREVENT:
            if hasattr(event,"action"):
                if self.game.toolList.has_key(event.action): self.game.setTool(event.action)
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            self.game.canvas.grab_focus()
            handled = False
        else:
            handled = False
        return handled                                     
    def draw(self):
        # default drawing method is don't draw anything
        pass
    def cancel(self):
        # default cancel doesn't do anything
        pass

    def to_b2vec(self, pt):
        return self.game.world.add.to_b2vec(pt)

# The circle creation tool        
class CircleTool(Tool): 
    name = "circle"
    icon = "circle"
    toolTip = "Circle"
   
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Circle"
        self.pt1 = None
        self.radius = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(CircleTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:                    
                    if self.radius > 1: # elements doesn't like tiny shapes :(
                        self.game.world.add.ball(self.pt1,self.radius, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                    self.pt1 = None        
                    self.radius = None            
    def draw(self):
        # draw a circle from pt1 to mouse
        if self.pt1 != None:
            self.radius = distance(self.pt1,pygame.mouse.get_pos())
            if self.radius > 3: 
                thick = 3
            else:
                thick = 0
            pygame.draw.circle(self.game.screen, (100,180,255),self.pt1,self.radius,thick) 
            pygame.draw.line(self.game.screen,(100,180,255),self.pt1,pygame.mouse.get_pos(),1)  
    def cancel(self):
        self.pt1 = None  
        self.radius = None
   
# The box creation tool        
class BoxTool(Tool):    
    name = "box"
    icon = "box"
    toolTip = "Box"

    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Box"
        self.pt1 = None        
        self.rect = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(BoxTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1 and self.pt1!=None:                                 
                    if self.rect.width > 10 and self.rect.height > 10: # elements doesn't like small shapes :(
                        self.game.world.add.rect(self.rect.center, self.rect.width/2, self.rect.height/2, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                   
                    self.pt1 = None        
                                
    def draw(self):
        # draw a box from pt1 to mouse
        if self.pt1 != None:
            width = pygame.mouse.get_pos()[0] - self.pt1[0]
            height = pygame.mouse.get_pos()[1] - self.pt1[1]
            self.rect = pygame.Rect(self.pt1, (width, height))
            self.rect.normalize()           
            pygame.draw.rect(self.game.screen, (100,180,255),self.rect,3)   
    def cancel(self):
        self.pt1 = None  
        self.rect = None      
           
# The triangle creation tool        
class TriangleTool(Tool): 
    name = "triangle"
    icon = "triangle"
    toolTip = "Triangle"
   
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Triangle"
        self.pt1 = None
        self.vertices = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(TriangleTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1 and self.pt1!= None:                    
                    if distance(self.pt1,pygame.mouse.get_pos()) > 15: # elements doesn't like tiny shapes :(
                        self.game.world.add.convexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                    self.pt1 = None
                    self.vertices = None                    
    def draw(self):
        # draw a triangle from pt1 to mouse
        if self.pt1 != None:
            self.vertices = constructTriangleFromLine(self.pt1,pygame.mouse.get_pos())
            pygame.draw.polygon(self.game.screen, (100,180,255),self.vertices, 3) 
            pygame.draw.line(self.game.screen,(100,180,255),self.pt1,pygame.mouse.get_pos(),1)  
    
    def cancel(self):
        self.pt1 = None        
        self.vertices = None

# The Polygon creation tool        
class PolygonTool(Tool):  
    name = "polygon"
    icon = "polygon"
    toolTip = "Polygon"
  
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Polygon"
        self.vertices = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(PolygonTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if not self.vertices:
                        self.vertices=[event.pos]  
                    elif distance(event.pos,self.vertices[0]) < 15:                     
                        #self.vertices.append(self.vertices[0]) #connect the polygon
                        gons = decomposePoly(self.vertices)
                        for g in gons:
                            self.game.world.add.convexPoly(g, dynamic=True, density=1.0, restitution=0.16, friction=0.5)
                        #self.game.world.add.complexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                        self.vertices = None  
                    else:
                        self.vertices.append(event.pos)
                if event.button == 3:
                    if self.vertices:
                        self.vertices.append(event.pos)
                        self.game.world.add.complexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)
                        self.vertices = None
                                
    def draw(self):
        # draw the poly being created
        if self.vertices:
            for i in range(len(self.vertices)-1):
                pygame.draw.line(self.game.screen,(100,180,255),self.vertices[i],self.vertices[i+1],3)
            pygame.draw.line(self.game.screen,(100,180,255),self.vertices[-1],pygame.mouse.get_pos(),3)
            pygame.draw.circle(self.game.screen,(100,180,255),self.vertices[0],15,3)  
    
    def cancel(self):       
        self.vertices = None

# The magic pen tool        
class MagicPenTool(Tool):
    name = "magicpen"
    icon = "magicpen"
    toolTip = "Magic Pen"
    
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Magic Pen"
        self.vertices = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(MagicPenTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if not self.vertices:
                        self.vertices=[event.pos]  
                    elif distance(event.pos,self.vertices[0]) < 15:                     
                        self.vertices.append(self.vertices[0]) #connect the polygon
                        self.game.world.add.complexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                        self.vertices = None  
                    else:
                        self.vertices.append(event.pos)
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:
                        if self.vertices:
                            self.game.world.add.complexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                        self.vertices = None
            elif event.type == MOUSEMOTION:
                if self.vertices:
                    self.vertices.append(event.pos)
                                
    def draw(self):
        # draw the poly being created
        if self.vertices:
            for i in range(len(self.vertices)-1):
                pygame.draw.line(self.game.screen,(100,180,255),self.vertices[i],self.vertices[i+1],3)
            pygame.draw.line(self.game.screen,(100,180,255),self.vertices[-1],pygame.mouse.get_pos(),3)
            pygame.draw.circle(self.game.screen,(100,180,255),self.vertices[0],15,3)  
    
    def cancel(self):       
        self.vertices = None

# The grab tool        
class GrabTool(Tool):
    name = "grab"
    icon = "grab"
    toolTip = "Grab"
    
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Grab"
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(GrabTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    # grab the first object at the mouse pointer
                    bodylist = self.game.world.get_bodies_at_pos(event.pos, include_static=False) 
                    if bodylist and len(bodylist) > 0:
                        self.game.world.add.mouseJoint(bodylist[0], event.pos)               
            elif event.type == MOUSEBUTTONUP:
                # let it go
                if event.button == 1:
                    self.game.world.add.remove_mouseJoint()
            # use box2D mouse motion
            elif event.type == MOUSEMOTION and event.buttons[0]:
                self.game.world.mouse_move(event.pos)
            
    def cancel(self):
        self.game.world.add.remove_mouseJoint()                        
    
# The joint tool        
class JointTool(Tool):
    name = "joint"
    icon = "joint"
    toolTip = "Joint"
    
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Joint"
        self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(JointTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button >= 1:
                    # grab the first body
                    self.jb1pos = event.pos
                    self.jb1 = self.game.world.get_bodies_at_pos(event.pos)
                    self.jb2 = self.jb2pos = None
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:
                    # grab the second body
                    self.jb2pos = event.pos
                    self.jb2 = self.game.world.get_bodies_at_pos(event.pos)
                    # if we have two distinct bodies, add a distance joint!
                    if self.jb1 and self.jb2 and str(self.jb1) != str(self.jb2):
                        self.game.world.add.distanceJoint(self.jb1[0],self.jb2[0],self.jb1pos,self.jb2pos)                    
                    # If there's only one body, add a fixed joint
                    elif self.jb2:
                        self.game.world.add.fixedJoint(self.jb2[0],self.jb2pos)
                    # regardless, clean everything up
                    self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None
                if event.button == 3:
                    # add a centered fixed joint
                    '''self.jb2 = self.game.world.get_bodies_at_pos(event.pos)
                    if self.jb2:
                        self.game.world.add.fixedJoint(self.jb2[0])
                    # regardless, clean everything up
                    self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None'''
                    if self.jb1:
                        self.game.world.add.motor(self.jb1[0],self.jb1pos)
                    self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None
    def draw(self):
        if self.jb1:
            pygame.draw.line(self.game.screen,(100,180,255),self.jb1pos,pygame.mouse.get_pos(),3)
    
    def cancel(self):
        self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None             
        
# The destroy tool        
class DestroyTool(Tool):
    name = "destroy"
    icon = "destroy"
    toolTip = "Destroy"
    
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Destroy"
        self.vertices = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(DestroyTool,self).handleEvents(event):
            if pygame.mouse.get_pressed()[0]:
                if not self.vertices: self.vertices = []
                self.vertices.append(pygame.mouse.get_pos())
                if len(self.vertices) > 10:
                    self.vertices.pop(0)
                tokill = self.game.world.get_bodies_at_pos(pygame.mouse.get_pos())
                if tokill:                        
                        self.game.world.world.DestroyBody(tokill[0])
            elif event.type == MOUSEBUTTONUP and event.button == 1:
                self.cancel()
    def draw(self):
        # draw the trail
        if self.vertices:
            if len(self.vertices) > 1:
                pygame.draw.lines(self.game.screen,(255,0,0),False,self.vertices,3)

    def cancel(self):
        self.vertices = None     

 # The joystick tool        
class JoystickTool(Tool):
    name = "joystick"
    icon = "magicpen" # for now
    toolTip = "Joystick"

    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Joystick"
        self.vertices = None
        #self.joystickobject
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        super(JoystickTool,self).handleEvents(event)
        if pygame.mouse.get_pressed()[0]:
            if not self.vertices:
                self.vertices = []
                self.vertices.append(pygame.mouse.get_pos())
            if len(self.vertices) > 10:
                self.vertices.pop(0)
            self.game.joystickobject = self.game.world.get_bodies_at_pos(pygame.mouse.get_pos())
            if self.game.joystickobject:
                print "hi1"                
        #self.game.world.world.DestroyBody(self.joystickobject[0])
        elif event.type == MOUSEBUTTONUP and event.button == 1:
            self.cancel()
    def draw(self):
        # draw the trail
        if self.vertices:
            if len(self.vertices) > 1:
                pygame.draw.lines(self.game.screen,(255,0,0),False,self.vertices,3)

    def cancel(self):
        self.vertices = None


# The joint tool        
class BridgeJointTool(Tool):
    name = "bridgejoint"
    icon = "joint"
    toolTip = "Bridge Joint"
    
    def __init__(self,gameInstance):
        self.game = gameInstance
        self.name = "Bridge Joint"
        self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if super(BridgeJointTool,self).handleEvents(event):
            return
        if event.type != MOUSEBUTTONUP or event.button != 1:
            return

        print "mouse button up"
        bodies = self.game.world.get_bodies_at_pos(event.pos)
        if not bodies or len(bodies) != 2:
            return
        
        print bodies[0]
        print bodies[1]
        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(bodies[0], bodies[1], self.to_b2vec(event.pos))
        joint = self.game.world.world.CreateJoint(jointDef)
        self.game.bridge.joint_added(joint)

    def draw(self):
        return
    
    def cancel(self):
        self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None  

def getAllTools():
    this_mod = __import__(__name__)
    all = [val for val in this_mod.__dict__.values() if isinstance(val, type)]
    allTools = []
    for a in all:
        if getmro(a).__contains__(Tool) and a!= Tool: allTools.append(a)
    return allTools
            

allTools = getAllTools()