Mercurial > nagslang
annotate tools/area_editor.py @ 209:ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Tue, 03 Sep 2013 23:45:56 +0200 |
parents | 42c565c5ce76 |
children | 06c52529e2ed |
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 | |
152
8bbe57d9de0c
Rejig sys.path hacking above albow import, so I can hide it in the root of my repo
Stefano Rivera <stefano@rivera.za.net>
parents:
151
diff
changeset
|
20 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
8bbe57d9de0c
Rejig sys.path hacking above albow import, so I can hide it in the root of my repo
Stefano Rivera <stefano@rivera.za.net>
parents:
151
diff
changeset
|
21 |
153
6c5f1a78db75
Levels need a pymunk Space to load
Stefano Rivera <stefano@rivera.za.net>
parents:
152
diff
changeset
|
22 import pymunk |
6c5f1a78db75
Levels need a pymunk Space to load
Stefano Rivera <stefano@rivera.za.net>
parents:
152
diff
changeset
|
23 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
24 from albow.root import RootWidget |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
25 from albow.widget import Widget |
198 | 26 from albow.controls import Button, Label, CheckBox |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
27 from albow.dialogs import alert |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
28 |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
29 from nagslang.options import parse_args |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
30 from nagslang.constants import SCREEN |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
31 from nagslang.level import Level, POLY_COLORS, LINE_COLOR |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
32 from nagslang.enemies import Enemy |
51 | 33 |
34 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
35 # layout constants |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
36 MENU_BUTTON_HEIGHT = 35 |
115 | 37 MENU_PAD = 6 |
38 MENU_HALF_PAD = MENU_PAD // 2 | |
39 MENU_LEFT = SCREEN[0] + MENU_HALF_PAD | |
40 MENU_WIDTH = 200 - MENU_PAD | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
41 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
42 |
51 | 43 class EditorLevel(Level): |
44 | |
45 def __init__(self, name, x=800, y=600): | |
46 super(EditorLevel, self).__init__(name) | |
47 self.x = x | |
48 self.y = y | |
49 | |
50 def round_point(self, pos): | |
51 return (10 * (pos[0] // 10), 10 * (pos[1] // 10)) | |
52 | |
53 def point_to_pymunk(self, pos): | |
54 # inverse of point_to_pygame | |
55 # (this is also the same as point_to_pygame, but a additional | |
56 # function for sanity later in pyweek). | |
57 return (pos[0], self.y - pos[1]) | |
58 | |
59 def add_point(self, poly_index, pos): | |
60 self.polygons.setdefault(poly_index, []) | |
61 if not self.polygons[poly_index]: | |
99 | 62 point = self.point_to_pymunk(self.round_point(pos)) |
63 self.polygons[poly_index].append(point) | |
51 | 64 else: |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
65 add_pos = self.fix_poly_angle(poly_index, pos) |
51 | 66 self.polygons[poly_index].append(add_pos) |
67 | |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
68 def _fix_angle(self, point1, pos): |
51 | 69 # We want the line (point1 to pos) to be an angle of |
70 # 0, 45, 90, 135, 180, 225, 270, 305 | |
71 # However, we only need to consider half the circle | |
72 # This is a hack to approximate the right thing | |
73 pos0 = (pos[0], point1[1]) | |
74 pos90 = (point1[0], pos[1]) | |
75 dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1])) | |
76 pos45 = (point1[0] + dist, point1[1] + dist) | |
77 pos135 = (point1[0] + dist, point1[1] - dist) | |
78 pos225 = (point1[0] - dist, point1[1] - dist) | |
79 pos305 = (point1[0] - dist, point1[1] + dist) | |
80 min_dist = 9999999 | |
81 new_pos = point1 | |
82 for cand in [pos0, pos90, pos45, pos135, pos225, pos305]: | |
83 dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2 | |
84 if dist < min_dist: | |
85 new_pos = cand | |
86 min_dist = dist | |
87 return self.point_to_pymunk(new_pos) | |
88 | |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
89 def fix_line_angle(self, start_pos, pos): |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
90 start_pos = self.round_point(start_pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
91 pos = self.round_point(pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
92 return self._fix_angle(start_pos, pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
93 |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
94 def fix_poly_angle(self, index, pos): |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
95 # Last point |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
96 point1 = self.point_to_pygame(self.polygons[index][-1]) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
97 pos = self.round_point(pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
98 return self._fix_angle(point1, pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
99 |
51 | 100 def delete_point(self, index): |
101 if index in self.polygons and len(self.polygons[index]) > 0: | |
102 self.polygons[index].pop() | |
103 | |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
104 def close_poly(self, index): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
105 """Attempts to close the current polygon. |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
106 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
107 We allow a small additional step to close the polygon, but |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
108 it's limited as it's a magic point addition""" |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
109 if len(self.polygons[index]) < 2: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
110 # Too small |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
111 return False |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
112 first = self.polygons[index][0] |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
113 if self.fix_poly_angle(index, self.point_to_pygame(first)) == first: |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
114 self.add_point(index, self.point_to_pygame(first)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
115 return True |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
116 candidates = [(first[0] + 10 * i, first[1]) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
117 i in (-3, -2, -1, 1, 2, 3)] |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
118 candidates.extend([(first[0], first[1] + 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
119 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
120 candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
121 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
122 candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
123 i in (-3, -2, -1, 1, 2, 3)]) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
124 min_dist = 99999 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
125 poss = None |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
126 for cand in candidates: |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
127 if self.fix_poly_angle(index, self.point_to_pygame(cand)) == cand: |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
128 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2 |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
129 if dist < min_dist: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
130 poss = cand |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
131 if poss is not None: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
132 self.add_point(index, self.point_to_pygame(poss)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
133 self.add_point(index, self.point_to_pygame(first)) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
134 return True |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
135 return False |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
136 |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
137 def add_line(self, start_pos, end_pos): |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
138 endpoint = self.fix_line_angle(start_pos, end_pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
139 startpoint = self.point_to_pymunk(self.round_point(start_pos)) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
140 self.lines.append([startpoint, endpoint]) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
141 |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
142 def draw(self, mouse_pos, mouse_poly, filled, draw_cand_line, start_pos): |
51 | 143 self._draw_background(True) |
144 # 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
|
145 if filled: |
a9b564c38bef
Support showing exterior filled view in the editor
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
146 self._draw_exterior(True) |
51 | 147 for index, polygon in self.polygons.items(): |
148 color = POLY_COLORS[index] | |
149 if len(polygon) > 1: | |
150 pointlist = [self.point_to_pygame(p) for p in polygon] | |
151 pygame.draw.lines(self._surface, color, False, pointlist, 2) | |
152 if index == mouse_poly and mouse_pos: | |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
153 endpoint = self.fix_poly_angle(index, mouse_pos) |
51 | 154 pygame.draw.line(self._surface, color, |
99 | 155 self.point_to_pygame(polygon[-1]), |
156 self.point_to_pygame(endpoint)) | |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
157 for line in self.lines: |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
158 pointlist = [self.point_to_pygame(p) for p in line] |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
159 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2) |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
160 if draw_cand_line and start_pos and mouse_pos: |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
161 endpoint = self.fix_line_angle(start_pos, mouse_pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
162 pointlist = [self.round_point(start_pos), |
206 | 163 self.point_to_pygame(endpoint)] |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
164 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 1) |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
165 return self._surface.copy() |
51 | 166 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
167 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
168 class LevelWidget(Widget): |
51 | 169 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
170 def __init__(self, level): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
171 super(LevelWidget, self).__init__(pygame.rect.Rect(0, 0, |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
172 SCREEN[0], SCREEN[1])) |
51 | 173 self.level = level |
174 self.pos = (0, 0) | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
175 self.filled_mode = False |
51 | 176 self.mouse_pos = None |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
177 self.cur_poly = None |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
178 self._mouse_drag = False |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
179 self._draw_objects = False |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
180 self._draw_enemies = False |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
181 self._draw_lines = False |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
182 self._start_pos = None |
51 | 183 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
184 def _level_coordinates(self, pos): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
185 # Move positions to level values |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
186 if not pos: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
187 return (0, 0) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
188 return pos[0] + self.pos[0], pos[1] + self.pos[1] |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
189 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
190 def _move_view(self, offset): |
51 | 191 new_pos = [self.pos[0] + offset[0], self.pos[1] + offset[1]] |
192 if new_pos[0] < 0: | |
193 new_pos[0] = self.pos[0] | |
194 elif new_pos[0] > self.level.x - SCREEN[0]: | |
195 new_pos[0] = self.pos[0] | |
196 if new_pos[1] < 0: | |
197 new_pos[1] = self.pos[1] | |
198 elif new_pos[1] > self.level.y - SCREEN[1]: | |
199 new_pos[1] = self.pos[1] | |
200 self.pos = tuple(new_pos) | |
201 | |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
202 def set_objects(self, value): |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
203 if self._draw_objects != value: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
204 self._draw_objects = value |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
205 self.invalidate() |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
206 |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
207 def set_enemies(self, value): |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
208 if self._draw_enemies != value: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
209 self._draw_enemies = value |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
210 self.invalidate() |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
211 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
212 def draw(self, surface): |
51 | 213 if (self.cur_poly is not None and self.cur_poly in self.level.polygons |
214 and len(self.level.polygons[self.cur_poly])): | |
215 # We have an active polygon | |
216 mouse_pos = self._level_coordinates(self.mouse_pos) | |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
217 elif self._draw_lines: |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
218 # Interior wall mode |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
219 mouse_pos = self._level_coordinates(self.mouse_pos) |
51 | 220 else: |
221 mouse_pos = None | |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
222 level_surface = level.draw(mouse_pos, self.cur_poly, self.filled_mode, |
206 | 223 self._draw_lines, self._start_pos) |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
224 if self._draw_objects: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
225 for thing in self.level.drawables: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
226 if not isinstance(thing, Enemy): |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
227 thing.render(level_surface) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
228 if self._draw_enemies: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
229 for thing in self.level.drawables: |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
230 if isinstance(thing, Enemy): |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
231 thing.render(level_surface) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
232 surface_area = pygame.rect.Rect(self.pos, SCREEN) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
233 surface.blit(level_surface, (0, 0), surface_area) |
51 | 234 |
115 | 235 def change_poly(self, new_poly): |
236 self.cur_poly = new_poly | |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
237 self._draw_lines = False |
115 | 238 if self.cur_poly is not None: |
239 self.filled_mode = False | |
240 | |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
241 def line_mode(self): |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
242 self.cur_poly = None |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
243 self._draw_lines = True |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
244 self.filled_mode = False |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
245 self._start_pos = None |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
246 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
247 def key_down(self, ev): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
248 if ev.key == pgl.K_LEFT: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
249 self._move_view((-10, 0)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
250 elif ev.key == pgl.K_RIGHT: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
251 self._move_view((10, 0)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
252 elif ev.key == pgl.K_UP: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
253 self._move_view((0, -10)) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
254 elif ev.key == pgl.K_DOWN: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
255 self._move_view((0, 10)) |
115 | 256 elif ev.key in (pgl.K_1, pgl.K_2, pgl.K_3, pgl.K_4, pgl.K_5, pgl.K_6): |
257 self.change_poly(ev.key - pgl.K_0) | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
258 elif ev.key == pgl.K_0: |
115 | 259 self.change_poly(None) |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
260 elif ev.key == pgl.K_d and self.cur_poly: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
261 self.level.delete_point(self.cur_poly) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
262 elif ev.key == pgl.K_f: |
117 | 263 self.set_filled() |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
264 elif ev.key == pgl.K_c: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
265 self.close_poly() |
117 | 266 |
267 def set_filled(self): | |
165
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
268 closed, _ = self.level.all_closed() |
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
269 if closed: |
117 | 270 self.cur_poly = None |
271 self.filled_mode = True | |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
272 self._draw_lines = False |
117 | 273 else: |
165
dba8bc454a43
Fix 'fill exterior' button
Neil Muller <drnlmuller@gmail.com>
parents:
157
diff
changeset
|
274 alert('Not all polygons closed, so not filling') |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
275 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
276 def mouse_move(self, ev): |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
277 old_pos = self.mouse_pos |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
278 self.mouse_pos = ev.pos |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
279 if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines): |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
280 self.invalidate() |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
281 |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
282 def mouse_drag(self, ev): |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
283 if self._mouse_drag: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
284 old_pos = self.mouse_pos |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
285 self.mouse_pos = ev.pos |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
286 diff = (-self.mouse_pos[0] + old_pos[0], |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
287 -self.mouse_pos[1] + old_pos[1]) |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
288 self._move_view(diff) |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
289 self.invalidate() |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
290 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
291 def mouse_down(self, ev): |
184
e003b994c48b
Output coordinates from the level editor, to help place things
Stefano Rivera <stefano@rivera.za.net>
parents:
167
diff
changeset
|
292 if ev.button == 1: |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
293 if self._draw_lines: |
205
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
294 if self._start_pos is None: |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
295 self._start_pos = ev.pos |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
296 else: |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
297 self.level.add_line(self._start_pos, ev.pos) |
51f979ddddbb
Munge in interior wall drawing
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
298 self._start_pos = None |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
299 else: |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
300 print "Click: %r" % ( |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
301 self.level.point_to_pymunk( |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
302 self._level_coordinates(ev.pos)),) |
157 | 303 if ev.button == 4: # Scroll up |
304 self._move_view((0, -10)) | |
305 elif ev.button == 5: # Scroll down | |
306 self._move_view((0, 10)) | |
307 elif ev.button == 6: # Scroll left | |
308 self._move_view((-10, 0)) | |
309 elif ev.button == 7: # Scroll right | |
310 self._move_view((10, 0)) | |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
311 elif self.cur_poly and ev.button == 1: |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
312 # Add a point |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
313 self.level.add_point(self.cur_poly, |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
314 self._level_coordinates(ev.pos)) |
167
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
315 elif ev.button == 3: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
316 self._mouse_drag = True |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
317 |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
318 def mouse_up(self, ev): |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
319 if ev.button == 3: |
bb297f3f99f4
Allow using right mouse button to drag level display
Neil Muller <drnlmuller@gmail.com>
parents:
166
diff
changeset
|
320 self._mouse_drag = False |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
321 |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
322 def close_poly(self): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
323 if self.cur_poly is None: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
324 return |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
325 if self.level.close_poly(self.cur_poly): |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
326 alert("Successfully closed the polygon") |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
327 self.change_poly(None) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
328 else: |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
329 alert("Failed to close the polygon") |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
330 |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
331 |
115 | 332 class PolyButton(Button): |
333 """Button for coosing the correct polygon""" | |
334 | |
335 def __init__(self, index, level_widget): | |
336 if index is not None: | |
337 text = "Draw: %s" % index | |
338 else: | |
339 text = 'Exit Draw Mode' | |
340 super(PolyButton, self).__init__(text) | |
341 self.index = index | |
342 self.level_widget = level_widget | |
343 | |
344 def action(self): | |
345 self.level_widget.change_poly(self.index) | |
346 | |
347 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
348 class EditorApp(RootWidget): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
349 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
350 def __init__(self, level, surface): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
351 super(EditorApp, self).__init__(surface) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
352 self.level = level |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
353 self.level_widget = LevelWidget(self.level) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
354 self.add(self.level_widget) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
355 |
115 | 356 # Add poly buttons |
357 y = 15 | |
358 for poly in range(1, 7): | |
359 but = PolyButton(poly, self.level_widget) | |
360 but.rect = pygame.rect.Rect(0, 0, MENU_WIDTH // 2 - MENU_PAD, | |
361 MENU_BUTTON_HEIGHT) | |
362 if poly % 2: | |
363 but.rect.move_ip(MENU_LEFT, y) | |
364 else: | |
365 but.rect.move_ip(MENU_LEFT + MENU_WIDTH // 2 - MENU_HALF_PAD, | |
366 y) | |
367 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
368 self.add(but) | |
369 | |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
370 button_rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT) |
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
371 |
198 | 372 check_rect = pygame.rect.Rect(0, 0, MENU_BUTTON_HEIGHT // 2, |
373 MENU_BUTTON_HEIGHT // 2) | |
374 | |
115 | 375 end_poly_but = PolyButton(None, self.level_widget) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
376 end_poly_but.rect = button_rect.copy() |
115 | 377 end_poly_but.rect.move_ip(MENU_LEFT, y) |
378 self.add(end_poly_but) | |
379 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
380 | |
204
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
381 draw_line = Button("Draw interior wall", self.level_widget.line_mode) |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
382 draw_line.rect = button_rect.copy() |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
383 draw_line.rect.move_ip(MENU_LEFT, y) |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
384 self.add(draw_line) |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
385 y += MENU_BUTTON_HEIGHT + MENU_PAD |
687459429550
Display interior walls and start working towards drawing them
Neil Muller <drnlmuller@gmail.com>
parents:
199
diff
changeset
|
386 |
117 | 387 fill_but = Button('Fill exterior', action=self.level_widget.set_filled) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
388 fill_but.rect = button_rect.copy() |
117 | 389 fill_but.rect.move_ip(MENU_LEFT, y) |
390 self.add(fill_but) | |
391 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
392 | |
116 | 393 save_but = Button('Save Level', action=self.save) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
394 save_but.rect = button_rect.copy() |
116 | 395 save_but.rect.move_ip(MENU_LEFT, y) |
396 self.add(save_but) | |
397 y += MENU_BUTTON_HEIGHT + MENU_PAD | |
398 | |
135
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
399 close_poly_but = Button('Close Polygon', |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
400 action=self.level_widget.close_poly) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
401 close_poly_but.rect = button_rect.copy() |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
402 close_poly_but.rect.move_ip(MENU_LEFT, y) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
403 self.add(close_poly_but) |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
404 y += MENU_BUTTON_HEIGHT + MENU_PAD |
c9b6685ebebf
Add hack'ish 'close polygon' option
Neil Muller <drnlmuller@gmail.com>
parents:
134
diff
changeset
|
405 |
198 | 406 white = pygame.color.Color("white") |
407 self.show_objs = CheckBox(fg_color=white) | |
408 self.show_objs.rect = check_rect.copy() | |
409 self.show_objs.rect.move_ip(MENU_LEFT, y) | |
410 label = Label("Show Objects", fg_color=white) | |
411 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y) | |
412 self.add(self.show_objs) | |
413 self.add(label) | |
414 y += label.rect.height + MENU_PAD | |
415 | |
416 self.show_enemies = CheckBox(fg_color=white) | |
417 self.show_enemies.rect = check_rect.copy() | |
418 self.show_enemies.rect.move_ip(MENU_LEFT, y) | |
419 label = Label("Show enemy start pos", fg_color=white) | |
420 label.rect.move_ip(MENU_LEFT + MENU_BUTTON_HEIGHT // 2 + MENU_PAD, y) | |
421 self.add(self.show_enemies) | |
422 self.add(label) | |
423 y += label.rect.height + MENU_PAD | |
424 | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
425 quit_but = Button('Quit', action=self.quit) |
134
2e1059b1a247
Tweak button creation code
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
426 quit_but.rect = button_rect.copy() |
115 | 427 quit_but.rect.move_ip(MENU_LEFT, y) |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
428 self.add(quit_but) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
429 |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
430 def key_down(self, ev): |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
431 if ev.key == pgl.K_ESCAPE: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
432 self.quit() |
116 | 433 elif ev.key == pgl.K_s: |
434 self.save() | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
435 else: |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
436 self.level_widget.key_down(ev) |
51 | 437 |
116 | 438 def save(self): |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
439 closed, messages = self.level.all_closed() |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
440 if closed: |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
441 self.level.save() |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
442 # display success |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
443 alert("Level %s saved successfully." % self.level.name) |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
444 else: |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
445 # display errors |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
446 alert("Failed to save level.\n\n%s" % '\n'.join(messages)) |
116 | 447 |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
448 def mouse_move(self, ev): |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
449 self.level_widget.mouse_move(ev) |
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
450 |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
451 def draw(self, surface): |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
452 # Update checkbox state |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
453 self.level_widget.set_objects(self.show_objs.value) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
454 self.level_widget.set_enemies(self.show_enemies.value) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
455 super(EditorApp, self).draw(surface) |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
456 |
51 | 457 |
458 if __name__ == "__main__": | |
195
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
459 if len(sys.argv) not in [2, 4]: |
51 | 460 print 'Please supply a levelname or levelname and level size' |
461 sys.exit() | |
199
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
462 # Need to ensure we have defaults for rendering |
c291fd4b49bf
Add showing objects to the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
198
diff
changeset
|
463 parse_args([]) |
51 | 464 pygame.display.init() |
465 pygame.font.init() | |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
466 pygame.display.set_mode((SCREEN[0] + MENU_WIDTH, SCREEN[1]), |
109
817d4a62135c
Partially albowify the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
467 pgl.SWSURFACE) |
195
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
468 if len(sys.argv) == 2: |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
469 level = EditorLevel(sys.argv[1]) |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
470 level.load(pymunk.Space()) |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
471 elif len(sys.argv) == 4: |
b8701c0bb184
Reorder startup to avoid pygame init issues
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
472 level = EditorLevel(sys.argv[1], int(sys.argv[2]), int(sys.argv[3])) |
51 | 473 pygame.display.set_caption('Nagslang Area Editor') |
108
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
474 pygame.key.set_repeat(200, 100) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
475 app = EditorApp(level, pygame.display.get_surface()) |
dde91f8c4335
Add fonts needed for Albowifying the editor
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
476 app.run() |