src/editmodes/rectangleMode.cpp

Thu, 11 Jan 2018 16:46:22 +0200

author
Santeri Piippo
date
Thu, 11 Jan 2018 16:46:22 +0200
changeset 1253
468a32ae8100
parent 1222
34def2630300
permissions
-rw-r--r--

add operators to LDDocument

841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1222
34def2630300 happy new year 2018
Santeri Piippo
parents: 1217
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
8 * (at your option) any later version.
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <QPainter>
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <QMouseEvent>
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "rectangleMode.h"
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "../ldObject.h"
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "../glRenderer.h"
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
25 RectangleMode::RectangleMode(GLRenderer* renderer) :
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
26 Super(renderer),
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
27 m_rectangleVerts(QVector<Vertex>(4)) {}
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 EditModeType RectangleMode::type() const
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 {
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 return EditModeType::Rectangle;
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
34 void RectangleMode::render(QPainter& painter) const
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
36 renderPolygon(painter,(m_drawedVerts.size() > 0) ? m_rectangleVerts :
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
37 QVector<Vertex>({renderer()->position3D()}), true, false);
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
1009
85fd352bae45 Fix rectangle mode for real
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
40 void RectangleMode::endDraw()
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 {
1009
85fd352bae45 Fix rectangle mode for real
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
42 if (m_drawedVerts.size() == 2)
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
1009
85fd352bae45 Fix rectangle mode for real
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
44 LDQuad* quad = LDSpawn<LDQuad>();
85fd352bae45 Fix rectangle mode for real
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
45 updateRectVerts();
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
1009
85fd352bae45 Fix rectangle mode for real
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
47 for (int i = 0; i < quad->numVertices(); ++i)
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
48 quad->setVertex(i, m_rectangleVerts[i]);
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
50 quad->setColor(MainColor);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
51 finishDraw(LDObjectList({quad}));
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 //
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 // Update rect vertices when the mouse moves since the 3d position likely has changed
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 //
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
58 bool RectangleMode::mouseMoved(QMouseEvent*)
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 {
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 updateRectVerts();
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 return false;
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 void RectangleMode::updateRectVerts()
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
66 if (m_drawedVerts.isEmpty())
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 {
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 for (int i = 0; i < 4; ++i)
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
69 m_rectangleVerts[i] = renderer()->position3D();
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 return;
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
74 Vertex v0 = m_drawedVerts[0],
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
75 v1 = (m_drawedVerts.size() >= 2) ? m_drawedVerts[1] : renderer()->position3D();
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
77 const Axis localx = renderer()->getCameraAxis(false),
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
78 localy = renderer()->getCameraAxis(true),
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
79 localz = (Axis)(3 - localx - localy);
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 for (int i = 0; i < 4; ++i)
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
82 m_rectangleVerts[i].setCoordinate(localz, renderer()->getDepthValue());
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
84 m_rectangleVerts[0].setCoordinate(localx, v0[localx]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
85 m_rectangleVerts[0].setCoordinate(localy, v0[localy]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
86 m_rectangleVerts[1].setCoordinate(localx, v1[localx]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
87 m_rectangleVerts[1].setCoordinate(localy, v0[localy]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
88 m_rectangleVerts[2].setCoordinate(localx, v1[localx]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
89 m_rectangleVerts[2].setCoordinate(localy, v1[localy]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
90 m_rectangleVerts[3].setCoordinate(localx, v0[localx]);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1009
diff changeset
91 m_rectangleVerts[3].setCoordinate(localy, v1[localy]);
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 }

mercurial