1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 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 "drawMode.h" |
|
22 #include "../ldObject.h" |
|
23 #include "../glRenderer.h" |
|
24 #include "../miscallenous.h" |
|
25 |
|
26 DrawMode::DrawMode (GLRenderer* renderer) : |
|
27 Super (renderer) {} |
|
28 |
|
29 EditModeType DrawMode::type() const |
|
30 { |
|
31 return EditModeType::Draw; |
|
32 } |
|
33 |
|
34 void DrawMode::render (QPainter& painter) const |
|
35 { |
|
36 QVector<Vertex> poly; |
|
37 QFontMetrics metrics = QFontMetrics (QFont()); |
|
38 |
|
39 for (Vertex const& vert : m_drawedVerts) |
|
40 poly << vert; |
|
41 |
|
42 // Draw the cursor vertex as the last one in the list. |
|
43 if (poly.size() < 4) |
|
44 poly << getCursorVertex(); |
|
45 |
|
46 renderPolygon (painter, poly, true, true); |
|
47 } |
|
48 |
|
49 bool DrawMode::preAddVertex (Vertex const& pos) |
|
50 { |
|
51 // If we picked an already-existing vertex, stop drawing |
|
52 for (Vertex& vert : m_drawedVerts) |
|
53 { |
|
54 if (vert == pos) |
|
55 { |
|
56 endDraw(); |
|
57 return true; |
|
58 } |
|
59 } |
|
60 |
|
61 return false; |
|
62 } |
|
63 |
|
64 bool DrawMode::mouseReleased (MouseEventData const& data) |
|
65 { |
|
66 if (Super::mouseReleased (data)) |
|
67 return true; |
|
68 |
|
69 if (data.releasedButtons & Qt::LeftButton) |
|
70 { |
|
71 // If we have 4 verts, stop drawing. |
|
72 if (m_drawedVerts.size() >= 4) |
|
73 { |
|
74 endDraw(); |
|
75 return true; |
|
76 } |
|
77 |
|
78 addDrawnVertex (getCursorVertex()); |
|
79 return true; |
|
80 } |
|
81 |
|
82 return false; |
|
83 } |
|
84 |
|
85 void DrawMode::endDraw() |
|
86 { |
|
87 // Clean the selection and create the object |
|
88 QList<Vertex>& verts = m_drawedVerts; |
|
89 LDObjectList objs; |
|
90 |
|
91 switch (verts.size()) |
|
92 { |
|
93 case 1: |
|
94 { |
|
95 // 1 vertex - add a vertex object |
|
96 LDVertex* obj = LDSpawn<LDVertex>(); |
|
97 obj->pos = verts[0]; |
|
98 obj->setColor (MainColor); |
|
99 objs << obj; |
|
100 break; |
|
101 } |
|
102 |
|
103 case 2: |
|
104 { |
|
105 // 2 verts - make a line |
|
106 LDLine* obj = LDSpawn<LDLine> (verts[0], verts[1]); |
|
107 obj->setColor (EdgeColor); |
|
108 objs << obj; |
|
109 break; |
|
110 } |
|
111 |
|
112 case 3: |
|
113 case 4: |
|
114 { |
|
115 LDObject* obj = (verts.size() == 3) ? |
|
116 static_cast<LDObject*> (LDSpawn<LDTriangle>()) : |
|
117 static_cast<LDObject*> (LDSpawn<LDQuad>()); |
|
118 |
|
119 obj->setColor (MainColor); |
|
120 |
|
121 for (int i = 0; i < verts.size(); ++i) |
|
122 obj->setVertex (i, verts[i]); |
|
123 |
|
124 objs << obj; |
|
125 break; |
|
126 } |
|
127 } |
|
128 |
|
129 finishDraw (objs); |
|
130 } |
|
131 |
|
132 template<typename _Type> |
|
133 _Type IntervalClamp (_Type a, _Type interval) |
|
134 { |
|
135 _Type remainder = a % interval; |
|
136 |
|
137 if (remainder >= float (interval / 2)) |
|
138 a += interval; |
|
139 |
|
140 a -= remainder; |
|
141 return a; |
|
142 } |
|
143 |
|
144 Vertex DrawMode::getCursorVertex() const |
|
145 { |
|
146 Vertex result = renderer()->position3D(); |
|
147 |
|
148 if (renderer()->keyboardModifiers() & Qt::ControlModifier |
|
149 and not m_drawedVerts.isEmpty()) |
|
150 { |
|
151 Vertex const& v0 = m_drawedVerts.last(); |
|
152 Vertex const& v1 = result; |
|
153 Axis relX, relY; |
|
154 |
|
155 renderer()->getRelativeAxes (relX, relY); |
|
156 QLineF ln (v0[relX], v0[relY], v1[relX], v1[relY]); |
|
157 ln.setAngle (IntervalClamp<int> (ln.angle(), 45)); |
|
158 result.setCoordinate (relX, Grid::Snap (ln.x2(), Grid::Coordinate)); |
|
159 result.setCoordinate (relY, Grid::Snap (ln.y2(), Grid::Coordinate)); |
|
160 } |
|
161 |
|
162 return result; |
|
163 } |
|