source: tools/area_editor.py@ 51:9c4681f35866

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

The level editor

File size: 7.3 KB
Line 
1# The basic area editor
2#
3# To edit an existing level, use
4# editor levelname
5#
6# To create a new level:
7#
8# editor levelname <xsize> <ysiz>
9# (size specified in pixels
10#
11
12import pygame
13import pygame.locals as pgl
14
15from nagslang.resources import resources
16from nagslang.constants import SCREEN, FPS
17from nagslang.level import Level, POLY_COLORS
18
19import sys
20
21
22class EditorLevel(Level):
23
24 def __init__(self, name, x=800, y=600):
25 super(EditorLevel, self).__init__(name)
26 self.x = x
27 self.y = y
28
29 def round_point(self, pos):
30 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
31
32 def point_to_pymunk(self, pos):
33 # inverse of point_to_pygame
34 # (this is also the same as point_to_pygame, but a additional
35 # function for sanity later in pyweek).
36 return (pos[0], self.y - pos[1])
37
38 def add_point(self, poly_index, pos):
39 self.polygons.setdefault(poly_index, [])
40 if not self.polygons[poly_index]:
41 self.polygons[poly_index].append(
42 self.point_to_pymunk(self.round_point(pos)))
43 else:
44 add_pos = self.fix_angle(poly_index, pos)
45 self.polygons[poly_index].append(add_pos)
46
47 def fix_angle(self, index, pos):
48 # Last point
49 point1 = self.point_to_pygame(self.polygons[index][-1])
50 pos = self.round_point(pos)
51 # We want the line (point1 to pos) to be an angle of
52 # 0, 45, 90, 135, 180, 225, 270, 305
53 # However, we only need to consider half the circle
54 # This is a hack to approximate the right thing
55 pos0 = (pos[0], point1[1])
56 pos90 = (point1[0], pos[1])
57 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
58 pos45 = (point1[0] + dist, point1[1] + dist)
59 pos135 = (point1[0] + dist, point1[1] - dist)
60 pos225 = (point1[0] - dist, point1[1] - dist)
61 pos305 = (point1[0] - dist, point1[1] + dist)
62 min_dist = 9999999
63 new_pos = point1
64 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
65 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
66 if dist < min_dist:
67 new_pos = cand
68 min_dist = dist
69 return self.point_to_pymunk(new_pos)
70
71 def delete_point(self, index):
72 if index in self.polygons and len(self.polygons[index]) > 0:
73 self.polygons[index].pop()
74
75 def draw(self, surface, topleft, mouse_pos, mouse_poly):
76 self._draw_background(True)
77 # Draw polygons as needed for the editor
78 for index, polygon in self.polygons.items():
79 color = POLY_COLORS[index]
80 if len(polygon) > 1:
81 pointlist = [self.point_to_pygame(p) for p in polygon]
82 pygame.draw.lines(self._surface, color, False, pointlist, 2)
83 if index == mouse_poly and mouse_pos:
84 endpoint = self.fix_angle(index, mouse_pos)
85 pygame.draw.line(self._surface, color,
86 self.point_to_pygame(polygon[-1]),
87 self.point_to_pygame(endpoint))
88 surface_area = pygame.rect.Rect(topleft, SCREEN)
89 surface.blit(self._surface, (0, 0), surface_area)
90
91 def save(self):
92 levelfile = resources.get_resource_path(self.name)
93 with file(levelfile, 'w') as f:
94 f.write('X-Size: %s\n' % self.x)
95 f.write('Y-Size: %s\n' % self.y)
96 f.write('Base tile: %s\n' % self.basetile)
97 for i, poly in self.polygons.items():
98 f.write('Polygon %d : %d\n' % (i, len(poly)))
99 for point in poly:
100 f.write('Point: %d %d\n' % point)
101
102
103class Editor(object):
104
105 def __init__(self, level, surface):
106 self.level = level
107 self.surface = surface
108 self.pos = (0, 0)
109 self.cur_poly = None
110 self.mouse_pos = None
111
112 def move_view(self, offset):
113 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
114 if new_pos[0] < 0:
115 new_pos[0] = self.pos[0]
116 elif new_pos[0] > self.level.x - SCREEN[0]:
117 new_pos[0] = self.pos[0]
118 if new_pos[1] < 0:
119 new_pos[1] = self.pos[1]
120 elif new_pos[1] > self.level.y - SCREEN[1]:
121 new_pos[1] = self.pos[1]
122 self.pos = tuple(new_pos)
123
124 def draw(self):
125 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
126 and len(self.level.polygons[self.cur_poly])):
127 # We have an active polygon
128 mouse_pos = self._level_coordinates(self.mouse_pos)
129 else:
130 mouse_pos = None
131 self.level.draw(self.surface, self.pos, mouse_pos,
132 self.cur_poly)
133
134 def _level_coordinates(self, pos):
135 # Move positions to level values
136 if not pos:
137 return (0, 0)
138 return pos[0] + self.pos[0], pos[1] + self.pos[1]
139
140 def run(self):
141 pygame.key.set_repeat(100, 50)
142 running = True
143 clock = pygame.time.Clock()
144 while running:
145 for ev in pygame.event.get():
146 if ev.type == pgl.QUIT:
147 running = False
148 elif ev.type == pgl.KEYDOWN:
149 if ev.key == pgl.K_ESCAPE:
150 running = False
151 elif ev.key == pgl.K_LEFT:
152 self.move_view((-10, 0))
153 elif ev.key == pgl.K_RIGHT:
154 self.move_view((10, 0))
155 elif ev.key == pgl.K_UP:
156 self.move_view((0, -10))
157 elif ev.key == pgl.K_DOWN:
158 self.move_view((0, 10))
159 if ev.key == pgl.K_1:
160 self.cur_poly = 1
161 if ev.key == pgl.K_2:
162 self.cur_poly = 2
163 if ev.key == pgl.K_3:
164 self.cur_poly = 3
165 if ev.key == pgl.K_4:
166 self.cur_poly = 4
167 if ev.key == pgl.K_5:
168 self.cur_poly = 5
169 if ev.key == pgl.K_6:
170 self.cur_poly = 6
171 if ev.key == pgl.K_0:
172 self.cur_poly = None
173 if ev.key == pgl.K_d and self.cur_poly:
174 self.level.delete_point(self.cur_poly)
175 if ev.key == pgl.K_s:
176 level.save()
177 elif ev.type == pgl.MOUSEBUTTONDOWN and self.cur_poly:
178 # Add a point
179 self.level.add_point(self.cur_poly,
180 self._level_coordinates(ev.pos))
181 elif ev.type == pgl.MOUSEMOTION:
182 self.mouse_pos = ev.pos
183 self.draw()
184 pygame.display.flip()
185 clock.tick(FPS)
186
187
188if __name__ == "__main__":
189 if len(sys.argv) == 2:
190 level = EditorLevel(sys.argv[1])
191 level.load()
192 elif len(sys.argv) == 4:
193 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
194 else:
195 print 'Please supply a levelname or levelname and level size'
196 sys.exit()
197 pygame.display.init()
198 pygame.font.init()
199 pygame.display.set_mode(SCREEN, pgl.SWSURFACE)
200 pygame.display.set_caption('Nagslang Area Editor')
201 editor = Editor(level, pygame.display.get_surface())
202 editor.run()
Note: See TracBrowser for help on using the repository browser.