source: tools/area_editor.py@ 151:c59c04c3e971

Last change on this file since 151:c59c04c3e971 was 151:c59c04c3e971, checked in by Stefano Rivera <stefano@…>, 8 years ago

Revert that crazy hack, nobody was meant to see it

  • Property exe set to *
File size: 11.8 KB
Line 
1#!/usr/bin/env python
2
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
14import os
15import sys
16
17import pygame
18import pygame.locals as pgl
19
20from albow.root import RootWidget
21from albow.widget import Widget
22from albow.controls import Button
23from albow.dialogs import alert
24
25sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
26
27from nagslang.constants import SCREEN
28from nagslang.level import Level, POLY_COLORS
29
30
31# layout constants
32MENU_BUTTON_HEIGHT = 35
33MENU_PAD = 6
34MENU_HALF_PAD = MENU_PAD // 2
35MENU_LEFT = SCREEN[0] + MENU_HALF_PAD
36MENU_WIDTH = 200 - MENU_PAD
37
38
39class EditorLevel(Level):
40
41 def __init__(self, name, x=800, y=600):
42 super(EditorLevel, self).__init__(name)
43 self.x = x
44 self.y = y
45
46 def round_point(self, pos):
47 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
48
49 def point_to_pymunk(self, pos):
50 # inverse of point_to_pygame
51 # (this is also the same as point_to_pygame, but a additional
52 # function for sanity later in pyweek).
53 return (pos[0], self.y - pos[1])
54
55 def add_point(self, poly_index, pos):
56 self.polygons.setdefault(poly_index, [])
57 if not self.polygons[poly_index]:
58 point = self.point_to_pymunk(self.round_point(pos))
59 self.polygons[poly_index].append(point)
60 else:
61 add_pos = self.fix_angle(poly_index, pos)
62 self.polygons[poly_index].append(add_pos)
63
64 def fix_angle(self, index, pos):
65 # Last point
66 point1 = self.point_to_pygame(self.polygons[index][-1])
67 pos = self.round_point(pos)
68 # We want the line (point1 to pos) to be an angle of
69 # 0, 45, 90, 135, 180, 225, 270, 305
70 # However, we only need to consider half the circle
71 # This is a hack to approximate the right thing
72 pos0 = (pos[0], point1[1])
73 pos90 = (point1[0], pos[1])
74 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
75 pos45 = (point1[0] + dist, point1[1] + dist)
76 pos135 = (point1[0] + dist, point1[1] - dist)
77 pos225 = (point1[0] - dist, point1[1] - dist)
78 pos305 = (point1[0] - dist, point1[1] + dist)
79 min_dist = 9999999
80 new_pos = point1
81 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
82 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
83 if dist < min_dist:
84 new_pos = cand
85 min_dist = dist
86 return self.point_to_pymunk(new_pos)
87
88 def delete_point(self, index):
89 if index in self.polygons and len(self.polygons[index]) > 0:
90 self.polygons[index].pop()
91
92 def close_poly(self, index):
93 """Attempts to close the current polygon.
94
95 We allow a small additional step to close the polygon, but
96 it's limited as it's a magic point addition"""
97 if len(self.polygons[index]) < 2:
98 # Too small
99 return False
100 first = self.polygons[index][0]
101 last = self.polygons[index][-1]
102 print first, last
103 print self.fix_angle(index, self.point_to_pygame(first))
104 if self.fix_angle(index, self.point_to_pygame(first)) == first:
105 self.add_point(index, self.point_to_pygame(first))
106 return True
107 candidates = [(first[0] + 10 * i, first[1]) for
108 i in (-3, -2, -1, 1, 2, 3)]
109 candidates.extend([(first[0], first[1] + 10 * i) for
110 i in (-3, -2, -1, 1, 2, 3)])
111 candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
112 i in (-3, -2, -1, 1, 2, 3)])
113 candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
114 i in (-3, -2, -1, 1, 2, 3)])
115 min_dist = 99999
116 poss = None
117 for cand in candidates:
118 if self.fix_angle(index, self.point_to_pygame(cand)) == cand:
119 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
120 if dist < min_dist:
121 poss = cand
122 if poss is not None:
123 self.add_point(index, self.point_to_pygame(poss))
124 self.add_point(index, self.point_to_pygame(first))
125 return True
126 return False
127
128 def draw(self, surface, topleft, mouse_pos, mouse_poly, filled):
129 self._draw_background(True)
130 # Draw polygons as needed for the editor
131 if filled:
132 self._draw_exterior(True)
133 for index, polygon in self.polygons.items():
134 color = POLY_COLORS[index]
135 if len(polygon) > 1:
136 pointlist = [self.point_to_pygame(p) for p in polygon]
137 pygame.draw.lines(self._surface, color, False, pointlist, 2)
138 if index == mouse_poly and mouse_pos:
139 endpoint = self.fix_angle(index, mouse_pos)
140 pygame.draw.line(self._surface, color,
141 self.point_to_pygame(polygon[-1]),
142 self.point_to_pygame(endpoint))
143 surface_area = pygame.rect.Rect(topleft, SCREEN)
144 surface.blit(self._surface, (0, 0), surface_area)
145
146
147class LevelWidget(Widget):
148
149 def __init__(self, level):
150 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
151 SCREEN[0], SCREEN[1]))
152 self.level = level
153 self.pos = (0, 0)
154 self.filled_mode = False
155 self.mouse_pos = None
156 self.cur_poly = None
157
158 def _level_coordinates(self, pos):
159 # Move positions to level values
160 if not pos:
161 return (0, 0)
162 return pos[0] + self.pos[0], pos[1] + self.pos[1]
163
164 def _move_view(self, offset):
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
176 def draw(self, surface):
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
183 level.draw(surface, self.pos, mouse_pos, self.cur_poly,
184 self.filled_mode)
185
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
191 def key_down(self, ev):
192 if ev.key == pgl.K_LEFT:
193 self._move_view((-10, 0))
194 elif ev.key == pgl.K_RIGHT:
195 self._move_view((10, 0))
196 elif ev.key == pgl.K_UP:
197 self._move_view((0, -10))
198 elif ev.key == pgl.K_DOWN:
199 self._move_view((0, 10))
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)
202 elif ev.key == pgl.K_0:
203 self.change_poly(None)
204 elif ev.key == pgl.K_d and self.cur_poly:
205 self.level.delete_point(self.cur_poly)
206 elif ev.key == pgl.K_f:
207 self.set_filled()
208 elif ev.key == pgl.K_c:
209 self.close_poly()
210
211 def set_filled(self):
212 if level.all_closed():
213 self.cur_poly = None
214 self.filled_mode = True
215 else:
216 print 'Not all polygons closed, so not filling'
217
218 def mouse_move(self, ev):
219 old_pos = self.mouse_pos
220 self.mouse_pos = ev.pos
221 if self.cur_poly and old_pos != self.mouse_pos:
222 self.invalidate()
223
224 def mouse_down(self, ev):
225 if self.cur_poly:
226 # Add a point
227 self.level.add_point(self.cur_poly,
228 self._level_coordinates(ev.pos))
229
230 def close_poly(self):
231 if self.cur_poly is None:
232 return
233 if self.level.close_poly(self.cur_poly):
234 alert("Successfully closed the polygon")
235 self.change_poly(None)
236 else:
237 alert("Failed to close the polygon")
238
239
240class PolyButton(Button):
241 """Button for coosing the correct polygon"""
242
243 def __init__(self, index, level_widget):
244 if index is not None:
245 text = "Draw: %s" % index
246 else:
247 text = 'Exit Draw Mode'
248 super(PolyButton, self).__init__(text)
249 self.index = index
250 self.level_widget = level_widget
251
252 def action(self):
253 self.level_widget.change_poly(self.index)
254
255
256class EditorApp(RootWidget):
257
258 def __init__(self, level, surface):
259 super(EditorApp, self).__init__(surface)
260 self.level = level
261 self.level_widget = LevelWidget(self.level)
262 self.add(self.level_widget)
263
264 # Add poly buttons
265 y = 15
266 for poly in range(1, 7):
267 but = PolyButton(poly, self.level_widget)
268 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD,
269 MENU_BUTTON_HEIGHT)
270 if poly % 2:
271 but.rect.move_ip(MENU_LEFT, y)
272 else:
273 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD,
274 y)
275 y += MENU_BUTTON_HEIGHT + MENU_PAD
276 self.add(but)
277
278 button_rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
279
280 end_poly_but = PolyButton(None, self.level_widget)
281 end_poly_but.rect = button_rect.copy()
282 end_poly_but.rect.move_ip(MENU_LEFT, y)
283 self.add(end_poly_but)
284 y += MENU_BUTTON_HEIGHT + MENU_PAD
285
286 fill_but = Button('Fill exterior', action=self.level_widget.set_filled)
287 fill_but.rect = button_rect.copy()
288 fill_but.rect.move_ip(MENU_LEFT, y)
289 self.add(fill_but)
290 y += MENU_BUTTON_HEIGHT + MENU_PAD
291
292 save_but = Button('Save Level', action=self.save)
293 save_but.rect = button_rect.copy()
294 save_but.rect.move_ip(MENU_LEFT, y)
295 self.add(save_but)
296 y += MENU_BUTTON_HEIGHT + MENU_PAD
297
298 close_poly_but = Button('Close Polygon',
299 action=self.level_widget.close_poly)
300 close_poly_but.rect = button_rect.copy()
301 close_poly_but.rect.move_ip(MENU_LEFT, y)
302 self.add(close_poly_but)
303 y += MENU_BUTTON_HEIGHT + MENU_PAD
304
305 quit_but = Button('Quit', action=self.quit)
306 quit_but.rect = button_rect.copy()
307 quit_but.rect.move_ip(MENU_LEFT, y)
308 self.add(quit_but)
309
310 def key_down(self, ev):
311 if ev.key == pgl.K_ESCAPE:
312 self.quit()
313 elif ev.key == pgl.K_s:
314 self.save()
315 else:
316 self.level_widget.key_down(ev)
317
318 def save(self):
319 closed, messages = self.level.all_closed()
320 if closed:
321 self.level.save()
322 # display success
323 alert("Level %s saved successfully." % self.level.name)
324 else:
325 # display errors
326 alert("Failed to save level.\n\n%s" % '\n'.join(messages))
327
328 def mouse_move(self, ev):
329 self.level_widget.mouse_move(ev)
330
331
332if __name__ == "__main__":
333 if len(sys.argv) == 2:
334 level = EditorLevel(sys.argv[1])
335 level.load()
336 elif len(sys.argv) == 4:
337 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
338 else:
339 print 'Please supply a levelname or levelname and level size'
340 sys.exit()
341 pygame.display.init()
342 pygame.font.init()
343 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
344 pgl.SWSURFACE)
345 pygame.display.set_caption('Nagslang Area Editor')
346 pygame.key.set_repeat(200, 100)
347 app = EditorApp(level, pygame.display.get_surface())
348 app.run()
Note: See TracBrowser for help on using the repository browser.