source: tools/area_editor.py@ 220:06c52529e2ed

Last change on this file since 220:06c52529e2ed was 220:06c52529e2ed, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Add placeholder object mode menu

  • Property exe set to *
File size: 18.7 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
20sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
21
22import pymunk
23
24from albow.root import RootWidget
25from albow.widget import Widget
26from albow.controls import Button, Label, CheckBox
27from albow.dialogs import alert
28
29from nagslang.options import parse_args
30from nagslang.constants import SCREEN
31from nagslang.level import Level, POLY_COLORS, LINE_COLOR
32from nagslang.enemies import Enemy
33
34
35# layout constants
36MENU_BUTTON_HEIGHT = 35
37MENU_PAD = 6
38MENU_HALF_PAD = MENU_PAD // 2
39MENU_LEFT = SCREEN[0] + MENU_HALF_PAD
40MENU_WIDTH = 200 - MENU_PAD
41
42BUTTON_RECT = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
43CHECK_RECT = pygame.rect.Rect(0, 0, MENU_BUTTON_HEIGHT // 2,
44 MENU_BUTTON_HEIGHT // 2)
45
46
47class EditorLevel(Level):
48
49 def __init__(self, name, x=800, y=600):
50 super(EditorLevel, self).__init__(name)
51 self.x = x
52 self.y = y
53
54 def round_point(self, pos):
55 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
56
57 def point_to_pymunk(self, pos):
58 # inverse of point_to_pygame
59 # (this is also the same as point_to_pygame, but a additional
60 # function for sanity later in pyweek).
61 return (pos[0], self.y - pos[1])
62
63 def add_point(self, poly_index, pos):
64 self.polygons.setdefault(poly_index, [])
65 if not self.polygons[poly_index]:
66 point = self.point_to_pymunk(self.round_point(pos))
67 self.polygons[poly_index].append(point)
68 else:
69 add_pos = self.fix_poly_angle(poly_index, pos)
70 self.polygons[poly_index].append(add_pos)
71
72 def _fix_angle(self, point1, pos):
73 # We want the line (point1 to pos) to be an angle of
74 # 0, 45, 90, 135, 180, 225, 270, 305
75 # However, we only need to consider half the circle
76 # This is a hack to approximate the right thing
77 pos0 = (pos[0], point1[1])
78 pos90 = (point1[0], pos[1])
79 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
80 pos45 = (point1[0] + dist, point1[1] + dist)
81 pos135 = (point1[0] + dist, point1[1] - dist)
82 pos225 = (point1[0] - dist, point1[1] - dist)
83 pos305 = (point1[0] - dist, point1[1] + dist)
84 min_dist = 9999999
85 new_pos = point1
86 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
87 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
88 if dist < min_dist:
89 new_pos = cand
90 min_dist = dist
91 return self.point_to_pymunk(new_pos)
92
93 def fix_line_angle(self, start_pos, pos):
94 start_pos = self.round_point(start_pos)
95 pos = self.round_point(pos)
96 return self._fix_angle(start_pos, pos)
97
98 def fix_poly_angle(self, index, pos):
99 # Last point
100 point1 = self.point_to_pygame(self.polygons[index][-1])
101 pos = self.round_point(pos)
102 return self._fix_angle(point1, pos)
103
104 def delete_point(self, index):
105 if index in self.polygons and len(self.polygons[index]) > 0:
106 self.polygons[index].pop()
107
108 def close_poly(self, index):
109 """Attempts to close the current polygon.
110
111 We allow a small additional step to close the polygon, but
112 it's limited as it's a magic point addition"""
113 if len(self.polygons[index]) < 2:
114 # Too small
115 return False
116 first = self.polygons[index][0]
117 if self.fix_poly_angle(index, self.point_to_pygame(first)) == first:
118 self.add_point(index, self.point_to_pygame(first))
119 return True
120 candidates = [(first[0] + 10 * i, first[1]) for
121 i in (-3, -2, -1, 1, 2, 3)]
122 candidates.extend([(first[0], first[1] + 10 * i) for
123 i in (-3, -2, -1, 1, 2, 3)])
124 candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
125 i in (-3, -2, -1, 1, 2, 3)])
126 candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
127 i in (-3, -2, -1, 1, 2, 3)])
128 min_dist = 99999
129 poss = None
130 for cand in candidates:
131 if self.fix_poly_angle(index, self.point_to_pygame(cand)) == cand:
132 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
133 if dist < min_dist:
134 poss = cand
135 if poss is not None:
136 self.add_point(index, self.point_to_pygame(poss))
137 self.add_point(index, self.point_to_pygame(first))
138 return True
139 return False
140
141 def add_line(self, start_pos, end_pos):
142 endpoint = self.fix_line_angle(start_pos, end_pos)
143 startpoint = self.point_to_pymunk(self.round_point(start_pos))
144 self.lines.append([startpoint, endpoint])
145
146 def draw(self, mouse_pos, mouse_poly, filled, draw_cand_line, start_pos):
147 self._draw_background(True)
148 # Draw polygons as needed for the editor
149 if filled:
150 self._draw_exterior(True)
151 for index, polygon in self.polygons.items():
152 color = POLY_COLORS[index]
153 if len(polygon) > 1:
154 pointlist = [self.point_to_pygame(p) for p in polygon]
155 pygame.draw.lines(self._surface, color, False, pointlist, 2)
156 if index == mouse_poly and mouse_pos:
157 endpoint = self.fix_poly_angle(index, mouse_pos)
158 pygame.draw.line(self._surface, color,
159 self.point_to_pygame(polygon[-1]),
160 self.point_to_pygame(endpoint))
161 for line in self.lines:
162 pointlist = [self.point_to_pygame(p) for p in line]
163 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
164 if draw_cand_line and start_pos and mouse_pos:
165 endpoint = self.fix_line_angle(start_pos, mouse_pos)
166 pointlist = [self.round_point(start_pos),
167 self.point_to_pygame(endpoint)]
168 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 1)
169 return self._surface.copy()
170
171
172class LevelWidget(Widget):
173
174 def __init__(self, level):
175 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
176 SCREEN[0], SCREEN[1]))
177 self.level = level
178 self.pos = (0, 0)
179 self.filled_mode = False
180 self.mouse_pos = None
181 self.cur_poly = None
182 self._mouse_drag = False
183 self._draw_objects = False
184 self._draw_enemies = False
185 self._draw_lines = False
186 self._start_pos = None
187
188 def _level_coordinates(self, pos):
189 # Move positions to level values
190 if not pos:
191 return (0, 0)
192 return pos[0] + self.pos[0], pos[1] + self.pos[1]
193
194 def _move_view(self, offset):
195 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
196 if new_pos[0] < 0:
197 new_pos[0] = self.pos[0]
198 elif new_pos[0] > self.level.x - SCREEN[0]:
199 new_pos[0] = self.pos[0]
200 if new_pos[1] < 0:
201 new_pos[1] = self.pos[1]
202 elif new_pos[1] > self.level.y - SCREEN[1]:
203 new_pos[1] = self.pos[1]
204 self.pos = tuple(new_pos)
205
206 def set_objects(self, value):
207 if self._draw_objects != value:
208 self._draw_objects = value
209 self.invalidate()
210
211 def set_enemies(self, value):
212 if self._draw_enemies != value:
213 self._draw_enemies = value
214 self.invalidate()
215
216 def draw(self, surface):
217 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
218 and len(self.level.polygons[self.cur_poly])):
219 # We have an active polygon
220 mouse_pos = self._level_coordinates(self.mouse_pos)
221 elif self._draw_lines:
222 # Interior wall mode
223 mouse_pos = self._level_coordinates(self.mouse_pos)
224 else:
225 mouse_pos = None
226 level_surface = level.draw(mouse_pos, self.cur_poly, self.filled_mode,
227 self._draw_lines, self._start_pos)
228 if self._draw_objects:
229 for thing in self.level.drawables:
230 if not isinstance(thing, Enemy):
231 thing.render(level_surface)
232 if self._draw_enemies:
233 for thing in self.level.drawables:
234 if isinstance(thing, Enemy):
235 thing.render(level_surface)
236 surface_area = pygame.rect.Rect(self.pos, SCREEN)
237 surface.blit(level_surface, (0, 0), surface_area)
238
239 def change_poly(self, new_poly):
240 self.cur_poly = new_poly
241 self._draw_lines = False
242 if self.cur_poly is not None:
243 self.filled_mode = False
244
245 def line_mode(self):
246 self.cur_poly = None
247 self._draw_lines = True
248 self.filled_mode = False
249 self._start_pos = None
250
251 def key_down(self, ev):
252 if ev.key == pgl.K_LEFT:
253 self._move_view((-10, 0))
254 elif ev.key == pgl.K_RIGHT:
255 self._move_view((10, 0))
256 elif ev.key == pgl.K_UP:
257 self._move_view((0, -10))
258 elif ev.key == pgl.K_DOWN:
259 self._move_view((0, 10))
260 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6):
261 self.change_poly(ev.key - pgl.K_0)
262 elif ev.key == pgl.K_0:
263 self.change_poly(None)
264 elif ev.key == pgl.K_d and self.cur_poly:
265 self.level.delete_point(self.cur_poly)
266 elif ev.key == pgl.K_f:
267 self.set_filled()
268 elif ev.key == pgl.K_c:
269 self.close_poly()
270
271 def set_filled(self):
272 closed, _ = self.level.all_closed()
273 if closed:
274 self.cur_poly = None
275 self.filled_mode = True
276 self._draw_lines = False
277 else:
278 alert('Not all polygons closed, so not filling')
279
280 def mouse_move(self, ev):
281 old_pos = self.mouse_pos
282 self.mouse_pos = ev.pos
283 if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines):
284 self.invalidate()
285
286 def mouse_drag(self, ev):
287 if self._mouse_drag:
288 old_pos = self.mouse_pos
289 self.mouse_pos = ev.pos
290 diff = (-self.mouse_pos[0] + old_pos[0],
291 -self.mouse_pos[1] + old_pos[1])
292 self._move_view(diff)
293 self.invalidate()
294
295 def mouse_down(self, ev):
296 if ev.button == 1:
297 if self._draw_lines:
298 if self._start_pos is None:
299 self._start_pos = ev.pos
300 else:
301 self.level.add_line(self._start_pos, ev.pos)
302 self._start_pos = None
303 else:
304 print "Click: %r" % (
305 self.level.point_to_pymunk(
306 self._level_coordinates(ev.pos)),)
307 if ev.button == 4: # Scroll up
308 self._move_view((0, -10))
309 elif ev.button == 5: # Scroll down
310 self._move_view((0, 10))
311 elif ev.button == 6: # Scroll left
312 self._move_view((-10, 0))
313 elif ev.button == 7: # Scroll right
314 self._move_view((10, 0))
315 elif self.cur_poly and ev.button == 1:
316 # Add a point
317 self.level.add_point(self.cur_poly,
318 self._level_coordinates(ev.pos))
319 elif ev.button == 3:
320 self._mouse_drag = True
321
322 def mouse_up(self, ev):
323 if ev.button == 3:
324 self._mouse_drag = False
325
326 def close_poly(self):
327 if self.cur_poly is None:
328 return
329 if self.level.close_poly(self.cur_poly):
330 alert("Successfully closed the polygon")
331 self.change_poly(None)
332 else:
333 alert("Failed to close the polygon")
334
335
336class PolyButton(Button):
337 """Button for coosing the correct polygon"""
338
339 def __init__(self, index, level_widget):
340 if index is not None:
341 text = "Draw: %s" % index
342 else:
343 text = 'Exit Draw Mode'
344 super(PolyButton, self).__init__(text)
345 self.index = index
346 self.level_widget = level_widget
347
348 def action(self):
349 self.level_widget.change_poly(self.index)
350
351
352class EditorApp(RootWidget):
353
354 def __init__(self, level, surface):
355 super(EditorApp, self).__init__(surface)
356 self.level = level
357 self.level_widget = LevelWidget(self.level)
358 self.add(self.level_widget)
359
360 self._dMenus = {}
361
362 self._make_draw_menu()
363 self._make_objects_menu()
364
365 self._menu_mode = 'drawing'
366 self._populate_menu()
367
368 def _make_draw_menu(self):
369 widgets = []
370
371 # Add poly buttons
372 y = 15
373 for poly in range(1, 7):
374 but = PolyButton(poly, self.level_widget)
375 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD,
376 MENU_BUTTON_HEIGHT)
377 if poly % 2:
378 but.rect.move_ip(MENU_LEFT, y)
379 else:
380 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD,
381 y)
382 y += MENU_BUTTON_HEIGHT + MENU_PAD
383 widgets.append(but)
384
385 end_poly_but = PolyButton(None, self.level_widget)
386 end_poly_but.rect = BUTTON_RECT.copy()
387 end_poly_but.rect.move_ip(MENU_LEFT, y)
388 widgets.append(end_poly_but)
389 y += MENU_BUTTON_HEIGHT + MENU_PAD
390
391 draw_line = Button("Draw interior wall", self.level_widget.line_mode)
392 draw_line.rect = BUTTON_RECT.copy()
393 draw_line.rect.move_ip(MENU_LEFT, y)
394 widgets.append(draw_line)
395 y += MENU_BUTTON_HEIGHT + MENU_PAD
396
397 fill_but = Button('Fill exterior', action=self.level_widget.set_filled)
398 fill_but.rect = BUTTON_RECT.copy()
399 fill_but.rect.move_ip(MENU_LEFT, y)
400 widgets.append(fill_but)
401 y += MENU_BUTTON_HEIGHT + MENU_PAD
402
403 save_but = Button('Save Level', action=self.save)
404 save_but.rect = BUTTON_RECT.copy()
405 save_but.rect.move_ip(MENU_LEFT, y)
406 widgets.append(save_but)
407 y += MENU_BUTTON_HEIGHT + MENU_PAD
408
409 close_poly_but = Button('Close Polygon',
410 action=self.level_widget.close_poly)
411 close_poly_but.rect = BUTTON_RECT.copy()
412 close_poly_but.rect.move_ip(MENU_LEFT, y)
413 widgets.append(close_poly_but)
414 y += MENU_BUTTON_HEIGHT + MENU_PAD
415
416 white = pygame.color.Color("white")
417 self.show_objs = CheckBox(fg_color=white)
418 self.show_objs.rect = CHECK_RECT.copy()
419 self.show_objs.rect.move_ip(MENU_LEFT, y)
420 label = Label("Show Objects", fg_color=white)
421 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
422 widgets.append(self.show_objs)
423 widgets.append(label)
424 y += label.rect.height + MENU_PAD
425
426 self.show_enemies = CheckBox(fg_color=white)
427 self.show_enemies.rect = CHECK_RECT.copy()
428 self.show_enemies.rect.move_ip(MENU_LEFT, y)
429 label = Label("Show enemy start pos", fg_color=white)
430 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
431 widgets.append(self.show_enemies)
432 widgets.append(label)
433 y += label.rect.height + MENU_PAD
434
435 switch_but = Button('Switch to Objects', action=self.switch_to_objects)
436 switch_but.rect = BUTTON_RECT.copy()
437 switch_but.rect.move_ip(MENU_LEFT, y)
438 widgets.append(switch_but)
439 y += switch_but.rect.height + MENU_PAD
440
441 quit_but = Button('Quit', action=self.quit)
442 quit_but.rect = BUTTON_RECT.copy()
443 quit_but.rect.move_ip(MENU_LEFT, y)
444 widgets.append(quit_but)
445
446 self._dMenus['drawing'] = widgets
447
448 def _make_objects_menu(self):
449 widgets = []
450
451 # Add poly buttons
452 y = 15
453
454 save_but = Button('Save Level', action=self.save)
455 save_but.rect = BUTTON_RECT.copy()
456 save_but.rect.move_ip(MENU_LEFT, y)
457 widgets.append(save_but)
458 y += MENU_BUTTON_HEIGHT + MENU_PAD
459
460 switch_but = Button('Switch to Drawing', action=self.switch_to_draw)
461 switch_but.rect = BUTTON_RECT.copy()
462 switch_but.rect.move_ip(MENU_LEFT, y)
463 widgets.append(switch_but)
464 y += switch_but.rect.height + MENU_PAD
465
466 quit_but = Button('Quit', action=self.quit)
467 quit_but.rect = BUTTON_RECT.copy()
468 quit_but.rect.move_ip(MENU_LEFT, y)
469 widgets.append(quit_but)
470
471 self._dMenus['objects'] = widgets
472
473 def key_down(self, ev):
474 if ev.key == pgl.K_ESCAPE:
475 self.quit()
476 elif ev.key == pgl.K_s:
477 self.save()
478 else:
479 self.level_widget.key_down(ev)
480
481 def save(self):
482 closed, messages = self.level.all_closed()
483 if closed:
484 self.level.save()
485 # display success
486 alert("Level %s saved successfully." % self.level.name)
487 else:
488 # display errors
489 alert("Failed to save level.\n\n%s" % '\n'.join(messages))
490
491 def switch_to_draw(self):
492 if self._menu_mode != 'drawing':
493 self._clear_menu()
494 self._menu_mode = 'drawing'
495 self._populate_menu()
496
497 def switch_to_objects(self):
498 if self._menu_mode != 'objects':
499 self._clear_menu()
500 self._menu_mode = 'objects'
501 self._populate_menu()
502
503 def _clear_menu(self):
504 for widget in self._dMenus[self._menu_mode]:
505 self.remove(widget)
506
507 def _populate_menu(self):
508 self.level_widget.change_poly(None)
509 for widget in self._dMenus[self._menu_mode]:
510 self.add(widget)
511 self.invalidate()
512
513 def mouse_move(self, ev):
514 self.level_widget.mouse_move(ev)
515
516 def draw(self, surface):
517 # Update checkbox state
518 self.level_widget.set_objects(self.show_objs.value)
519 self.level_widget.set_enemies(self.show_enemies.value)
520 super(EditorApp, self).draw(surface)
521
522
523if __name__ == "__main__":
524 if len(sys.argv) not in [2, 4]:
525 print 'Please supply a levelname or levelname and level size'
526 sys.exit()
527 # Need to ensure we have defaults for rendering
528 parse_args([])
529 pygame.display.init()
530 pygame.font.init()
531 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
532 pgl.SWSURFACE)
533 if len(sys.argv) == 2:
534 level = EditorLevel(sys.argv[1])
535 level.load(pymunk.Space())
536 elif len(sys.argv) == 4:
537 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
538 pygame.display.set_caption('Nagslang Area Editor')
539 pygame.key.set_repeat(200, 100)
540 app = EditorApp(level, pygame.display.get_surface())
541 app.run()
Note: See TracBrowser for help on using the repository browser.