Editor displays something, although it's hack'ish
authorNeil <neil@dip.sun.ac.za>
Sun, 14 Apr 2013 15:23:11 +0000 (17:23 +0200)
committerNeil <neil@dip.sun.ac.za>
Sun, 14 Apr 2013 15:23:11 +0000 (17:23 +0200)
data/tiles/door.bmp [new file with mode: 0644]
data/tiles/floor.bmp [new file with mode: 0644]
data/tiles/wall.bmp [new file with mode: 0644]
erdslangetjie/data.py
erdslangetjie/editor/editor.py
erdslangetjie/level.py [new file with mode: 0644]

diff --git a/data/tiles/door.bmp b/data/tiles/door.bmp
new file mode 100644 (file)
index 0000000..96ce07b
Binary files /dev/null and b/data/tiles/door.bmp differ
diff --git a/data/tiles/floor.bmp b/data/tiles/floor.bmp
new file mode 100644 (file)
index 0000000..b9fa86f
Binary files /dev/null and b/data/tiles/floor.bmp differ
diff --git a/data/tiles/wall.bmp b/data/tiles/wall.bmp
new file mode 100644 (file)
index 0000000..124cb2b
Binary files /dev/null and b/data/tiles/wall.bmp differ
index d33d47a754e587e4cde2e20e0c17fe3aba14f359..6cca470f4f926b04ea0e84cb727df843cc585b2d 100644 (file)
@@ -9,9 +9,11 @@ pyglet.resource search path.
 '''
 
 import os
+from kivy.core.image import ImageLoader
 
 data_py = os.path.abspath(os.path.dirname(__file__))
 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
+loader = ImageLoader()
 
 
 def filepath(filename):
@@ -27,3 +29,9 @@ def load(filename, mode='rb'):
     "mode" is passed as the second arg to open().
     '''
     return open(filepath(filename), mode)
+
+
+def load_image(filename):
+    '''Load an image into a kivy texture. We rely on kivy's caching
+       infrastructure'''
+    return loader.load(filepath(filename))
index 998ad4057e90e4d4c3ea4b0cfad29a9e3eb42474..ecdbec7cc61f3c3cc36f65248d656042ba7a60ee 100644 (file)
@@ -2,6 +2,7 @@ import sys
 import os
 import kivy
 from erdslangetjie.data import filepath, load
+from erdslangetjie.level import Level
 
 kivy.require('1.6.0')
 
@@ -9,24 +10,47 @@ from kivy.app import App
 from kivy.logger import Logger, LoggerHistory
 from kivy.uix.widget import Widget
 
+from kivy.uix.floatlayout import FloatLayout
+from kivy.uix.scatter import Scatter
+from kivy.graphics import Color, Rectangle
 
 
-class EditorWindow(Widget):
+class EditorWindow(FloatLayout):
+
     def __init__(self, level):
+        super(EditorWindow, self).__init__()
         if os.path.exists(filepath(level)):
-            self.level_data = load(level)
+            level_data = load(level)
             self.level = level
         elif os.path.exists(filepath('levels/' + level)):
-            self.level_data = load('levels/' + level)
+            level_data = load('levels/' + level)
             self.level = 'levels/' + level
         else:
             print 'Unable to find %s - treating this as a new level' % level
-            self.level_data = load('levels/blank.txt')
+            level_data = load('levels/blank.txt')
             if 'levels/' in level:
                 self.level = level
             else:
                 self.level = 'levels/' + level
-        super(EditorWindow, self).__init__()
+        self.level_obj = Level()
+        self.level_obj.load(level_data)
+        level_data.close()
+
+    def build(self):
+        self.clear_widgets()
+        tiles = self.level_obj.get_tiles()
+        tile_size = 24
+        bx, by = 0, 0
+        for tile_line in tiles:
+            bx = 0
+            for tile in tile_line:
+                node = Scatter(pos=(bx, by), size=(tile_size, tile_size))
+                with node.canvas:
+                    Color(1, 1, 1)
+                    Rectangle(size=node.size, texture=tile.texture)
+                self.add_widget(node)
+                bx += tile_size
+            by += tile_size
 
 
 class EditorApp(App):
@@ -36,7 +60,9 @@ class EditorApp(App):
         super(EditorApp, self).__init__()
 
     def build(self):
-        return EditorWindow(self.level)
+        editor = EditorWindow(self.level)
+        editor.build()
+        return editor
 
 
 def main():
diff --git a/erdslangetjie/level.py b/erdslangetjie/level.py
new file mode 100644 (file)
index 0000000..a2c2a2d
--- /dev/null
@@ -0,0 +1,30 @@
+# The level object
+
+from data import load_image
+
+
+class Level(object):
+
+    def __init__(self):
+        self.data = []
+
+    def load(self, levelfile):
+        """Load the level"""
+        self.data = []
+        for line in levelfile.readlines():
+            self.data.append(list(line))
+
+    def get_tiles(self):
+        """Return a list of tiles for the level"""
+        tiles = []
+        for j, line in enumerate(self.data):
+            tile_line = []
+            for i, c in enumerate(line):
+                if c == ' ':
+                    tile_line.append(load_image('tiles/floor.bmp'))
+                elif c == '.':
+                    tile_line.append(load_image('tiles/wall.bmp'))
+                elif c == 'E' or c == 'X':
+                    tile_line.append(load_image('tiles/door.bmp'))
+            tiles.append(tile_line)
+        return tiles