source: tools/area_editor.py@ 71:c449a3507a6b

Last change on this file since 71:c449a3507a6b was 71:c449a3507a6b, checked in by Stefano Rivera <stefano@…>, 8 years ago

Shebang and path hacking, so that the area_editor runs

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