Skip to content

Commit

Permalink
ui: Adds scenario edit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dervelakos committed Dec 30, 2024
1 parent ec17012 commit 99aa075
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 51 deletions.
136 changes: 95 additions & 41 deletions scenarios/default.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,98 @@
aliases:
- type: Wall
render: RectangleRender
name: Wall
- type: Vechile
render: SimpleVehicleRender
name: Vehicle
- name: Wall
render: RectangleRender
type: Wall
- name: Vehicle
render: SimpleVehicleRender
type: Vechile
objects:
dynamic: []
static:
- alias: Wall
loc: [400, 100]
angle: 0
dim: [100, 10]
- alias: Wall
loc: [200, 400]
angle: 90
dim: [100, 10]
- alias: Wall
loc: [1000, 0]
angle: 90
dim: [2000, 10]
- alias: Wall
loc: [1000, 2000]
angle: 90
dim: [2000, 10]
- alias: Wall
loc: [0, 1000]
angle: 0
dim: [2000, 10]
- alias: Wall
loc: [2000, 1000]
angle: 0
dim: [2000, 10]
- alias: Wall
loc: [300, 800]
angle: 90
dim: [600, 10]
- alias: Wall
loc: [800, 300]
angle: 0
dim: [600, 10]
dynamic:
- alias: Vehicle
loc: [100, 100]
- alias: Wall
angle: 0
dim:
- 100
- 10
loc:
- 400.0
- 100.0
- alias: Wall
angle: 90
dim:
- 100
- 10
loc:
- 200.0
- 400.0
- alias: Wall
angle: 90
dim:
- 2000
- 10
loc:
- 1000.0
- 0.0
- alias: Wall
angle: 90
dim:
- 2000
- 10
loc:
- 1000.0
- 2000.0
- alias: Wall
angle: 0
dim:
- 2000
- 10
loc:
- 0.0
- 1000.0
- alias: Wall
angle: 0
dim:
- 2000
- 10
loc:
- 2000.0
- 1000.0
- alias: Wall
angle: 90
dim:
- 600
- 10
loc:
- 300.0
- 800.0
- alias: Wall
angle: 0
dim:
- 600
- 10
loc:
- 800.0
- 300.0
- alias: Wall
angle: 225.80409849608924
dim:
- 806.1811210888035
- 10
loc:
- 1132.0
- 1048.0
- alias: Wall
angle: -0.37943670141898167
dim:
- 453.00993366591865
- 10
loc:
- 1419.0
- 534.0
- alias: Wall
angle: -89.60280858444557
dim:
- 577.0138646514484
- 10
loc:
- 545.0
- 1316.0
44 changes: 44 additions & 0 deletions scenarios/default.yaml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
aliases:
- type: Wall
render: RectangleRender
name: Wall
- type: Vechile
render: SimpleVehicleRender
name: Vehicle
objects:
static:
- alias: Wall
loc: [400, 100]
angle: 0
dim: [100, 10]
- alias: Wall
loc: [200, 400]
angle: 90
dim: [100, 10]
- alias: Wall
loc: [1000, 0]
angle: 90
dim: [2000, 10]
- alias: Wall
loc: [1000, 2000]
angle: 90
dim: [2000, 10]
- alias: Wall
loc: [0, 1000]
angle: 0
dim: [2000, 10]
- alias: Wall
loc: [2000, 1000]
angle: 0
dim: [2000, 10]
- alias: Wall
loc: [300, 800]
angle: 90
dim: [600, 10]
- alias: Wall
loc: [800, 300]
angle: 0
dim: [600, 10]
dynamic:
- alias: Vehicle
loc: [100, 100]
80 changes: 75 additions & 5 deletions src/GraphicalWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,54 @@
from SimEngine import RenderEngine

class UIController:
def __init__(self, mainArea):
def __init__(self, mainArea, simEngine, renderEngine, aliases):
self.mainArea = mainArea
self.simEngine = simEngine
self.renderEngine = renderEngine
self.aliases = aliases
self.drawArea = None

self.editMode = False

def toggleEditMode(self):
self.editMode = not self.editMode
return self.editMode

def createObject(self):
if not self.editMode:
return

delta = QPoint(self.drawArea.dragPosition - self.drawArea.dragStartPosition)
angle = math.degrees(math.atan2(delta.y(), delta.x()))

length = math.hypot(delta.x(), delta.y())
width = 10
# Calculate the perpendicular vector for height
# Rotate 90 degrees clockwise
dx = width * math.sin(math.radians(angle)) # Change in x for height
dy = -width * math.cos(math.radians(angle)) # Change in y for height

