src/editmodes/circleMode.cc

Sun, 13 Jul 2014 20:40:24 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 13 Jul 2014 20:40:24 +0300
changeset 840
d077dd19bf9a
parent 830
a741a0b9df49
child 842
e1c9010eb9e8
permissions
-rw-r--r--

- changed `&&` and `||` operators to their named variants `and` and `or`

829
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
1 /*
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
2 * LDForge: LDraw parts authoring CAD
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
3 * Copyright (C) 2013, 2014 Santeri Piippo
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
4 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
5 * This program is free software: you can redistribute it and/or modify
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
6 * it under the terms of the GNU General Public License as published by
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
8 * (at your option) any later version.
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
9 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
10 * This program is distributed in the hope that it will be useful,
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
13 * GNU General Public License for more details.
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
14 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
15 * You should have received a copy of the GNU General Public License
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
17 */
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
18
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <QPainter>
830
a741a0b9df49 - updated filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents: 829
diff changeset
20 #include "circleMode.h"
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "../miscallenous.h"
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "../ldObject.h"
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
23 #include "../ldDocument.h"
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
24 #include "../misc/ringFinder.h"
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
25 #include "../primitives.h"
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
26 #include "../glRenderer.h"
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 CircleMode::CircleMode (GLRenderer* renderer) :
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 Super (renderer) {}
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
31 EditModeType CircleMode::type() const
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
32 {
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
33 return EditModeType::Circle;
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
34 }
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
35
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 double CircleMode::getCircleDrawDist (int pos) const
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 {
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
38 assert (_drawedVerts.size() >= pos + 1);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
39 Vertex v1 = (_drawedVerts.size() >= pos + 2) ? _drawedVerts[pos + 1] :
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 renderer()->coordconv2_3 (renderer()->mousePosition(), false);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 Axis localx, localy;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 renderer()->getRelativeAxes (localx, localy);
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
43 double dx = _drawedVerts[0][localx] - v1[localx];
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
44 double dy = _drawedVerts[0][localy] - v1[localy];
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 return Grid::snap (sqrt ((dx * dx) + (dy * dy)), Grid::Coordinate);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 Matrix CircleMode::getCircleDrawMatrix (double scale)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 // Matrix templates. 2 is substituted with the scale value, 1 is inverted to -1 if needed.
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 static const Matrix templates[3] =
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 { 2, 0, 0, 0, 1, 0, 0, 0, 2 },
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 { 2, 0, 0, 0, 0, 2, 0, 1, 0 },
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 { 0, 1, 0, 2, 0, 0, 0, 0, 2 },
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 };
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 Matrix transform = templates[renderer()->camera() % 3];
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 for (int i = 0; i < 9; ++i)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 if (transform[i] == 2)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 transform[i] = scale;
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
64 elif (transform[i] == 1 and renderer()->camera() >= 3)
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 transform[i] = -1;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 return transform;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
71 void CircleMode::buildCircle()
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
72 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
73 LDObjectList objs;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
74 const int segs = g_lores, divs = g_lores; // TODO: make customizable
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
75 double dist0 = getCircleDrawDist (0),
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
76 dist1 = getCircleDrawDist (1);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
77 LDDocumentPtr refFile;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
78 Matrix transform;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
79 bool circleOrDisc = false;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
80
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
81 if (dist1 < dist0)
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
82 std::swap<double> (dist0, dist1);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
83
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
84 if (dist0 == dist1)
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
85 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
86 // If the radii are the same, there's no ring space to fill. Use a circle.
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
87 refFile = getDocument ("4-4edge.dat");
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
88 transform = getCircleDrawMatrix (dist0);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
89 circleOrDisc = true;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
90 }
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
91 elif (dist0 == 0 or dist1 == 0)
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
92 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
93 // If either radii is 0, use a disc.
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
94 refFile = getDocument ("4-4disc.dat");
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
95 transform = getCircleDrawMatrix ((dist0 != 0) ? dist0 : dist1);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
96 circleOrDisc = true;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
97 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
98 elif (g_RingFinder.findRings (dist0, dist1))
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
99 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
100 // The ring finder found a solution, use that. Add the component rings to the file.
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
101 for (const RingFinder::Component& cmp : g_RingFinder.bestSolution()->getComponents())
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
102 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
103 // Get a ref file for this primitive. If we cannot find it in the
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
104 // LDraw library, generate it.
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
105 if ((refFile = ::getDocument (radialFileName (::Ring, g_lores, g_lores, cmp.num))) == null)
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
106 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
107 refFile = generatePrimitive (::Ring, g_lores, g_lores, cmp.num);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
108 refFile->setImplicit (false);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
109 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
110
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
111 LDSubfilePtr ref = spawn<LDSubfile>();
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
112 ref->setFileInfo (refFile);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
113 ref->setTransform (getCircleDrawMatrix (cmp.scale));
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
114 ref->setPosition (_drawedVerts[0]);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
115 ref->setColor (maincolor());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
116 objs << ref;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
117 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
118 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
119 else
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
120 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
121 // Ring finder failed, last resort: draw the ring with quads
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
122 QList<QLineF> c0, c1;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
123 Axis localx, localy, localz;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
124 renderer()->getRelativeAxes (localx, localy);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
125 localz = (Axis) (3 - localx - localy);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
126 double x0 = _drawedVerts[0][localx],
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
127 y0 = _drawedVerts[0][localy];
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
128
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
129 Vertex templ;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
130 templ.setCoordinate (localx, x0);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
131 templ.setCoordinate (localy, y0);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
132 templ.setCoordinate (localz, renderer()->getDepthValue());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
133
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
134 // Calculate circle coords
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
135 makeCircle (segs, divs, dist0, c0);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
136 makeCircle (segs, divs, dist1, c1);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
137
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
138 for (int i = 0; i < segs; ++i)
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
139 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
140 Vertex v0, v1, v2, v3;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
141 v0 = v1 = v2 = v3 = templ;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
142 v0.setCoordinate (localx, v0[localx] + c0[i].x1());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
143 v0.setCoordinate (localy, v0[localy] + c0[i].y1());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
144 v1.setCoordinate (localx, v1[localx] + c0[i].x2());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
145 v1.setCoordinate (localy, v1[localy] + c0[i].y2());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
146 v2.setCoordinate (localx, v2[localx] + c1[i].x2());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
147 v2.setCoordinate (localy, v2[localy] + c1[i].y2());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
148 v3.setCoordinate (localx, v3[localx] + c1[i].x1());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
149 v3.setCoordinate (localy, v3[localy] + c1[i].y1());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
150
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
151 LDQuadPtr quad (spawn<LDQuad> (v0, v1, v2, v3));
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
152 quad->setColor (maincolor());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
153
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
154 // Ensure the quads always are BFC-front towards the camera
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
155 if (renderer()->camera() % 3 <= 0)
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
156 quad->invert();
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
157
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
158 objs << quad;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
159 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
160 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
161
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
162 if (circleOrDisc and refFile != null)
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
163 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
164 LDSubfilePtr ref = spawn<LDSubfile>();
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
165 ref->setFileInfo (refFile);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
166 ref->setTransform (transform);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
167 ref->setPosition (_drawedVerts[0]);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
168 ref->setColor (maincolor());
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
169 objs << ref;
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
170 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
171
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
172 finishDraw (objs);
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
173 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
174
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 void CircleMode::render (QPainter& painter) const
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 {
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
177 QFontMetrics metrics = QFontMetrics (QFont());
824
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
178
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 // If we have not specified the center point of the circle yet, preview it on the screen.
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
180 if (_drawedVerts.isEmpty())
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 renderer()->drawBlip (painter, renderer()->coordconv3_2 (renderer()->position3D()));
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 else
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 QVector<Vertex> verts, verts2;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 const double dist0 = getCircleDrawDist (0),
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
188 dist1 = (_drawedVerts.size() >= 2) ? getCircleDrawDist (1) : -1;
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 const int segs = g_lores;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 const double angleUnit = (2 * pi) / segs;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 Axis relX, relY;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 QVector<QPoint> ringpoints, circlepoints, circle2points;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 renderer()->getRelativeAxes (relX, relY);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 // Calculate the preview positions of vertices
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 for (int i = 0; i < segs; ++i)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 Vertex v = g_origin;
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
200 v.setCoordinate (relX, _drawedVerts[0][relX] + (cos (i * angleUnit) * dist0));
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
201 v.setCoordinate (relY, _drawedVerts[0][relY] + (sin (i * angleUnit) * dist0));
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 verts << v;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 if (dist1 != -1)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 {
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
206 v.setCoordinate (relX, _drawedVerts[0][relX] + (cos (i * angleUnit) * dist1));
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
207 v.setCoordinate (relY, _drawedVerts[0][relY] + (sin (i * angleUnit) * dist1));
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 verts2 << v;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 int i = 0;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 for (const Vertex& v : verts + verts2)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 // Calculate the 2D point of the vertex
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 QPoint point = renderer()->coordconv3_2 (v);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 // Draw a green blip at where it is
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 renderer()->drawBlip (painter, point);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 // Add it to the list of points for the green ring fill.
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 ringpoints << point;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 // Also add the circle points to separate lists
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 if (i < verts.size())
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 circlepoints << point;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 else
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 circle2points << point;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 ++i;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 // Insert the first point as the seventeenth one so that
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 // the ring polygon is closed properly.
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 if (ringpoints.size() >= 16)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 ringpoints.insert (16, ringpoints[0]);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 // Same for the outer ring. Note that the indices are offset by 1
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 // because of the insertion done above bumps the values.
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 if (ringpoints.size() >= 33)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 ringpoints.insert (33, ringpoints[17]);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 // Draw the ring
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
244 painter.setBrush ((_drawedVerts.size() >= 2) ? _polybrush : Qt::NoBrush);
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 painter.setPen (Qt::NoPen);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 painter.drawPolygon (QPolygon (ringpoints));
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 // Draw the circles
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 painter.setBrush (Qt::NoBrush);
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
250 painter.setPen (renderer()->linePen());
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 painter.drawPolygon (QPolygon (circlepoints));
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 painter.drawPolygon (QPolygon (circle2points));
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 // Draw the current radius in the middle of the circle.
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
255 QPoint origin = renderer()->coordconv3_2 (_drawedVerts[0]);
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 QString label = QString::number (dist0);
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
257 painter.setPen (renderer()->textPen());
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 painter.drawText (origin.x() - (metrics.width (label) / 2), origin.y(), label);
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
260 if (_drawedVerts.size() >= 2)
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 painter.drawText (origin.x() - (metrics.width (label) / 2),
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 origin.y() + metrics.height(), QString::number (dist1));
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 }
824
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
266 }
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
267
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
268 bool CircleMode::mouseReleased (MouseEventData const& data)
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
269 {
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
270 if (Super::mouseReleased (data))
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
271 return true;
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
272
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
273 if (data.releasedButtons & Qt::LeftButton)
824
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
274 {
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
275 if (_drawedVerts.size() < 3)
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
276 addDrawnVertex (renderer()->position3D());
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
277 else
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
278 buildCircle();
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
279
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
280 return true;
824
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
281 }
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
282
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
283 return false;
824
6add2126e7ff - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 823
diff changeset
284 }

mercurial