diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index 1e41bb6..ecf605c 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -137,8 +137,39 @@ void ViewportWidget::paintGL() } } + if (m_isSnappingOrigin) { + const float rectSize = 0.2f; + glColor4f(1.0, 1.0, 0.0, 0.5f); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBegin(GL_QUADS); + if (m_currentPlane == SketchPlane::XY) { + glVertex3f(-rectSize, -rectSize, 0); + glVertex3f( rectSize, -rectSize, 0); + glVertex3f( rectSize, rectSize, 0); + glVertex3f(-rectSize, rectSize, 0); + } else if (m_currentPlane == SketchPlane::XZ) { + glVertex3f(-rectSize, 0, -rectSize); + glVertex3f( rectSize, 0, -rectSize); + glVertex3f( rectSize, 0, rectSize); + glVertex3f(-rectSize, 0, rectSize); + } else if (m_currentPlane == SketchPlane::YZ) { + glVertex3f(0, -rectSize, -rectSize); + glVertex3f(0, rectSize, -rectSize); + glVertex3f(0, rectSize, rectSize); + glVertex3f(0, -rectSize, rectSize); + } + glEnd(); + glDisable(GL_BLEND); + } + if (m_isDefiningLine && m_activeTool == static_cast(ApplicationController::ToolType::Line)) { QVector3D worldPos = unproject(m_currentMousePos); + if (m_isSnappingOrigin) { + worldPos.setX(0); + worldPos.setY(0); + worldPos.setZ(0); + } glBegin(GL_LINES); glColor3f(1.0, 1.0, 0.0); glVertex3d(m_firstLinePoint.X(), m_firstLinePoint.Y(), m_firstLinePoint.Z()); @@ -175,8 +206,13 @@ void ViewportWidget::resizeGL(int w, int h) void ViewportWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && m_currentPlane != SketchPlane::NONE && m_activeTool == static_cast(ApplicationController::ToolType::Line)) { - QVector3D worldPos = unproject(event->pos()); - gp_Pnt p(worldPos.x(), worldPos.y(), worldPos.z()); + gp_Pnt p; + if (m_isSnappingOrigin) { + p.SetCoord(0, 0, 0); + } else { + QVector3D worldPos = unproject(event->pos()); + p.SetCoord(worldPos.x(), worldPos.y(), worldPos.z()); + } if (!m_isDefiningLine) { m_firstLinePoint = p; @@ -194,6 +230,19 @@ void ViewportWidget::mousePressEvent(QMouseEvent *event) void ViewportWidget::mouseMoveEvent(QMouseEvent *event) { m_currentMousePos = event->pos(); + + bool shouldSnap = false; + if (m_currentPlane != SketchPlane::NONE) { + QVector3D worldPos = unproject(m_currentMousePos); + const float snapThreshold = 0.5f; + shouldSnap = worldPos.length() < snapThreshold; + } + + if (shouldSnap != m_isSnappingOrigin) { + m_isSnappingOrigin = shouldSnap; + update(); + } + int dx = event->pos().x() - lastPos.x(); int dy = event->pos().y() - lastPos.y(); diff --git a/src/ViewportWidget.h b/src/ViewportWidget.h index 373cb02..9cdd08f 100644 --- a/src/ViewportWidget.h +++ b/src/ViewportWidget.h @@ -90,6 +90,7 @@ private: bool m_isDefiningLine = false; gp_Pnt m_firstLinePoint; QPoint m_currentMousePos; + bool m_isSnappingOrigin = false; QMap m_toolIcons; QSvgRenderer* m_cursorRenderer = nullptr;