From bbbe943d8b35fd351814aaef8f73a171c0f600b3 Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 14 Apr 2013 17:23:11 +0200 Subject: [PATCH] Editor displays something, although it's hack'ish --- data/tiles/door.bmp | Bin 0 -> 2102 bytes data/tiles/floor.bmp | Bin 0 -> 2102 bytes data/tiles/wall.bmp | Bin 0 -> 2102 bytes erdslangetjie/data.py | 8 +++++++ erdslangetjie/editor/editor.py | 38 +++++++++++++++++++++++++++------ erdslangetjie/level.py | 30 ++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 data/tiles/door.bmp create mode 100644 data/tiles/floor.bmp create mode 100644 data/tiles/wall.bmp create mode 100644 erdslangetjie/level.py diff --git a/data/tiles/door.bmp b/data/tiles/door.bmp new file mode 100644 index 0000000000000000000000000000000000000000..96ce07bf8ed2b472e9f8cc602601d398d5af4628 GIT binary patch literal 2102 zcmb`GJ!lj`7>2)qg@whH*SW9Bs67Q))E`!lH$# ztf#QDI|m|Joo zoT(Q-Tg!)ro51u;$Fxkt)XbO}F{jLlXl*4;ofzXK%Ys-7tC@ELTRlk~+b!D-+qK9o z_$cBTegbdd9lVDR@C|GmgIFvIBJnJ-44j5*d0D1M(riiDYCtkjOWE*65XrsB+!2Sh zT|*2lGK;(EsaK1G_wWI}Y5BGWYGIM~9rdtg40UK?z{|h%Vp(%3JbK9B+r4cJVzCIH z!fSZy))D*^KEk~z?oE&^TO3AelH&c2vh zBkG>gi=o~*J?8XU(6gua4ej$P@PJ;X^kS@mpTpBY_AbNB+DK5zj0GVy{YDNmX^7<`ZM-Qb_bzZm~4{yF^f_!sc6 z!M}C{hG#g2Wf+EL#Eb@mLhBW|9Nt-rZqPLusLR4<@CH7G*R0?)+3}palamu!EEaNj zc*uLp`ED}4p%g!2K8cC!K6)jOUw)S7?|;ghPe*d_y^(Lf8oB-8iA0%|EIUMf*#xqQ3^U2TY; zsAaPA|A|UE`Hv}Qq#s|_SFg}jeW{Q6ZKXPVIpVi_`Ts}n`#0);yQf!b(4SIOr~k65 zhI%#7tG++g_3CvsieGo@Q}s}S|NSMiwfgZ$pQ)sC iK58}O(R=4>h2G)IRq?z2jdXRT`pebV($dxHdH%n0xPS%# literal 0 HcmV?d00001 diff --git a/data/tiles/floor.bmp b/data/tiles/floor.bmp new file mode 100644 index 0000000000000000000000000000000000000000..b9fa86f22d3924ce97eaa65ecee56802f1c1eb48 GIT binary patch literal 2102 zcmd^;p>GsH6vkf@1OjpL94ipW6{M-0V5n)b0znXDBqR;lkU-8cV8F4JcWOdHvH?LA zdISn}3ID(t`~`(*vi#oLy}P|au0b=~X=mqq-+SNpUGMJ6;dQZfcunLsr()O#G604S zzYf+f9f!__x)=RGbh`yaeuI1s^=u@U>QZ(2Gm>6?_LD z-L8N?ggm-PRFTN731-hqr z@#vl5vB7JL=Y;nw?DJ8O0bZtf@xjBN!EfNV@Y7ZH(OeQ`L6lRX^+Y^FcSGbYv*GWz zPn7Tr`1}tAyu=y&27U`4A0$A0wIl(PiZ{mz4!M`)?#a)QUrBy}{37`!^2_Ack)J-9 zC-Fp{z~gxwPs!5}P}Z!N70E6Ha1ZX7fUW?)hVS85@EtcK&2%#7v|6oXxm?Qm`8l6k z!}q4-6-V`s)@)e_x8}nlILiy7h6fIOUc_*?gL>3u! zi0xTw7?JDo+4jt9B!geT@8I{!*BO|FAo4TjA!aRe7;3~vIIVOm%`UvtwAk< z@Dun3K69H0{v3XUdvn~I0Xb4KQVS>@RC@Dwp8YXiM3KS5n6aEJ(6~fvj^+aGTH-wW zVrGq)dyW>%ybClg(VC;VK>G#qxe7c$%N#A%S@;Y1OZYkb;v)IX9AjmKm2<4MSiE5F zC6?#(hJU_%tb`xI=N)or$rkXJ@N@X+zya(_r4TSl_+~i4!S@*7E&dt&i}BClUxt4L z{&o1*c!>_%3`QFR5yALHwo!or$ zNlaGA{22#Z-c)tdY6e9%_?Gq)4n6c$Qs_%{W|{ay zUHwn%&_h8c8TpmYqJ0T_YUx==t=nO~P%~84Q{bEW)0^F zZ?~*zzQ3gpHf(U}(-l#pbQP%zNB@vs)oMbJ=}9bA=^4D#v6(*Dpi}xFdDP#Qs=b64 zq_0X2;e3j#+e1vP(F)pY!qos>T7NjxPOogZ-!1Za3op3;@%5#85Ux#GoYiUckND77 z6@dm}96S?Gco3uGgNC!Rc>_kAATJ$P#=AA%FKVJFy literal 0 HcmV?d00001 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 -- 2.34.1