# HG changeset patch # User Neil Muller # Date 1315954301 -7200 # Node ID dabf13abb3fd790435bdce0a6c07c6c37ac9d4f2 # Parent 4c60f732ffc92af73dc0d4e45fd83567582d3618 Add basic drawing to level editor diff -r 4c60f732ffc9 -r dabf13abb3fd mamba/habitats/editor.py --- a/mamba/habitats/editor.py Tue Sep 13 23:52:39 2011 +0200 +++ b/mamba/habitats/editor.py Wed Sep 14 00:51:41 2011 +0200 @@ -15,7 +15,8 @@ def __init__(self, level_name): super(EditorHabitat, self).__init__(EDIT_SCREEN) self.level = Level(level_name) - self.container.add(EditLevelWidget(self.level)) + self.edit_widget = EditLevelWidget(self.level) + self.container.add(self.edit_widget) self.container.add_callback(KEYDOWN, self.keydown_event) def on_enter(self): @@ -47,6 +48,7 @@ button_height = 20 button_left = 820 button_padding = 10 + levelname = TextWidget( (button_left, button_height), 'Level: %s' % self.level.name, color='white') @@ -58,10 +60,18 @@ 'Tileset: %s' % self.level.tileset.name, color='white') self.container.add(tilesetname) button_height += tilesetname.surface.get_height() + button_padding + # TODO: Add Image widget for the current tool + self.current_tool = TextWidget((button_left, button_height), + 'Tool: Floor', color='white') + self.container.add(self.current_tool) + button_height += self.current_tool.surface.get_height() + button_height += button_padding floor_button = ImageButtonWidget( (button_left, button_height), self.level.tileset.floor, 'Floor', color='white') self.container.add(floor_button) + floor_button.add_callback('clicked', self.change_tool, + '.', 'Floor') button_height += floor_button.surface.get_height() + button_padding for tile_char in TILE_MAP: try: @@ -78,6 +88,13 @@ tile_button = ImageButtonWidget( (button_left, button_height), tile.image, text, color='white') + tile_button.add_callback('clicked', self.change_tool, + tile_char, text) self.container.add(tile_button) button_height += \ tile_button.surface.get_height() + button_padding + + def change_tool(self, ev, widget, new_tool, text): + self.edit_widget.set_tool(new_tool) + self.current_tool.text = 'Tool: %s' % text + self.current_tool.prepare() diff -r 4c60f732ffc9 -r dabf13abb3fd mamba/widgets/level.py --- a/mamba/widgets/level.py Tue Sep 13 23:52:39 2011 +0200 +++ b/mamba/widgets/level.py Wed Sep 14 00:51:41 2011 +0200 @@ -1,17 +1,47 @@ from pygame.rect import Rect +from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION from mamba.widgets.base import Widget +from mamba.constants import TILE_SIZE class EditLevelWidget(Widget): def __init__(self, level, offset=(0, 0)): self.level = level level_rect = Rect(offset, level.get_size()) + self.tool = None + self.drawing = False super(EditLevelWidget, self).__init__(level_rect) def draw(self, surface): self.level.draw(surface) + def set_tool(self, new_tool): + self.tool = new_tool + def event(self, event): - # TODO: Implement - pass + if event.type == MOUSEBUTTONDOWN: + self.drawing = True + self.update_tile(event.pos) + elif event.type == MOUSEBUTTONUP: + self.drawing = False + elif event.type == MOUSEMOTION and self.drawing: + # FIXME: Need to consider leaving and re-entering the widget + self.update_tile(event.pos) + + def update_tile(self, pixel_pos): + """Update the tile at the current mouse position""" + # We convert our current position into a tile position + # and replace the tile with the current tool + tile_pos = (pixel_pos[0] / TILE_SIZE[0], + pixel_pos[1] / TILE_SIZE[1]) + old_tile = self.level.tiles[tile_pos[1]][tile_pos[0]] + if self.tool == '.' and old_tile is None: + return + elif old_tile is not None and old_tile.tile_char == self.tool: + return + if old_tile is not None: + old_tile.remove(self.level.sprites) + new_tile = self.level.tileset.get_tile(self.tool, tile_pos, + self.level.sprites) + self.level.tiles[tile_pos[1]][tile_pos[0]] = new_tile