source: tools/area_editor.py@ 199:c291fd4b49bf

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

Add showing objects to the level editor

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