Files
autoPlanner2025/buttonEditor.py
T

110 lines
3.9 KiB
Python
Raw Normal View History

2024-07-03 14:15:25 -06:00
import sys
import os
import numpy as np
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QMessageBox
from PySide6.QtGui import QPixmap, QMouseEvent, QPainter, QPen, QColor, QPainterPath, QPolygon, QFont
from PySide6.QtCore import Qt, QPoint, QRect
class ButtonEditor(QMainWindow):
2024-07-03 17:44:46 -06:00
def __init__(self, pathPlanner):
2024-07-03 14:15:25 -06:00
super().__init__()
self.setWindowTitle("Button Editor")
2024-07-03 17:44:46 -06:00
self.pathPlanner = pathPlanner
2024-07-03 14:15:25 -06:00
2024-07-03 17:44:46 -06:00
self.imageLabel = QLabel(self)
2024-07-03 14:15:25 -06:00
2024-07-03 17:44:46 -06:00
scriptDir = os.path.dirname(os.path.abspath(__file__))
imagePath = os.path.join(scriptDir, "images", "Field.png")
self.pixmap = QPixmap(imagePath)
2024-07-03 14:15:25 -06:00
if self.pixmap.isNull():
2024-07-03 17:44:46 -06:00
self.imageLabel.setText(f"Image not found at: {imagePath}")
2024-07-03 14:15:25 -06:00
else:
2024-07-03 17:44:46 -06:00
self.imageLabel.setPixmap(self.pixmap)
2024-07-03 14:15:25 -06:00
2024-07-03 17:44:46 -06:00
self.pathPlannerButton = QPushButton("Main Window")
self.pathPlannerButton.clicked.connect(self.showPathPlanner)
self.buttonEditorButton = QPushButton("Button Editor")
self.buttonEditorButton.clicked.connect(self.showButtonEditor)
2024-07-03 14:15:25 -06:00
2024-07-03 17:44:46 -06:00
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(self.pathPlannerButton)
buttonLayout.addWidget(self.buttonEditorButton)
2024-07-03 14:15:25 -06:00
layout = QVBoxLayout()
2024-07-03 17:44:46 -06:00
layout.addLayout(buttonLayout)
layout.addWidget(self.imageLabel)
2024-07-03 14:15:25 -06:00
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.resize(self.pixmap.width(), self.pixmap.height() + 60)
2024-07-03 17:44:46 -06:00
# Variables
self.matchLength = 15
self.TPS = 50
self.matchTicks = self.matchLength * self.TPS
self.displayTickResolution = 4
self.displayTicks = round(self.matchTicks / self.displayTickResolution)
self.keyFrames = []
def addKeyFrames(self):
currentFrame = 0
for i in range(0, self.displayTicks):
self.keyFrames.append(currentFrame)
def showButtonEditor(self):
2024-07-03 14:15:25 -06:00
self.show()
2024-07-03 17:44:46 -06:00
self.pathPlanner.hide()
2024-07-03 14:15:25 -06:00
2024-07-03 17:44:46 -06:00
def showPathPlanner(self):
2024-07-03 14:15:25 -06:00
self.hide()
2024-07-03 17:44:46 -06:00
self.pathPlanner.show()
2024-07-03 15:12:25 -06:00
2024-07-03 17:44:46 -06:00
def updateScene(self):
2024-07-03 15:12:25 -06:00
self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png"))
painter = QPainter(self.pixmap)
2024-07-03 17:44:46 -06:00
greyPen = QPen(QColor(127, 127, 127))
greyPen.setWidth(2)
painter.setPen(greyPen)
2024-07-03 15:12:25 -06:00
# Draw the Bezier curve segments
2024-07-03 17:44:46 -06:00
if self.pathPlanner.coordinates.size > 0:
for i in range(len(self.pathPlanner.coordinates) - 1):
start = QPoint(self.pathPlanner.coordinates[i][0], self.pathPlanner.coordinates[i][1])
end = QPoint(self.pathPlanner.coordinates[i + 1][0], self.pathPlanner.coordinates[i + 1][1])
2024-07-03 15:12:25 -06:00
2024-07-03 17:44:46 -06:00
if i < len(self.pathPlanner.controlPoints):
controlPair = self.pathPlanner.controlPoints[i]
2024-07-03 15:12:25 -06:00
pen = QPen(Qt.yellow)
pen.setWidth(2)
painter.setPen(pen)
path = QPainterPath()
path.moveTo(start)
2024-07-03 17:44:46 -06:00
path.cubicTo(controlPair[0], controlPair[1], end)
2024-07-03 15:12:25 -06:00
painter.drawPath(path)
# Draw the nodes
painter.setPen(Qt.white)
font = painter.font()
font.setPointSize(25)
painter.setFont(font)
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.white)
2024-07-03 17:44:46 -06:00
for i, (x, y) in enumerate(self.pathPlanner.coordinates):
nodeRect = QRect(x - self.pathPlanner.nodeSize // 6, y - self.pathPlanner.nodeSize // 6,
self.pathPlanner.nodeSize // 3, self.pathPlanner.nodeSize // 3)
painter.drawEllipse(nodeRect)
painter.drawText(nodeRect, Qt.AlignCenter, str(i + 1))
2024-07-03 15:12:25 -06:00
painter.end()
2024-07-03 17:44:46 -06:00
self.imageLabel.setPixmap(self.pixmap)