source: tools/area_editor.py@ 236:261fd65a8816

Last change on this file since 236:261fd65a8816 was 236:261fd65a8816, checked in by Neil Muller <drnlmuller@…>, 8 years ago

More work towards object editing

  • Property exe set to *
File size: 24.0 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
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
175class ObjectTable(TableView):
176
177 columns = [TableColumn("Object", 690, 'l', '%r')]
178
179 def __init__(self, data):
180 super(ObjectTable, self).__init__(height=450)
181 self.data = data
182 self.selected_row = -1
183
184 def num_rows(self):
185 return len(self.data)
186
187 def row_data(self, i):
188 data = self.data[i]
189 if 'name' in data:
190 return ('%s (%s)' % (data['classname'], data['name']), )
191 return (data['classname'], )
192
193 def row_is_selected(self, i):
194 return self.selected_row == i
195
196 def click_row(self, i, ev):
197 self.selected_row = i
198
199 def get_selection(self):
200 if self.selected_row >= 0:
201 return self.data[self.selected_row]
202 return None
203
204
205class LevelWidget(Widget):
206
207 def __init__(self, level):
208 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
209 SCREEN[0], SCREEN[1]))
210 self.level = level
211 self.pos = (0, 0)
212 self.filled_mode = False
213 self.mouse_pos = None
214 self.cur_poly = None
215 self._mouse_drag = False
216 self._draw_objects = False
217 self._draw_enemies = False
218 self._draw_lines = False
219 self._start_pos = None
220
221 def _level_coordinates(self, pos):
222 # Move positions to level values
223 if not pos:
224 return (0, 0)
225 return pos[0] + self.pos[0], pos[1] + self.pos[1]
226
227 def _move_view(self, offset):
228 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
229 if new_pos[0] < 0:
230 new_pos[0] = self.pos[0]
231 elif new_pos[0] > self.level.x - SCREEN[0]:
232 new_pos[0] = self.pos[0]
233 if new_pos[1] < 0:
234 new_pos[1] = self.pos[1]
235 elif new_pos[1] > self.level.y - SCREEN[1]:
236 new_pos[1] = self.pos[1]
237 self.pos = tuple(new_pos)
238
239 def set_objects(self, value):
240 if self._draw_objects != value:
241 self._draw_objects = value
242 self.invalidate()
243
244 def set_enemies(self, value):
245 if self._draw_enemies != value:
246 self._draw_enemies = value
247 self.invalidate()
248
249 def draw(self, surface):
250 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
251 and len(self.level.polygons[self.cur_poly])):
252 # We have an active polygon
253 mouse_pos = self._level_coordinates(self.mouse_pos)
254 elif self._draw_lines:
255 # Interior wall mode
256 mouse_pos = self._level_coordinates(self.mouse_pos)
257 else:
258 mouse_pos = None
259 level_surface = level.draw(mouse_pos, self.cur_poly, self.filled_mode,
260 self._draw_lines, self._start_pos)
261 if self._draw_objects:
262 for thing in self.level.drawables:
263 if not isinstance(thing, Enemy):
264 thing.render(level_surface)
265 if self._draw_enemies:
266 for thing in self.level.drawables:
267 if isinstance(thing, Enemy):
268 thing.render(level_surface)
269 surface_area = pygame.rect.Rect(self.pos, SCREEN)
270 surface.blit(level_surface, (0, 0), surface_area)
271
272 def change_poly(self, new_poly):
273 self.cur_poly = new_poly
274 self._draw_lines = False
275 if self.cur_poly is not None:
276 self.filled_mode = False
277
278 def line_mode(self):
279 self.cur_poly = None
280 self._draw_lines = True
281 self.filled_mode = False
282 self._start_pos = None
283
284 def key_down(self, ev):
285 if ev.key == pgl.K_LEFT:
286 self._move_view((-10, 0))
287 elif ev.key == pgl.K_RIGHT:
288 self._move_view((10, 0))
289 elif ev.key == pgl.K_UP:
290 self._move_view((0, -10))
291 elif ev.key == pgl.K_DOWN:
292 self._move_view((0, 10))
293 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6):
294 self.change_poly(ev.key - pgl.K_0)
295 elif ev.key == pgl.K_0:
296 self.change_poly(None)
297 elif ev.key == pgl.K_d and self.cur_poly:
298 self.level.delete_point(self.cur_poly)
299 elif ev.key == pgl.K_f:
300 self.set_filled()
301 elif ev.key == pgl.K_c:
302 self.close_poly()
303
304 def set_filled(self):
305 closed, _ = self.level.all_closed()
306 if closed:
307 self.cur_poly = None
308 self.filled_mode = True
309 self._draw_lines = False
310 else:
311 alert('Not all polygons closed, so not filling')
312
313 def mouse_move(self, ev):
314 old_pos = self.mouse_pos
315 self.mouse_pos = ev.pos
316 if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines):
317 self.invalidate()
318
319 def mouse_drag(self, ev):
320 if self._mouse_drag:
321 old_pos = self.mouse_pos
322 self.mouse_pos = ev.pos
323 diff = (-self.mouse_pos[0] + old_pos[0],
324 -self.mouse_pos[1] + old_pos[1])
325 self._move_view(diff)
326 self.invalidate()
327
328 def mouse_down(self, ev):
329 if ev.button == 1:
330 if self._draw_lines:
331 if self._start_pos is None:
332 self._start_pos = ev.pos
333 else:
334 self.level.add_line(self._start_pos, ev.pos)
335 self._start_pos = None
336 else:
337 print "Click: %r" % (
338 self.level.point_to_pymunk(
339 self._level_coordinates(ev.pos)),)
340 if ev.button == 4: # Scroll up
341 self._move_view((0, -10))
342 elif ev.button == 5: # Scroll down
343 self._move_view((0, 10))
344 elif ev.button == 6: # Scroll left
345 self._move_view((-10, 0))
346 elif ev.button == 7: # Scroll right
347 self._move_view((10, 0))
348 elif self.cur_poly and ev.button == 1:
349 # Add a point
350 self.level.add_point(self.cur_poly,
351 self._level_coordinates(ev.pos))
352 elif ev.button == 3:
353 self._mouse_drag = True
354
355 def mouse_up(self, ev):
356 if ev.button == 3:
357 self._mouse_drag = False
358
359 def close_poly(self):
360 if self.cur_poly is None:
361 return
362 if self.level.close_poly(self.cur_poly):
363 alert("Successfully closed the polygon")
364 self.change_poly(None)
365 else:
366 alert("Failed to close the polygon")
367
368 def _make_edit_dialog(self, entries):
369 # Dialog to hold the editor
370 edit_box = Dialog()
371 edit_box.rect = pygame.rect.Rect(0, 0, 700, 500)
372 table = ObjectTable(entries)
373 edit_box.add(table)
374 buttons = []
375 for text in ['OK', 'Delete', 'Cancel']:
376 but = Button(text, action=lambda x=text: edit_box.dismiss(x))
377 buttons.append(but)
378 row = Row(buttons)
379 row.rect = pygame.rect.Rect(0, 450, 700, 50)
380 edit_box.add(row)
381 edit_box.get_selection = lambda: table.get_selection()
382 return edit_box
383
384 def edit_objects(self):
385 edit_box = self._make_edit_dialog(self.level._game_objects)
386 res = edit_box.present()
387 choice = edit_box.get_selection()
388 if choice is None:
389 return
390 if res == 'OK':
391 # Edit object stuff goes here
392 pass
393 elif res == 'Delete':
394 pass
395
396 def edit_enemies(self):
397 edit_box = self._make_edit_dialog(self.level._enemies)
398 res = edit_box.present()
399 choice = edit_box.get_selection()
400 if choice is None:
401 return
402 if res == 'OK':
403 # Edit object stuff goes here
404 pass
405 elif res == 'Delete':
406 pass
407
408 def _make_choice_dialog(self, classes):
409 # Dialog to hold the editor
410 data = []
411 for cls_name, cls in classes:
412 data.append({"classname": cls_name, "class": cls})
413 edit_box = Dialog()
414 edit_box.rect = pygame.rect.Rect(0, 0, 700, 500)
415 table = ObjectTable(data)
416 edit_box.add(table)
417 buttons = []
418 for text in ['OK', 'Cancel']:
419 but = Button(text, action=lambda x=text: edit_box.dismiss(x))
420 buttons.append(but)
421 row = Row(buttons)
422 row.rect = pygame.rect.Rect(0, 450, 700, 50)
423 edit_box.add(row)
424 edit_box.get_selection = lambda: table.get_selection()
425 return edit_box
426
427 def add_game_object(self):
428 classes = get_editable_game_objects()
429 choose = self._make_choice_dialog(classes)
430 res = choose.present()
431 choice = choose.get_selection()
432 if res == 'OK' and choice is not None:
433 pass
434
435 def add_enemy(self):
436 classes = get_editable_enemies()
437 choose = self._make_choice_dialog(classes)
438 res = choose.present()
439 choice = choose.get_selection()
440 if res == 'OK' and choice is not None:
441 pass
442
443 def add_puzzler(self):
444 classes = get_editable_puzzlers()
445 choose = self._make_choice_dialog(classes)
446 res = choose.present()
447 choice = choose.get_selection()
448 if res == 'OK' and choice is not None:
449 pass
450
451
452class PolyButton(Button):
453 """Button for coosing the correct polygon"""
454
455 def __init__(self, index, level_widget):
456 if index is not None:
457 text = "Draw: %s" % index
458 else:
459 text = 'Exit Draw Mode'
460 super(PolyButton, self).__init__(text)
461 self.index = index
462 self.level_widget = level_widget
463
464 def action(self):
465 self.level_widget.change_poly(self.index)
466
467
468class EditorApp(RootWidget):
469
470 def __init__(self, level, surface):
471 super(EditorApp, self).__init__(surface)
472 self.level = level
473 self.level_widget = LevelWidget(self.level)
474 self.add(self.level_widget)
475
476 self._dMenus = {}
477
478 self._make_draw_menu()
479 self._make_objects_menu()
480
481 self._menu_mode = 'drawing'
482 self._populate_menu()
483
484 def _make_draw_menu(self):
485 widgets = []
486
487 # Add poly buttons
488 y = 15
489 for poly in range(1, 7):
490 but = PolyButton(poly, self.level_widget)
491 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD,
492 MENU_BUTTON_HEIGHT)
493 if poly % 2:
494 but.rect.move_ip(MENU_LEFT, y)
495 else:
496 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD,
497 y)
498 y += MENU_BUTTON_HEIGHT + MENU_PAD
499 widgets.append(but)
500
501 end_poly_but = PolyButton(None, self.level_widget)
502 end_poly_but.rect = BUTTON_RECT.copy()
503 end_poly_but.rect.move_ip(MENU_LEFT, y)
504 widgets.append(end_poly_but)
505 y += MENU_BUTTON_HEIGHT + MENU_PAD
506
507 draw_line = Button("Draw interior wall", self.level_widget.line_mode)
508 draw_line.rect = BUTTON_RECT.copy()
509 draw_line.rect.move_ip(MENU_LEFT, y)
510 widgets.append(draw_line)
511 y += MENU_BUTTON_HEIGHT + MENU_PAD
512
513 fill_but = Button('Fill exterior', action=self.level_widget.set_filled)
514 fill_but.rect = BUTTON_RECT.copy()
515 fill_but.rect.move_ip(MENU_LEFT, y)
516 widgets.append(fill_but)
517 y += MENU_BUTTON_HEIGHT + MENU_PAD
518
519 close_poly_but = Button('Close Polygon',
520 action=self.level_widget.close_poly)
521 close_poly_but.rect = BUTTON_RECT.copy()
522 close_poly_but.rect.move_ip(MENU_LEFT, y)
523 widgets.append(close_poly_but)
524 y += MENU_BUTTON_HEIGHT + MENU_PAD
525
526 white = pygame.color.Color("white")
527 self.show_objs = CheckBox(fg_color=white)
528 self.show_objs.rect = CHECK_RECT.copy()
529 self.show_objs.rect.move_ip(MENU_LEFT, y)
530 label = Label("Show Objects", fg_color=white)
531 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
532 widgets.append(self.show_objs)
533 widgets.append(label)
534 y += label.rect.height + MENU_PAD
535
536 self.show_enemies = CheckBox(fg_color=white)
537 self.show_enemies.rect = CHECK_RECT.copy()
538 self.show_enemies.rect.move_ip(MENU_LEFT, y)
539 label = Label("Show enemy start pos", fg_color=white)
540 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y)
541 widgets.append(self.show_enemies)
542 widgets.append(label)
543 y += label.rect.height + MENU_PAD
544
545 y += MENU_PAD
546 switch_but = Button('Switch to Objects', action=self.switch_to_objects)
547 switch_but.rect = BUTTON_RECT.copy()
548 switch_but.rect.move_ip(MENU_LEFT, y)
549 widgets.append(switch_but)
550 y += switch_but.rect.height + MENU_PAD
551
552 save_but = Button('Save Level', action=self.save)
553 save_but.rect = BUTTON_RECT.copy()
554 save_but.rect.move_ip(MENU_LEFT, y)
555 widgets.append(save_but)
556 y += MENU_BUTTON_HEIGHT + MENU_PAD
557
558 y += MENU_PAD
559 quit_but = Button('Quit', action=self.quit)
560 quit_but.rect = BUTTON_RECT.copy()
561 quit_but.rect.move_ip(MENU_LEFT, y)
562 widgets.append(quit_but)
563
564 self._dMenus['drawing'] = widgets
565
566 def _make_objects_menu(self):
567 widgets = []
568
569 # Add poly buttons
570 y = 15
571
572 edit_objs_but = Button('Edit Objects',
573 action=self.level_widget.edit_objects)
574 edit_objs_but.rect = BUTTON_RECT.copy()
575 edit_objs_but.rect.move_ip(MENU_LEFT, y)
576 widgets.append(edit_objs_but)
577 y += MENU_BUTTON_HEIGHT + MENU_PAD
578
579 edir_enemies_but = Button('Edit Enemies',
580 action=self.level_widget.edit_enemies)
581 edir_enemies_but.rect = BUTTON_RECT.copy()
582 edir_enemies_but.rect.move_ip(MENU_LEFT, y)
583 widgets.append(edir_enemies_but)
584 y += MENU_BUTTON_HEIGHT + MENU_PAD
585
586 add_obj_but = Button('Add Game Object',
587 action=self.level_widget.add_game_object)
588 add_obj_but.rect = BUTTON_RECT.copy()
589 add_obj_but.rect.move_ip(MENU_LEFT, y)
590 widgets.append(add_obj_but)
591 y += MENU_BUTTON_HEIGHT + MENU_PAD
592
593 add_puzzle_but = Button('Add Puzzler',
594 action=self.level_widget.add_puzzler)
595 add_puzzle_but.rect = BUTTON_RECT.copy()
596 add_puzzle_but.rect.move_ip(MENU_LEFT, y)
597 widgets.append(add_puzzle_but)
598 y += MENU_BUTTON_HEIGHT + MENU_PAD
599
600 add_enemy_but = Button('Add Enemy',
601 action=self.level_widget.add_enemy)
602 add_enemy_but.rect = BUTTON_RECT.copy()
603 add_enemy_but.rect.move_ip(MENU_LEFT, y)
604 widgets.append(add_enemy_but)
605 y += MENU_BUTTON_HEIGHT + MENU_PAD
606
607 y += MENU_PAD
608 switch_but = Button('Switch to Drawing', action=self.switch_to_draw)
609 switch_but.rect = BUTTON_RECT.copy()
610 switch_but.rect.move_ip(MENU_LEFT, y)
611 widgets.append(switch_but)
612 y += switch_but.rect.height + MENU_PAD
613
614 save_but = Button('Save Level', action=self.save)
615 save_but.rect = BUTTON_RECT.copy()
616 save_but.rect.move_ip(MENU_LEFT, y)
617 widgets.append(save_but)
618 y += MENU_BUTTON_HEIGHT + MENU_PAD
619
620 y += MENU_PAD
621 quit_but = Button('Quit', action=self.quit)
622 quit_but.rect = BUTTON_RECT.copy()
623 quit_but.rect.move_ip(MENU_LEFT, y)
624 widgets.append(quit_but)
625
626 self._dMenus['objects'] = widgets
627
628 def key_down(self, ev):
629 if ev.key == pgl.K_ESCAPE:
630 self.quit()
631 elif ev.key == pgl.K_s:
632 self.save()
633 else:
634 self.level_widget.key_down(ev)
635
636 def save(self):
637 closed, messages = self.level.all_closed()
638 if closed:
639 self.level.save()
640 # display success
641 alert("Level %s saved successfully." % self.level.name)
642 else:
643 # display errors
644 alert("Failed to save level.\n\n%s" % '\n'.join(messages))
645
646 def switch_to_draw(self):
647 if self._menu_mode != 'drawing':
648 self._clear_menu()
649 self._menu_mode = 'drawing'
650 self._populate_menu()
651
652 def switch_to_objects(self):
653 if self._menu_mode != 'objects':
654 self._clear_menu()
655 self._menu_mode = 'objects'
656 self._populate_menu()
657
658 def _clear_menu(self):
659 for widget in self._dMenus[self._menu_mode]:
660 self.remove(widget)
661
662 def _populate_menu(self):
663 self.level_widget.change_poly(None)
664 for widget in self._dMenus[self._menu_mode]:
665 self.add(widget)
666 self.invalidate()
667
668 def mouse_move(self, ev):
669 self.level_widget.mouse_move(ev)
670
671 def draw(self, surface):
672 # Update checkbox state
673 if self._menu_mode == 'drawing':
674 self.level_widget.set_objects(self.show_objs.value)
675 self.level_widget.set_enemies(self.show_enemies.value)
676 else:
677 self.level_widget.set_objects(True)
678 self.level_widget.set_enemies(True)
679 super(EditorApp, self).draw(surface)
680
681
682if __name__ == "__main__":
683 if len(sys.argv) not in [2, 4]:
684 print 'Please supply a levelname or levelname and level size'
685 sys.exit()
686 # Need to ensure we have defaults for rendering
687 parse_args([])
688 pygame.display.init()
689 pygame.font.init()
690 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
691 pgl.SWSURFACE)
692 if len(sys.argv) == 2:
693 level = EditorLevel(sys.argv[1])
694 level.load(pymunk.Space())
695 elif len(sys.argv) == 4:
696 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
697 pygame.display.set_caption('Nagslang Area Editor')
698 pygame.key.set_repeat(200, 100)
699 app = EditorApp(level, pygame.display.get_surface())
700 app.run()
Note: See TracBrowser for help on using the repository browser.