view mamba/habitats/editor.py @ 170:77f1dae64019

Use name in editor
author Neil Muller <drnlmuller@gmail.com>
date Tue, 13 Sep 2011 23:52:16 +0200
parents 6b1cdbdd34ca
children dabf13abb3fd
line wrap: on
line source

"""Habitat for editing levels."""

import pygame.display
from pygame.locals import SWSURFACE, KEYDOWN

from mamba.engine import Habitat, NewHabitatEvent
from mamba.widgets.level import EditLevelWidget
from mamba.widgets.text import TextWidget
from mamba.widgets.imagebutton import ImageButtonWidget
from mamba.level import Level, TILE_MAP
from mamba.constants import SCREEN, EDIT_SCREEN, NAME, ESCAPE_KEYS


class EditorHabitat(Habitat):
    def __init__(self, level_name):
        super(EditorHabitat, self).__init__(EDIT_SCREEN)
        self.level = Level(level_name)
        self.container.add(EditLevelWidget(self.level))
        self.container.add_callback(KEYDOWN, self.keydown_event)

    def on_enter(self):
        # We need to juggle the display to the correct size
        # This is a horrible hack
        pygame.display.quit()
        pygame.display.init()
        pygame.display.set_mode(EDIT_SCREEN, SWSURFACE)
        pygame.display.set_caption('%s Level editor' % NAME)
        super(EditorHabitat, self).on_enter()
        self.setup_toolbar()

    def on_exit(self):
        # We need to juggle the display to the correct size
        # This is a horrible hack
        super(EditorHabitat, self).on_exit()
        pygame.display.quit()
        pygame.display.init()
        pygame.display.set_mode(SCREEN, SWSURFACE)
        pygame.display.set_caption(NAME)

    def keydown_event(self, ev, widget):
        if ev.key in ESCAPE_KEYS:
            from mamba.habitats.mainmenu import MainMenu
            NewHabitatEvent.post(MainMenu())

    def setup_toolbar(self):
        """Draw the editor toolbar"""
        button_height = 20
        button_left = 820
        button_padding = 10
        levelname = TextWidget(
                (button_left, button_height),
                'Level: %s' % self.level.name, color='white')
        self.container.add(levelname)
        button_height += levelname.surface.get_height() + button_padding

        tilesetname = TextWidget(
                (button_left, button_height),
                'Tileset: %s' % self.level.tileset.name, color='white')
        self.container.add(tilesetname)
        button_height += tilesetname.surface.get_height() + button_padding
        floor_button = ImageButtonWidget(
                (button_left, button_height), self.level.tileset.floor,
                'Floor', color='white')
        self.container.add(floor_button)
        button_height += floor_button.surface.get_height() + button_padding
        for tile_char in TILE_MAP:
            try:
                tile = self.level.tileset[tile_char]
            except pygame.error:
                # Ignore stuff we can't load for now
                continue
            if tile is None:
                continue
            if tile.name:
                text = tile.name
            else:
                text = 'Tile'
            tile_button = ImageButtonWidget(
                    (button_left, button_height), tile.image,
                    text, color='white')
            self.container.add(tile_button)
            button_height += \
                    tile_button.surface.get_height() + button_padding