refactor: Extract camera logic to Camera class

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-15 12:03:41 -07:00
parent 58d154b4ff
commit af121ce6eb
4 changed files with 220 additions and 148 deletions

View File

@@ -1,4 +1,5 @@
#include "ViewportWidget.h"
#include "Camera.h"
#include "ViewCube.h"
#include "SketchGrid.h"
#include "Document.h"
@@ -25,18 +26,14 @@
ViewportWidget::ViewportWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
m_camera = new Camera(this);
connect(m_camera, &Camera::cameraChanged, this, QOverload<>::of(&QWidget::update));
m_viewCube = new ViewCube();
m_sketchGrid = new SketchGrid();
m_featureBrowser = new FeatureBrowser();
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
// Set initial view to an isometric angle on the XY plane and show grid
m_xRot = 30 * 16;
m_yRot = -45 * 16;
m_zoom = -20.0f;
m_panX = 0.0f;
m_panY = 0.0f;
m_currentPlane = SketchPlane::XY;
m_toolIcons.insert(static_cast<int>(ApplicationController::ToolType::Line), new QSvgRenderer(QString(":/icons/line.svg"), this));
@@ -59,51 +56,6 @@ void ViewportWidget::setDocument(Document* document)
m_featureBrowser->setDocument(document);
}
float ViewportWidget::xRotation() const { return m_xRot; }
void ViewportWidget::setXRotation(float angle)
{
if (angle != m_xRot) {
m_xRot = angle;
update();
}
}
float ViewportWidget::yRotation() const { return m_yRot; }
void ViewportWidget::setYRotation(float angle)
{
if (angle != m_yRot) {
m_yRot = angle;
update();
}
}
float ViewportWidget::zoom() const { return m_zoom; }
void ViewportWidget::setZoom(float value)
{
if (value != m_zoom) {
m_zoom = value;
update();
}
}
float ViewportWidget::panX() const { return m_panX; }
void ViewportWidget::setPanX(float value)
{
if (value != m_panX) {
m_panX = value;
update();
}
}
float ViewportWidget::panY() const { return m_panY; }
void ViewportWidget::setPanY(float value)
{
if (value != m_panY) {
m_panY = value;
update();
}
}
void ViewportWidget::initializeGL()
{
initializeOpenGLFunctions();
@@ -122,10 +74,7 @@ void ViewportWidget::paintGL()
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
QMatrix4x4 model;
model.translate(m_panX, m_panY, m_zoom);
model.rotate(m_xRot / 16.0f, 1, 0, 0);
model.rotate(m_yRot / 16.0f, 0, 1, 0);
QMatrix4x4 model = m_camera->modelViewMatrix();
// For simplicity, we'll use a fixed-function pipeline style for drawing.
// In a real app, this would use shaders.
@@ -155,7 +104,7 @@ void ViewportWidget::paintGL()
}
if (m_isSnappingOrigin) {
const float rectSize = 0.0075f * -m_zoom;
const float rectSize = 0.0075f * -m_camera->zoom();
glColor4f(1.0, 1.0, 0.0, 0.5f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -179,7 +128,7 @@ void ViewportWidget::paintGL()
glEnd();
glDisable(GL_BLEND);
} else if (m_isSnappingVertex) {
const float rectSize = 0.0075f * -m_zoom;
const float rectSize = 0.0075f * -m_camera->zoom();
glColor4f(1.0, 1.0, 0.0, 0.5f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -232,8 +181,8 @@ void ViewportWidget::paintGL()
if (m_isSnappingHorizontal || m_isSnappingVertical) {
QVector3D startPos(m_firstLinePoint.X(), m_firstLinePoint.Y(), m_firstLinePoint.Z());
QVector3D midPoint = (startPos + worldPos) / 2.0;
const float indicatorSize = 0.02f * -m_zoom;
const float indicatorOffset = 0.02f * -m_zoom;
const float indicatorSize = 0.02f * -m_camera->zoom();
const float indicatorOffset = 0.02f * -m_camera->zoom();
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINES);
@@ -267,8 +216,8 @@ void ViewportWidget::paintGL()
// View cube rendering
QMatrix4x4 viewCubeModel;
viewCubeModel.rotate(m_xRot / 16.0f, 1, 0, 0);
viewCubeModel.rotate(m_yRot / 16.0f, 0, 1, 0);
viewCubeModel.rotate(m_camera->xRotation() / 16.0f, 1, 0, 0);
viewCubeModel.rotate(m_camera->yRotation() / 16.0f, 0, 1, 0);
m_viewCube->paintGL(viewCubeModel, width(), height());
glViewport(0, 0, width(), height());
@@ -353,7 +302,7 @@ void ViewportWidget::mouseMoveEvent(QMouseEvent *event)
bool shouldSnap = false;
if (m_currentPlane != SketchPlane::NONE && m_activeTool != static_cast<int>(ApplicationController::ToolType::None)) {
QVector3D worldPos = unproject(m_currentMousePos, m_currentPlane);
const float snapRectHalfSize = 0.0075f * -m_zoom;
const float snapRectHalfSize = 0.0075f * -m_camera->zoom();
switch (m_currentPlane) {
case SketchPlane::XY:
@@ -382,7 +331,7 @@ void ViewportWidget::mouseMoveEvent(QMouseEvent *event)
m_isSnappingVertex = false;
if (!m_isSnappingOrigin && m_document && m_currentPlane != SketchPlane::NONE && m_activeTool != static_cast<int>(ApplicationController::ToolType::None)) {
QVector3D worldPos = unproject(m_currentMousePos, m_currentPlane);
const float snapRectHalfSize = 0.0075f * -m_zoom;
const float snapRectHalfSize = 0.0075f * -m_camera->zoom();
for (Feature* feature : m_document->features()) {
if (auto sketch = dynamic_cast<SketchFeature*>(feature)) {
@@ -457,19 +406,8 @@ void ViewportWidget::mouseMoveEvent(QMouseEvent *event)
update();
}
int dx = event->pos().x() - lastPos.x();
int dy = event->pos().y() - lastPos.y();
if (event->buttons() & Qt::MiddleButton) {
if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
// Pan
m_panX += dx / 100.0f;
m_panY -= dy / 100.0f;
} else {
// Rotate
m_xRot += 8 * dy;
m_yRot += 8 * dx;
}
m_camera->processMouseMovement(event, lastPos);
}
lastPos = event->pos();
update();
@@ -477,11 +415,7 @@ void ViewportWidget::mouseMoveEvent(QMouseEvent *event)
void ViewportWidget::wheelEvent(QWheelEvent *event)
{
QPoint numDegrees = event->angleDelta() / 8;
if (!numDegrees.isNull()) {
m_zoom += numDegrees.y() / 5.0f;
}
update();
m_camera->processWheel(event);
}
void ViewportWidget::keyPressEvent(QKeyEvent *event)
@@ -507,14 +441,10 @@ void ViewportWidget::onSketchModeStarted(SketchPlane plane)
{
m_currentPlane = plane;
m_savedXRot = m_xRot;
m_savedYRot = m_yRot;
m_savedPanX = m_panX;
m_savedPanY = m_panY;
m_savedZoom = m_zoom;
m_camera->saveState();
float targetXRot = m_xRot;
float targetYRot = m_yRot;
float targetXRot = m_camera->xRotation();
float targetYRot = m_camera->yRotation();
switch (plane) {
case SketchPlane::XY: // Top view
targetXRot = 90 * 16;
@@ -534,37 +464,37 @@ void ViewportWidget::onSketchModeStarted(SketchPlane plane)
auto* animGroup = new QParallelAnimationGroup(this);
auto* xRotAnim = new QPropertyAnimation(this, "xRotation");
auto* xRotAnim = new QPropertyAnimation(m_camera, "xRotation");
xRotAnim->setDuration(300);
xRotAnim->setStartValue(m_xRot);
xRotAnim->setStartValue(m_camera->xRotation());
xRotAnim->setEndValue(targetXRot);
xRotAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(xRotAnim);
auto* yRotAnim = new QPropertyAnimation(this, "yRotation");
auto* yRotAnim = new QPropertyAnimation(m_camera, "yRotation");
yRotAnim->setDuration(300);
yRotAnim->setStartValue(m_yRot);
yRotAnim->setStartValue(m_camera->yRotation());
yRotAnim->setEndValue(targetYRot);
yRotAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(yRotAnim);
auto* panXAnim = new QPropertyAnimation(this, "panX");
auto* panXAnim = new QPropertyAnimation(m_camera, "panX");
panXAnim->setDuration(300);
panXAnim->setStartValue(m_panX);
panXAnim->setStartValue(m_camera->panX());
panXAnim->setEndValue(0.0f);
panXAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(panXAnim);
auto* panYAnim = new QPropertyAnimation(this, "panY");
auto* panYAnim = new QPropertyAnimation(m_camera, "panY");
panYAnim->setDuration(300);
panYAnim->setStartValue(m_panY);
panYAnim->setStartValue(m_camera->panY());
panYAnim->setEndValue(0.0f);
panYAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(panYAnim);
auto* zoomAnim = new QPropertyAnimation(this, "zoom");
auto* zoomAnim = new QPropertyAnimation(m_camera, "zoom");
zoomAnim->setDuration(300);
zoomAnim->setStartValue(m_zoom);
zoomAnim->setStartValue(m_camera->zoom());
zoomAnim->setEndValue(-20.0f);
zoomAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(zoomAnim);
@@ -584,38 +514,38 @@ void ViewportWidget::onSketchModeEnded()
{
auto* animGroup = new QParallelAnimationGroup(this);
auto* xRotAnim = new QPropertyAnimation(this, "xRotation");
auto* xRotAnim = new QPropertyAnimation(m_camera, "xRotation");
xRotAnim->setDuration(300);
xRotAnim->setStartValue(m_xRot);
xRotAnim->setEndValue(m_savedXRot);
xRotAnim->setStartValue(m_camera->xRotation());
xRotAnim->setEndValue(m_camera->savedXRot());
xRotAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(xRotAnim);
auto* yRotAnim = new QPropertyAnimation(this, "yRotation");
auto* yRotAnim = new QPropertyAnimation(m_camera, "yRotation");
yRotAnim->setDuration(300);
yRotAnim->setStartValue(m_yRot);
yRotAnim->setEndValue(m_savedYRot);
yRotAnim->setStartValue(m_camera->yRotation());
yRotAnim->setEndValue(m_camera->savedYRot());
yRotAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(yRotAnim);
auto* panXAnim = new QPropertyAnimation(this, "panX");
auto* panXAnim = new QPropertyAnimation(m_camera, "panX");
panXAnim->setDuration(300);
panXAnim->setStartValue(m_panX);
panXAnim->setEndValue(m_savedPanX);
panXAnim->setStartValue(m_camera->panX());
panXAnim->setEndValue(m_camera->savedPanX());
panXAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(panXAnim);
auto* panYAnim = new QPropertyAnimation(this, "panY");
auto* panYAnim = new QPropertyAnimation(m_camera, "panY");
panYAnim->setDuration(300);
panYAnim->setStartValue(m_panY);
panYAnim->setEndValue(m_savedPanY);
panYAnim->setStartValue(m_camera->panY());
panYAnim->setEndValue(m_camera->savedPanY());
panYAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(panYAnim);
auto* zoomAnim = new QPropertyAnimation(this, "zoom");
auto* zoomAnim = new QPropertyAnimation(m_camera, "zoom");
zoomAnim->setDuration(300);
zoomAnim->setStartValue(m_zoom);
zoomAnim->setEndValue(m_savedZoom);
zoomAnim->setStartValue(m_camera->zoom());
zoomAnim->setEndValue(m_camera->savedZoom());
zoomAnim->setEasingCurve(QEasingCurve::InOutQuad);
animGroup->addAnimation(zoomAnim);
@@ -722,10 +652,7 @@ void ViewportWidget::onActiveToolChanged(int tool)
QVector3D ViewportWidget::unproject(const QPoint& screenPos, SketchPlane plane)
{
QMatrix4x4 model;
model.translate(m_panX, m_panY, m_zoom);
model.rotate(m_xRot / 16.0f, 1, 0, 0);
model.rotate(m_yRot / 16.0f, 0, 1, 0);
QMatrix4x4 model = m_camera->modelViewMatrix();
bool invertible;
QMatrix4x4 inv = (projection * model).inverted(&invertible);