Mercurial > boomslang
annotate tools/rect_drawer.py @ 243:0ea4661d134c
Show close button area in helper
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Fri, 27 Aug 2010 13:01:42 +0200 |
parents | 2510e0b0e901 |
children | df57386908c0 |
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 |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
21 from gamelib.state import initial_state |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
22 from gamelib import constants |
243
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
23 from gamelib.widgets import BoomLabel |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
24 |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
25 constants.DEBUG = True |
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 |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
28 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
29 class AppPalette(PaletteView): |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
30 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
31 sel_width = 5 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
32 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
33 colors = [ |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
34 '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
|
35 '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
|
36 '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
|
37 '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
|
38 ] |
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 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
|
41 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
|
42 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
|
43 self.selection = 0 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
44 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
|
45 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
46 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
|
47 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
|
48 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
58 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
|
59 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
|
60 |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
61 |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
62 class AppImage(Widget): |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
63 |
181
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
64 rect_thick = 3 |
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
65 draw_thick = 1 |
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
66 |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
67 def __init__(self, state): |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
68 self.state = state |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
69 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
|
70 self.mode = 'draw' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
71 self.rects = [] |
200 | 72 self.images = [] |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
73 self.start_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
74 self.end_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
75 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
|
76 self.rect_color = pygame.color.Color('white') |
200 | 77 self.current_image = None |
199 | 78 self.place_image_menu = None |
243
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 if self.state.current_detail: |
0ea4661d134c
Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents:
238
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
88 def draw_mode(self): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
89 self.mode = 'draw' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
90 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
91 def del_mode(self): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
92 self.mode = 'del' |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
93 self.start_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
94 self.end_pos = None |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
95 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
96 def draw(self, surface): |
238
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
97 if self.state.current_detail: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
98 self.state.draw_detail(surface, None) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
99 else: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 for (col, rect) in self.rects: |
181
f98bc17f5e67
Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents:
179
diff
changeset
|
107 frame_rect(surface, col, rect, self.rect_thick) |
200 | 108 for image in self.images: |
109 if image.rect.colliderect(surface.get_rect()): | |
110 cropped_rect = image.rect.clip(surface.get_rect()) | |
111 sub = surface.subsurface(cropped_rect) | |
112 image.draw(sub) | |
113 else: | |
114 print 'image outside surface', image | |
115 if self.current_image and self.mode == 'image': | |
116 if self.current_image.rect.colliderect(surface.get_rect()): | |
117 cropped_rect = self.current_image.rect.clip(surface.get_rect()) | |
118 sub = surface.subsurface(cropped_rect) | |
119 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
|
120 |
165
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
121 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
|
122 d = {} |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
123 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
|
124 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
|
125 d.setdefault(col, []) |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
126 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
|
127 return d |
9b3bba5e65f3
add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents:
162
diff
changeset
|
128 |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 for r in d[col]: |
179
39f75faf16cb
Add trailing , for copy-n-paste
Neil Muller <neil@dip.sun.ac.za>
parents:
174
diff
changeset
|
134 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
|
135 print |
200 | 136 for i, image in enumerate(self.images): |
137 print 'Image %d' % i | |
138 r = image.rect | |
139 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) | |
140 print | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
141 |
199 | 142 def image_load(self): |
143 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER) | |
144 imagename = request_old_filename(directory=image_path) | |
145 try: | |
200 | 146 image_data = pygame.image.load(imagename) |
147 self.current_image = Image(image_data) | |
199 | 148 self.place_image_menu.enabled = True |
200 | 149 # ensure we're off screen to start |
150 self.current_image.rect = image_data.get_rect().move(1000, 600) | |
199 | 151 except pygame.error, e: |
152 print 'Unable to load image %s' % e | |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
153 |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
154 def image_mode(self): |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
155 self.mode = 'image' |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
156 self.start_pos = None |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
157 self.end_pos = None |
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
158 |
200 | 159 def mouse_move(self, e): |
160 if self.mode == 'image' and self.current_image: | |
161 self.current_image.rect.topleft = e.pos | |
162 self.invalidate() | |
163 | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
164 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
|
165 if self.mode == 'del': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
166 pos = e.pos |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
167 cand = None |
200 | 168 # Images are drawn above rectangles, so search those first |
169 for image in self.images: | |
170 if image.rect.collidepoint(pos): | |
171 cand = image | |
172 break | |
173 if cand: | |
174 self.images.remove(cand) | |
175 self.invalidate() | |
176 return | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
177 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
|
178 if rect.collidepoint(pos): |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
179 cand = (col, rect) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
180 break |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
181 if cand: |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
182 self.rects.remove(cand) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
183 self.invalidate() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
184 elif self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
185 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
|
186 self.end_pos = e.pos |
200 | 187 elif self.mode == 'image': |
188 if self.current_image: | |
189 self.images.append(self.current_image) | |
190 self.current_image = None | |
191 self.invalidate() | |
192 else: | |
193 cand = None | |
194 for image in self.images: | |
195 if image.rect.collidepoint(e.pos): | |
196 cand = image | |
197 break | |
198 if cand: | |
199 self.images.remove(cand) | |
200 self.current_image = cand | |
201 self.invalidate() | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
202 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
203 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
|
204 if self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 rect.normalize() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
209 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
|
210 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
|
211 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
212 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
|
213 if self.mode == 'draw': |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
214 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
|
215 self.invalidate() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
216 |
197 | 217 def make_button(text, action, ypos): |
218 button = Button(text, action=action) | |
219 button.align = 'l' | |
220 button.rect = pygame.rect.Rect(0, 0, 200, 40) | |
221 button.rect.move_ip(805, ypos) | |
222 return button | |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
223 |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
224 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
|
225 # 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
|
226 # background image |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
227 if len(sys.argv) < 2: |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
228 print 'Please provide a scene name' |
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
229 sys.exit(0) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
230 pygame.display.init() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
231 pygame.font.init() |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
232 display = pygame.display.set_mode((1000, 600)) |
196 | 233 state = initial_state() |
238
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
234 if len(sys.argv) < 3: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
235 try: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
236 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
|
237 state.do_check = None |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
238 except KeyError: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
239 print 'Invalid scene name' |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
240 sys.exit(1) |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
241 else: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
242 try: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
243 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
|
244 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
|
245 state.do_check = None |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
246 except KeyError: |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
247 print 'Invalid scene name' |
2510e0b0e901
Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents:
213
diff
changeset
|
248 sys.exit(1) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
249 app = RootWidget(display) |
174
a2d041e0ab83
Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents:
165
diff
changeset
|
250 image = AppImage(state) |
162
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
251 app.add(image) |
197 | 252 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
|
253 app.add(draw) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
254 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
|
255 app.add(load_image) |
200 | 256 add_image = make_button("Place/Move images", image.image_mode, 80) |
199 | 257 add_image.enabled = False |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
258 app.add(add_image) |
199 | 259 image.place_image_menu = add_image |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
260 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
|
261 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
|
262 palette = AppPalette(image) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
263 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
|
264 app.add(palette) |
198
e73d78d6dd83
Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents:
197
diff
changeset
|
265 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
|
266 app.add(print_rects) |
197 | 267 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
|
268 app.add(quit_but) |
225e3a4b1e85
Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff
changeset
|
269 app.run() |