source: tools/area_editor.py@ 72:5db052531510

Last change on this file since 72:5db052531510 was 72:5db052531510, checked in by Stefano Rivera <stefano@…>, 8 years ago

Move save() to Level

  • Property exe set to *
File size: 6.9 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):
80 self._draw_background(True)
81 # Draw polygons as needed for the editor
82 for index, polygon in self.polygons.items():
83 color = POLY_COLORS[index]
84 if len(polygon) > 1:
85 pointlist = [self.point_to_pygame(p) for p in polygon]
86 pygame.draw.lines(self._surface, color, False, pointlist, 2)
87 if index == mouse_poly and mouse_pos:
88 endpoint = self.fix_angle(index, mouse_pos)
89 pygame.draw.line(self._surface, color,
90 self.point_to_pygame(polygon[-1]),
91 self.point_to_pygame(endpoint))
92 surface_area = pygame.rect.Rect(topleft, SCREEN)
93 surface.blit(self._surface, (0, 0), surface_area)
94
95
96class Editor(object):
97
98 def __init__(self, level, surface):
99 self.level = level
100 self.surface = surface
101 self.pos = (0, 0)
102 self.cur_poly = None
103 self.mouse_pos = None
104
105 def move_view(self, offset):
106 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]]
107 if new_pos[0] < 0:
108 new_pos[0] = self.pos[0]
109 elif new_pos[0] > self.level.x - SCREEN[0]:
110 new_pos[0] = self.pos[0]
111 if new_pos[1] < 0:
112 new_pos[1] = self.pos[1]
113 elif new_pos[1] > self.level.y - SCREEN[1]:
114 new_pos[1] = self.pos[1]
115 self.pos = tuple(new_pos)
116
117 def draw(self):
118 if (self.cur_poly is not None and self.cur_poly in self.level.polygons
119 and len(self.level.polygons[self.cur_poly])):
120 # We have an active polygon
121 mouse_pos = self._level_coordinates(self.mouse_pos)
122 else:
123 mouse_pos = None
124 self.level.draw(self.surface, self.pos, mouse_pos,
125 self.cur_poly)
126
127 def _level_coordinates(self, pos):
128 # Move positions to level values
129 if not pos:
130 return (0, 0)
131 return pos[0] + self.pos[0], pos[1] + self.pos[1]
132
133 def run(self):
134 pygame.key.set_repeat(100, 50)
135 running = True
136 clock = pygame.time.Clock()
137 while running:
138 for ev in pygame.event.get():
139 if ev.type == pgl.QUIT:
140 running = False
141 elif ev.type == pgl.KEYDOWN:
142 if ev.key == pgl.K_ESCAPE:
143 running = False
144 elif ev.key == pgl.K_LEFT:
145 self.move_view((-10, 0))
146 elif ev.key == pgl.K_RIGHT:
147 self.move_view((10, 0))
148 elif ev.key == pgl.K_UP:
149 self.move_view((0, -10))
150 elif ev.key == pgl.K_DOWN:
151 self.move_view((0, 10))
152 if ev.key == pgl.K_1:
153 self.cur_poly = 1
154 if ev.key == pgl.K_2:
155 self.cur_poly = 2
156 if ev.key == pgl.K_3:
157 self.cur_poly = 3
158 if ev.key == pgl.K_4:
159 self.cur_poly = 4
160 if ev.key == pgl.K_5:
161 self.cur_poly = 5
162 if ev.key == pgl.K_6:
163 self.cur_poly = 6
164 if ev.key == pgl.K_0:
165 self.cur_poly = None
166 if ev.key == pgl.K_d and self.cur_poly:
167 self.level.delete_point(self.cur_poly)
168 if ev.key == pgl.K_s:
169 level.save()
170 elif ev.type == pgl.MOUSEBUTTONDOWN and self.cur_poly:
171 # Add a point
172 self.level.add_point(self.cur_poly,
173 self._level_coordinates(ev.pos))
174 elif ev.type == pgl.MOUSEMOTION:
175 self.mouse_pos = ev.pos
176 self.draw()
177 pygame.display.flip()
178 clock.tick(FPS)
179
180
181if __name__ == "__main__":
182 if len(sys.argv) == 2:
183 level = EditorLevel(sys.argv[1])
184 level.load()
185 elif len(sys.argv) == 4:
186 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
187 else:
188 print 'Please supply a levelname or levelname and level size'
189 sys.exit()
190 pygame.display.init()
191 pygame.font.init()
192 pygame.display.set_mode(SCREEN, pgl.SWSURFACE)
193 pygame.display.set_caption('Nagslang Area Editor')
194 editor = Editor(level, pygame.display.get_surface())
195 editor.run()
Note: See TracBrowser for help on using the repository browser.