Mercurial > nagslang
annotate tools/area_editor.py @ 198:05c2c592ce2e
Add dummy check boxes
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 03 Sep 2013 22:19:20 +0200 |
parents | b8701c0bb184 |
children | c291fd4b49bf |
rev | line source |
---|---|
71
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
1 #!/usr/bin/env python |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
2 |
51 | 3 # The basic area editor |
4 # | |
5 # To edit an existing level, use | |
6 # editor levelname | |
7 # | |
8 # To create a new level: | |
9 # | |
10 # editor levelname <xsize> <ysiz> | |
11 # (size specified in pixels | |
12 # | |
13 | |
71
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
14 import os |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
15 import sys |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
16 |
51 | 17 import pygame |
18 import pygame.locals as pgl | |
19 | |
152
8bbe57d9de0c
Rejig sys.path hacking above albow import, so I can hide it in the root of my repo
Stefano Rivera <stefano@rivera.za.net>
parents:
151
diff
changeset
|
20 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
8bbe57d9de0c
Rejig sys.path hacking above albow import, so I can hide it in the root of my repo
Stefano Rivera <stefano@rivera.za.net>
parents:
151
diff
changeset
|
21 |
153
6c5f1a78db75
Levels need a pymunk Space to load
Stefano Rivera <stefano@rivera.za.net>
parents:
152
diff
changeset
|
22 import pymunk |
6c5f1a78db75
Levels need a pymunk Space to load
Stefano Rivera <stefano@rivera.za.net>
parents:
152
diff
changeset
|
23 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
24 from albow.root import RootWidget |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
25 from albow.widget import Widget |
198 | 26 from albow.controls import Button, Label, CheckBox |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
27 from albow.dialogs import alert |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
28 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
29 from nagslang.constants import SCREEN |
51 | 30 from nagslang.level import Level, POLY_COLORS |
31 | |
32 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
33 # layout constants |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
34 MENU_BUTTON_HEIGHT = 35 |
115 | 35 MENU_PAD = 6 |
36 MENU_HALF_PAD = MENU_PAD // 2 | |
37 MENU_LEFT = SCREEN[0] + MENU_HALF_PAD | |
38 MENU_WIDTH = 200 - MENU_PAD | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
39 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
40 |
51 | 41 class EditorLevel(Level): |
42 | |
43 def __init__(self, name, x=800, y=600): | |
44 super(EditorLevel, self).__init__(name) | |
45 self.x = x | |
46 self.y = y | |
47 | |
48 def round_point(self, pos): | |
49 return (10 * (pos[0] // 10), 10 * (pos[1] // 10)) | |
50 | |
51 def point_to_pymunk(self, pos): | |
52 # inverse of point_to_pygame | |
53 # (this is also the same as point_to_pygame, but a additional | |
54 # function for sanity later in pyweek). | |
55 return (pos[0], self.y - pos[1]) | |
56 | |
57 def add_point(self, poly_index, pos): | |
58 self.polygons.setdefault(poly_index, []) | |
59 if not self.polygons[poly_index]: | |
99 | 60 point = self.point_to_pymunk(self.round_point(pos)) |
61 self.polygons[poly_index].append(point) | |
51 | 62 else: |
63 add_pos = self.fix_angle(poly_index, pos) | |
64 self.polygons[poly_index].append(add_pos) | |
65 | |
66 def fix_angle(self, index, pos): | |
67 # Last point | |
68 point1 = self.point_to_pygame(self.polygons[index][-1]) | |
69 pos = self.round_point(pos) | |
70 # We want the line (point1 to pos) to be an angle of | |
71 # 0, 45, 90, 135, 180, 225, 270, 305 | |
72 # However, we only need to consider half the circle | |
73 # This is a hack to approximate the right thing | |
74 pos0 = (pos[0], point1[1]) | |
75 pos90 = (point1[0], pos[1]) | |
76 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1])) | |
77 pos45 = (point1[0] + dist, point1[1] + dist) | |
78 pos135 = (point1[0] + dist, point1[1] - dist) | |
79 pos225 = (point1[0] - dist, point1[1] - dist) | |
80 pos305 = (point1[0] - dist, point1[1] + dist) | |
81 min_dist = 9999999 | |
82 new_pos = point1 | |
83 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]: | |
84 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2 | |
85 if dist < min_dist: | |
86 new_pos = cand | |
87 min_dist = dist | |
88 return self.point_to_pymunk(new_pos) | |
89 | |
90 def delete_point(self, index): | |
91 if index in self.polygons and len(self.polygons[index]) > 0: | |
92 self.polygons[index].pop() | |
93 | |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
94 def close_poly(self, index): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
95 """Attempts to close the current polygon. |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
96 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
97 We allow a small additional step to close the polygon, but |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
98 it's limited as it's a magic point addition""" |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
99 if len(self.polygons[index]) < 2: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
100 # Too small |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
101 return False |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
102 first = self.polygons[index][0] |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
103 if self.fix_angle(index, self.point_to_pygame(first)) == first: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
104 self.add_point(index, self.point_to_pygame(first)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
105 return True |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
106 candidates = [(first[0] + 10 * i, first[1]) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
107 i in (-3, -2, -1, 1, 2, 3)] |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
108 candidates.extend([(first[0], first[1] + 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
109 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
110 candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
111 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
112 candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
113 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
114 min_dist = 99999 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
115 poss = None |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
116 for cand in candidates: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
117 if self.fix_angle(index, self.point_to_pygame(cand)) == cand: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
118 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
119 if dist < min_dist: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
120 poss = cand |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
121 if poss is not None: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
122 self.add_point(index, self.point_to_pygame(poss)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
123 self.add_point(index, self.point_to_pygame(first)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
124 return True |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
125 return False |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
126 |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
127 def draw(self, surface, topleft, mouse_pos, mouse_poly, filled): |
51 | 128 self._draw_background(True) |
129 # Draw polygons as needed for the editor | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
130 if filled: |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
131 self._draw_exterior(True) |
51 | 132 for index, polygon in self.polygons.items(): |
133 color = POLY_COLORS[index] | |
134 if len(polygon) > 1: | |
135 pointlist = [self.point_to_pygame(p) for p in polygon] | |
136 pygame.draw.lines(self._surface, color, False, pointlist, 2) | |
137 if index == mouse_poly and mouse_pos: | |
138 endpoint = self.fix_angle(index, mouse_pos) | |
139 pygame.draw.line(self._surface, color, | |
99 | 140 self.point_to_pygame(polygon[-1]), |
141 self.point_to_pygame(endpoint)) | |
51 | 142 surface_area = pygame.rect.Rect(topleft, SCREEN) |
143 surface.blit(self._surface, (0, 0), surface_area) | |
144 | |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
145 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
146 class LevelWidget(Widget): |
51 | 147 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
148 def __init__(self, level): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
149 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0, |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
150 SCREEN[0], SCREEN[1])) |
51 | 151 self.level = level |
152 self.pos = (0, 0) | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
153 self.filled_mode = False |
51 | 154 self.mouse_pos = None |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
155 self.cur_poly = None |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
156 self._mouse_drag = False |
51 | 157 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
158 def _level_coordinates(self, pos): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
159 # Move positions to level values |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
160 if not pos: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
161 return (0, 0) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
162 return pos[0] + self.pos[0], pos[1] + self.pos[1] |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
163 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
164 def _move_view(self, offset): |
51 | 165 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]] |
166 if new_pos[0] < 0: | |
167 new_pos[0] = self.pos[0] | |
168 elif new_pos[0] > self.level.x - SCREEN[0]: | |
169 new_pos[0] = self.pos[0] | |
170 if new_pos[1] < 0: | |
171 new_pos[1] = self.pos[1] | |
172 elif new_pos[1] > self.level.y - SCREEN[1]: | |
173 new_pos[1] = self.pos[1] | |
174 self.pos = tuple(new_pos) | |
175 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
176 def draw(self, surface): |
51 | 177 if (self.cur_poly is not None and self.cur_poly in self.level.polygons |
178 and len(self.level.polygons[self.cur_poly])): | |
179 # We have an active polygon | |
180 mouse_pos = self._level_coordinates(self.mouse_pos) | |
181 else: | |
182 mouse_pos = None | |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
183 level.draw(surface, self.pos, mouse_pos, self.cur_poly, |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
184 self.filled_mode) |
51 | 185 |
115 | 186 def change_poly(self, new_poly): |
187 self.cur_poly = new_poly | |
188 if self.cur_poly is not None: | |
189 self.filled_mode = False | |
190 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
191 def key_down(self, ev): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
192 if ev.key == pgl.K_LEFT: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
193 self._move_view((-10, 0)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
194 elif ev.key == pgl.K_RIGHT: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
195 self._move_view((10, 0)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
196 elif ev.key == pgl.K_UP: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
197 self._move_view((0, -10)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
198 elif ev.key == pgl.K_DOWN: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
199 self._move_view((0, 10)) |
115 | 200 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6): |
201 self.change_poly(ev.key - pgl.K_0) | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
202 elif ev.key == pgl.K_0: |
115 | 203 self.change_poly(None) |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
204 elif ev.key == pgl.K_d and self.cur_poly: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
205 self.level.delete_point(self.cur_poly) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
206 elif ev.key == pgl.K_f: |
117 | 207 self.set_filled() |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
208 elif ev.key == pgl.K_c: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
209 self.close_poly() |
117 | 210 |
211 def set_filled(self): | |
165
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
212 closed, _ = self.level.all_closed() |
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
213 if closed: |
117 | 214 self.cur_poly = None |
215 self.filled_mode = True | |
216 else: | |
165
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
217 alert('Not all polygons closed, so not filling') |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
218 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
219 def mouse_move(self, ev): |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
220 old_pos = self.mouse_pos |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
221 self.mouse_pos = ev.pos |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
222 if self.cur_poly and old_pos != self.mouse_pos: |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
223 self.invalidate() |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
224 |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
225 def mouse_drag(self, ev): |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
226 if self._mouse_drag: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
227 old_pos = self.mouse_pos |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
228 self.mouse_pos = ev.pos |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
229 diff = (-self.mouse_pos[0] + old_pos[0], |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
230 -self.mouse_pos[1] + old_pos[1]) |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
231 self._move_view(diff) |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
232 self.invalidate() |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
233 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
234 def mouse_down(self, ev): |
184
e003b994c48b
Output coordinates from the level editor, to help place things
Stefano Rivera <stefano@rivera.za.net>
parents:
167
diff
changeset
|
235 if ev.button == 1: |
187
42a10c4f70c8
Err, in pymunk coordinates
Stefano Rivera <stefano@rivera.za.net>
parents:
184
diff
changeset
|
236 print "Click: %r" % ( |
42a10c4f70c8
Err, in pymunk coordinates
Stefano Rivera <stefano@rivera.za.net>
parents:
184
diff
changeset
|
237 self.level.point_to_pymunk(self._level_coordinates(ev.pos)),) |
157 | 238 if ev.button == 4: # Scroll up |
239 self._move_view((0, -10)) | |
240 elif ev.button == 5: # Scroll down | |
241 self._move_view((0, 10)) | |
242 elif ev.button == 6: # Scroll left | |
243 self._move_view((-10, 0)) | |
244 elif ev.button == 7: # Scroll right | |
245 self._move_view((10, 0)) | |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
246 elif self.cur_poly and ev.button == 1: |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
247 # Add a point |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
248 self.level.add_point(self.cur_poly, |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
249 self._level_coordinates(ev.pos)) |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
250 elif ev.button == 3: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
251 self._mouse_drag = True |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
252 |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
253 def mouse_up(self, ev): |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
254 if ev.button == 3: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
255 self._mouse_drag = False |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
256 |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
257 def close_poly(self): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
258 if self.cur_poly is None: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
259 return |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
260 if self.level.close_poly(self.cur_poly): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
261 alert("Successfully closed the polygon") |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
262 self.change_poly(None) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
263 else: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
264 alert("Failed to close the polygon") |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
265 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
266 |
115 | 267 class PolyButton(Button): |
268 """Button for coosing the correct polygon""" | |
269 | |
270 def __init__(self, index, level_widget): | |
271 if index is not None: | |
272 text = "Draw: %s" % index | |
273 else: | |
274 text = 'Exit Draw Mode' | |
275 super(PolyButton, self).__init__(text) | |
276 self.index = index | |
277 self.level_widget = level_widget | |
278 | |
279 def action(self): | |
280 self.level_widget.change_poly(self.index) | |
281 | |
282 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
283 class EditorApp(RootWidget): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
284 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
285 def __init__(self, level, surface): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
286 super(EditorApp, self).__init__(surface) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
287 self.level = level |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
288 self.level_widget = LevelWidget(self.level) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
289 self.add(self.level_widget) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
290 |
115 | 291 # Add poly buttons |
292 y = 15 | |
293 for poly in range(1, 7): | |
294 but = PolyButton(poly, self.level_widget) | |
295 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD, | |
296 MENU_BUTTON_HEIGHT) | |
297 if poly % 2: | |
298 but.rect.move_ip(MENU_LEFT, y) | |
299 else: | |
300 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD, | |
301 y) | |
302 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
303 self.add(but) | |
304 | |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
305 button_rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT) |
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
306 |
198 | 307 check_rect = pygame.rect.Rect(0, 0, MENU_BUTTON_HEIGHT // 2, |
308 MENU_BUTTON_HEIGHT // 2) | |
309 | |
115 | 310 end_poly_but = PolyButton(None, self.level_widget) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
311 end_poly_but.rect = button_rect.copy() |
115 | 312 end_poly_but.rect.move_ip(MENU_LEFT, y) |
313 self.add(end_poly_but) | |
314 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
315 | |
117 | 316 fill_but = Button('Fill exterior', action=self.level_widget.set_filled) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
317 fill_but.rect = button_rect.copy() |
117 | 318 fill_but.rect.move_ip(MENU_LEFT, y) |
319 self.add(fill_but) | |
320 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
321 | |
116 | 322 save_but = Button('Save Level', action=self.save) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
323 save_but.rect = button_rect.copy() |
116 | 324 save_but.rect.move_ip(MENU_LEFT, y) |
325 self.add(save_but) | |
326 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
327 | |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
328 close_poly_but = Button('Close Polygon', |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
329 action=self.level_widget.close_poly) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
330 close_poly_but.rect = button_rect.copy() |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
331 close_poly_but.rect.move_ip(MENU_LEFT, y) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
332 self.add(close_poly_but) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
333 y += MENU_BUTTON_HEIGHT + MENU_PAD |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
334 |
198 | 335 white = pygame.color.Color("white") |
336 self.show_objs = CheckBox(fg_color=white) | |
337 self.show_objs.rect = check_rect.copy() | |
338 self.show_objs.rect.move_ip(MENU_LEFT, y) | |
339 label = Label("Show Objects", fg_color=white) | |
340 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y) | |
341 self.add(self.show_objs) | |
342 self.add(label) | |
343 y += label.rect.height + MENU_PAD | |
344 | |
345 self.show_enemies = CheckBox(fg_color=white) | |
346 self.show_enemies.rect = check_rect.copy() | |
347 self.show_enemies.rect.move_ip(MENU_LEFT, y) | |
348 label = Label("Show enemy start pos", fg_color=white) | |
349 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y) | |
350 self.add(self.show_enemies) | |
351 self.add(label) | |
352 y += label.rect.height + MENU_PAD | |
353 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
354 quit_but = Button('Quit', action=self.quit) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
355 quit_but.rect = button_rect.copy() |
115 | 356 quit_but.rect.move_ip(MENU_LEFT, y) |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
357 self.add(quit_but) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
358 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
359 def key_down(self, ev): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
360 if ev.key == pgl.K_ESCAPE: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
361 self.quit() |
116 | 362 elif ev.key == pgl.K_s: |
363 self.save() | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
364 else: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
365 self.level_widget.key_down(ev) |
51 | 366 |
116 | 367 def save(self): |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
368 closed, messages = self.level.all_closed() |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
369 if closed: |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
370 self.level.save() |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
371 # display success |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
372 alert("Level %s saved successfully." % self.level.name) |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
373 else: |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
374 # display errors |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
375 alert("Failed to save level.\n\n%s" % '\n'.join(messages)) |
116 | 376 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
377 def mouse_move(self, ev): |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
378 self.level_widget.mouse_move(ev) |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
379 |
51 | 380 |
381 if __name__ == "__main__": | |
195
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
382 if len(sys.argv) not in [2, 4]: |
51 | 383 print 'Please supply a levelname or levelname and level size' |
384 sys.exit() | |
385 pygame.display.init() | |
386 pygame.font.init() | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
387 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]), |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
388 pgl.SWSURFACE) |
195
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
389 if len(sys.argv) == 2: |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
390 level = EditorLevel(sys.argv[1]) |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
391 level.load(pymunk.Space()) |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
392 elif len(sys.argv) == 4: |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
393 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3])) |
51 | 394 pygame.display.set_caption('Nagslang Area Editor') |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
395 pygame.key.set_repeat(200, 100) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
396 app = EditorApp(level, pygame.display.get_surface()) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
397 app.run() |