source: tools/area_editor.py@ 108:dde91f8c4335

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

Add fonts needed for Albowifying the editor

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