annotate tools/rect_drawer.py @ 481:ec40cd4be1ce 1.0.1

Merged into default
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 21 Jun 2014 21:41:46 +0200
parents 0630a37cb371
children 2bef605a0ef2
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
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
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
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
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
df57386908c0 Tweak constant overriding
Neil Muller <neil@dip.sun.ac.za>
parents: 243
diff changeset
21 from gamelib import constants
df57386908c0 Tweak constant overriding
Neil Muller <neil@dip.sun.ac.za>
parents: 243
diff changeset
22 constants.DEBUG = True
df57386908c0 Tweak constant overriding
Neil Muller <neil@dip.sun.ac.za>
parents: 243
diff changeset
23
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
24 from gamelib import state
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
25 state.DEBUG_RECTS = True
243
0ea4661d134c Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents: 238
diff changeset
26 from gamelib.widgets import BoomLabel
174
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 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
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
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
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
75 self.rect_color = pygame.color.Color('white')
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
76 self.current_image = None
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
77 self.place_image_menu = None
243
0ea4661d134c Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents: 238
diff changeset
78 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
79 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
80 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
81 if self.state.current_detail:
0ea4661d134c Show close button area in helper
Neil Muller <neil@dip.sun.ac.za>
parents: 238
diff changeset
82 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
83 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
84 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
85 self.add(self.close_button)
279
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
86 self.offset = (0, 0)
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
87 else:
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
88 self.offset = (-self.state.current_scene.OFFSET[0], - self.state.current_scene.OFFSET[1])
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
89 self.draw_rects = True
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
90 self.draw_things = True
312
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
91 self.draw_thing_rects = True
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
92 self.draw_images = True
275
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
93 self.draw_toolbar = True
323
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
94 self.find_existing_intersects()
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
95
323
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
96 def find_existing_intersects(self):
320
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
97 """Parse the things in the scene for overlaps"""
323
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
98 if self.state.current_detail:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
99 scene = self.state.current_detail
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
100 else:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
101 scene = self.state.current_scene
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
102 # Pylint hates this function
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
103 for thing in scene.things.itervalues():
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
104 for interact_name in thing.interacts:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
105 thing.set_interact(interact_name)
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
106 if hasattr(thing.rect, 'collidepoint'):
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
107 thing_rects = [thing.rect]
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
108 else:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
109 thing_rects = thing.rect
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
110 for thing2 in scene.things.itervalues():
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
111 if thing is thing2:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
112 continue
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
113 for interact2_name in thing2.interacts:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
114 thing2.set_interact(interact2_name)
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
115 if hasattr(thing2.rect, 'collidepoint'):
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
116 thing2_rects = [thing2.rect]
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
117 else:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
118 thing2_rects = thing2.rect
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
119 for my_rect in thing_rects:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
120 for other_rect in thing2_rects:
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
121 if my_rect.colliderect(other_rect):
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
122 print 'Existing Intersecting rects'
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
123 print " Thing1 %s Interact %s" % (thing.name, interact_name)
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
124 print " Thing2 %s Interact %s" % (thing2.name, interact2_name)
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
125 print " Rects", my_rect, other_rect
0630a37cb371 Print existing intersection in scene on startup
Neil Muller <neil@dip.sun.ac.za>
parents: 321
diff changeset
126 print
320
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
127
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
128 def find_intersecting_rects(self, d):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
129 """Find if any rect collections intersect"""
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
130 # I loath N^X brute search algorithm's, but whatever, hey
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
131 if self.state.current_detail:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
132 scene = self.state.current_detail
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
133 else:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
134 scene = self.state.current_scene
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
135 for (num, col) in enumerate(d):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
136 rect_list = d[col]
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
137 for thing in scene.things.itervalues():
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
138 for interact_name in thing.interacts:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
139 thing.set_interact(interact_name)
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
140 if hasattr(thing.rect, 'collidepoint'):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
141 thing_rects = [thing.rect]
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
142 else:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
143 thing_rects = thing.rect
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
144 for other_rect in thing_rects:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
145 for my_rect in rect_list:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
146 if my_rect.colliderect(other_rect):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
147 print 'Intersecting rects'
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
148 print " Object %s" % num
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
149 print " Thing %s Interact %s" % (thing.name, interact_name)
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
150 print " Rects", my_rect, other_rect
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
151 if thing.INITIAL:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
152 thing.set_interact(thing.INITIAL)
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
153 print
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
154 for (num2, col2) in enumerate(d):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
155 if num2 == num:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
156 continue
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
157 other_list = d[col2]
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
158 for my_rect in rect_list:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
159 for other_rect in other_list:
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
160 if my_rect.colliderect(other_rect):
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
161 print 'Intersecting rects',
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
162 print ' Object %s and %s' % (num, num2)
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
163 print " Rects", my_rect, other_rect
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
164 print
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
165 print
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
166
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
167 def toggle_things(self):
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
168 self.draw_things = not self.draw_things
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
169
312
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
170 def toggle_thing_rects(self):
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
171 self.draw_thing_rects = not self.draw_thing_rects
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
172 if self.state.current_detail:
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
173 scene = self.state.current_detail
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
174 else:
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
175 scene = self.state.current_scene
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
176 for thing in scene.things.itervalues():
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
177 if not self.draw_thing_rects:
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
178 if not hasattr(thing, 'old_colour'):
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
179 thing.old_colour = thing._interact_hilight_color
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
180 thing._interact_hilight_color = None
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
181 else:
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
182 thing._interact_hilight_color = thing.old_colour
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
183
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
184 def toggle_images(self):
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
185 self.draw_images = not self.draw_images
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
186
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
187 def toggle_rects(self):
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
188 self.draw_rects = not self.draw_rects
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
189
275
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
190 def toggle_toolbar(self):
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
191 self.draw_toolbar = not self.draw_toolbar
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
192
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
193 def draw_mode(self):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
194 self.mode = 'draw'
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
195
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
196 def del_mode(self):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
197 self.mode = 'del'
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
198 self.start_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
199 self.end_pos = None
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
200
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
201 def draw(self, surface):
238
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
202 if self.state.current_detail:
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
203 if self.draw_things:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
204 self.state.draw_detail(surface, None)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
205 else:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
206 self.state.current_detail.draw_background(surface)
238
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
207 else:
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
208 if self.draw_things:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
209 self.state.draw(surface, None)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
210 else:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
211 self.state.current_scene.draw_background(surface)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
212 if self.mode == 'draw' and self.start_pos and self.draw_rects:
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
213 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
214 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
215 self.end_pos[1] - self.start_pos[1])
321
e5f3a97ee812 Fix rect drawing bug
Neil Muller <neil@dip.sun.ac.za>
parents: 320
diff changeset
216 rect.normalize()
320
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
217 frame_rect(surface, self.rect_color, rect, self.draw_thick)
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
218 if self.draw_rects:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
219 for (col, rect) in self.rects:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
220 frame_rect(surface, col, rect, self.rect_thick)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
221 if self.draw_images:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
222 for image in self.images:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
223 if image.rect.colliderect(surface.get_rect()):
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
224 cropped_rect = image.rect.clip(surface.get_rect())
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
225 sub = surface.subsurface(cropped_rect)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
226 image.draw(sub)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
227 else:
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
228 print 'image outside surface', image
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
229 if self.current_image and self.mode == 'image':
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
230 if self.current_image.rect.colliderect(surface.get_rect()):
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
231 cropped_rect = self.current_image.rect.clip(surface.get_rect())
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
232 sub = surface.subsurface(cropped_rect)
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
233 self.current_image.draw(sub)
275
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
234 if self.draw_toolbar:
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
235 toolbar_rect = pygame.rect.Rect(0, 550, 800, 50)
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
236 tb_surf = surface.subsurface(0, 550, 800, 50).convert_alpha()
276
75f4ee46ac58 Better (and fixed) rect tool with toolbar overlay.
Jeremy Thurgood <firxen@gmail.com>
parents: 275
diff changeset
237 tb_surf.fill(pygame.color.Color(127, 0, 0, 191))
75f4ee46ac58 Better (and fixed) rect tool with toolbar overlay.
Jeremy Thurgood <firxen@gmail.com>
parents: 275
diff changeset
238 surface.blit(tb_surf, (0, 550))
75f4ee46ac58 Better (and fixed) rect tool with toolbar overlay.
Jeremy Thurgood <firxen@gmail.com>
parents: 275
diff changeset
239 # frame_rect(surface, (127, 0, 0), toolbar_rect, 2)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
240
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
241 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
242 d = {}
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
243 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
244 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
245 d.setdefault(col, [])
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
246 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
247 return d
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
248
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
249 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
250 d = self._make_dict()
320
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
251 self.find_intersecting_rects(d)
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
252 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
253 print 'Rect %d : ' % num
279
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
254 for rect in d[col]:
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
255 r = rect.move(self.offset)
179
39f75faf16cb Add trailing , for copy-n-paste
Neil Muller <neil@dip.sun.ac.za>
parents: 174
diff changeset
256 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
257 print
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
258 for i, image in enumerate(self.images):
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
259 print 'Image %d' % i
279
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
260 rect = image.rect
c67a4a4d78f6 Compensate for offset in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 276
diff changeset
261 r = rect.move(self.offset)
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
262 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h)
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
263 print
320
c295b06b27f8 Warn about intersecting existing things and other drawn rects
Neil Muller <neil@dip.sun.ac.za>
parents: 312
diff changeset
264 print
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
265
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
266 def image_load(self):
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
267 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
268 imagename = request_old_filename(directory=image_path)
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
269 try:
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
270 image_data = pygame.image.load(imagename)
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
271 self.current_image = Image(image_data)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
272 self.place_image_menu.enabled = True
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
273 # ensure we're off screen to start
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
274 self.current_image.rect = image_data.get_rect().move(1000, 600)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
275 except pygame.error, e:
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
276 print 'Unable to load image %s' % e
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
277
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
278 def image_mode(self):
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
279 self.mode = 'image'
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
280 self.start_pos = None
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
281 self.end_pos = None
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
282
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
283 def mouse_move(self, e):
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
284 if self.mode == 'image' and self.current_image:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
285 self.current_image.rect.topleft = e.pos
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
286 self.invalidate()
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
287
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
288 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
289 if self.mode == 'del':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
290 pos = e.pos
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
291 cand = None
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
292 # Images are drawn above rectangles, so search those first
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
293 for image in self.images:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
294 if image.rect.collidepoint(pos):
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
295 cand = image
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
296 break
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
297 if cand:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
298 self.images.remove(cand)
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
299 self.invalidate()
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
300 return
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
301 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
302 if rect.collidepoint(pos):
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
303 cand = (col, rect)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
304 break
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
305 if cand:
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
306 self.rects.remove(cand)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
307 self.invalidate()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
308 elif self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
309 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
310 self.end_pos = e.pos
200
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
311 elif self.mode == 'image':
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
312 if self.current_image:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
313 self.images.append(self.current_image)
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
314 self.current_image = None
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
315 self.invalidate()
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
316 else:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
317 cand = None
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
318 for image in self.images:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
319 if image.rect.collidepoint(e.pos):
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
320 cand = image
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
321 break
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
322 if cand:
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
323 self.images.remove(cand)
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
324 self.current_image = cand
7bedca2376f5 Image placement
Neil Muller <neil@dip.sun.ac.za>
parents: 199
diff changeset
325 self.invalidate()
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
326
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
327 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
328 if self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
329 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
330 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
331 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
332 rect.normalize()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
333 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
334 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
335
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
336 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
337 if self.mode == 'draw':
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
338 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
339 self.invalidate()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
340
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
341 def make_button(text, action, ypos):
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
342 button = Button(text, action=action)
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
343 button.align = 'l'
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
344 button.rect = pygame.rect.Rect(0, 0, 200, 35)
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
345 button.rect.move_ip(805, ypos)
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
346 return button
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
347
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
348 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
349 # 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
350 # background image
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
351 if len(sys.argv) < 2:
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
352 print 'Please provide a scene name'
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
353 sys.exit(0)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
354 pygame.display.init()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
355 pygame.font.init()
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
356 display = pygame.display.set_mode((1000, 600))
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
357 state = state.initial_state()
238
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
358 if len(sys.argv) < 3:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
359 try:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
360 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
361 state.do_check = None
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
362 except KeyError:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
363 print 'Invalid scene name'
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
364 sys.exit(1)
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
365 else:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
366 try:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
367 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
368 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
369 state.do_check = None
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
370 except KeyError:
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
371 print 'Invalid scene name'
2510e0b0e901 Add detail view support to helper
Neil Muller <neil@dip.sun.ac.za>
parents: 213
diff changeset
372 sys.exit(1)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
373 app = RootWidget(display)
174
a2d041e0ab83 Make tool use scenes, not images
Neil Muller <neil@dip.sun.ac.za>
parents: 165
diff changeset
374 image = AppImage(state)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
375 app.add(image)
197
640044d7ddda Tweak layout
Neil Muller <neil@dip.sun.ac.za>
parents: 196
diff changeset
376 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
377 app.add(draw)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
378 load_image = make_button("Load image", image.image_load, 35)
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
379 app.add(load_image)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
380 add_image = make_button("Place/Move images", image.image_mode, 70)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
381 add_image.enabled = False
198
e73d78d6dd83 Stubs for ftuture functionality
Neil Muller <neil@dip.sun.ac.za>
parents: 197
diff changeset
382 app.add(add_image)
199
4821c290286d Image loading
Neil Muller <neil@dip.sun.ac.za>
parents: 198
diff changeset
383 image.place_image_menu = add_image
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
384 delete = make_button('Delete Objects', image.del_mode, 105)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
385 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
386 palette = AppPalette(image)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
387 palette.rect.move_ip(810, 140)
165
9b3bba5e65f3 add some features to rect helper. now vaguely useful
Neil Muller <neil@dip.sun.ac.za>
parents: 162
diff changeset
388 app.add(palette)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
389 print_rects = make_button("Print objects", image.print_objs, 300)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
390 app.add(print_rects)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
391 toggle_things = make_button("Toggle Things", image.toggle_things, 335)
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
392 app.add(toggle_things)
312
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
393 toggle_thing_rects = make_button("Toggle Thing Rects", image.toggle_thing_rects, 370)
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
394 app.add(toggle_thing_rects)
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
395 toggle_images = make_button("Toggle Images", image.toggle_images, 405)
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
396 app.add(toggle_images)
312
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
397 toggle_rects = make_button("Toggle Rects", image.toggle_rects, 440)
268
e4ea9def56b2 Add some toggle options to helper script
Neil Muller <neil@dip.sun.ac.za>
parents: 250
diff changeset
398 app.add(toggle_rects)
312
a0d66c0f5a89 Add toggle thing rects option
Neil Muller <neil@dip.sun.ac.za>
parents: 286
diff changeset
399 toggle_toolbar = make_button("Toggle Toolbar", image.toggle_toolbar, 475)
275
d78ce15bccc8 Crew quarters background and toolbar on the rect tool.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
400 app.add(toggle_toolbar)
286
776bcf563ea0 Re-enable thing rects by default in rect_drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 279
diff changeset
401 quit_but = make_button("Quit", app.quit, 565)
162
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
402 app.add(quit_but)
225e3a4b1e85 Start of a tool to help construct interact rectangles
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
403 app.run()