source: tools/area_editor.py@ 109:817d4a62135c

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

Partially albowify the level editor

  • Property exe set to *
File size: 7.8 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
23
24sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
25
26from nagslang.constants import SCREEN
27from nagslang.level import Level, POLY_COLORS
28
29
30# layout constants
31MENU_WIDTH = 200
32MENU_BUTTON_HEIGHT = 35
33
34
35class EditorLevel(Level):
36
37 def __init__(self, name, x=800, y=600):
38 super(EditorLevel, self).__init__(name)
39 self.x = x
40 self.y = y
41
42 def round_point(self, pos):
43 return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
44
45 def point_to_pymunk(self, pos):
46 # inverse of point_to_pygame
47 # (this is also the same as point_to_pygame, but a additional
48 # function for sanity later in pyweek).
49 return (pos[0], self.y - pos[1])
50
51 def add_point(self, poly_index, pos):
52 self.polygons.setdefault(poly_index, [])
53 if not self.polygons[poly_index]:
54 point = self.point_to_pymunk(self.round_point(pos))
55 self.polygons[poly_index].append(point)
56 else:
57 add_pos = self.fix_angle(poly_index, pos)
58 self.polygons[poly_index].append(add_pos)
59
60 def fix_angle(self, index, pos):
61 # Last point
62 point1 = self.point_to_pygame(self.polygons[index][-1])
63 pos = self.round_point(pos)
64 # We want the line (point1 to pos) to be an angle of
65 # 0, 45, 90, 135, 180, 225, 270, 305
66 # However, we only need to consider half the circle
67 # This is a hack to approximate the right thing
68 pos0 = (pos[0], point1[1])
69 pos90 = (point1[0], pos[1])
70 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
71 pos45 = (point1[0] + dist, point1[1] + dist)
72 pos135 = (point1[0] + dist, point1[1] - dist)
73 pos225 = (point1[0] - dist, point1[1] - dist)
74 pos305 = (point1[0] - dist, point1[1] + dist)
75 min_dist = 9999999
76 new_pos = point1
77 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
78 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
79 if dist < min_dist:
80 new_pos = cand
81 min_dist = dist
82 return self.point_to_pymunk(new_pos)
83
84 def delete_point(self, index):
85 if index in self.polygons and len(self.polygons[index]) > 0:
86 self.polygons[index].pop()
87
88 def draw(self, surface, topleft, mouse_pos, mouse_poly, filled):
89 self._draw_background(True)
90 # Draw polygons as needed for the editor
91 if filled:
92 self._draw_exterior(True)
93 for index, polygon in self.polygons.items():
94 color = POLY_COLORS[index]
95 if len(polygon) > 1:
96 pointlist = [self.point_to_pygame(p) for p in polygon]
97 pygame.draw.lines(self._surface, color, False, pointlist, 2)
98 if index == mouse_poly and mouse_pos:
99 endpoint = self.fix_angle(index, mouse_pos)
100 pygame.draw.line(self._surface, color,
101 self.point_to_pygame(polygon[-1]),
102 self.point_to_pygame(endpoint))
103 surface_area = pygame.rect.Rect(topleft, SCREEN)
104 surface.blit(self._surface, (0, 0), surface_area)
105
106
107class LevelWidget(Widget):
108
109 def __init__(self, level):
110 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0,
111 SCREEN[0], SCREEN[1]))
112 self.level = level
113 self.pos = (0, 0)
114 self.filled_mode = False
115 self.mouse_pos = None
116 self.cur_poly = None
117
118 def _level_coordinates(self, pos):
119 # Move positions to level values
120 if not pos:
121 return (0, 0)
122 return pos[0] + self.pos[0], pos[1] + self.pos[1]
123
124 def _move_view(self, offset):
125 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
126 if new_pos[0] < 0:
127 new_pos[0] = self.pos[0]
128 elif new_pos[0] > self.level.x - SCREEN[0]:
129 new_pos[0] = self.pos[0]
130 if new_pos[1] < 0:
131 new_pos[1] = self.pos[1]
132 elif new_pos[1] > self.level.y - SCREEN[1]:
133 new_pos[1] = self.pos[1]
134 self.pos = tuple(new_pos)
135
136 def draw(self, surface):
137 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
138 and len(self.level.polygons[self.cur_poly])):
139 # We have an active polygon
140 mouse_pos = self._level_coordinates(self.mouse_pos)
141 else:
142 mouse_pos = None
143 level.draw(surface, self.pos, mouse_pos, self.cur_poly,
144 self.filled_mode)
145
146 def key_down(self, ev):
147 if 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 elif ev.key == pgl.K_1:
156 self.cur_poly = 1
157 self.filled_mode = False
158 elif ev.key == pgl.K_2:
159 self.cur_poly = 2
160 self.filled_mode = False
161 elif ev.key == pgl.K_3:
162 self.cur_poly = 3
163 self.filled_mode = False
164 elif ev.key == pgl.K_4:
165 self.cur_poly = 4
166 self.filled_mode = False
167 elif ev.key == pgl.K_5:
168 self.cur_poly = 5
169 self.filled_mode = False
170 elif ev.key == pgl.K_6:
171 self.cur_poly = 6
172 self.filled_mode = False
173 elif ev.key == pgl.K_0:
174 self.cur_poly = None
175 elif ev.key == pgl.K_d and self.cur_poly:
176 self.level.delete_point(self.cur_poly)
177 elif ev.key == pgl.K_s:
178 level.save()
179 elif 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
186 def mouse_move(self, ev):
187 old_pos = self.mouse_pos
188 self.mouse_pos = ev.pos
189 if self.cur_poly and old_pos != self.mouse_pos:
190 self.invalidate()
191
192 def mouse_down(self, ev):
193 if self.cur_poly:
194 # Add a point
195 self.level.add_point(self.cur_poly,
196 self._level_coordinates(ev.pos))
197
198
199class EditorApp(RootWidget):
200
201 def __init__(self, level, surface):
202 super(EditorApp, self).__init__(surface)
203 self.level = level
204 self.level_widget = LevelWidget(self.level)
205 self.add(self.level_widget)
206
207 quit_but = Button('Quit', action=self.quit)
208 quit_but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
209 quit_but.rect.move_ip(SCREEN[0], 25)
210 self.add(quit_but)
211
212 def key_down(self, ev):
213 if ev.key == pgl.K_ESCAPE:
214 self.quit()
215 else:
216 self.level_widget.key_down(ev)
217
218 def mouse_move(self, ev):
219 self.level_widget.mouse_move(ev)
220
221
222if __name__ == "__main__":
223 if len(sys.argv) == 2:
224 level = EditorLevel(sys.argv[1])
225 level.load()
226 elif len(sys.argv) == 4:
227 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
228 else:
229 print 'Please supply a levelname or levelname and level size'
230 sys.exit()
231 pygame.display.init()
232 pygame.font.init()
233 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]),
234 pgl.SWSURFACE)
235 pygame.display.set_caption('Nagslang Area Editor')
236 pygame.key.set_repeat(200, 100)
237 app = EditorApp(level, pygame.display.get_surface())
238 app.run()
Note: See TracBrowser for help on using the repository browser.