File: leveledit.py

package info (click to toggle)
bouncy 0.6.20071104-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,924 kB
  • ctags: 914
  • sloc: python: 4,672; makefile: 2; sh: 2
file content (399 lines) | stat: -rw-r--r-- 14,270 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import sys, pygame, csv, shutil, os
import fonts
from pygame.locals import *
from pygame.constants import *

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

pygame.init()
glutInit(sys.argv[1:])
viewport = (1024, 768)
pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF)

import objects

import pyglyph

class excel_colon(csv.excel):
    delimiter = ':'

class LevelEditor:
    def __init__(self, viewport):
        self.viewport = viewport
        self.width, self.height = viewport

        # set up 2d mode
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        x, y = self.viewport
        glViewport(0, 0, x, y)
        glOrtho(0, x, 0, y, -50, 50)
        glLightfv(GL_LIGHT0, GL_POSITION,  (500, 500, 500, 0.0))
        glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
        glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
        glEnable(GL_LIGHT0)
        glEnable(GL_COLOR_MATERIAL)
        glShadeModel(GL_SMOOTH)

        # load the objects
        objects.load()

        self.active_add = None
        self.undo_move = None
        self.mode = 'props'

        # load up fonts
        self.sans20 = fonts.sans20
        self.sans40 = fonts.sans40

        # button labels and callback funcs
        self.buttons = [
            (pyglyph.layout_text(label, font=self.sans20), func,
                    Rect(0, i*32, 120, 32))
            for i, (label, func) in enumerate([
                ('Quit (ESC)', self.do_quit),
                ('Reset (L)', self.do_reset),
                ('Save (S)', self.do_save),
                ('Props (P)', lambda: self.set_mode('props')),
                ('Food (F)', lambda: self.set_mode('food')),
            ])
        ]

        self.prop_buttons = [
            (pyglyph.layout_text(label, font=self.sans20), func,
                    Rect(0, (i+len(self.buttons)+1)*32, 120, 32))
            for i, (label, func) in enumerate([
                ('Fence', lambda: self.set_active_add(objects.Fence)),
                ('Gate', lambda: self.set_active_add(objects.Gate)),
                ('Row', lambda: self.set_active_add(objects.Row)),
                ('Farmer', lambda: self.set_active_add(objects.Farmer)),
                ('Player', lambda: self.set_active_add(objects.Player)),
                ('Tree', lambda: self.set_active_add(objects.Tree)),
                ('Hedge', lambda: self.set_active_add(objects.Hedge)),
                ('Long hedge', lambda: self.set_active_add(objects.LongHedge)),
                ('Scarecrow', lambda: self.set_active_add(objects.Scarecrow)),
                ('Bucket', lambda: self.set_active_add(objects.Bucket)),
                ('Pie', lambda: self.set_active_add(objects.Pie)),
            ])
        ]

        self.food_buttons = [
            (pyglyph.layout_text(label, font=self.sans20), func,
                    Rect(0, (i+len(self.buttons)+1)*32, 120, 32))
            for i, (label, func) in enumerate([
                ('Carrot', lambda: self.set_active_add(objects.Carrot)),
                ('Lettuce', lambda: self.set_active_add(objects.Lettuce)),
                ('Tomato', lambda: self.set_active_add(objects.Tomato)),
            ])
        ]

        self.pick_menu_buttons = [
            (pyglyph.layout_text(label, font=self.sans20), func,
                    Rect(0, i*32, 150, 32))
            for i, (label, func) in enumerate([
                ('Delete', self.delete_pick),
                ('Move', self.move_pick),
                ('Rotate CW', self.rotate_pick_cw),
                ('Rotate CCW', self.rotate_pick_ccw),
            ])
        ]

    def set_mode(self, mode): self.mode = mode
    def set_active_add(self, cls): self.active_add = cls

    def do_quit(self):
        self.running = False

    def do_save(self):
        if os.path.exists(self.filename):
            shutil.copyfile(self.filename, self.filename+'.bak')

        writer = csv.writer(open(self.filename+'.tmp', 'w'), excel_colon)
        writer.writerow(['object', 'position', 'rotation'])
        for object in self.objects:
            writer.writerow([object.name, ','.join(map(str, object.position)),
                object.rotation])
        os.rename(self.filename+'.tmp', self.filename)

        self.ok_dialog('Map saved, press a key')

    def do_reset(self):
        self.objects = []

        if not os.path.exists(self.filename): return

        reader = csv.reader(open(self.filename), excel_colon)
        for name, position, rotation in list(reader)[1:]:
            if name[0] == '#': continue
            position = map(float, position.split(','))
            rotation = float(rotation)
            object = objects.map_elements[name](rotation, position)
            self.objects.append(object)

    def delete_pick(self):
        for i, object in enumerate(self.objects):
            if object is self.picked_obj:
                del self.objects[i]
        self.picked_obj = None
    def move_pick(self):
        self.active_add = self.picked_obj.__class__
        self.obj_rotation = self.picked_obj.rotation
        self.undo_move = self.picked_obj
        self.delete_pick()
    def rotate_pick_cw(self):
        self.picked_obj.rotation -= 90
        self.picked_obj = None
    def rotate_pick_ccw(self):
        self.picked_obj.rotation += 90
        self.picked_obj = None

    def draw_menu(self, menu, hover=None, center=None, click=False):
        if center:
            all = menu[0][2]
            for x, x, rect in menu[1:]:
                all = all.union(rect)
            all.center = center
            all = all.clamp((0, 0, self.width, self.height))
        handled = False
        for label, func, rect in menu:

            if center:
                ox, oy = all.topleft
                rect = Rect(rect)
                rect.x += ox
                rect.y += oy

            if rect.collidepoint(hover):
                glColor4f(1., 1., .7, .6)
                if click:
                    func()
                    handled = True
            else:
                glColor4f(.9, .9, .9, .6)
            glBegin(GL_QUADS)
            glVertex2f(rect.left + 1, self.height - (rect.top + 1))
            glVertex2f(rect.left + rect.width + 1, self.height - (rect.top + 1))
            glVertex2f(rect.left + rect.width + 1, self.height - (rect.top + 31))
            glVertex2f(rect.left + 1, self.height - (rect.top + 31))
            glEnd()

            pyglyph.begin()
            label.draw(pos=(rect.left + 5, self.height - (rect.top + 2)),
                anchor=(pyglyph.Align.left, pyglyph.Align.top))
            pyglyph.end()
        return handled

    def run(self, filename):
        self.filename = filename

        clock = pygame.time.Clock()
        self.running = True
        self.picked_obj = None
        rotation = 0
        scale = 20
        self.obj_rotation = 0

        self.do_reset()

        move = False
        vx, vy = (self.width/2, self.height/2)
        while self.running:
            ts = clock.tick(30)
            turn = 0
            hover = None
            click = menu_click = False
            for e in pygame.event.get():
                if e.type == QUIT: sys.exit()
                elif e.type == MOUSEBUTTONDOWN:
                    if e.button == 1: click = True
                    elif e.button == 2: move = True
                    elif e.button == 3:
                        if self.active_add is not None:
                            self.active_add = None
                            if self.undo_move is not None:
                                self.objects.append(self.undo_move)
                                self.undo_move = None
                        else:
                            menu_click = True
                    elif e.button == 4: scale += 1
                    elif e.button == 5: scale = max(1, scale-1)
                    continue
                elif e.type == MOUSEBUTTONUP:
                    if e.button == 2: move = False
                elif e.type == MOUSEMOTION:
                    i, j = e.rel
                    if move:
                        vx += i
                        vy -= j
                    continue
                if not hasattr(e, 'key'):
                    continue
                if e.key == K_ESCAPE: return
                down = e.type == KEYDOWN
                if e.key == K_LEFT and down: self.obj_rotation += 90
                if e.key == K_RIGHT and down: self.obj_rotation -= 90

            # set up projection
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            x, y = self.viewport
            glViewport(0, 0, x, y)
            glOrtho(0, x, 0, y, -50, 50)
            glMatrixMode(GL_MODELVIEW)

            # init drawing
            glClearColor(0.32, .48, .27, 0)
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            glLoadIdentity()

            # now into perspective view for the map
            glEnable(GL_DEPTH_TEST)
            glEnable(GL_LIGHTING)

            glPushMatrix()
            glTranslate(vx, vy, 0)
            glScalef(scale, scale, 1)
            glRotate(90, 1, 0, 0)

            # draw objs
            for n, object in enumerate(self.objects):
                object.render()

            if self.active_add is not None and x > 120:
                x, y = pygame.mouse.get_pos()
                w2, h2 = self.width/2, self.height/2
                offx, offy = w2 - vx, h2 - vy
                tx, ty = (x - w2 + offx)/scale, (y - h2 - offy)/scale
                glTranslate(tx, 0, ty)
                glRotate(self.obj_rotation, 0, 1, 0)
                glCallList(self.active_add.obj.gl_list)

                if click:
                    if self.active_add is objects.Player:
                        for i, object in enumerate(self.objects):
                            if isinstance(object, objects.Player):
                                del self.objects[i]
                                break
                    self.objects.append(self.active_add(self.obj_rotation,
                        (tx, 0, ty)))
                    self.active_add = None
                    click = False
            glPopMatrix()

            # draw grid
            glDisable(GL_LIGHTING)
            glEnable(GL_BLEND)
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
            glPushMatrix()
            left = -4*scale + vx%(4 * scale)
            top = -4*scale + vy%(4 * scale)
            glColor(1., 1., 1., .3)
            glBegin(GL_LINES)
            for x in range(left, left + self.width  + 4*scale, 4*scale):
                glVertex3f(x, top, 30)
                glVertex3f(x, top+self.height + 4*scale, 30)
            for y in range(top, top + self.height + 4*scale, 4*scale):
                glVertex3f(left, y, 30)
                glVertex3f(left+self.width + 4*scale, y, 30)
            glEnd()
            glPopMatrix()
            glDisable(GL_BLEND)

            glDisable(GL_DEPTH_TEST)
            x,y = pygame.mouse.get_pos()
            # draw buttons
            if self.draw_menu(self.buttons, hover=(x,y), click=click):
                click = False
                if self.picked_obj is not None:
                    self.picked_obj = None

            # secondary menu
            hover = None
            if self.mode == 'props': secondary = self.prop_buttons
            elif self.mode == 'food': secondary = self.food_buttons
            if self.draw_menu(secondary, hover=(x,y), click=click):
                click = False
                if self.picked_obj is not None:
                    self.picked_obj = None

            # object menu
            if self.picked_obj is not None:
                handled = self.draw_menu(self.pick_menu_buttons, hover=(x,y),
                    center=self.pick_center, click=click)
                if click and not handled:
                    self.picked_obj = None

            # object picking
            if menu_click and self.active_add is None:
                glMatrixMode(GL_PROJECTION)
                glLoadIdentity()
                glInitNames()
                gluPickMatrix(x, self.height-y, 5, 5,
                    (0, 0, self.width, self.height))
                glOrtho(0, self.width, 0, self.height, -50, 50)
                glSelectBuffer(512)
                glRenderMode(GL_SELECT)

                glMatrixMode(GL_MODELVIEW)
                glTranslate(vx, vy, 0)
                glScalef(scale, scale, 1)
                glRotate(90, 1, 0, 0)
                for n, object in enumerate(self.objects):
                    glPushName(n + 1)
                    object.render()

                pick = glRenderMode(GL_RENDER)
                if pick:
                    for a, b, names in pick:
                        pick = self.objects[names[-1] - 1]
                        break
                    self.picked_obj = pick
                    self.pick_center = (x, y)
                else:
                    self.picked_obj = None

            pygame.display.flip()

    def ok_dialog(self, text):
        text = pyglyph.layout_text(text, font=self.sans40)

        # set up 2d mode
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        x, y = self.viewport
        glViewport(0, 0, x, y)
        glOrtho(0, x, 0, y, -50, 50)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        glDisable(GL_LIGHTING)
        glDisable(GL_DEPTH_TEST)

        glBegin(GL_QUADS)
        glColor4f(.9, .9, .9, 1)
        glVertex2f(x/2 - 300, y/2-100)
        glVertex2f(x/2 + 300, y/2-100)
        glVertex2f(x/2 + 300, y/2+100)
        glVertex2f(x/2 - 300, y/2+100)
        glEnd()

        pyglyph.begin()
        text.draw(pos=(x/2, y/2),
            anchor=(pyglyph.Align.center, pyglyph.Align.center))
        pyglyph.end()

        clock = pygame.time.Clock()
        while 1:
            ts = clock.tick(10)
            for e in pygame.event.get():
                if e.type == MOUSEBUTTONDOWN: return
                if not hasattr(e, 'key'): continue
                if e.type == KEYDOWN: return
            pygame.display.flip()

if __name__ == '__main__':
    editor = LevelEditor(viewport)
    editor.run(sys.argv[1])