source: tools/area_editor.py@ 96:a9b564c38bef

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

Support showing exterior filled view in the editor

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