source: tools/area_editor.py@ 239:30137dc83a72

Last change on this file since 239:30137dc83a72 was 239:30137dc83a72, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Can has delete objects

  • Property exe set to *
File size: 24.6 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, Dialog
28from albow.layout import Row
29from albow.table_view import TableView, TableColumn
30
31from nagslang.options import parse_args
32from nagslang.constants import SCREEN
33from nagslang.level import Level, POLY_COLORS, LINE_COLOR
34from nagslang.enemies import Enemy, get_editable_enemies
35from nagslang.game_object import get_editable_game_objects
36from nagslang.puzzle import get_editable_puzzlers, PuzzleGlue
37
38# layout constants
39MENU_BUTTON_HEIGHT = 35
40MENU_PAD = 6
41MENU_HALF_PAD = MENU_PAD // 2
42MENU_LEFT = SCREEN[0] + MENU_HALF_PAD
43MENU_WIDTH = 200 - MENU_PAD
44
45BUTTON_RECT = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
46CHECK_RECT = pygame.rect.Rect(0, 0, MENU_BUTTON_HEIGHT // 2,
47 MENU_BUTTON_HEIGHT // 2)
48
49
50class EditorLevel(Level):
51
52 def __init__(self, name, x=800, y=600):
53 super(EditorLevel, self).__init__(name)
54 self.x = x
55 self.y = y
56
57 def round_point(self, pos):
58 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
59
60 def point_to_pymunk(self, pos):
61 # inverse of point_to_pygame
62 # (this is also the same as point_to_pygame, but a additional
63 # function for sanity later in pyweek).
64 return (pos[0], self.y - pos[1])
65
66 def add_point(self, poly_index, pos):
67 self.polygons.setdefault(poly_index, [])
68 if not self.polygons[poly_index]:
69 point = self.point_to_pymunk(self.round_point(pos))
70 self.polygons[poly_index].append(point)
71 else:
72 add_pos = self.fix_poly_angle(poly_index, pos)
73 self.polygons[poly_index].append(add_pos)
74
75 def _fix_angle(self, point1, pos):
76 # We want the line (point1 to pos) to be an angle of
77 # 0, 45, 90, 135, 180, 225, 270, 305
78 # However, we only need to consider half the circle
79 # This is a hack to approximate the right thing
80 pos0 = (pos[0], point1[1])
81 pos90 = (point1[0], pos[1])
82 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
83 pos45 = (point1[0] + dist, point1[1] + dist)
84 pos135 = (point1[0] + dist, point1[1] - dist)
85 pos225 = (point1[0] - dist, point1[1] - dist)
86 pos305 = (point1[0] - dist, point1[1] + dist)
87 min_dist = 9999999
88 new_pos = point1
89 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
90 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
91 if dist < min_dist:
92 new_pos = cand
93 min_dist = dist
94 return self.point_to_pymunk(new_pos)
95
96 def fix_line_angle(self, start_pos, pos):
97 start_pos = self.round_point(start_pos)
98 pos = self.round_point(pos)
99 return self._fix_angle(start_pos, pos)
100
101 def fix_poly_angle(self, index, pos):
102 # Last point
103 point1 = self.point_to_pygame(self.polygons[index][-1])
104 pos = self.round_point(pos)
105 return self._fix_angle(point1, pos)
106
107 def delete_point(self, index):
108 if index in self.polygons and len(self.polygons[index]) > 0:
109 self.polygons[index].pop()
110
111 def close_poly(self, index):
112 """Attempts to close the current polygon.
113
114 We allow a small additional step to close the polygon, but
115 it's limited as it's a magic point addition"""
116 if len(self.polygons[index]) < 2:
117 # Too small
118 return False
119 first = self.polygons[index][0]
120 if self.fix_poly_angle(index, self.point_to_pygame(first)) == first:
121 self.add_point(index, self.point_to_pygame(first))
122 return True
123 candidates = [(first[0] + 10 * i, first[1]) for
124 i in (-3, -2, -1, 1, 2, 3)]
125 candidates.extend([(first[0], first[1] + 10 * i) for
126 i in (-3, -2, -1, 1, 2, 3)])
127 candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
128 i in (-3, -2, -1, 1, 2, 3)])
129 candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
130 i in (-3, -2, -1, 1, 2, 3)])
131 min_dist = 99999
132 poss = None
133 for cand in candidates:
134 if self.fix_poly_angle(index, self.point_to_pygame(cand)) == cand:
135 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
136 if dist < min_dist:
137 poss = cand
138 if poss is not None:
139 self.add_point(index, self.point_to_pygame(poss))
140 self.add_point(index, self.point_to_pygame(first))
141 return True
142 return False
143
144 def add_line(self, start_pos, end_pos):
145 endpoint = self.fix_line_angle(start_pos, end_pos)
146 startpoint = self.point_to_pymunk(self.round_point(start_pos))
147 self.lines.append([startpoint, endpoint])
148
149 def draw(self, mouse_pos, mouse_poly, filled, draw_cand_line, start_pos):
150 self._draw_background(True)
151 # Draw polygons as needed for the editor
152 if filled:
153 self._draw_exterior(True)
154 for index, polygon in self.polygons.items():
155 color = POLY_COLORS[index]
156 if len(polygon) > 1:
157 pointlist = [self.point_to_pygame(p) for p in polygon]
158 pygame.draw.lines(self._surface, color, False, pointlist, 2)
159 if index == mouse_poly and mouse_pos:
160 endpoint = self.fix_poly_angle(index, mouse_pos)
161 pygame.draw.line(self._surface, color,
162 self.point_to_pygame(polygon[-1]),
163 self.point_to_pygame(endpoint))
164 for line in self.lines:
165 pointlist = [self.point_to_pygame(p) for p in line]
166 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
167 if draw_cand_line and start_pos and mouse_pos:
168 endpoint = self.fix_line_angle(start_pos, mouse_pos)
169 pointlist = [self.round_point(start_pos),
170 self.point_to_pygame(endpoint)]
171 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 1)
172 return self._surface.copy()
173
174 def reset_objs(self):
175 # Reset the object state - needed when changing stuff
176 self.drawables = []
177 self.overlay_drawables = []
178 self._glue = PuzzleGlue()
179 for game_object_dict in self._game_objects:
180 self._create_game_object(pymunk.Space(), **game_object_dict)
181 for enemy_dict in self._enemies:
182 self._create_enemy(pymunk.Space(), **enemy_dict)
183
184
185class ObjectTable(TableView):
186
187 columns = [TableColumn("Object", 690, 'l', '%r')]
188
189 def __init__(self, data):
190 super(ObjectTable, self).__init__(height=450)
191 self.data = data
192 self.selected_row = -1
193
194 def num_rows(self):
195 return len(self.data)
196
197 def row_data(self, i):
198 data = self.data[i]
199 if 'name' in data:
200 return ('%s (%s)' % (data['classname'], data['name']), )
201 return (data['classname'], )
202
203 def row_is_selected(self, i):
204 return self.selected_row == i
205
206 def click_row(self, i, ev):
207 self.selected_row = i
208
209 def get_selection(self):
210 if self.selected_row >= 0:
211 return self.data[self.selected_row]
212 return None
213
214
215class LevelWidget(Widget):
216
217 def __init__(self, level):
218 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
219 SCREEN[0], SCREEN[1]))
220 self.level = level
221 self.pos = (0, 0)
222 self.filled_mode = False
223 self.mouse_pos = None
224 self.cur_poly = None
225 self._mouse_drag = False
226 self._draw_objects = False
227 self._draw_enemies = False
228 self._draw_lines = False
229 self._start_pos = None
230
231 def _level_coordinates(self, pos):
232 # Move positions to level values
233 if not pos:
234 return (0, 0)
235 return pos[0] + self.pos[0], pos[1] + self.pos[1]
236
237 def _move_view(self, offset):
238 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
239 if new_pos[0] < 0:
240 new_pos[0] = self.pos[0]
241 elif new_pos[0] > self.level.x - SCREEN[0]:
242 new_pos[0] = self.pos[0]
243 if new_pos[1] < 0:
244 new_pos[1] = self.pos[1]
245 elif new_pos[1] > self.level.y - SCREEN[1]:
246 new_pos[1] = self.pos[1]
247 self.pos = tuple(new_pos)
248
249 def set_objects(self, value):
250 if self._draw_objects != value:
251 self._draw_objects = value
252 self.invalidate()
253
254 def set_enemies(self, value):
255 if self._draw_enemies != value:
256 self._draw_enemies = value
257 self.invalidate()
258
259 def draw(self, surface):
260 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
261 and len(self.level.polygons[self.cur_poly])):
262 # We have an active polygon
263 mouse_pos = self._level_coordinates(self.mouse_pos)
264 elif self._draw_lines:
265 # Interior wall mode
266 mouse_pos = self._level_coordinates(self.mouse_pos)
267 else:
268 mouse_pos = None
269 level_surface = level.draw(mouse_pos, self.cur_poly, self.filled_mode,
270 self._draw_lines, self._start_pos)
271 if self._draw_objects:
272 for thing in self.level.drawables:
273 if not isinstance(thing, Enemy):
274 thing.render(level_surface)
275 if self._draw_enemies:
276 for thing in self.level.drawables:
277 if isinstance(thing, Enemy):
278 thing.render(level_surface)
279 surface_area = pygame.rect.Rect(self.pos, SCREEN)
280 surface.blit(level_surface, (0, 0), surface_area)
281
282 def change_poly(self, new_poly):
283 self.cur_poly = new_poly
284 self._draw_lines = False
285 if self.cur_poly is not None:
286 self.filled_mode = False
287
288 def line_mode(self):
289 self.cur_poly = None
290 self._draw_lines = True
291 self.filled_mode = False
292 self._start_pos = None
293
294 def key_down(self, ev):
295 if ev.key == pgl.K_LEFT:
296 self._move_view((-10, 0))
297 elif ev.key == pgl.K_RIGHT:
298 self._move_view((10, 0))
299 elif ev.key == pgl.K_UP:
300 self._move_view((0, -10))
301 elif ev.key == pgl.K_DOWN:
302 self._move_view((0, 10))
303 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6):
304 self.change_poly(ev.key - pgl.K_0)
305 elif ev.key == pgl.K_0:
306 self.change_poly(None)
307 elif ev.key == pgl.K_d and self.cur_poly:
308 self.level.delete_point(self.cur_poly)
309 elif ev.key == pgl.K_f:
310 self.set_filled()
311 elif ev.key == pgl.K_c:
312 self.close_poly()
313
314 def set_filled(self):
315 closed, _ = self.level.all_closed()
316 if closed:
317 self.cur_poly = None
318 self.filled_mode = True
319 self._draw_lines = False
320 else:
321 alert('Not all polygons closed, so not filling')
322
323 def mouse_move(self, ev):
324 old_pos = self.mouse_pos
325 self.mouse_pos = ev.pos
326 if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines):
327 self.invalidate()
328
329 def mouse_drag(self, ev):
330 if self._mouse_drag:
331 old_pos = self.mouse_pos
332 self.mouse_pos = ev.pos
333 diff = (-self.mouse_pos[0] + old_pos[0],
334 -self.mouse_pos[1] + old_pos[1])
335 self._move_view(diff)
336 self.invalidate()
337
338 def mouse_down(self, ev):
339 if ev.button == 1:
340 if self._draw_lines:
341 if self._start_pos is None:
342 self._start_pos = ev.pos
343 else:
344 self.level.add_line(self._start_pos, ev.pos)
345 self._start_pos = None
346 else:
347 print "Click: %r" % (
348 self.level.point_to_pymunk(
349 self._level_coordinates(ev.pos)),)
350 if ev.button == 4: # Scroll up
351 self._move_view((0, -10))
352 elif ev.button == 5: # Scroll down
353 self._move_view((0, 10))
354 elif ev.button == 6: # Scroll left
355 self._move_view((-10, 0))
356 elif ev.button == 7: # Scroll right
357 self._move_view((10, 0))
358 elif self.cur_poly and ev.button == 1:
359 # Add a point
360 self.level.add_point(self.cur_poly,
361 self._level_coordinates(ev.pos))
362 elif ev.button == 3:
363 self._mouse_drag = True
364
365 def mouse_up(self, ev):
366 if ev.button == 3:
367 self._mouse_drag = False
368
369 def close_poly(self):
370 if self.cur_poly is None:
371 return
372 if self.level.close_poly(self.cur_poly):
373 alert("Successfully closed the polygon")
374 self.change_poly(None)
375 else:
376 alert("Failed to close the polygon")
377
378 def _make_edit_dialog(self, entries):
379 # Dialog to hold the editor
380 edit_box = Dialog()
381 edit_box.rect = pygame.rect.Rect(0, 0, 700, 500)
382 table = ObjectTable(entries)
383 edit_box.add(table)
384 buttons = []
385 for text in ['OK', 'Delete', 'Cancel']:
386 but = Button(text, action=lambda x=text: edit_box.dismiss(x))
387 buttons.append(but)
388 row = Row(buttons)
389 row.rect = pygame.rect.Rect(0, 450, 700, 50)
390 edit_box.add(row)
391 edit_box.get_selection = lambda: table.get_selection()
392 return edit_box
393
394 def edit_objects(self):
395 edit_box = self._make_edit_dialog(self.level._game_objects)
396 res = edit_box.present()
397 choice = edit_box.get_selection()
398 if choice is None:
399 return
400 if res == 'OK':
401 # Edit object stuff goes here
402 pass
403 elif res == 'Delete':
404 self.level._game_objects.remove(choice)
405 self.level.reset_objs()
406
407 def edit_enemies(self):
408 edit_box = self._make_edit_dialog(self.level._enemies)
409 res = edit_box.present()
410 choice = edit_box.get_selection()
411 if choice is None:
412 return
413 if res == 'OK':
414 # Edit object stuff goes here
415 pass
416 elif res == 'Delete':
417 self.level._enemies.remove(choice)
418 self.level.reset_objs()
419
420 def _make_choice_dialog(self, classes):
421 # Dialog to hold the editor
422 data = []
423 for cls_name, cls in classes:
424 data.append({"classname": cls_name, "class": cls})
425 choice_box = Dialog()
426 choice_box.rect = pygame.rect.Rect(0, 0, 700, 500)
427 table = ObjectTable(data)
428 choice_box.add(table)
429 buttons = []
430 for text in ['OK', 'Cancel']:
431 but = Button(text, action=lambda x=text: choice_box.dismiss(x))
432 buttons.append(but)
433 row = Row(buttons)
434 row.rect = pygame.rect.Rect(0, 450, 700, 50)
435 choice_box.add(row)
436 choice_box.get_selection = lambda: table.get_selection()
437 return choice_box
438
439 def add_game_object(self):
440 classes = get_editable_game_objects()
441 choose = self._make_choice_dialog(classes)
442 res = choose.present()
443 choice = choose.get_selection()
444 if res == 'OK' and choice is not None:
445 pass
446
447 def add_enemy(self):
448 classes = get_editable_enemies()
449 choose = self._make_choice_dialog(classes)
450 res = choose.present()
451 choice = choose.get_selection()
452 if res == 'OK' and choice is not None:
453 pass
454
455 def add_puzzler(self):
456 classes = get_editable_puzzlers()
457 choose = self._make_choice_dialog(classes)
458 res = choose.present()
459 choice = choose.get_selection()
460 if res == 'OK' and choice is not None:
461 pass
462
463
464class PolyButton(Button):
465 """Button for coosing the correct polygon"""
466
467 def __init__(self, index, level_widget):
468 if index is not None:
469 text = "Draw: %s" % index
470 else:
471 text = 'Exit Draw Mode'
472 super(PolyButton, self).__init__(text)
473 self.index = index
474 self.level_widget = level_widget
475
476 def action(self):
477 self.level_widget.change_poly(self.index)
478
479
480class EditorApp(RootWidget):
481
482 def __init__(self, level, surface):
483 super(EditorApp, self).__init__(surface)
484 self.level = level
485 self.level_widget = LevelWidget(self.level)
486 self.add(self.level_widget)
487
488 self._dMenus = {}
489
490 self._make_draw_menu()
491 self._make_objects_menu()
492
493 self._menu_mode = 'drawing'
494 self._populate_menu()
495
496 def _make_draw_menu(self):
497 widgets = []
498
499 # Add poly buttons
500 y = 15
501 for poly in range(1, 7):
502 but = PolyButton(poly, self.level_widget)
503 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD,
504 MENU_BUTTON_HEIGHT)
505 if poly % 2:
506 but.rect.move_ip(MENU_LEFT, y)
507 else:
508 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD,
509 y)
510 y += MENU_BUTTON_HEIGHT + MENU_PAD
511 widgets.append(but)
512
513 end_poly_but = PolyButton(None, self.level_widget)
514 end_poly_but.rect = BUTTON_RECT.copy()
515 end_poly_but.rect.move_ip(MENU_LEFT, y)
516 widgets.append(end_poly_but)
517 y += MENU_BUTTON_HEIGHT + MENU_PAD
518
519 draw_line = Button("Draw interior wall", self.level_widget.line_mode)
520 draw_line.rect = BUTTON_RECT.copy()
521 draw_line.rect.move_ip(MENU_LEFT, y)
522 widgets.append(draw_line)
523 y += MENU_BUTTON_HEIGHT + MENU_PAD
524
525 fill_but = Button('Fill exterior', action=self.level_widget.set_filled)
526 fill_but.rect = BUTTON_RECT.copy()
527 fill_but.rect.move_ip(MENU_LEFT, y)
528 widgets.append(fill_but)
529 y += MENU_BUTTON_HEIGHT + MENU_PAD
530
531 close_poly_but = Button('Close Polygon',
532 action=self.level_widget.close_poly)
533 close_poly_but.rect = BUTTON_RECT.copy()
534 close_poly_but.rect.move_ip(MENU_LEFT, y)
535 widgets.append(close_poly_but)
536 y += MENU_BUTTON_HEIGHT + MENU_PAD
537
538 white = pygame.color.Color("white")
539 self.show_objs = CheckBox(fg_color=white)
540 self.show_objs.rect = CHECK_RECT.copy()
541 self.show_objs.rect.move_ip(MENU_LEFT, y)
542 label = Label("Show Objects", fg_color=white)
543 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
544 widgets.append(self.show_objs)
545 widgets.append(label)
546 y += label.rect.height + MENU_PAD
547
548 self.show_enemies = CheckBox(fg_color=white)
549 self.show_enemies.rect = CHECK_RECT.copy()
550 self.show_enemies.rect.move_ip(MENU_LEFT, y)
551 label = Label("Show enemy start pos", fg_color=white)
552 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
553 widgets.append(self.show_enemies)
554 widgets.append(label)
555 y += label.rect.height + MENU_PAD
556
557 y += MENU_PAD
558 switch_but = Button('Switch to Objects', action=self.switch_to_objects)
559 switch_but.rect = BUTTON_RECT.copy()
560 switch_but.rect.move_ip(MENU_LEFT, y)
561 widgets.append(switch_but)
562 y += switch_but.rect.height + MENU_PAD
563
564 save_but = Button('Save Level', action=self.save)
565 save_but.rect = BUTTON_RECT.copy()
566 save_but.rect.move_ip(MENU_LEFT, y)
567 widgets.append(save_but)
568 y += MENU_BUTTON_HEIGHT + MENU_PAD
569
570 y += MENU_PAD
571 quit_but = Button('Quit', action=self.quit)
572 quit_but.rect = BUTTON_RECT.copy()
573 quit_but.rect.move_ip(MENU_LEFT, y)
574 widgets.append(quit_but)
575
576 self._dMenus['drawing'] = widgets
577
578 def _make_objects_menu(self):
579 widgets = []
580
581 # Add poly buttons
582 y = 15
583
584 edit_objs_but = Button('Edit Objects',
585 action=self.level_widget.edit_objects)
586 edit_objs_but.rect = BUTTON_RECT.copy()
587 edit_objs_but.rect.move_ip(MENU_LEFT, y)
588 widgets.append(edit_objs_but)
589 y += MENU_BUTTON_HEIGHT + MENU_PAD
590
591 edir_enemies_but = Button('Edit Enemies',
592 action=self.level_widget.edit_enemies)
593 edir_enemies_but.rect = BUTTON_RECT.copy()
594 edir_enemies_but.rect.move_ip(MENU_LEFT, y)
595 widgets.append(edir_enemies_but)
596 y += MENU_BUTTON_HEIGHT + MENU_PAD
597
598 add_obj_but = Button('Add Game Object',
599 action=self.level_widget.add_game_object)
600 add_obj_but.rect = BUTTON_RECT.copy()
601 add_obj_but.rect.move_ip(MENU_LEFT, y)
602 widgets.append(add_obj_but)
603 y += MENU_BUTTON_HEIGHT + MENU_PAD
604
605 add_puzzle_but = Button('Add Puzzler',
606 action=self.level_widget.add_puzzler)
607 add_puzzle_but.rect = BUTTON_RECT.copy()
608 add_puzzle_but.rect.move_ip(MENU_LEFT, y)
609 widgets.append(add_puzzle_but)
610 y += MENU_BUTTON_HEIGHT + MENU_PAD
611
612 add_enemy_but = Button('Add Enemy',
613 action=self.level_widget.add_enemy)
614 add_enemy_but.rect = BUTTON_RECT.copy()
615 add_enemy_but.rect.move_ip(MENU_LEFT, y)
616 widgets.append(add_enemy_but)
617 y += MENU_BUTTON_HEIGHT + MENU_PAD
618
619 y += MENU_PAD
620 switch_but = Button('Switch to Drawing', action=self.switch_to_draw)
621 switch_but.rect = BUTTON_RECT.copy()
622 switch_but.rect.move_ip(MENU_LEFT, y)
623 widgets.append(switch_but)
624 y += switch_but.rect.height + MENU_PAD
625
626 save_but = Button('Save Level', action=self.save)
627 save_but.rect = BUTTON_RECT.copy()
628 save_but.rect.move_ip(MENU_LEFT, y)
629 widgets.append(save_but)
630 y += MENU_BUTTON_HEIGHT + MENU_PAD
631
632 y += MENU_PAD
633 quit_but = Button('Quit', action=self.quit)
634 quit_but.rect = BUTTON_RECT.copy()
635 quit_but.rect.move_ip(MENU_LEFT, y)
636 widgets.append(quit_but)
637
638 self._dMenus['objects'] = widgets
639
640 def key_down(self, ev):
641 if ev.key == pgl.K_ESCAPE:
642 self.quit()
643 elif ev.key == pgl.K_s:
644 self.save()
645 else:
646 self.level_widget.key_down(ev)
647
648 def save(self):
649 closed, messages = self.level.all_closed()
650 if closed:
651 self.level.save()
652 # display success
653 alert("Level %s saved successfully." % self.level.name)
654 else:
655 # display errors
656 alert("Failed to save level.\n\n%s" % '\n'.join(messages))
657
658 def switch_to_draw(self):
659 if self._menu_mode != 'drawing':
660 self._clear_menu()
661 self._menu_mode = 'drawing'
662 self._populate_menu()
663
664 def switch_to_objects(self):
665 if self._menu_mode != 'objects':
666 self._clear_menu()
667 self._menu_mode = 'objects'
668 self._populate_menu()
669
670 def _clear_menu(self):
671 for widget in self._dMenus[self._menu_mode]:
672 self.remove(widget)
673
674 def _populate_menu(self):
675 self.level_widget.change_poly(None)
676 for widget in self._dMenus[self._menu_mode]:
677 self.add(widget)
678 self.invalidate()
679
680 def mouse_move(self, ev):
681 self.level_widget.mouse_move(ev)
682
683 def draw(self, surface):
684 # Update checkbox state
685 if self._menu_mode == 'drawing':
686 self.level_widget.set_objects(self.show_objs.value)
687 self.level_widget.set_enemies(self.show_enemies.value)
688 else:
689 self.level_widget.set_objects(True)
690 self.level_widget.set_enemies(True)
691 super(EditorApp, self).draw(surface)
692
693
694if __name__ == "__main__":
695 if len(sys.argv) not in [2, 4]:
696 print 'Please supply a levelname or levelname and level size'
697 sys.exit()
698 # Need to ensure we have defaults for rendering
699 parse_args([])
700 pygame.display.init()
701 pygame.font.init()
702 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
703 pgl.SWSURFACE)
704 if len(sys.argv) == 2:
705 level = EditorLevel(sys.argv[1])
706 level.load(pymunk.Space())
707 elif len(sys.argv) == 4:
708 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
709 pygame.display.set_caption('Nagslang Area Editor')
710 pygame.key.set_repeat(200, 100)
711 app = EditorApp(level, pygame.display.get_surface())
712 app.run()
Note: See TracBrowser for help on using the repository browser.