-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
641 lines (493 loc) · 18.1 KB
/
Copy pathtools.py
File metadata and controls
641 lines (493 loc) · 18.1 KB
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import pymunk as cymunk
from pymunk import Vec2d
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Ellipse, Color, Line, Rectangle, Triangle
from kivy.utils import get_color_from_hex
from kivy.lang import Builder
Builder.load_file('kv/tools.kv')
import utils
LINE_WIDTH = 1.1
MOTOR_VELOCITY = -20
class Tool:
name = "default"
icon = "resources/wrench.png"
def __init__(self, game):
self.game = game
def draw(self,x, y):
pass
def on_touch_down(self, touch):
pass
def on_touch_up(self, touch):
pass
def on_touch_move(self, touch):
self.draw(touch.x, touch.y)
class CircleTool(Tool):
name = "Circle"
icon = "resources/circle.svg"
help_text = "Drag and draw!"
def __init__(self, game):
self.game = game
self.draw_circle = None
self.draw_line = None
self.init_pos = None
self.final_pos = None
self.saved = None
def draw(self, x, y):
d = int(utils.distance((x, y), self.init_pos))
x1, y1 = self.init_pos
if self.draw_circle is not None:
self.draw_circle.pos = (x1-d, y1-d)
self.draw_circle.size = (2*d, 2*d)
self.draw_line.points = [x1, y1, x, y]
else:
with self.game.canvas:
self.color = utils.random_color()
Color(*self.color, mode="rgba")
self.draw_circle = \
Ellipse(pos=(x+d, y+d),
size=(d/2, d/2))
Color(*get_color_from_hex("#33B5E5"))
self.draw_line = \
Line(points=[x1, y1, x, y],
width=LINE_WIDTH, cap="round")
def on_touch_down(self, touch):
self.init_pos = touch.x, touch.y
def on_touch_up(self, touch):
if self.init_pos is None:
return
self.final_pos = touch.x, touch.y
x1, y1 = self.init_pos
d = int(utils.distance(self.final_pos, self.init_pos))
if not (d<10):
self.game.renderer.add_circle(x1, y1, d, self.color)
self.saved = d
elif self.saved is not None:
# Add saved one
d = self.saved
x1, y1 = touch.x, touch.y
self.game.renderer.add_circle(x1, y1, d, self.color)
if self.draw_circle is not None and self.draw_line is not None:
self.game.canvas.remove(self.draw_circle)
self.game.canvas.remove(self.draw_line)
self.draw_circle = None
self.draw_line = None
self.init_pos = None
self.final_pos = None
class RectangleTool(Tool):
name = "Rectangle"
icon = "resources/circle.svg"
help_text = "Drag and draw!"
def __init__(self, game):
self.game = game
self.draw_rectangle = None
self.draw_line = None
self.init_pos = None
self.saved = None
def draw(self, x, y):
d = int(utils.distance((x, y), self.init_pos))
x1, y1 = self.init_pos
if self.draw_rectangle is not None:
hw = abs(x1-x)
hh = abs(y1-y)
self.draw_rectangle.pos = (x1-hw, y1-hh)
self.draw_rectangle.size = (2*hw, 2*hh)
self.draw_line.points = [x1, y1, x, y]
else:
with self.game.canvas:
self.color = utils.random_color()
Color(*self.color, mode="rgba")
self.draw_rectangle = \
Rectangle(pos=(x-d, y-d),
size=(d/2, d/2))
Color(*get_color_from_hex("#33B5E5"), mode="rgba")
self.draw_line = \
Line(points=[x1, y1, x, y],
width=LINE_WIDTH, cap="round")
def on_touch_down(self, touch):
self.init_pos = touch.x, touch.y
def on_touch_up(self, touch):
if self.init_pos is None:
return
x, y = touch.x, touch.y
x1, y1 = self.init_pos
hw = abs(x1-x)
hh = abs(y1-y)
if not (hw<5 or hh<5):
self.game.renderer.add_box(x1, y1, 2*hw, 2*hh, self.color)
self.saved = (hw, hh)
elif self.saved is not None:
# Add saved one
hw, hh = self.saved
x1, y1 = touch.x, touch.y
self.game.renderer.add_box(x1, y1, 2*hw, 2*hh, self.color)
if self.draw_rectangle is not None and self.draw_line is not None:
self.game.canvas.remove(self.draw_rectangle)
self.game.canvas.remove(self.draw_line)
self.draw_rectangle = None
self.draw_line = None
self.init_pos = None
self.final_pos = None
class TriangleTool(Tool):
name = """Triangle
[color=ff0000]Experimental[/color]"""
icon = "resources/circle.svg"
help_text = "Drag and draw!"
def __init__(self, game):
self.game = game
self.draw_altitude = None
self.draw_triangle = None
self.init_pos = None
self.final_pos = None
self.saved = None
def draw(self, x, y):
x1, y1 = self.init_pos
if self.draw_triangle is not None:
mouse_x_y = x, y
vertices = utils.constructTriangleFromLine(self.init_pos, mouse_x_y)
self.draw_triangle.points = vertices
self.draw_altitude.points = [x, y, x1, y1]
else:
with self.game.canvas:
self.color = utils.random_color()
Color(*self.color, mode="rgba")
self.draw_triangle = Triangle(
points=utils.get_triangle_points(x1, y1, x, y))
Color(*utils.random_color(), mode="rgba")
self.draw_altitude = \
Line(points=[x1, y1, x, y],
width=LINE_WIDTH, cap="square")
def on_touch_down(self, touch):
self.init_pos = touch.x, touch.y
def on_touch_up(self, touch):
if self.init_pos is None:
return
self.final_pos = touch.x, touch.y
x1, y1 = self.init_pos
d = int(utils.distance(self.final_pos, self.init_pos))
mouse_x_y = touch.x, touch.y
if d>20:
vertices = utils.constructTriangleFromLine(self.init_pos,
mouse_x_y)
self.game.renderer.add_triangle(vertices, self.color)
self.saved = d
elif self.saved is not None:
vertices = utils.constructTriangleFromLine(
self.init_pos,
(self.init_pos[0], self.init_pos[1]-self.saved))
self.game.renderer.add_triangle(vertices, self.color)
if self.draw_altitude is not None and self.draw_triangle is not None:
self.game.canvas.remove(self.draw_altitude)
self.game.canvas.remove(self.draw_triangle)
self.draw_altitude = None
self.draw_triangle = None
self.init_pos = None
self.final_pos = None
class PinTool(Tool):
name = "Pin"
icon = "resources/pin.png"
help_text = "Click and pin the object to the place!"
def __init__(self, game):
self.game = game
def draw(self, x, y):
pass
def on_touch_down(self, touch):
x, y = touch.x, touch.y
space = self.game.get_space()
shape = space.point_query_first(Vec2d(x, y))
if shape is None:
# print "lel u iz mudi egent or wat? i wll roit ur houze fr da lulz."+\
# " heuhuehhue"
pass
else:
body = shape.body
joint = cymunk.constraint.PivotJoint(body,
space.static_body,
Vec2d(x, y))
space.add(joint)
class MotorTool(Tool):
name = "Motor"
icon = "resources/pin.png"
help_text = "Click and see it spin!"
def __init__(self, game):
self.game = game
def draw(self, x, y):
pass
def on_touch_down(self, touch):
x, y = touch.x, touch.y
space = self.game.get_space()
shape = space.point_query_first(Vec2d(x, y))
if shape is None:
# print "lel u iz mudi egent or wat? i wll roit ur houze fr da lulz."+\
# " heuhuehhue"
pass
else:
body = shape.body
joint = cymunk.constraint.SimpleMotor(body,
space.static_body,
MOTOR_VELOCITY)
joint2 = cymunk.constraint.PivotJoint(body,
space.static_body,
Vec2d(x, y))
space.add([joint, joint2])
class JointTool(Tool):
name = "Joint"
icon = "resources/joint.png"
help_text = "Attach two things together!"
def __init__(self, game):
self.game = game
self.space = self.game.get_space()
self.draw_line = None
self.color = []
self.clean_up()
def draw(self, x, y):
if self.draw_line is not None:
self.draw_line.points = [self.init_pos[0], self.init_pos[1], x, y]
elif self.init_pos:
with self.game.canvas.after:
self.color = utils.random_color()
# print "drawing tool:", self.color
Color(*self.color, mode="rgba")
self.draw_line = \
Line(points=[self.init_pos[0], self.init_pos[1], x, y],
width=LINE_WIDTH)
def on_touch_down(self, touch):
shape = self.space.point_query_first(Vec2d(touch.x, touch.y))
if shape is None:
# print "lel u iz mudi egent or wat? i wll roit ur houze fr da lulz."+\
# " heuhuehhue"
self.clean_up()
else:
self.init_pos = (touch.x, touch.y)
self.body1 = shape.body
def on_touch_up(self, touch):
shape = self.space.point_query_first(Vec2d(touch.x, touch.y))
if shape is None:
# print "lel u iz mudi egent or wat? i wll roit ur houze fr da lulz."+\
# " heuhuehhue"
self.clean_up()
else:
self.final_pos = (touch.x, touch.y)
self.body2 = shape.body
if self.init_pos is None:
self.clean_up()
return
if utils.distance(self.init_pos, self.final_pos)<10:
self.clean_up()
return
if self.body1 is self.body2:
self.clean_up()
return
ix, iy = self.init_pos
fx, fy = self.final_pos
x1, y1 = self.body1.position.x, self.body1.position.y
x2, y2 = self.body2.position.x, self.body2.position.y
anchr1 = (ix-x1, iy-y1)
anchr2 = (fx-x2, fy-y2)
joint = cymunk.PinJoint(
self.body1, self.body2,
anchr1, anchr2
)
joint.color = self.color
self.space.add(joint)
self.clean_up()
def clean_up(self):
self.init_pos = None
self.body1 = None
self.final_pos = None
self.body2 = None
if self.draw_line is not None:
self.game.canvas.after.remove(self.draw_line)
# print "Removed that shitty line"
self.draw_line = None
class GrabTool(Tool):
name = "Grab"
icon = "resources/grab.png"
help_text = "Drag things around (Maybe shaky)"
def __init__(self, game):
self.game = game
self.space = self.game.get_space()
self.clean_up()
def draw(self, x, y):
pass
def on_touch_down(self, touch):
cpos = Vec2d(touch.x, touch.y)
space = self.game.get_space()
shape = space.point_query_first(cpos)
if shape is not None:
body = shape.body
x1, y1 = cpos.int_tuple
x2, y2 = body.position.int_tuple
self.delta = (x1-x2, y1-y2)
self.current_body = body
def on_touch_up(self, touch):
for b in self.space.bodies:
b.activate()
self.clean_up()
def on_touch_move(self, touch):
if self.delta is None or self.current_body is None:
return
x, y = touch.x, touch.y
dx, dy = self.delta
posx = x-dx
posy = y-dy
self.current_body.position.x = posx
self.current_body.position.y = posy
def clean_up(self):
self.delta = None
self.current_body = None
class AntiGravityTool(Tool):
name = "Anti Gravity"
icon = "resources/gravity.png"
help_text = "import antigravity"
def __init__(self, game):
self.game = game
def click_button_cb(self):
gnow = self.game.get_space().gravity[1]
self.game.get_space().gravity = (0, -gnow)
# print gnow
for b in self.game.get_space().bodies:
b.activate()
class EraserTool(Tool):
name = "Eraser"
icon = "resources/pin.png"
help_text = "Drag and destroy everything in your path!"
def __init__(self, game):
self.game = game
self.draw_vertices = []
self.draw_line = None
def draw(self):
if self.draw_line is None:
with self.game.canvas:
Color(*utils.random_color(), mode="rgba")
self.draw_line = Line(points=self.draw_vertices,
width=3)
else:
self.draw_line.points = self.draw_vertices
def on_touch_up(self, touch):
self.draw_vertices = []
if self.draw_line: self.game.canvas.remove(self.draw_line)
self.draw_line = None
def on_touch_down(self, touch):
x, y = touch.x, touch.y
space = self.game.get_space()
shape = space.point_query_first(Vec2d(x, y))
if shape is None:
pass
else:
body = shape.body
if not hasattr(body, 'data'):
return
data = body.data
check_joints = False
j = None
for c in space.constraints[::-1]:
if c.a is body or c.b is body:
check_joints = True
j = c
if not isinstance(j, cymunk.constraint.SimpleMotor):
self.game.canvas.remove(
self.game.renderer.joints_drawn[j])
del self.game.renderer.joints_drawn[j]
break
if check_joints:
space.remove(j)
else:
# print "Nope"
for s in data["shapes"]:
space.remove(s)
for ins in data["instruction"]:
self.game.canvas.before.remove(ins)
space.remove(body)
def on_touch_move(self, touch):
if len(self.draw_vertices)>18:
self.draw_vertices.pop(0)
self.draw_vertices.pop(0)
self.draw_vertices.extend([touch.x, touch.y])
if len(self.draw_vertices)>2:
self.draw()
self.on_touch_down(touch)
class EraseAllTool(Tool):
name = "Erase All"
icon = "resources/gravity.png"
help_text = "Destroyed them all!"
def __init__(self, game):
self.game = game
def click_button_cb(self):
space = self.game.get_space()
for b in space.bodies:
# print b
if b is not space.static_body:
ins = b.data["instruction"]
for i in ins:
self.game.canvas.before.remove(i)
space.remove(b)
for c in space.constraints:
if c in self.game.renderer.joints_drawn:
self.game.canvas.remove(self.game.renderer.joints_drawn[c])
del self.game.renderer.joints_drawn[c]
space.remove(c)
for s in space.shapes:
if s.body is not space.static_body: space.remove(s)
self.game.renderer.update_bounds(new=True)
all_tools = [
CircleTool,
RectangleTool,
TriangleTool,
GrabTool,
PinTool,
MotorTool,
JointTool,
AntiGravityTool,
EraserTool,
EraseAllTool
]
from kivy.properties import ObjectProperty
class SublimeButton(Button):
on_press1 = ObjectProperty(lambda *a: a)
def_color = [1, 0.7, 0, 0]
def __init__(self, **kw):
super(SublimeButton, self).__init__(**kw)
def activated(self):
self.background_color = \
[0.2, 0.7098039215686275, 0.8980392156862745, 0.7]
def deactivated(self):
self.on_press1()
self.background_color = self.def_color
from functools import partial
class ToolBox(BoxLayout):
def __init__(self, **kw):
super(ToolBox, self).__init__(**kw)
self.scroller = ScrollView(size_hint=(1, 1))
self.scroll_box = GridLayout(rows=1, size_hint=(None, 1))
self.scroll_box.bind(
minimum_width=self.scroll_box.setter('width'))
pause_res = SublimeButton(text="Pause")
pause_res.on_press1 = self.toogle_game_play_pause
self.pause_res = pause_res
self.scroll_box.add_widget(pause_res)
for tool in all_tools:
b = SublimeButton()
b.text = tool.name
b.on_press1=partial(self.button_cb, tool.name)
self.scroll_box.add_widget(b)
self.scroller.add_widget(self.scroll_box)
self.add_widget(self.scroller)
def button_cb(self, tool_name, *args):
game = self.parent.parent.ids.game
if hasattr(game.tools[tool_name], 'click_button_cb'):
game.tools[tool_name].click_button_cb()
else:
game.set_tool(tool_name)
game.show_help_text(game.tools[tool_name].help_text)
def toogle_game_play_pause(self, *args):
game = self.parent.parent.ids.game
game.toggle_game_state()
if game.game_paused()==1:
self.pause_res.text = "Pause"
else:
self.pause_res.text = "Resume"