Editor now changes the map
authorNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 11:44:38 +0000 (13:44 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 11:44:38 +0000 (13:44 +0200)
erdslangetjie/editor/editor.py
erdslangetjie/level.py

index f2bf09b9d12e1afc071ebcebf914933a35a7149d..1606fd63bcd6f2d92a184ff197e6341941652716 100644 (file)
@@ -12,7 +12,8 @@ from kivy.utils import platform
 from kivy.config import Config
 
 from erdslangetjie.data import filepath, load
-from erdslangetjie.level import Level
+from erdslangetjie.level import (Level, WALL, GATE, BUTTON,
+        FLOOR, ENTRY, EXIT)
 
 
 if platform() != 'android':
@@ -20,6 +21,16 @@ if platform() != 'android':
     Config.set('graphics', 'height', '760')
 
 
+tool_map = {
+        'button': BUTTON,
+        'gate': GATE,
+        'wall': WALL,
+        'floor': FLOOR,
+        'add entry': ENTRY,
+        'add exit': EXIT
+        }
+
+
 class EditorWindow(FloatLayout):
 
     def __init__(self, level):
@@ -52,15 +63,7 @@ class EditorWindow(FloatLayout):
         for tile_line in tiles:
             bx = 0
             for tile in tile_line:
-                node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=(bx, by))
-                node.bind(on_touch_down=self.change_node)
-                node.bind(on_touch_move=self.change_node)
-                with node.canvas:
-                    Color(1, 1, 1)
-                    Rectangle(pos=node.pos, size=node.size,
-                            texture=tile.texture)
-                self.add_widget(node)
-                self.nodes[(bx, by)] = node
+                self._draw_tile((bx, by), tile)
                 bx += TILE_SIZE
             by += TILE_SIZE
 
@@ -69,6 +72,17 @@ class EditorWindow(FloatLayout):
         self.add_widget(name)
 
         self.draw_toolbar()
+        self.draw_save_button()
+
+    def _draw_tile(self, tile_pos, tile):
+        node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=tile_pos)
+        node.bind(on_touch_down=self.change_node)
+        node.bind(on_touch_move=self.change_node)
+        with node.canvas:
+            Color(1, 1, 1)
+            Rectangle(pos=node.pos, size=node.size, texture=tile.texture)
+        self.add_widget(node)
+        self.nodes[tile_pos] = node
 
     def draw_toolbar(self):
         for widget in self.tool_widgets:
@@ -76,12 +90,12 @@ class EditorWindow(FloatLayout):
             self.remove_widget(widget)
         self.tool_widgets = []
         y = 300
-        for tool in ['wall', 'floor', 'add entrance', 'add exit']:
+        for tool in sorted(tool_map):
             if self.tool == tool:
                 color = '00cccc'
             else:
                 color = 'cccccc'
-            tool_label = Label(text='[color=%s][ref=%s]%s[/color]' %
+            tool_label = Label(text='[color=%s][ref=%s]%s[/ref][/color]' %
                     (color, tool, tool), markup=True, font_size=24,
                     pos=(500, y))
             y -= 50
@@ -89,6 +103,15 @@ class EditorWindow(FloatLayout):
             self.add_widget(tool_label)
             self.tool_widgets.append(tool_label)
 
+    def draw_save_button(self):
+        save_label = Label(text='[color=ffffff][ref=save]Save[/ref][/color]',
+                markup=True, font_size=24, pos=(500, -100))
+        save_label.bind(on_ref_press=self.do_save)
+        self.add_widget(save_label)
+
+    def do_save(self, label, ref):
+        pass
+
     def change_tool(self, label, ref):
         self.tool = ref
         self.draw_toolbar()
@@ -101,8 +124,16 @@ class EditorWindow(FloatLayout):
         return True
 
     def change_node(self, node, touch):
-        if self.contained(node, touch.pos):
-            print touch, node, node.size, node.pos
+        if self.tool and self.contained(node, touch.pos):
+            tile_pos = (node.pos[0] / TILE_SIZE, node.pos[1] / TILE_SIZE)
+            action = tool_map[self.tool]
+            if self.level_obj.get_tile_type(tile_pos) != action:
+                self.level_obj.set_tile_type(tile_pos, action)
+                for map_pos, new_tile in self.level_obj.get_changed_tiles():
+                    node_pos = (map_pos[0] * TILE_SIZE, map_pos[1] * TILE_SIZE)
+                    old_node = self.nodes[node_pos]
+                    self.remove_widget(old_node)
+                    self._draw_tile(node_pos, new_tile)
 
 
 class EditorApp(App):
index 8efc539e13a08d6b6ab7cafab4bd7fc1715f5197..73878aa927bf438439d6b6c08d06dabf2b021a25 100644 (file)
@@ -93,17 +93,24 @@ class Level(object):
         return self._data[pos[1]][pos[0]]
 
     def set_tile_type(self, pos, new_type):
+        print '\n'.join([''.join(x) for x in self._data])
         self._data[pos[1]][pos[0]] = new_type
-        new_tile = self._get_tile_image(new_type, pos)
+        print
+        print '\n'.join([''.join(x) for x in self._data])
+        print pos, self._in_limits(pos)
+        new_tile = self._get_tile_image(pos, new_type)
         self._tiles[pos[1]][pos[0]] = new_tile
         self._changed.append((pos, new_tile))
         # Also update neighbourhood for wall types, etc.
         for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]),
-                (pos[0], pos[1] - 1), (pos[0], pos[1] + 1)]:
+                (pos[0] - 1, pos[1] - 1), (pos[0] + 1, pos[1] + 1),
+                (pos[0], pos[1] - 1), (pos[0], pos[1] + 1),
+                (pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]:
             if not self._in_limits(new_pos):
                 continue
             tile = self._data[new_pos[1]][new_pos[0]]
-            new_tile = self._get_tile_image(tile, pos)
+            print new_pos, tile
+            new_tile = self._get_tile_image(new_pos, tile)
             self._tiles[new_pos[1]][new_pos[0]] = new_tile
             self._changed.append((new_pos, new_tile))
 
@@ -174,6 +181,7 @@ class Level(object):
             self._data[pos[1]][pos[0]]
         except IndexError:
             return False
+        print pos, self._data[pos[1]][pos[0]]
         return True
 
     def blocked(self, pos):