source: tools/area_editor.py@ 57:1261c0731385

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

Use resource get_file

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