forked from jmoenig/morphic.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphic.py
executable file
·2877 lines (2412 loc) · 90 KB
/
morphic.py
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# morphic.py
#
# a tree-based GUI for Python
# inspired by Squeak
#
# written by Jens Mönig
# jens@moenig.org
#
# version 2009-Nov-06
#
# Copyright (C) 2009 by Jens Mönig
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# yet to implement:
# - make World customizable before opening it
# - scroll bars
# - tool tips
# - skinnables
# - turtle trails
# - better string editing
# - multi-line edits
import pygame
import copy
import math
pygame.init()
version = '2009-Nov-06'
TRANSPARENT = pygame.Color(1, 1, 1)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return self.x.__str__() + '@' + self.y.__str__()
#Point comparison:
def __eq__(self, other):
if isinstance(other, Point):
return self.x == other.x and self.y == other.y
return False
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
if isinstance(other, Point):
return self.x < other.x and self.y < other.y
return NotImplemented
def __gt__(self, other):
if isinstance(other, Point):
return self.x > other.x and self.y > other.y
return NotImplemented
def __le__(self, other):
if isinstance(other, Point):
return self.x <= other.x and self.y <= other.y
return NotImplemented
def __ge__(self, other):
if isinstance(other, Point):
return self.x >= other.x and self.y >= other.y
return NotImplemented
def __round__(self):
return Point(round(self.x), round(self.y))
def max(self, other):
return Point(max(self.x, other.x), max(self.y, other.y))
def min(self, other):
return Point(min(self.x, other.x), min(self.y, other.y))
#Point arithmetic:
def __add__(self, other):
if isinstance(other, Point):
return Point(self.x + other.x, self.y + other.y)
return Point(self.x + other, self.y + other)
def __sub__(self, other):
if isinstance(other, Point):
return Point(self.x - other.x, self.y - other.y)
return Point(self.x - other, self.y - other)
def __mul__(self, other):
if isinstance(other, Point):
return Point(self.x * other.x, self.y * other.y)
return Point(self.x * other, self.y * other)
def __div__(self, other):
if isinstance(other, Point):
return Point(self.x / other.x, self.y / other.y)
return Point(self.x / other, self.y / other)
def __floordiv__(self, other):
if isinstance(other, Point):
return Point(self.x // other.x, self.y // other.y)
return Point(self.x // other, self.y // other)
def __abs__(self):
return Point(abs(self.x), abs(self.y))
def __neg__(self):
return Point(-self.x, -self.y)
#Point functions:
def dot_product(self, other):
return self.x * other.x + self.y * other.y
def cross_product(self, other):
return self.x * other.y - self.y * other.x
def distance_to(self, other):
return (other - self).r()
def rotate(self, direction, center):
"direction must be 'right', 'left' or 'pi'"
offset = self - center
if direction == 'right':
return Point(-offset.y, offset,y) + center
elif direction == 'left':
return Point(offset.y, -offset.y) + center
elif direction == 'pi':
return center - offset
else:
return NotImplemented
def flip(self, directon, center):
"direction must be 'vertical' or 'horizontal'"
if direction == 'vertical':
return Point(self.x, center.y * 2 - self.y)
elif direction == 'horizontal':
return Point(center,x * 2 - self.x, self.y)
else:
return NotImplemented
#Point polar coordinates:
def r(self):
return math.sqrt(self.dot_product(self))
#Point transforming:
def scale_by(self, scalePoint):
return Point(scalePoint.x * self.x, scalePoint.y * self.y)
def translate_by(self, deltaPoint):
return Point(deltaPoint.x + self.x, deltaPoint.y + self.y)
#Point converting:
def as_list(self):
return [self.x, self.y]
def corner(self, cornerPoint):
return Rectangle(self, cornerPoint)
def rectangle(self, aPoint):
return Rectangle(self.min(aPoint), self.max(aPoint))
def extent(self, extentPoint):
return Rectangle(self, self + extentPoint)
class Rectangle:
def __init__(self, origin, corner):
self.origin = origin
self.corner = corner
def __repr__(self):
return ('(' + self.origin.__str__() + ' | '
+ self.corner.__str__() + ')')
#Rectangle accessing - getting:
def area(self):
w = self.width()
if w < 0:
return 0
else:
return max(w * self.height, 0)
def bottom(self):
return self.corner.y
def bottom_center(self):
return Point(self.center().x, self.bottom())
def bottom_left(self):
return Point(self.origin.x, self.corner.y)
def bottom_right(self):
return self.corner
def bounding_box(self):
return self
def center(self):
return (self.top_left() + self.bottom_right()) // 2
def corners(self):
return [self.top_left(),
self.bottom_left(),
self.bottom_right(),
self.top_right()]
def extent(self):
return self.corner - self.origin
def height(self):
return self.corner.y - self.origin.y
def left(self):
return self.origin.x
def left_center(self):
return Point(self.left(), self.center().y)
def right(self):
return self.corner.x
def right_center(self):
return Point(self.right(), self.center().y)
def top(self):
return self.origin.y
def top_center(self):
return Point(self.center().x, self.top())
def top_left(self):
return self.origin
def top_right(self):
return Point(self.corner.x, self.origin.y)
def width(self):
return self.corner.x - self.origin.x
def position(self):
return self.origin
#Rectangle comparison:
def __eq__(self, other):
if isinstance(other, Rectangle):
return (self.origin == other.origin
and self.corner == other.corner)
return False
def __ne__(self, other):
return not self.__eq__(other)
#Rectangle functions:
def inset_by(self, delta):
if isinstance(delta, Rectangle):
return Rectangle(self.origin + delta.origin)
else:
return Rectangle(self.origin + delta, self.corner - delta)
def expand_by(self, integer):
return Rectangle(self.origin - integer, self.corner + integer)
def intersect(self, aRectangle):
return Rectangle(self.origin.max(aRectangle.origin),
self.corner.min(aRectangle.corner))
def merge(self, aRectangle):
return Rectangle(self.origin.min(aRectangle.origin),
self.corner.max(aRectangle.corner))
#Rectangle testing:
def contains_point(self, point):
return self.origin <= point and point < self.corner
def contains_rectangle(self, rectangle):
return (rectangle.origin >= self.origin
and rectangle.corner <= self.corner)
def intersects(self, rectangle):
ro = rectangle.origin
rc = rectangle.corner
if rc.x < self.origin.x:
return False
elif rc.y < self.origin.y:
return False
elif ro.x > self.corner.x:
return False
elif ro.y > self.corner.y:
return False
else:
return True
#Rectangle transforming:
def scale_by(self, scale):
"scale can be either a Point or a scalar"
return Rectangle(self.origin * scale, self.corner * scale)
def translate_by(self, factor):
"factor can be either a Point or a scalar"
return Rectangle(self.origin + factor, self.corner + factor)
#Rectangle converting:
def as_rect(self):
return pygame.Rect(self.left(),
self.top(),
self.width(),
self.height())
class Node(object):
def __init__(self, name = 'node'):
self.parent = None
self.children = []
self.name = name
def __repr__(self):
return 'aNode(' + self.name + ')'
#Node accessing:
def add_child(self, node):
self.children.append(node)
node.parent = self
def remove_child(self, node):
self.children.remove(node)
node.parent = None
#Node functions:
def root(self):
if self.parent == None:
return self
else:
return self.parent.root()
def depth(self):
if self.parent == None:
return 0
else:
return self.parent.depth() + 1
def all_children(self):
"includes myself"
result = [self]
for child in self.children:
result.extend(child.all_children())
return result
def all_leafs(self):
result = []
for element in self.all_children():
if element.children == []:
result.append(element)
return(result)
def all_parents(self):
"includes myself"
result = [self]
if self.parent != None:
result.extend(self.parent.all_parents())
return result
def siblings(self):
result = []
for element in self.parent.children:
if element is not self:
result.append(element)
return result
def parent_of_class(self, aClass):
"answer the first of my parents which is an instance of aClass"
for element in self.all_parents():
if isinstance(element, aClass):
return element
return None
def child_of_class(self, aClass):
"answer the first of my children which is an instance of aClass"
for element in self.all_children():
if isinstance(element, aClass):
return element
return None
class Morph(Node):
def __init__(self):
super(Morph, self).__init__()
self.bounds = Point(0, 0).corner(Point(50,40))
self.color = pygame.Color(80, 80, 80)
self.alpha = 255
self.is_visible = True
self.is_draggable = True
self.draw_new()
self.fps = 0
self.last_time = pygame.time.get_ticks()
def __repr__(self):
return self.__class__.__name__
def delete(self):
if self.parent != None:
self.full_changed()
self.parent.remove_child(self)
#stepping:
def wants_to_step(self):
return self.is_visible
def step_frame(self):
if not self.wants_to_step():
return
current = pygame.time.get_ticks()
elapsed = current - self.last_time
if self.fps > 0:
leftover = (1000 // self.fps) - elapsed
else:
leftover = 0
if leftover < 1:
self.last_time = current
self.step()
for child in self.children:
child.step_frame()
def step(self):
pass
#Morph accessing - geometry getting:
def left(self):
return self.bounds.left()
def right(self):
return self.bounds.right()
def top(self):
return self.bounds.top()
def bottom(self):
return self.bounds.bottom()
def center(self):
return self.bounds.center()
def bottom_center(self):
return self.bounds.bottom_center()
def bottom_left(self):
return self.bounds.bottom_left()
def bottom_right(self):
return self.bounds.bottom_right()
def bounding_box(self):
return self.bounds
def corners(self):
return self.bounds.corners()
def left_center(self):
return self.bounds.left_center()
def right_center(self):
return self.bounds.right_center()
def top_center(self):
return self.bounds.top_center()
def top_left(self):
return self.bounds.top_left()
def top_right(self):
return self.bounds.top_right()
def position(self):
return self.bounds.origin
def extent(self):
return self.bounds.extent()
def width(self):
return self.bounds.width()
def height(self):
return self.bounds.height()
def full_bounds(self):
result = self.bounds
for child in self.children:
result = result.merge(child.full_bounds())
return result
#Morph accessing - changing:
def set_position(self, aPoint):
delta = aPoint - self.top_left()
if delta.x != 0 or delta.y != 0:
self.move_by(delta)
def set_center(self, aPoint):
self.set_position(aPoint - (self.extent() // 2))
def set_full_center(self, aPoint):
self.set_position(aPoint - (self.full_bounds().extent() // 2))
def set_width(self, width):
self.changed()
self.bounds.corner = Point(self.bounds.origin.x + width,
self.bounds.corner.y)
self.changed()
def set_height(self, height):
self.changed()
self.bounds.corner = Point(self.bounds.corner.x,
self.bounds.origin.y + height)
self.changed()
def set_extent(self, aPoint):
self.set_width(max(aPoint.x,0))
self.set_height(max(aPoint.y,0))
def move_by(self, delta):
"move myself by a delta point value"
self.changed()
self.bounds = self.bounds.translate_by(delta)
for child in self.children:
child.move_by(delta)
self.changed()
def keep_within(self, morph):
"make sure I am completely within another morph's bounds"
left_off = self.full_bounds().left() - morph.left()
if left_off < 0:
self.move_by(Point(-left_off, 0))
right_off = self.full_bounds().right() - morph.right()
if right_off > 0:
self.move_by(Point(-right_off, 0))
top_off = self.full_bounds().top() - morph.top()
if top_off < 0:
self.move_by(Point(0, -top_off))
bottom_off = self.full_bounds().bottom() - morph.bottom()
if bottom_off > 0:
self.move_by(Point(0, -bottom_off))
#Morph displaying:
def draw_new(self):
"initialize my surface"
self.image = pygame.Surface(self.extent().as_list())
self.image.fill(self.color)
self.image.set_alpha(self.alpha)
def draw_on(self, surface, rectangle=None):
if not self.is_visible:
return
if rectangle == None:
rectangle = self.bounds
area = rectangle.intersect(self.bounds)
if area.extent() > Point(0,0):
p = area.origin.as_list()
area.origin = area.origin - self.bounds.origin
area.corner = area.corner - self.bounds.origin
surface.blit(self.image, p, area.as_rect())
def full_draw_on(self, surface, rectangle=None):
if not self.is_visible:
return
if rectangle == None:
rectangle = self.full_bounds()
self.draw_on(surface, rectangle)
for child in self.children:
child.full_draw_on(surface, rectangle)
def hide(self):
self.is_visible = False
self.changed()
for morph in self.children:
morph.hide()
def show(self):
self.is_visible = True
self.changed()
for morph in self.children:
morph.show()
def toggle_visibility(self):
self.is_visible = not self.is_visible
self.changed()
for morph in self.children:
morph.toggle_visibility()
#Morph conversion:
def full_surface(self):
img = pygame.Surface(self.full_bounds().extent().as_list())
img.fill(TRANSPARENT)
img.set_colorkey(TRANSPARENT)
img.set_alpha(self.alpha)
for morph in self.all_children():
pos = morph.position() - self.position()
img.blit(morph.image, pos.as_list())
return img
img = pygame.Surface(self.full_bounds().extent().as_list())
def shadow_surface(self, offset, alpha):
img = self.full_surface()
tmp = pygame.Surface(self.full_bounds().extent().as_list())
sha = pygame.Surface(self.full_bounds().extent().as_list())
pygame.transform.threshold(tmp, img, (1,1,1))
tmp.blit(img, (-offset).as_list())
pygame.transform.threshold(sha, tmp, (0,0,0),(0,0,0), (1,1,1))
sha.set_colorkey(TRANSPARENT)
sha.set_alpha(alpha)
return sha
def shadow(self, offset, alpha):
shadow = Shadow()
shadow.set_extent(self.full_bounds().extent())
shadow.draw_new()
shadow.image = self.shadow_surface(offset, alpha)
shadow.set_position(self.position() + offset)
return shadow
def add_shadow(self, name="shadow", offset=Point(7,7), alpha=50):
shadow = self.shadow(offset, alpha)
shadow.name = name
self.add(shadow)
self.full_changed()
def get_shadow(self):
for child in self.children[::-1]:
if isinstance (child, Shadow):
return child
return None
def remove_shadow(self):
self.full_changed()
shadow = self.get_shadow()
if shadow != None:
self.remove_child(shadow)
#Morph updating:
def changed(self):
w = self.root()
if isinstance(w, World):
w.broken.append(copy.copy(self.bounds))
def full_changed(self):
w = self.root()
if isinstance(w, World):
w.broken.append(copy.copy(self.full_bounds()))
#Morph accessing - structure:
def world(self):
if isinstance(self.root(), World):
return self.root()
def add(self, morph):
parent = morph.parent
if parent is not None:
parent.remove_child(morph)
self.add_child(morph)
def morph_at(self, point):
morphs = self.all_children()
for m in morphs[::-1]:
if m.full_bounds().contains_point(point) and not(
isinstance(m, Shadow)):
return m
#Morph duplicating:
def full_copy(self):
new = copy.copy(self)
lst = []
for m in self.children:
new_child = m.full_copy()
new_child.parent = new
lst.append(new_child)
new.children = lst
return new
def duplicate(self):
clone = self.full_copy()
clone.parent = None
clone.pick_up()
#Morph dragging and dropping:
def root_for_grab(self):
if self.parent == None or isinstance(self.parent, Frame):
return self
else:
return self.parent.root_for_grab()
def wants_drop_of(self, morph):
"default is False - change for subclasses"
return False
def pick_up(self):
self.set_position(world.hand.position() - (self.extent() // 2))
world.hand.grab(self)
#Morph events:
def handles_mouse_over(self):
return False
def handles_mouse_click(self):
return False
def handles_mouse_move(self):
return False
def mouse_down_left(self, pos):
pass
def mouse_up_left(self, pos):
pass
def mouse_click_left(self, pos):
pass
def mouse_down_middle(self, pos):
pass
def mouse_up_middle(self, pos):
pass
def mouse_click_middle(self, pos):
pass
def mouse_down_right(self, pos):
pass
def mouse_up_right(self, pos):
pass
def mouse_click_right(self, pos):
pass
def mouse_enter(self):
pass
def mouse_enter_dragging(self):
pass
def mouse_leave(self):
pass
def mouse_leave_dragging(self):
pass
def mouse_move(pos):
pass
#Morph menus:
def context_menu(self):
if world.is_dev_mode:
return self.developers_menu()
else:
return None
def developers_menu(self):
menu = Menu(self, self.__class__.__name__)
menu.add_item("pick up", 'pick_up')
menu.add_item("attach...", 'choose_parent')
menu.add_item("duplicate...", 'duplicate')
menu.add_line()
menu.add_item("transparency...", 'choose_alpha')
menu.add_item("resize...", 'resize')
if not isinstance(self.parent, Frame):
menu.add_item("adjust position...", 'adjust_position')
menu.add_item("color...", 'choose_color')
menu.add_line()
menu.add_item("hide", 'hide')
menu.add_item("close", 'delete')
return menu
def choose_alpha(self):
result = self.prompt(self.__class__.__name__ + "\nalpha\nvalue:",
str(self.alpha),
50)
if result != None:
self.alpha = min(max(int(result),0),254)
self.draw_new()
self.changed()
def choose_color(self):
result = self.pick_color(self.__class__.__name__ + "\ncolor:",
self.color)
if result != None:
self.color = result
self.draw_new()
self.changed()
def resize(self):
handle = Resizer(self)
while self.is_resizing(handle):
world.do_one_cycle()
handle.delete()
def is_resizing(self, handle):
if world.hand.is_dragging(handle):
return True
elif pygame.mouse.get_pressed() != (1,0,0):
return True
elif world.hand.morph_at_pointer() is handle:
return True
else:
return False
def change_extent_to(self, point):
self.changed()
self.set_extent(point)
self.draw_new()
self.changed()
def adjust_position(self):
self.add_shadow()
p = pygame.mouse.get_pos()
pos = Point(p[0], p[1])
offset = pos - self.bounds.origin
while pygame.mouse.get_pressed() == (0,0,0):
mousepos = pygame.mouse.get_pos()
self.set_position(Point(mousepos[0], mousepos[1]) - offset)
world.do_one_cycle()
self.remove_shadow()
def choose_parent(self):
self.choose_morph().add(self)
self.changed()
def choose_morph(self):
self.hint('click on a morph\nto select it')
while pygame.mouse.get_pressed() == (0,0,0):
world.do_one_cycle()
return world.hand.morph_at_pointer()
#Morph utilities:
def hint(self, msg):
m = Menu()
m.title = msg.__str__()
m.is_draggable = True
m.popup_centered_at_hand()
def inform(self, msg):
m = Menu()
m.title = msg.__str__()
m.add_item("Ok")
m.is_draggable = True
m.popup_centered_at_hand()
def ask_yes_no(self, msg):
m = SelectionMenu()
m.title = msg.__str__()
m.add_item("Yes", True)
m.add_item("No", False)
m.is_draggable = True
return m.get_user_choice()
def prompt(self, msg, default='', width=100):
m = SelectionMenu()
m.title = msg.__str__()
m.add_entry(default, width)
m.add_line(2)
m.add_item("Ok", True)
m.add_item("Cancel", False)
m.is_draggable = True
if m.get_user_choice():
return m.get_entries()[0]
else:
return None
def pick_color(self, msg, default=pygame.Color(0,0,0,)):
m = SelectionMenu()
m.title = msg.__str__()
m.add_color_picker(default)
m.add_line(2)
m.add_item("Ok", True)
m.add_item("Cancel", False)
m.is_draggable = True
if m.get_user_choice():
return m.get_color_picks()[0]
else:
return None
class Resizer(Morph):
def __init__(self, target=None):
self.target = target
super(Resizer, self).__init__()
self.is_draggable = False
self.color = TRANSPARENT
self.set_extent(Point(15,15))
self.draw_new()
def draw_new(self):
super(Resizer, self).draw_new()
self.image.set_colorkey(TRANSPARENT)
p1 = self.bottom_left() - self.position()
p2 = self.top_right() - self.position()
for offset in range(0,5):
p1 += Point(3, 0)
p2 += Point(3, 0)
pygame.draw.line(self.image,
pygame.Color(0,0,0),
p1.as_list(),
p2.as_list(),
2)
p1 = self.bottom_left() - self.position()
p2 = self.top_right() - self.position()
for offset in range(1,6):
p1 += Point(3, 0)
p2 += Point(3, 0)
pygame.draw.line(self.image,
pygame.Color(128,128,128),
p1.as_list(),
p2.as_list(),
1)
self.set_position(self.target.bottom_right() - self.extent())
self.target.add(self)
self.target.changed()
def root_for_grab(self):
return self
def handles_mouse_click(self):
return True
def mouse_down_left(self, pos):
offset = pos - self.bounds.origin
while pygame.mouse.get_pressed() == (1,0,0):
mousepos = pygame.mouse.get_pos()
self.set_position(Point(mousepos[0], mousepos[1]) - offset)
self.target.change_extent_to(self.bottom_right()
- self.target.bounds.origin)
world.do_one_cycle()
class Blinker(Morph):
"can be used for text cursors"
def __init__(self, rate=2):
super(Blinker, self).__init__()
self.color = pygame.Color(0,0,0,)
self.fps = rate
self.draw_new()
def wants_to_step(self):
return True
def step(self):
self.toggle_visibility()
class Shadow(Morph):
def __init__(self):
super(Shadow, self).__init__()
class Widget(Morph):
def __init__(self):
super(Widget, self).__init__()