src/editmodes/rectangleMode.cc

changeset 933
f4c80d92e71e
parent 932
738673e8a6b4
child 934
be8128aff739
equal deleted inserted replaced
932:738673e8a6b4 933:f4c80d92e71e
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2015 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QPainter>
20 #include <QMouseEvent>
21 #include "rectangleMode.h"
22 #include "../ldObject.h"
23 #include "../glRenderer.h"
24
25 RectangleMode::RectangleMode (GLRenderer* renderer) :
26 Super (renderer),
27 m_rectangleVerts (QVector<Vertex>(4)) {}
28
29 EditModeType RectangleMode::type() const
30 {
31 return EditModeType::Rectangle;
32 }
33
34 void RectangleMode::render (QPainter& painter) const
35 {
36 renderPolygon (painter, (m_drawedVerts.size() > 0) ? m_rectangleVerts :
37 QVector<Vertex> ({renderer()->position3D()}), true, false);
38 }
39
40 bool RectangleMode::mouseReleased (MouseEventData const& data)
41 {
42 if (Super::mouseReleased (data))
43 return true;
44
45 if (data.releasedButtons & Qt::LeftButton)
46 {
47 if (m_drawedVerts.size() == 2)
48 {
49 LDQuadPtr quad (LDSpawn<LDQuad>());
50 updateRectVerts();
51
52 for (int i = 0; i < quad->numVertices(); ++i)
53 quad->setVertex (i, m_rectangleVerts[i]);
54
55 quad->setColor (MainColor());
56 finishDraw (LDObjectList ({quad}));
57 return true;
58 }
59
60 addDrawnVertex (renderer()->position3D());
61 return true;
62 }
63
64 return false;
65 }
66
67 //
68 // Update rect vertices when the mouse moves since the 3d position likely has changed
69 //
70 bool RectangleMode::mouseMoved (QMouseEvent*)
71 {
72 updateRectVerts();
73 return false;
74 }
75
76 void RectangleMode::updateRectVerts()
77 {
78 if (m_drawedVerts.isEmpty())
79 {
80 for (int i = 0; i < 4; ++i)
81 m_rectangleVerts[i] = renderer()->position3D();
82
83 return;
84 }
85
86 Vertex v0 = m_drawedVerts[0],
87 v1 = (m_drawedVerts.size() >= 2) ? m_drawedVerts[1] : renderer()->position3D();
88
89 const Axis localx = renderer()->getCameraAxis (false),
90 localy = renderer()->getCameraAxis (true),
91 localz = (Axis) (3 - localx - localy);
92
93 for (int i = 0; i < 4; ++i)
94 m_rectangleVerts[i].setCoordinate (localz, renderer()->getDepthValue());
95
96 m_rectangleVerts[0].setCoordinate (localx, v0[localx]);
97 m_rectangleVerts[0].setCoordinate (localy, v0[localy]);
98 m_rectangleVerts[1].setCoordinate (localx, v1[localx]);
99 m_rectangleVerts[1].setCoordinate (localy, v0[localy]);
100 m_rectangleVerts[2].setCoordinate (localx, v1[localx]);
101 m_rectangleVerts[2].setCoordinate (localy, v1[localy]);
102 m_rectangleVerts[3].setCoordinate (localx, v0[localx]);
103 m_rectangleVerts[3].setCoordinate (localy, v1[localy]);
104 }

mercurial