--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/editmodes/curvemode.cpp Sun Oct 04 04:26:11 2015 +0300 @@ -0,0 +1,86 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2015 Teemu Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "curvemode.h" +#include "../ldObject.h" +#include "../glRenderer.h" + +CurveMode::CurveMode (GLRenderer* renderer) : + Super (renderer) {} + +void CurveMode::render (QPainter& painter) const +{ + if (m_drawedVerts.size() >= 1) + { + Vertex curve[4]; + QPoint curve2d[4]; + + for (int i = 0; i < qMin (countof(curve), m_drawedVerts.size()); ++i) + curve[i] = m_drawedVerts[i]; + + // Factor the cursor into the preview + if (m_drawedVerts.size() < 4) + curve[m_drawedVerts.size()] = getCursorVertex(); + + // Default the control points to their vertex positions + if (m_drawedVerts.size() < 2) + curve[2] = curve[0]; + + if (m_drawedVerts.size() < 3) + curve[3] = curve[1]; + + for (int i = 0; i < countof(curve); ++i) + curve2d[i] = renderer()->convert3dTo2d (curve[i]); + + painter.setPen (renderer()->linePen()); + + for (int i = 0; i < qMin (countof(curve), m_drawedVerts.size() + 1); ++i) + { + if (i < 2) + renderer()->drawBlip (painter, curve2d[i]); + else + // Give control points a different color + renderer()->drawBlip (painter, curve2d[i], QColor (0, 96, 96)); + } + + QPainterPath path (curve2d[0]); + path.cubicTo (curve2d[2], curve2d[3], curve2d[1]); + painter.strokePath (path, renderer()->linePen()); + } + else + { + // Even if we have nothing, still draw the vertex at the cursor + painter.setPen (renderer()->linePen()); + renderer()->drawBlip (painter, renderer()->convert3dTo2d (getCursorVertex())); + } +} + +EditModeType CurveMode::type() const +{ + return EditModeType::Curve; +} + +void CurveMode::endDraw() +{ + if (m_drawedVerts.size() == 4) + { + LDObjectList objs; + objs << new LDBezierCurve (m_drawedVerts[0], m_drawedVerts[1], m_drawedVerts[2], m_drawedVerts[3]); + finishDraw (objs); + } +}