annotate tools/rect_drawer.py @ 199:4821c290286d

Image loading
author Neil Muller <neil@dip.sun.ac.za>
date Thu, 26 Aug 2010 09:02:46 +0200
parents e73d78d6dd83
children 7bedca2376f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
13 from albow.controls import Button
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
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
15 from albow.file_dialogs import request_old_filename
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
16 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
17 import pygame
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
18 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
19
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
20 from gamelib.state import initial_state
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
21 from gamelib import constants
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
22
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
23 constants.DEBUG = True
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
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
26
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
27 class AppPalette(PaletteView):
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 sel_width = 5
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 colors = [
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
32 '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
33 '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
34 '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
35 '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
36 ]
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
37
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
38 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
39 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
40 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
41 self.selection = 0
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
42 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
43
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
44 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
45 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
46
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
47 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
48 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
49 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
50 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
51
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
52 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
53 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
54 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
55
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
56 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
57 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
58
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
59
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
60
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
61 class AppImage(Widget):
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
62
181
f98bc17f5e67 Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents: 179
diff changeset
63 rect_thick = 3
f98bc17f5e67 Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents: 179
diff changeset
64 draw_thick = 1
f98bc17f5e67 Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents: 179
diff changeset
65
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
66 def __init__(self, state):
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
67 self.state = state
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
68 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
69 self.mode = 'draw'
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
70 self.rects = []
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
71 self.start_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
72 self.end_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
73 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
74 self.rect_color = pygame.color.Color('white')
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
75 self.curent_image = None
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
76 self.place_image_menu = None
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
77
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
78 def draw_mode(self):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
79 self.mode = 'draw'
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
80
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
81 def del_mode(self):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
82 self.mode = 'del'
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
83 self.start_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
84 self.end_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
85
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
86 def draw(self, surface):
196
b63ecd5b9a45 Unbeak helper
Neil Muller <neil@dip.sun.ac.za>
parents: 181
diff changeset
87 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
88 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
89 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
90 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
91 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
92 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
93 for (col, rect) in self.rects:
181
f98bc17f5e67 Add attributes for drawing rects
Neil Muller <neil@dip.sun.ac.za>
parents: 179
diff changeset
94 frame_rect(surface, col, rect, self.rect_thick)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
95
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
96 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
97 d = {}
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
98 for col, rect in self.rects:
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
99 d.setdefault(col, [])
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
100 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
101 return d
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
102
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
103 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
104 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
105 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
106 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
107 for r in d[col]:
179
39f75faf16cb Add trailing , for copy-n-paste
Neil Muller <neil@dip.sun.ac.za>
parents: 174
diff changeset
108 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
109 print
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
110
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
111 def image_load(self):
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
112 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER)
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
113 imagename = request_old_filename(directory=image_path)
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
114 try:
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
115 self.current_image = pygame.image.load(imagename)
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
116 self.place_image_menu.enabled = True
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
117 except pygame.error, e:
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
118 print 'Unable to load image %s' % e
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
119
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
120 def image_mode(self):
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
121 self.mode = 'image'
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
122 self.start_pos = None
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
123 self.end_pos = None
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
124
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
125 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
126 if self.mode == 'del':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
127 pos = e.pos
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
128 cand = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
129 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
130 if rect.collidepoint(pos):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
131 cand = (col, rect)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
132 break
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
133 if cand:
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
134 self.rects.remove(cand)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
135 self.invalidate()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
136 elif self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
137 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
138 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
139
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
140 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
141 if self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
142 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
143 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
144 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
145 rect.normalize()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
146 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
147 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
148
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
149 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
150 if self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
151 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
152 self.invalidate()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
153
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
154 def make_button(text, action, ypos):
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
155 button = Button(text, action=action)
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
156 button.align = 'l'
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
157 button.rect = pygame.rect.Rect(0, 0, 200, 40)
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
158 button.rect.move_ip(805, ypos)
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
159 return button
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
160
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
161 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
162 # 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
163 # background image
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
164 if len(sys.argv) < 2:
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
165 print 'Please provide a scene name'
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
166 sys.exit(0)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
167 pygame.display.init()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
168 pygame.font.init()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
169 display = pygame.display.set_mode((1000, 600))
196
b63ecd5b9a45 Unbeak helper
Neil Muller <neil@dip.sun.ac.za>
parents: 181
diff changeset
170 state = initial_state()
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
171 try:
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
172 state.set_current_scene(sys.argv[1])
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
173 state.do_check = None
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
174 except KeyError:
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
175 print 'Invalid scene name'
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
176 sys.exit(1)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
177 app = RootWidget(display)
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
178 image = AppImage(state)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
179 app.add(image)
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
180 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
181 app.add(draw)
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
182 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
183 app.add(load_image)
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
184 add_image = make_button("Place image", image.image_mode, 80)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
185 add_image.enabled = False
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
186 app.add(add_image)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
187 image.place_image_menu = add_image
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
188 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
189 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
190 palette = AppPalette(image)
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
191 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
192 app.add(palette)
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
193 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
194 app.add(print_rects)
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
195 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
196 app.add(quit_but)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
197 app.run()