Mercurial > nagslang
annotate tools/area_editor.py @ 97:c177cdc41477
Add WASD controls, switch to "c" for form change.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 02 Sep 2013 11:41:39 +0200 |
parents | a9b564c38bef |
children | 67f8b06a433d |
rev | line source |
---|---|
71
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
1 #!/usr/bin/env python |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
2 |
51 | 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 | |
71
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
14 import os |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
15 import sys |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
16 |
51 | 17 import pygame |
18 import pygame.locals as pgl | |
19 | |
71
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
20 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
c449a3507a6b
Shebang and path hacking, so that the area_editor runs
Stefano Rivera <stefano@rivera.za.net>
parents:
57
diff
changeset
|
21 |
51 | 22 from nagslang.constants import SCREEN, FPS |
23 from nagslang.level import Level, POLY_COLORS | |
24 | |
25 | |
26 class 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 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
79 def draw(self, surface, topleft, mouse_pos, mouse_poly, filled): |
51 | 80 self._draw_background(True) |
81 # Draw polygons as needed for the editor | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
82 if filled: |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
83 self._draw_exterior(True) |
51 | 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 | |
98 class 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 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
106 self.filled_mode = False |
51 | 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, | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
128 self.cur_poly, self.filled_mode) |
51 | 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 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
157 self.filled_mode = False |
51 | 158 if ev.key == pgl.K_2: |
159 self.cur_poly = 2 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
160 self.filled_mode = False |
51 | 161 if ev.key == pgl.K_3: |
162 self.cur_poly = 3 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
163 self.filled_mode = False |
51 | 164 if ev.key == pgl.K_4: |
165 self.cur_poly = 4 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
166 self.filled_mode = False |
51 | 167 if ev.key == pgl.K_5: |
168 self.cur_poly = 5 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
169 self.filled_mode = False |
51 | 170 if ev.key == pgl.K_6: |
171 self.cur_poly = 6 | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
172 self.filled_mode = False |
51 | 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() | |
96
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
179 if ev.key == pgl.K_f: |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
180 if level.all_closed(): |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
181 self.cur_poly = None |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
182 self.filled_mode = True |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
183 else: |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
184 print 'Not all polygons closed, so not filling' |
51 | 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 | |
196 if __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() |