|
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 <QMouseEvent> |
|
20 #include "magicWandMode.h" |
|
21 #include "../ldDocument.h" |
|
22 #include "../mainWindow.h" |
|
23 #include "../glRenderer.h" |
|
24 |
|
25 MagicWandMode::MagicWandMode (GLRenderer* renderer) : |
|
26 Super (renderer) |
|
27 { |
|
28 // Get vertex<->object data |
|
29 for (LDObjectPtr obj : CurrentDocument()->objects()) |
|
30 { |
|
31 // Note: this deliberately only takes vertex-objects into account. |
|
32 // The magic wand does not process subparts. |
|
33 for (int i = 0; i < obj->numVertices(); ++i) |
|
34 m_vertices[obj->vertex (i)] << obj; |
|
35 } |
|
36 } |
|
37 |
|
38 EditModeType MagicWandMode::type() const |
|
39 { |
|
40 return EditModeType::MagicWand; |
|
41 } |
|
42 |
|
43 void MagicWandMode::fillBoundaries (LDObjectPtr obj, QVector<BoundaryType>& boundaries, QVector<LDObjectPtr>& candidates) |
|
44 { |
|
45 // All boundaries obviously share vertices with the object, therefore they're all in the list |
|
46 // of candidates. |
|
47 for (auto it = candidates.begin(); it != candidates.end(); ++it) |
|
48 { |
|
49 if (not Eq ((*it)->type(), OBJ_Line, OBJ_CondLine) or (*it)->vertex (0) == (*it)->vertex (1)) |
|
50 continue; |
|
51 |
|
52 int matches = 0; |
|
53 |
|
54 for (int i = 0; i < obj->numVertices(); ++i) |
|
55 { |
|
56 if (not Eq (obj->vertex (i), (*it)->vertex (0), (*it)->vertex (1))) |
|
57 continue; |
|
58 |
|
59 if (++matches == 2) |
|
60 { |
|
61 // Boundary found. If it's an edgeline, add it to the boundaries list, if a |
|
62 // conditional line, select it. |
|
63 if ((*it)->type() == OBJ_CondLine) |
|
64 m_selection << *it; |
|
65 else |
|
66 boundaries.append (std::make_tuple ((*it)->vertex (0), (*it)->vertex (1))); |
|
67 |
|
68 break; |
|
69 } |
|
70 } |
|
71 } |
|
72 } |
|
73 |
|
74 void MagicWandMode::doMagic (LDObjectPtr obj, MagicWandMode::MagicType type) |
|
75 { |
|
76 if (obj == null) |
|
77 { |
|
78 if (type == Set) |
|
79 { |
|
80 CurrentDocument()->clearSelection(); |
|
81 g_win->buildObjList(); |
|
82 } |
|
83 |
|
84 return; |
|
85 } |
|
86 |
|
87 int matchesneeded = 0; |
|
88 QVector<BoundaryType> boundaries; |
|
89 LDObjectType objtype = obj->type(); |
|
90 |
|
91 if (type != InternalRecursion) |
|
92 { |
|
93 m_selection.clear(); |
|
94 m_selection.append (obj); |
|
95 } |
|
96 |
|
97 switch (obj->type()) |
|
98 { |
|
99 case OBJ_Line: |
|
100 case OBJ_CondLine: |
|
101 matchesneeded = 1; |
|
102 break; |
|
103 |
|
104 case OBJ_Triangle: |
|
105 case OBJ_Quad: |
|
106 matchesneeded = 2; |
|
107 break; |
|
108 |
|
109 default: |
|
110 return; |
|
111 } |
|
112 |
|
113 QVector<LDObjectPtr> candidates; |
|
114 |
|
115 // Get the list of objects that touch this object, i.e. share a vertex |
|
116 // with this. |
|
117 for (int i = 0; i < obj->numVertices(); ++i) |
|
118 candidates += m_vertices[obj->vertex (i)]; |
|
119 |
|
120 RemoveDuplicates (candidates); |
|
121 |
|
122 // If we're dealing with surfaces, get a list of boundaries. |
|
123 if (matchesneeded > 1) |
|
124 fillBoundaries (obj, boundaries, candidates); |
|
125 |
|
126 for (LDObjectPtr candidate : candidates) |
|
127 { |
|
128 try |
|
129 { |
|
130 // If we're doing this on lines, we need exact type match. Surface types (quads and |
|
131 // triangles) can be mixed. Also don't consider self a candidate, and don't consider |
|
132 // objects we have already processed. |
|
133 if ((candidate == obj) or |
|
134 (candidate->color() != obj->color()) or |
|
135 (m_selection.contains (candidate)) or |
|
136 (matchesneeded == 1 and (candidate->type() != objtype)) or |
|
137 ((candidate->numVertices() > 2) ^ (matchesneeded == 2))) |
|
138 { |
|
139 throw 0; |
|
140 } |
|
141 |
|
142 // Now ensure the two objects share enough vertices. |
|
143 QVector<Vertex> matches; |
|
144 |
|
145 for (int i = 0; i < obj->numVertices(); ++i) |
|
146 { |
|
147 for (int j = 0; j < candidate->numVertices(); ++j) |
|
148 { |
|
149 if (obj->vertex(i) == candidate->vertex(j)) |
|
150 { |
|
151 matches << obj->vertex(i); |
|
152 break; |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 if (matches.size() < matchesneeded) |
|
158 throw 0; // Not enough matches. |
|
159 |
|
160 // Check if a boundary gets in between the objects. |
|
161 for (auto boundary : boundaries) |
|
162 { |
|
163 if (Eq (matches[0], std::get<0> (boundary), std::get<1> (boundary)) and |
|
164 Eq (matches[1], std::get<0> (boundary), std::get<1> (boundary))) |
|
165 { |
|
166 throw 0; |
|
167 } |
|
168 } |
|
169 |
|
170 m_selection.append (candidate); |
|
171 doMagic (candidate, InternalRecursion); |
|
172 } |
|
173 catch (int&) |
|
174 { |
|
175 continue; |
|
176 } |
|
177 } |
|
178 |
|
179 switch (type) |
|
180 { |
|
181 case Set: |
|
182 CurrentDocument()->clearSelection(); |
|
183 case Additive: |
|
184 for (LDObjectPtr obj : m_selection) |
|
185 obj->select(); |
|
186 break; |
|
187 |
|
188 case Subtractive: |
|
189 for (LDObjectPtr obj : m_selection) |
|
190 obj->deselect(); |
|
191 break; |
|
192 |
|
193 case InternalRecursion: |
|
194 break; |
|
195 } |
|
196 |
|
197 if (type != InternalRecursion) |
|
198 g_win->buildObjList(); |
|
199 } |
|
200 |
|
201 bool MagicWandMode::mouseReleased (MouseEventData const& data) |
|
202 { |
|
203 if (Super::mouseReleased (data)) |
|
204 return true; |
|
205 |
|
206 if (data.releasedButtons & Qt::LeftButton and not data.mouseMoved) |
|
207 { |
|
208 MagicType wandtype = MagicWandMode::Set; |
|
209 |
|
210 if (data.keymods & Qt::ShiftModifier) |
|
211 wandtype = MagicWandMode::Additive; |
|
212 elif (data.keymods & Qt::ControlModifier) |
|
213 wandtype = MagicWandMode::Subtractive; |
|
214 |
|
215 doMagic (renderer()->pickOneObject (data.ev->x(), data.ev->y()), wandtype); |
|
216 return true; |
|
217 } |
|
218 |
|
219 return false; |
|
220 } |