diff --git a/examples/demo.py b/examples/demo.py index c40cebfc0805838e628324548831ff97868514d9..84c9cd2f8ef89ae7a16d32887a6fa3421a50cb01 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -192,7 +192,7 @@ class Demo: break -if False: +if True: demo_000 = Demo(Scenario_Generator.generate_random_scenario()) demo_000.run_demo() demo_000 = None diff --git a/flatland/utils/render_qt.py b/flatland/utils/render_qt.py index 2a4a02a78a1dbefe406f538c277ed7f91bc08094..0f3a0f899e106f71e8a4f14a4bafbb7ed7589efd 100644 --- a/flatland/utils/render_qt.py +++ b/flatland/utils/render_qt.py @@ -11,6 +11,19 @@ from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout from PyQt5 import QtSvg +def transform_string_svg(sSVG): + sSVG = sSVG.replace("ASCII", "UTF-8") + bySVG = bytearray(sSVG, encoding='utf-8') + return bySVG + +def create_QtSvgWidget_from_svg_string(sSVG): + svgWidget = QtSvg.QSvgWidget() + ret = svgWidget.renderer().load(transform_string_svg(sSVG)) + if ret == False: + print("create_QtSvgWidget_from_svg_string : failed to parse:", sSVG) + return svgWidget + + class QTGL(GraphicsLayer): def __init__(self, width, height): self.cell_pixels = 60 @@ -129,13 +142,7 @@ class QTSVG(GraphicsLayer): if False: for binTrans in self.track.dSvg.keys(): sSVG = self.track.dSvg[binTrans].to_string() - - bySVG = bytearray(sSVG, encoding='utf-8') - - svgWidget = QtSvg.QSvgWidget() - svgWidget.renderer().load(bySVG) - print(iRow, iCol) - self.layout.addWidget(svgWidget, iRow, iCol) + self.layout.addWidget(create_QtSvgWidget_from_svg_string(sSVG), iRow, iCol) iArt += 1 iRow = int(iArt / nCols) @@ -170,10 +177,7 @@ class QTSVG(GraphicsLayer): def setRailAt(self, row, col, binTrans): if binTrans in self.track.dSvg: sSVG = self.track.dSvg[binTrans].to_string() - sSVG = sSVG.replace("ASCII", "UTF-8") - bySVG = bytearray(sSVG, encoding='utf-8') - svgWidget = QtSvg.QSvgWidget() - svgWidget.renderer().load(bySVG) + svgWidget = create_QtSvgWidget_from_svg_string(sSVG) self.layout.addWidget(svgWidget, row, col) self.lwTrack.append(svgWidget) else: @@ -200,8 +204,7 @@ class QTSVG(GraphicsLayer): agentPrev.direction = iDirOut agentPrev.old_direction = iDirIn sSVG = self.zug.getSvg(iAgent, iDirIn, iDirOut, color=color).to_string() - bySVG = bytearray(sSVG, encoding='utf-8') - wAgent.renderer().load(bySVG) + wAgent.renderer().load(transform_string_svg(sSVG)) return # Ensure we have adequate slots in the list lwAgents @@ -211,13 +214,10 @@ class QTSVG(GraphicsLayer): # Create a new widget for the agent sSVG = self.zug.getSvg(iAgent, iDirIn, iDirOut, color=color).to_string() - bySVG = bytearray(sSVG, encoding='utf-8') - svgWidget = QtSvg.QSvgWidget() - svgWidget.renderer().load(bySVG) + svgWidget = create_QtSvgWidget_from_svg_string(sSVG) self.lwAgents[iAgent] = svgWidget self.agents_prev[iAgent] = EnvAgent((row, col), iDirOut, (0, 0), old_direction=iDirIn) self.layout.addWidget(svgWidget, row, col) - # print("Created ", iAgent, row, col) def show(self, block=False): self.wMain.update() diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py index a0398f258d6ddff8a879e05192671f1bef9343ac..d17bbef855e1cfcc9c62dea00d043018d8ec237d 100644 --- a/flatland/utils/rendertools.py +++ b/flatland/utils/rendertools.py @@ -764,12 +764,18 @@ class RenderTool(object): iAction = action_dict[iAgent] new_direction, action_isValid = self.env.check_action(agent, iAction) - if action_isValid: - self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction, color=oColor) + + # ** TODO *** + # why should we only update if the action is valid ? + if False: + if action_isValid: + self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction, color=oColor) + else: + pass + # print("invalid action - agent ", iAgent, " bend ", agent.direction, new_direction) + # self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction) else: - pass - # print("invalid action - agent ", iAgent, " bend ", agent.direction, new_direction) - # self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction) + self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction, color=oColor) self.gl.show() for i in range(3):