Mercurial > boomslang
annotate tools/rect_drawer.py @ 259:b6e1c4e4e59d
Superconductor Thing now works.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 27 Aug 2010 19:01:00 +0200 |
parents | df57386908c0 |
children | e4ea9def56b2 |
rev | line source |
---|---|
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
1 # Quickly hacked together helper for working out |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
2 # interactive regions in Suspended Sentence |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
3 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
4 import sys |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
5 import os.path |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
6 |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
7 script_path = os.path.realpath(os.path.dirname(os.path.dirname(__file__))) |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
8 sys.path.append(script_path) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
9 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
10 from albow.root import RootWidget |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
11 from albow.utils import frame_rect |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
12 from albow.widget import Widget |
200 | 13 from albow.controls import Button, Image |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
14 from albow.palette_view import PaletteView |
199 | 15 from albow.file_dialogs import request_old_filename |
243
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
16 from albow.resource import get_font |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
17 from pygame.locals import SWSURFACE |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
18 import pygame |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
19 from pygame.colordict import THECOLORS |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
20 |
250 | 21 from gamelib import constants |
22 constants.DEBUG = True | |
23 | |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
24 from gamelib.state import initial_state |
243
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
25 from gamelib.widgets import BoomLabel |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
26 |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
27 |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
28 |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
29 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
30 class AppPalette(PaletteView): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
31 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
32 sel_width = 5 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
33 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
34 colors = [ |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
35 'red', 'maroon1', 'palevioletred1', 'moccasin', 'orange', |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
36 'honeydew', 'yellow', 'gold', 'goldenrod', 'brown', |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
37 'blue', 'purple', 'darkorchid4', 'thistle', 'skyblue1', |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
38 'green', 'palegreen1', 'darkgreen', 'aquamarine', 'darkolivegreen', |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
39 ] |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
40 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
41 def __init__(self, app_image): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
42 self.image = app_image |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
43 super(AppPalette, self).__init__((35, 35), 5, 5, margin=2) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
44 self.selection = 0 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
45 self.image.rect_color = pygame.color.Color(self.colors[self.selection]) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
46 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
47 def num_items(self): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
48 return len(self.colors) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
49 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
50 def draw_item(self, surface, item_no, rect): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
51 d = -2 * self.sel_width |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
52 r = rect.inflate(d, d) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
53 surface.fill(pygame.color.Color(self.colors[item_no]), r) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
54 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
55 def click_item(self, item_no, event): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
56 self.selection = item_no |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
57 self.image.rect_color = pygame.color.Color(self.colors[item_no]) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
58 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
59 def item_is_selected(self, item_no): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
60 return self.selection == item_no |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
61 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
62 |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
63 class AppImage(Widget): |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
64 |
181
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
65 rect_thick = 3 |
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
66 draw_thick = 1 |
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
67 |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
68 def __init__(self, state): |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
69 self.state = state |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
70 super(AppImage, self).__init__(pygame.rect.Rect(0, 0, 800, 600)) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
71 self.mode = 'draw' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
72 self.rects = [] |
200 | 73 self.images = [] |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
74 self.start_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
75 self.end_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
76 self.draw_color = pygame.color.Color('white') |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
77 self.rect_color = pygame.color.Color('white') |
200 | 78 self.current_image = None |
199 | 79 self.place_image_menu = None |
243
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
80 self.close_button = BoomLabel('Close', font=get_font(20, 'Vera.ttf')) |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
81 self.close_button.fg_color = (0, 0, 0) |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
82 self.close_button.bg_color = (0, 0, 0) |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
83 if self.state.current_detail: |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
84 w, h = self.state.current_detail.get_detail_size() |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
85 rect = pygame.rect.Rect(0, 0, w, h) |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
86 self.close_button.rect.midbottom = rect.midbottom |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
87 self.add(self.close_button) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
88 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
89 def draw_mode(self): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
90 self.mode = 'draw' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
91 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
92 def del_mode(self): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
93 self.mode = 'del' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
94 self.start_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
95 self.end_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
96 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
97 def draw(self, surface): |
238
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
98 if self.state.current_detail: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
99 self.state.draw_detail(surface, None) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
100 else: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
101 self.state.draw(surface, None) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
102 if self.mode == 'draw' and self.start_pos: |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
103 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1], |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
104 self.end_pos[0] - self.start_pos[0], |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
105 self.end_pos[1] - self.start_pos[1]) |
181
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
106 frame_rect(surface, self.draw_color, rect, self.draw_thick) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
107 for (col, rect) in self.rects: |
181
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
108 frame_rect(surface, col, rect, self.rect_thick) |
200 | 109 for image in self.images: |
110 if image.rect.colliderect(surface.get_rect()): | |
111 cropped_rect = image.rect.clip(surface.get_rect()) | |
112 sub = surface.subsurface(cropped_rect) | |
113 image.draw(sub) | |
114 else: | |
115 print 'image outside surface', image | |
116 if self.current_image and self.mode == 'image': | |
117 if self.current_image.rect.colliderect(surface.get_rect()): | |
118 cropped_rect = self.current_image.rect.clip(surface.get_rect()) | |
119 sub = surface.subsurface(cropped_rect) | |
120 self.current_image.draw(sub) | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
121 |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
122 def _make_dict(self): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
123 d = {} |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
124 for col, rect in self.rects: |
213
20998c650ce1
Fixed rect_drawer tool and adjusted JIM message background.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
125 col = (col.r, col.g, col.b) |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
126 d.setdefault(col, []) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
127 d[col].append(rect) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
128 return d |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
129 |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
130 def print_objs(self): |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
131 d = self._make_dict() |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
132 for (num, col) in enumerate(d): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
133 print 'Rect %d : ' % num |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
134 for r in d[col]: |
179
39f75faf16cb
Add trailing , for copy-n-paste
Neil Muller <neil@dip.sun.ac.za>
parents:
174
diff
changeset
|
135 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
136 print |
200 | 137 for i, image in enumerate(self.images): |
138 print 'Image %d' % i | |
139 r = image.rect | |
140 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) | |
141 print | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
142 |
199 | 143 def image_load(self): |
144 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER) | |
145 imagename = request_old_filename(directory=image_path) | |
146 try: | |
200 | 147 image_data = pygame.image.load(imagename) |
148 self.current_image = Image(image_data) | |
199 | 149 self.place_image_menu.enabled = True |
200 | 150 # ensure we're off screen to start |
151 self.current_image.rect = image_data.get_rect().move(1000, 600) | |
199 | 152 except pygame.error, e: |
153 print 'Unable to load image %s' % e | |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
154 |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
155 def image_mode(self): |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
156 self.mode = 'image' |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
157 self.start_pos = None |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
158 self.end_pos = None |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
159 |
200 | 160 def mouse_move(self, e): |
161 if self.mode == 'image' and self.current_image: | |
162 self.current_image.rect.topleft = e.pos | |
163 self.invalidate() | |
164 | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
165 def mouse_down(self, e): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
166 if self.mode == 'del': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
167 pos = e.pos |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
168 cand = None |
200 | 169 # Images are drawn above rectangles, so search those first |
170 for image in self.images: | |
171 if image.rect.collidepoint(pos): | |
172 cand = image | |
173 break | |
174 if cand: | |
175 self.images.remove(cand) | |
176 self.invalidate() | |
177 return | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
178 for (col, rect) in self.rects: |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
179 if rect.collidepoint(pos): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
180 cand = (col, rect) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
181 break |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
182 if cand: |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
183 self.rects.remove(cand) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
184 self.invalidate() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
185 elif self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
186 self.start_pos = e.pos |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
187 self.end_pos = e.pos |
200 | 188 elif self.mode == 'image': |
189 if self.current_image: | |
190 self.images.append(self.current_image) | |
191 self.current_image = None | |
192 self.invalidate() | |
193 else: | |
194 cand = None | |
195 for image in self.images: | |
196 if image.rect.collidepoint(e.pos): | |
197 cand = image | |
198 break | |
199 if cand: | |
200 self.images.remove(cand) | |
201 self.current_image = cand | |
202 self.invalidate() | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
203 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
204 def mouse_up(self, e): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
205 if self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
206 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1], |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
207 self.end_pos[0] - self.start_pos[0], |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
208 self.end_pos[1] - self.start_pos[1]) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
209 rect.normalize() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
210 self.rects.append((self.rect_color, rect)) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
211 self.start_pos = self.end_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
212 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
213 def mouse_drag(self, e): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
214 if self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
215 self.end_pos = e.pos |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
216 self.invalidate() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
217 |
197 | 218 def make_button(text, action, ypos): |
219 button = Button(text, action=action) | |
220 button.align = 'l' | |
221 button.rect = pygame.rect.Rect(0, 0, 200, 40) | |
222 button.rect.move_ip(805, ypos) | |
223 return button | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
224 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
225 if __name__ == "__main__": |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
226 # FIXME: should load an actual scene with current things, not just a |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
227 # background image |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
228 if len(sys.argv) < 2: |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
229 print 'Please provide a scene name' |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
230 sys.exit(0) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
231 pygame.display.init() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
232 pygame.font.init() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
233 display = pygame.display.set_mode((1000, 600)) |
196 | 234 state = initial_state() |
238
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
235 if len(sys.argv) < 3: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
236 try: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
237 state.set_current_scene(sys.argv[1]) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
238 state.do_check = None |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
239 except KeyError: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
240 print 'Invalid scene name' |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
241 sys.exit(1) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
242 else: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
243 try: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
244 state.set_current_scene(sys.argv[1]) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
245 state.set_current_detail(sys.argv[2]) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
246 state.do_check = None |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
247 except KeyError: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
248 print 'Invalid scene name' |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
249 sys.exit(1) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
250 app = RootWidget(display) |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
251 image = AppImage(state) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
252 app.add(image) |
197 | 253 draw = make_button('Draw Rect', image.draw_mode, 0) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
254 app.add(draw) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
255 load_image = make_button("Load image", image.image_load, 40) |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
256 app.add(load_image) |
200 | 257 add_image = make_button("Place/Move images", image.image_mode, 80) |
199 | 258 add_image.enabled = False |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
259 app.add(add_image) |
199 | 260 image.place_image_menu = add_image |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
261 delete = make_button('Delete Objects', image.del_mode, 120) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
262 app.add(delete) |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
263 palette = AppPalette(image) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
264 palette.rect.move_ip(810, 160) |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
265 app.add(palette) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
266 print_rects = make_button("Print objects", image.print_objs, 320) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
267 app.add(print_rects) |
197 | 268 quit_but = make_button("Quit", app.quit, 560) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
269 app.add(quit_but) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
270 app.run() |