From: Neil Date: Sun, 14 Apr 2013 15:23:11 +0000 (+0200) Subject: Editor displays something, although it's hack'ish X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=bbbe943d8b35fd351814aaef8f73a171c0f600b3;p=erdslangetjie.git Editor displays something, although it's hack'ish --- diff --git a/data/tiles/door.bmp b/data/tiles/door.bmp new file mode 100644 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 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 index 0000000..124cb2b Binary files /dev/null and b/data/tiles/wall.bmp differ diff --git a/erdslangetjie/data.py b/erdslangetjie/data.py index d33d47a..6cca470 100644 --- a/erdslangetjie/data.py +++ b/erdslangetjie/data.py @@ -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)) diff --git a/erdslangetjie/editor/editor.py b/erdslangetjie/editor/editor.py index 998ad40..ecdbec7 100644 --- a/erdslangetjie/editor/editor.py +++ b/erdslangetjie/editor/editor.py @@ -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 index 0000000..a2c2a2d --- /dev/null +++ b/erdslangetjie/level.py @@ -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