source: tools/area_editor.py@ 122:02423600d958

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

Use dialogs to report save results

  • Property exe set to *
File size: 9.7 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
20from albow.root import RootWidget
21from albow.widget import Widget
22from albow.controls import Button
23from albow.dialogs import alert
24
25sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
26
27from nagslang.constants import SCREEN
28from nagslang.level import Level, POLY_COLORS
29
30
31# layout constants
32MENU_BUTTON_HEIGHT = 35
33MENU_PAD = 6
34MENU_HALF_PAD = MENU_PAD // 2
35MENU_LEFT = SCREEN[0] + MENU_HALF_PAD
36MENU_WIDTH = 200 - MENU_PAD
37
38
39class EditorLevel(Level):
40
41 def __init__(self, name, x=800, y=600):
42 super(EditorLevel, self).__init__(name)
43 self.x = x
44 self.y = y
45
46 def round_point(self, pos):
47 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
48
49 def point_to_pymunk(self, pos):
50 # inverse of point_to_pygame
51 # (this is also the same as point_to_pygame, but a additional
52 # function for sanity later in pyweek).
53 return (pos[0], self.y - pos[1])
54
55 def add_point(self, poly_index, pos):
56 self.polygons.setdefault(poly_index, [])
57 if not self.polygons[poly_index]:
58 point = self.point_to_pymunk(self.round_point(pos))
59 self.polygons[poly_index].append(point)
60 else:
61 add_pos = self.fix_angle(poly_index, pos)
62 self.polygons[poly_index].append(add_pos)
63
64 def fix_angle(self, index, pos):
65 # Last point
66 point1 = self.point_to_pygame(self.polygons[index][-1])
67 pos = self.round_point(pos)
68 # We want the line (point1 to pos) to be an angle of
69 # 0, 45, 90, 135, 180, 225, 270, 305
70 # However, we only need to consider half the circle
71 # This is a hack to approximate the right thing
72 pos0 = (pos[0], point1[1])
73 pos90 = (point1[0], pos[1])
74 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
75 pos45 = (point1[0] + dist, point1[1] + dist)
76 pos135 = (point1[0] + dist, point1[1] - dist)
77 pos225 = (point1[0] - dist, point1[1] - dist)
78 pos305 = (point1[0] - dist, point1[1] + dist)
79 min_dist = 9999999
80 new_pos = point1
81 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
82 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
83 if dist < min_dist:
84 new_pos = cand
85 min_dist = dist
86 return self.point_to_pymunk(new_pos)
87
88 def delete_point(self, index):
89 if index in self.polygons and len(self.polygons[index]) > 0:
90 self.polygons[index].pop()
91
92 def draw(self, surface, topleft, mouse_pos, mouse_poly, filled):
93 self._draw_background(True)
94 # Draw polygons as needed for the editor
95 if filled:
96 self._draw_exterior(True)
97 for index, polygon in self.polygons.items():
98 color = POLY_COLORS[index]
99 if len(polygon) > 1:
100 pointlist = [self.point_to_pygame(p) for p in polygon]
101 pygame.draw.lines(self._surface, color, False, pointlist, 2)
102 if index == mouse_poly and mouse_pos:
103 endpoint = self.fix_angle(index, mouse_pos)
104 pygame.draw.line(self._surface, color,
105 self.point_to_pygame(polygon[-1]),
106 self.point_to_pygame(endpoint))
107 surface_area = pygame.rect.Rect(topleft, SCREEN)
108 surface.blit(self._surface, (0, 0), surface_area)
109
110
111class LevelWidget(Widget):
112
113 def __init__(self, level):
114 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
115 SCREEN[0], SCREEN[1]))
116 self.level = level
117 self.pos = (0, 0)
118 self.filled_mode = False
119 self.mouse_pos = None
120 self.cur_poly = None
121
122 def _level_coordinates(self, pos):
123 # Move positions to level values
124 if not pos:
125 return (0, 0)
126 return pos[0] + self.pos[0], pos[1] + self.pos[1]
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, surface):
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 level.draw(surface, self.pos, mouse_pos, self.cur_poly,
148 self.filled_mode)
149
150 def change_poly(self, new_poly):
151 self.cur_poly = new_poly
152 if self.cur_poly is not None:
153 self.filled_mode = False
154
155 def key_down(self, ev):
156 if ev.key == pgl.K_LEFT:
157 self._move_view((-10, 0))
158 elif ev.key == pgl.K_RIGHT:
159 self._move_view((10, 0))
160 elif ev.key == pgl.K_UP:
161 self._move_view((0, -10))
162 elif ev.key == pgl.K_DOWN:
163 self._move_view((0, 10))
164 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6):
165 self.change_poly(ev.key - pgl.K_0)
166 elif ev.key == pgl.K_0:
167 self.change_poly(None)
168 elif ev.key == pgl.K_d and self.cur_poly:
169 self.level.delete_point(self.cur_poly)
170 elif ev.key == pgl.K_f:
171 self.set_filled()
172
173 def set_filled(self):
174 if level.all_closed():
175 self.cur_poly = None
176 self.filled_mode = True
177 else:
178 print 'Not all polygons closed, so not filling'
179
180 def mouse_move(self, ev):
181 old_pos = self.mouse_pos
182 self.mouse_pos = ev.pos
183 if self.cur_poly and old_pos != self.mouse_pos:
184 self.invalidate()
185
186 def mouse_down(self, ev):
187 if self.cur_poly:
188 # Add a point
189 self.level.add_point(self.cur_poly,
190 self._level_coordinates(ev.pos))
191
192
193class PolyButton(Button):
194 """Button for coosing the correct polygon"""
195
196 def __init__(self, index, level_widget):
197 if index is not None:
198 text = "Draw: %s" % index
199 else:
200 text = 'Exit Draw Mode'
201 super(PolyButton, self).__init__(text)
202 self.index = index
203 self.level_widget = level_widget
204
205 def action(self):
206 self.level_widget.change_poly(self.index)
207
208
209class EditorApp(RootWidget):
210
211 def __init__(self, level, surface):
212 super(EditorApp, self).__init__(surface)
213 self.level = level
214 self.level_widget = LevelWidget(self.level)
215 self.add(self.level_widget)
216
217 # Add poly buttons
218 y = 15
219 for poly in range(1, 7):
220 but = PolyButton(poly, self.level_widget)
221 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD,
222 MENU_BUTTON_HEIGHT)
223 if poly % 2:
224 but.rect.move_ip(MENU_LEFT, y)
225 else:
226 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD,
227 y)
228 y += MENU_BUTTON_HEIGHT + MENU_PAD
229 self.add(but)
230
231 end_poly_but = PolyButton(None, self.level_widget)
232 end_poly_but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH,
233 MENU_BUTTON_HEIGHT)
234 end_poly_but.rect.move_ip(MENU_LEFT, y)
235 self.add(end_poly_but)
236 y += MENU_BUTTON_HEIGHT + MENU_PAD
237
238 fill_but = Button('Fill exterior', action=self.level_widget.set_filled)
239 fill_but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
240 fill_but.rect.move_ip(MENU_LEFT, y)
241 self.add(fill_but)
242 y += MENU_BUTTON_HEIGHT + MENU_PAD
243
244 save_but = Button('Save Level', action=self.save)
245 save_but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
246 save_but.rect.move_ip(MENU_LEFT, y)
247 self.add(save_but)
248 y += MENU_BUTTON_HEIGHT + MENU_PAD
249
250 quit_but = Button('Quit', action=self.quit)
251 quit_but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
252 quit_but.rect.move_ip(MENU_LEFT, y)
253 self.add(quit_but)
254
255 def key_down(self, ev):
256 if ev.key == pgl.K_ESCAPE:
257 self.quit()
258 elif ev.key == pgl.K_s:
259 self.save()
260 else:
261 self.level_widget.key_down(ev)
262
263 def save(self):
264 closed, messages = self.level.all_closed()
265 if closed:
266 self.level.save()
267 # display success
268 alert("Level %s saved successfully." % self.level.name)
269 else:
270 # display errors
271 alert("Failed to save level.\n\n%s" % '\n'.join(messages))
272
273 def mouse_move(self, ev):
274 self.level_widget.mouse_move(ev)
275
276
277if __name__ == "__main__":
278 if len(sys.argv) == 2:
279 level = EditorLevel(sys.argv[1])
280 level.load()
281 elif len(sys.argv) == 4:
282 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
283 else:
284 print 'Please supply a levelname or levelname and level size'
285 sys.exit()
286 pygame.display.init()
287 pygame.font.init()
288 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
289 pgl.SWSURFACE)
290 pygame.display.set_caption('Nagslang Area Editor')
291 pygame.key.set_repeat(200, 100)
292 app = EditorApp(level, pygame.display.get_surface())
293 app.run()
Note: See TracBrowser for help on using the repository browser.