From b98f732a9dd6466d40f487552963654243c71e0a Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 20 Feb 2018 20:36:17 +0100 Subject: [PATCH] added more tiles, implemented plan.json --- src/TileSelector.py | 42 +++++++++++++++++++++++++----- src/Tiles.py | 58 ++++++++++++++++++++++++++++++++++++++++++ src/TrackPlan.py | 50 +++++++++++++++++++++++++++--------- src/web4rail.client.py | 4 ++- 4 files changed, 134 insertions(+), 20 deletions(-) diff --git a/src/TileSelector.py b/src/TileSelector.py index 06176c6..1685c8b 100644 --- a/src/TileSelector.py +++ b/src/TileSelector.py @@ -11,13 +11,41 @@ class TileSelector(Gtk.Window): self.grid = Gtk.Grid() - self.add_tile(StraightH,0,0) - self.add_tile(StraightV,1,0) - self.add_tile(Diag_TL,0,1) - self.add_tile(Diag_TR,1,1) - self.add_tile(Diag_BR,2,1) - self.add_tile(Diag_BL,3,1) - self.add_tile(TO_BRL,0,2) + x0=0 + x1=0 + x2=0 + + if connection in {Tile.LEFT,Tile.RIGHT,None}: + self.add_tile(StraightH,x0,0) + x0+=1 + + if connection in {Tile.TOP,Tile.BOTTOM,None}: + self.add_tile(StraightV,x0,0) + x0+=1 + + if connection in {Tile.TOP,Tile.LEFT,None}: + self.add_tile(Diag_TL,x1,1) + x1+=1 + + if connection in {Tile.TOP,Tile.RIGHT,None}: + self.add_tile(Diag_TR,x1,1) + x1+=1 + + if connection in {Tile.BOTTOM,Tile.RIGHT,None}: + self.add_tile(Diag_BR,x1,1) + x1+=1 + + if connection in {Tile.BOTTOM,Tile.LEFT,None}: + self.add_tile(Diag_BL,x1,1) + x1+=1 + + if connection in {Tile.BOTTOM,Tile.LEFT,Tile.RIGHT,None}: + self.add_tile(TO_BRL,0,2) + self.add_tile(TO_BLR,1,2) + + if connection in {Tile.TOP,Tile.LEFT,Tile.RIGHT,None}: + self.add_tile(TO_TRL,0,3) + self.add_tile(TO_TLR,1,3) self.add(self.grid) diff --git a/src/Tiles.py b/src/Tiles.py index b09a105..ad1d677 100644 --- a/src/Tiles.py +++ b/src/Tiles.py @@ -4,6 +4,11 @@ gi.require_version('Gtk', '3.0') from gi.repository import Gdk, Gtk class Tile(Gtk.DrawingArea): + TOP=0 + RIGHT=1 + BOTTOM=2 + LEFT=3 + def __init__(self): Gtk.DrawingArea.__init__(self) self.connect('draw',self.draw) @@ -27,6 +32,19 @@ class Tile(Gtk.DrawingArea): def connects_left(self): return self.connections()[3] + + def json(self,checked={}): + result = '{"'+self.__class__.__name__+'":{' + if hasattr(self,'top'): + result += '"top":'+self.top.json()+',' + if hasattr(self,'right'): + result += '"right":'+self.right.json()+',' + if hasattr(self,'bottom'): + result += '"bottom":'+self.bottom.json()+',' + if hasattr(self,'left'): + result += '"left":'+self.left.json()+',' + result += '}}' + return result.replace('},}','}}') class StraightH(Tile): def draw(self,widget,cr): @@ -104,3 +122,43 @@ class TO_BRL(Tile): def connections(self): return (False,True,True,True) + +class TO_BLR(Tile): + def draw(self,widget,cr): + cr.set_source_rgb(0,0,0) + cr.set_line_width(7) + cr.move_to(-5,11) + cr.line_to(21,37) + cr.stroke() + cr.rectangle(0,11,32,10) + cr.fill() + + def connections(self): + return (False,True,True,True) + +class TO_TRL(Tile): + def draw(self,widget,cr): + cr.set_source_rgb(0,0,0) + cr.set_line_width(7) + cr.move_to(12,-5) + cr.line_to(38,21) + cr.stroke() + cr.rectangle(0,11,32,10) + cr.fill() + + def connections(self): + return (True,True,False,True) + +class TO_TLR(Tile): + def draw(self,widget,cr): + cr.set_source_rgb(0,0,0) + cr.set_line_width(7) + cr.move_to(-5,21) + cr.line_to(21,-5) + cr.stroke() + cr.rectangle(0,11,32,10) + cr.fill() + + def connections(self): + return (True,True,False,True) + \ No newline at end of file diff --git a/src/TrackPlan.py b/src/TrackPlan.py index 9167ccf..19f751c 100644 --- a/src/TrackPlan.py +++ b/src/TrackPlan.py @@ -3,6 +3,18 @@ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk from TileSelector import * +import types +import os + +def dump_obj(obj, key='',level=0): + for key, value in obj.__dict__.items(): + if isinstance(value, (int, float, str, unicode, list, dict, set)): + print " " * level + "%s -> %s" % (key, value) + else: + print " " * level + "%s -> %s:" % (key, value.__class__.__name__) + dump_obj(value, key, level + 2) + + class TrackPlan(Gtk.Window): def __init__(self,json): @@ -11,26 +23,34 @@ class TrackPlan(Gtk.Window): self.grid = Gtk.Grid() - self.putButton(0,0,None) + self.putButton(0,0,None,None) self.add(self.grid) - self.show_all() - Gtk.main() + - def select_tile(self,widget,connections): + def select_tile(self,widget,origin,connection): #print 'Button at ({},{}) pressed'.format(widget.x, widget.y) - tileSelector = TileSelector(connections) + tileSelector = TileSelector(connection) tile = tileSelector.tile() + if origin != None: + if connection == Tile.TOP: + origin.bottom = tile + if connection == Tile.RIGHT: + origin.left = tile + if connection == Tile.BOTTOM: + origin.top = tile + if connection == Tile.LEFT: + origin.right = tile self.putTile(tile,widget.x,widget.y) widget.destroy() - def putButton(self,x,y,connections): + def putButton(self,x,y,origin,connection): #print 'putButton({},{})'.format(x,y) btn = Gtk.Button('?') btn.x = x btn.y = y - btn.connect('clicked',self.select_tile,connections) + btn.connect('clicked',self.select_tile,origin,connection) self.grid.attach(btn,x,y,1,1) btn.show() @@ -44,21 +64,27 @@ class TrackPlan(Gtk.Window): if tile.connects_left(): left = self.grid.get_child_at(x-1,y) if left == None: - self.putButton(x-1,y,tile.connections()) + self.putButton(x-1,y,tile,Tile.RIGHT) if tile.connects_right(): right = self.grid.get_child_at(x+1,y) if right == None: - self.putButton(x+1,y,tile.connections()) + self.putButton(x+1,y,tile,Tile.LEFT) if tile.connects_up(): top = self.grid.get_child_at(x,y-1) if top == None: - self.putButton(x,y-1,tile.connections()) + self.putButton(x,y-1,tile,Tile.BOTTOM) if tile.connects_down(): bottom = self.grid.get_child_at(x,y+1) if bottom == None: - self.putButton(x,y+1,tile.connections()) + self.putButton(x,y+1,tile,Tile.TOP) self.save() + def run(self): + self.show_all() + Gtk.main() + def save(self): - print 'Saving plan not implemented' \ No newline at end of file + seed_tile = self.grid.get_child_at(0,0) + os.system('clear') + print seed_tile.json() \ No newline at end of file diff --git a/src/web4rail.client.py b/src/web4rail.client.py index e559c6c..e0b542e 100755 --- a/src/web4rail.client.py +++ b/src/web4rail.client.py @@ -20,7 +20,7 @@ def readline(socket): buffer += more if buffer: yield buffer - + conn_dlg = ConnectDialog() conn_dlg.run() @@ -52,6 +52,8 @@ if conn_dlg.port != None: if line == 'PLAN:': json = client.next() plan = TrackPlan(json) + plan.run() + break continue if 'does not exist. Create' in line: