source: tools/area_editor.py@ 55:26d7bb8c09c8

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

Only save levels if the loops are closed

File size: 7.9 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 closed = True
93 for poly in self.polygons.values():
94 if len(poly) == 0:
95 # We ignore empty polygons
96 continue
97 elif len(poly) == 1:
98 closed = False
99 print "\033[31mError: polygon too small\033[0m"
100 elif poly[-1] != poly[0]:
101 closed = False
102 print "\033[31mError: polygon not closed\033[0m"
103 if not closed:
104 print 'Not saving the level'
105 return
106 levelfile = resources.get_resource_path(self.name)
107 with file(levelfile, 'w') as f:
108 f.write('X-Size: %s\n' % self.x)
109 f.write('Y-Size: %s\n' % self.y)
110 f.write('Base tile: %s\n' % self.basetile)
111 for i, poly in self.polygons.items():
112 if len(poly) == 0:
113 continue
114 f.write('Polygon %d : %d\n' % (i, len(poly)))
115 for point in poly:
116 f.write('Point: %d %d\n' % point)
117 print 'level %s saved' % self.name
118
119
120class Editor(object):
121
122 def __init__(self, level, surface):
123 self.level = level
124 self.surface = surface
125 self.pos = (0, 0)
126 self.cur_poly = None
127 self.mouse_pos = None
128
129 def move_view(self, offset):
130 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
131 if new_pos[0] < 0:
132 new_pos[0] = self.pos[0]
133 elif new_pos[0] > self.level.x - SCREEN[0]:
134 new_pos[0] = self.pos[0]
135 if new_pos[1] < 0:
136 new_pos[1] = self.pos[1]
137 elif new_pos[1] > self.level.y - SCREEN[1]:
138 new_pos[1] = self.pos[1]
139 self.pos = tuple(new_pos)
140
141 def draw(self):
142 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
143 and len(self.level.polygons[self.cur_poly])):
144 # We have an active polygon
145 mouse_pos = self._level_coordinates(self.mouse_pos)
146 else:
147 mouse_pos = None
148 self.level.draw(self.surface, self.pos, mouse_pos,
149 self.cur_poly)
150
151 def _level_coordinates(self, pos):
152 # Move positions to level values
153 if not pos:
154 return (0, 0)
155 return pos[0] + self.pos[0], pos[1] + self.pos[1]
156
157 def run(self):
158 pygame.key.set_repeat(100, 50)
159 running = True
160 clock = pygame.time.Clock()
161 while running:
162 for ev in pygame.event.get():
163 if ev.type == pgl.QUIT:
164 running = False
165 elif ev.type == pgl.KEYDOWN:
166 if ev.key == pgl.K_ESCAPE:
167 running = False
168 elif ev.key == pgl.K_LEFT:
169 self.move_view((-10, 0))
170 elif ev.key == pgl.K_RIGHT:
171 self.move_view((10, 0))
172 elif ev.key == pgl.K_UP:
173 self.move_view((0, -10))
174 elif ev.key == pgl.K_DOWN:
175 self.move_view((0, 10))
176 if ev.key == pgl.K_1:
177 self.cur_poly = 1
178 if ev.key == pgl.K_2:
179 self.cur_poly = 2
180 if ev.key == pgl.K_3:
181 self.cur_poly = 3
182 if ev.key == pgl.K_4:
183 self.cur_poly = 4
184 if ev.key == pgl.K_5:
185 self.cur_poly = 5
186 if ev.key == pgl.K_6:
187 self.cur_poly = 6
188 if ev.key == pgl.K_0:
189 self.cur_poly = None
190 if ev.key == pgl.K_d and self.cur_poly:
191 self.level.delete_point(self.cur_poly)
192 if ev.key == pgl.K_s:
193 level.save()
194 elif ev.type == pgl.MOUSEBUTTONDOWN and self.cur_poly:
195 # Add a point
196 self.level.add_point(self.cur_poly,
197 self._level_coordinates(ev.pos))
198 elif ev.type == pgl.MOUSEMOTION:
199 self.mouse_pos = ev.pos
200 self.draw()
201 pygame.display.flip()
202 clock.tick(FPS)
203
204
205if __name__ == "__main__":
206 if len(sys.argv) == 2:
207 level = EditorLevel(sys.argv[1])
208 level.load()
209 elif len(sys.argv) == 4:
210 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
211 else:
212 print 'Please supply a levelname or levelname and level size'
213 sys.exit()
214 pygame.display.init()
215 pygame.font.init()
216 pygame.display.set_mode(SCREEN, pgl.SWSURFACE)
217 pygame.display.set_caption('Nagslang Area Editor')
218 editor = Editor(level, pygame.display.get_surface())
219 editor.run()
Note: See TracBrowser for help on using the repository browser.