# Calculate bottom corners
x1 = self.drawArea.dragStartPosition.x()
y1 = self.drawArea.dragStartPosition.y()
x2 = self.drawArea.dragPosition.x()
y2 = self.drawArea.dragPosition.y()
x4, y4 = x2 - dx, y2 - dy # bottom-right

center_x = int((x1 + x4) / 2)
center_y = int((y1 + y4) / 2)

print (center_x, center_y, angle+90, length, width)

if self.editMode:
alias = self.aliases["Wall"]
tmp = alias.genObject([center_x, center_y],
angle+90,
[length, width])
tmp.setAlias(alias)
self.simEngine.registerStaticObject(tmp)
self.renderEngine.registerObject(alias.genRender(tmp))

def drawSelectionShadow(self, painter):
if (self.drawArea.dragStartPosition is None or
self.drawArea.dragPosition is None):
Expand Down Expand Up @@ -64,8 +108,16 @@ def drawSelectionShadow(self, painter):
center_y = int((y1 + y4) / 2)

painter.drawEllipse(QPoint(center_x, center_y), 1, 1)
print (center_x, center_y, angle+90, length, width)
#print (center_x, center_y, angle+90, length, width)

#if self.editMode:
# alias = self.aliases["Wall"]
# tmp = alias.genObject([center_x, center_y],
# angle+90,
# [length, width])
# tmp.setAlias(alias)
# self.simEngine.registerStaticObject(tmp)
# self.renderEngine.registerObject(alias.genRender(tmp))

class DrawWidget(QWidget):
def __init__(self, vehicle, renderEngine, controller):
Expand Down Expand Up @@ -106,6 +158,7 @@ def mouseMoveEvent(self, event: QMouseEvent):

def mouseReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.LeftButton:
self.controller.createObject()
self.dragging = False
print(f"Start:{self.dragStartPosition}, End: {event.pos()}")

Expand All @@ -116,12 +169,15 @@ class MainWindow(QMainWindow):
"""
The GUI window for displaying the simualtor state
"""
def __init__(self, vehicle):
def __init__(self, vehicle, scenario, simEngine):
super().__init__()
self.setGeometry(100, 100, 800, 800)
self.angle = 0 # Initial rotation anglea

self.vehicle = vehicle
self.scenario = scenario
self.simEngine = simEngine

self.renderEngine = RenderEngine()

menuBar = self.menuBar()
Expand All @@ -132,6 +188,11 @@ def __init__(self, vehicle):
editAction.triggered.connect(self.enableEditMode)
fileMenu.addAction(editAction)

saveAction = QAction("&Save", self)
saveAction.setShortcut("Ctrl+S")
saveAction.triggered.connect(self.saveScenario)
fileMenu.addAction(saveAction)

# Create a central widget and set it
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
Expand All @@ -143,7 +204,11 @@ def __init__(self, vehicle):
self.scrollArea = QScrollArea()
self.scrollArea.setWidgetResizable(True) # Allow the scroll area to resize

self.controller = UIController(self)
self.controller = UIController(self,
self.simEngine,
self.renderEngine,
self.scenario.getAliases())

# Create a widget to hold the content
self.contentWidget = DrawWidget(vehicle, self.renderEngine, self.controller)

Expand All @@ -167,7 +232,12 @@ def getRenderEngine(self):
return self.renderEngine

def enableEditMode(self):
print("Edit Mode Enabled")
status = self.controller.toggleEditMode()
print("Edit Mode Enabled", status)

def saveScenario(self):
print("Save Scenario")
self.scenario.saveScenario(self.simEngine)

def updateRotation(self):
self.contentWidget.update() # Request repaint
Expand Down
7 changes: 4 additions & 3 deletions src/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def handleSigint(signalReceived, frame):

args = parser.parse_args()

#Simulation Engine
simEngine = SimEngine()

scenario = ScenarioLoader(args.scenario)

#Create Vehicle
Expand All @@ -57,10 +60,8 @@ def handleSigint(signalReceived, frame):

if args.graphics:
app = QApplication(sys.argv)
window = MainWindow(truck)
window = MainWindow(truck, scenario, simEngine)

#Simulation Engine
simEngine = SimEngine()
simEngine.registerDynamicObject(truck)
if args.graphics:
window.getRenderEngine().registerVehicle(truckRender)
Expand Down
Loading

0 comments on commit 99aa075

Please sign in to comment.