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 |
|
20 #include "main.h" |
|
21 #include "ldObject.h" |
|
22 #include "ldDocument.h" |
|
23 #include "miscallenous.h" |
|
24 #include "mainWindow.h" |
|
25 #include "editHistory.h" |
|
26 #include "glRenderer.h" |
|
27 #include "colors.h" |
|
28 #include "glCompiler.h" |
|
29 |
|
30 CFGENTRY (String, DefaultName, "") |
|
31 CFGENTRY (String, DefaultUser, "") |
|
32 CFGENTRY (Bool, UseCALicense, true) |
|
33 |
|
34 // List of all LDObjects |
|
35 QMap<int32, LDObject*> g_allObjects; |
|
36 static int32 g_idcursor = 1; // 0 shalt be null |
|
37 |
|
38 enum { MAX_LDOBJECT_IDS = (1 << 24) }; |
|
39 |
|
40 #define LDOBJ_DEFAULT_CTOR(T,BASE) \ |
|
41 T :: T (LDDocument* document) : \ |
|
42 BASE (document) {} |
|
43 |
|
44 // ============================================================================= |
|
45 // LDObject constructors |
|
46 // |
|
47 LDObject::LDObject (LDDocument* document) : |
|
48 m_isHidden (false), |
|
49 m_isSelected (false), |
|
50 m_document (nullptr), |
|
51 qObjListEntry (null) |
|
52 { |
|
53 if (document) |
|
54 document->addObject (this); |
|
55 |
|
56 memset (m_coords, 0, sizeof m_coords); |
|
57 chooseID(); |
|
58 |
|
59 if (id() != 0) |
|
60 g_allObjects[id()] = this; |
|
61 |
|
62 setRandomColor (QColor::fromHsv (rand() % 360, rand() % 256, rand() % 96 + 128)); |
|
63 } |
|
64 |
|
65 LDSubfile::LDSubfile (LDDocument* document) : |
|
66 LDMatrixObject (document) {} |
|
67 |
|
68 LDOBJ_DEFAULT_CTOR (LDEmpty, LDObject) |
|
69 LDOBJ_DEFAULT_CTOR (LDError, LDObject) |
|
70 LDOBJ_DEFAULT_CTOR (LDLine, LDObject) |
|
71 LDOBJ_DEFAULT_CTOR (LDTriangle, LDObject) |
|
72 LDOBJ_DEFAULT_CTOR (LDCondLine, LDLine) |
|
73 LDOBJ_DEFAULT_CTOR (LDQuad, LDObject) |
|
74 LDOBJ_DEFAULT_CTOR (LDVertex, LDObject) |
|
75 LDOBJ_DEFAULT_CTOR (LDOverlay, LDObject) |
|
76 LDOBJ_DEFAULT_CTOR (LDBFC, LDObject) |
|
77 LDOBJ_DEFAULT_CTOR (LDComment, LDObject) |
|
78 |
|
79 LDObject::~LDObject() |
|
80 { |
|
81 // Don't bother during program termination |
|
82 if (IsExiting() == false) |
|
83 { |
|
84 deselect(); |
|
85 |
|
86 // If this object was associated to a file, remove it off it now |
|
87 if (document() != null) |
|
88 document()->forgetObject (this); |
|
89 |
|
90 // Delete the GL lists |
|
91 if (g_win != null) |
|
92 g_win->R()->forgetObject (this); |
|
93 |
|
94 // Remove this object from the list of LDObjects |
|
95 g_allObjects.erase (g_allObjects.find (id())); |
|
96 } |
|
97 } |
|
98 |
|
99 // ============================================================================= |
|
100 // |
|
101 void LDObject::chooseID() |
|
102 { |
|
103 // Let's hope that nobody goes to create 17 million objects anytime soon... |
|
104 if (g_idcursor < MAX_LDOBJECT_IDS) |
|
105 setID (g_idcursor++); |
|
106 else |
|
107 setID (0); |
|
108 } |
|
109 |
|
110 // ============================================================================= |
|
111 // |
|
112 void LDObject::setVertexCoord (int i, Axis ax, double value) |
|
113 { |
|
114 Vertex v = vertex (i); |
|
115 v.setCoordinate (ax, value); |
|
116 setVertex (i, v); |
|
117 } |
|
118 |
|
119 // ============================================================================= |
|
120 // |
|
121 QString LDComment::asText() const |
|
122 { |
|
123 return format ("0 %1", text()); |
|
124 } |
|
125 |
|
126 // ============================================================================= |
|
127 // |
|
128 QString LDSubfile::asText() const |
|
129 { |
|
130 QString val = format ("1 %1 %2 ", color(), position()); |
|
131 val += transform().toString(); |
|
132 val += ' '; |
|
133 val += fileInfo()->name(); |
|
134 return val; |
|
135 } |
|
136 |
|
137 // ============================================================================= |
|
138 // |
|
139 QString LDLine::asText() const |
|
140 { |
|
141 QString val = format ("2 %1", color()); |
|
142 |
|
143 for (int i = 0; i < 2; ++i) |
|
144 val += format (" %1", vertex (i)); |
|
145 |
|
146 return val; |
|
147 } |
|
148 |
|
149 // ============================================================================= |
|
150 // |
|
151 QString LDTriangle::asText() const |
|
152 { |
|
153 QString val = format ("3 %1", color()); |
|
154 |
|
155 for (int i = 0; i < 3; ++i) |
|
156 val += format (" %1", vertex (i)); |
|
157 |
|
158 return val; |
|
159 } |
|
160 |
|
161 // ============================================================================= |
|
162 // |
|
163 QString LDQuad::asText() const |
|
164 { |
|
165 QString val = format ("4 %1", color()); |
|
166 |
|
167 for (int i = 0; i < 4; ++i) |
|
168 val += format (" %1", vertex (i)); |
|
169 |
|
170 return val; |
|
171 } |
|
172 |
|
173 // ============================================================================= |
|
174 // |
|
175 QString LDCondLine::asText() const |
|
176 { |
|
177 QString val = format ("5 %1", color()); |
|
178 |
|
179 // Add the coordinates |
|
180 for (int i = 0; i < 4; ++i) |
|
181 val += format (" %1", vertex (i)); |
|
182 |
|
183 return val; |
|
184 } |
|
185 |
|
186 // ============================================================================= |
|
187 // |
|
188 QString LDError::asText() const |
|
189 { |
|
190 return contents(); |
|
191 } |
|
192 |
|
193 // ============================================================================= |
|
194 // |
|
195 QString LDVertex::asText() const |
|
196 { |
|
197 return format ("0 !LDFORGE VERTEX %1 %2", color(), pos); |
|
198 } |
|
199 |
|
200 // ============================================================================= |
|
201 // |
|
202 QString LDEmpty::asText() const |
|
203 { |
|
204 return ""; |
|
205 } |
|
206 |
|
207 // ============================================================================= |
|
208 // |
|
209 const char* LDBFC::StatementStrings[] = |
|
210 { |
|
211 "CERTIFY CCW", |
|
212 "CCW", |
|
213 "CERTIFY CW", |
|
214 "CW", |
|
215 "NOCERTIFY", |
|
216 "INVERTNEXT", |
|
217 "CLIP", |
|
218 "CLIP CCW", |
|
219 "CLIP CW", |
|
220 "NOCLIP", |
|
221 }; |
|
222 |
|
223 QString LDBFC::asText() const |
|
224 { |
|
225 return format ("0 BFC %1", StatementStrings[int (m_statement)]); |
|
226 } |
|
227 |
|
228 // ============================================================================= |
|
229 // |
|
230 QList<LDTriangle*> LDQuad::splitToTriangles() |
|
231 { |
|
232 // Create the two triangles based on this quadrilateral: |
|
233 // 0---3 0---3 3 |
|
234 // | | | / /| |
|
235 // | | ==> | / / | |
|
236 // | | |/ / | |
|
237 // 1---2 1 1---2 |
|
238 LDTriangle* tri1 (new LDTriangle (vertex (0), vertex (1), vertex (3))); |
|
239 LDTriangle* tri2 (new LDTriangle (vertex (1), vertex (2), vertex (3))); |
|
240 |
|
241 // The triangles also inherit the quad's color |
|
242 tri1->setColor (color()); |
|
243 tri2->setColor (color()); |
|
244 |
|
245 return {tri1, tri2}; |
|
246 } |
|
247 |
|
248 // ============================================================================= |
|
249 // |
|
250 void LDObject::replace (LDObject* other) |
|
251 { |
|
252 int idx = lineNumber(); |
|
253 |
|
254 if (idx != -1) |
|
255 { |
|
256 // Replace the instance of the old object with the new object |
|
257 document()->setObject (idx, other); |
|
258 |
|
259 // Remove the old object |
|
260 destroy(); |
|
261 } |
|
262 } |
|
263 |
|
264 // ============================================================================= |
|
265 // |
|
266 void LDObject::swap (LDObject* other) |
|
267 { |
|
268 assert (document() == other->document()); |
|
269 document()->swapObjects (this, other); |
|
270 } |
|
271 |
|
272 // ============================================================================= |
|
273 // |
|
274 LDLine::LDLine (Vertex v1, Vertex v2, LDDocument* document) : |
|
275 LDObject (document) |
|
276 { |
|
277 setVertex (0, v1); |
|
278 setVertex (1, v2); |
|
279 } |
|
280 |
|
281 // ============================================================================= |
|
282 // |
|
283 LDTriangle::LDTriangle (const Vertex& v1, const Vertex& v2, const Vertex& v3, LDDocument* document) : |
|
284 LDObject (document) |
|
285 { |
|
286 setVertex (0, v1); |
|
287 setVertex (1, v2); |
|
288 setVertex (2, v3); |
|
289 } |
|
290 |
|
291 // ============================================================================= |
|
292 // |
|
293 LDQuad::LDQuad (const Vertex& v1, const Vertex& v2, const Vertex& v3, const Vertex& v4, LDDocument* document) : |
|
294 LDObject (document) |
|
295 { |
|
296 setVertex (0, v1); |
|
297 setVertex (1, v2); |
|
298 setVertex (2, v3); |
|
299 setVertex (3, v4); |
|
300 } |
|
301 |
|
302 // ============================================================================= |
|
303 // |
|
304 LDCondLine::LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, LDDocument* document) : |
|
305 LDLine (document) |
|
306 { |
|
307 setVertex (0, v0); |
|
308 setVertex (1, v1); |
|
309 setVertex (2, v2); |
|
310 setVertex (3, v3); |
|
311 } |
|
312 |
|
313 // ============================================================================= |
|
314 // |
|
315 void LDObject::destroy() |
|
316 { |
|
317 delete this; |
|
318 } |
|
319 |
|
320 // ============================================================================= |
|
321 // |
|
322 void LDObject::setDocument (LDDocument* const& a) |
|
323 { |
|
324 m_document = a; |
|
325 |
|
326 if (a == null) |
|
327 setSelected (false); |
|
328 } |
|
329 |
|
330 // ============================================================================= |
|
331 // |
|
332 static void TransformObject (LDObject* obj, Matrix transform, Vertex pos, LDColor parentcolor) |
|
333 { |
|
334 switch (obj->type()) |
|
335 { |
|
336 case OBJ_Line: |
|
337 case OBJ_CondLine: |
|
338 case OBJ_Triangle: |
|
339 case OBJ_Quad: |
|
340 for (int i = 0; i < obj->numVertices(); ++i) |
|
341 { |
|
342 Vertex v = obj->vertex (i); |
|
343 v.transform (transform, pos); |
|
344 obj->setVertex (i, v); |
|
345 } |
|
346 break; |
|
347 |
|
348 case OBJ_Subfile: |
|
349 { |
|
350 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
|
351 Matrix newMatrix = transform * ref->transform(); |
|
352 Vertex newpos = ref->position(); |
|
353 newpos.transform (transform, pos); |
|
354 ref->setPosition (newpos); |
|
355 ref->setTransform (newMatrix); |
|
356 } |
|
357 break; |
|
358 |
|
359 default: |
|
360 break; |
|
361 } |
|
362 |
|
363 if (obj->color() == MainColor) |
|
364 obj->setColor (parentcolor); |
|
365 } |
|
366 |
|
367 // ============================================================================= |
|
368 // ----------------------------------------------------------------------------- |
|
369 LDObjectList LDSubfile::inlineContents (bool deep, bool render) |
|
370 { |
|
371 LDObjectList objs = fileInfo()->inlineContents (deep, render); |
|
372 |
|
373 // Transform the objects |
|
374 for (LDObject* obj : objs) |
|
375 { |
|
376 // assert (obj->type() != OBJ_Subfile); |
|
377 // Set the parent now so we know what inlined the object. |
|
378 obj->setParent (this); |
|
379 TransformObject (obj, transform(), position(), color()); |
|
380 } |
|
381 |
|
382 return objs; |
|
383 } |
|
384 |
|
385 // ============================================================================= |
|
386 // |
|
387 LDPolygon* LDObject::getPolygon() |
|
388 { |
|
389 LDObjectType ot = type(); |
|
390 int num = (ot == OBJ_Line) ? 2 |
|
391 : (ot == OBJ_Triangle) ? 3 |
|
392 : (ot == OBJ_Quad) ? 4 |
|
393 : (ot == OBJ_CondLine) ? 5 |
|
394 : 0; |
|
395 |
|
396 if (num == 0) |
|
397 return null; |
|
398 |
|
399 LDPolygon* data = new LDPolygon; |
|
400 data->id = id(); |
|
401 data->num = num; |
|
402 data->color = color().index(); |
|
403 |
|
404 for (int i = 0; i < data->numVertices(); ++i) |
|
405 data->vertices[i] = vertex (i); |
|
406 |
|
407 return data; |
|
408 } |
|
409 |
|
410 // ============================================================================= |
|
411 // |
|
412 QList<LDPolygon> LDSubfile::inlinePolygons() |
|
413 { |
|
414 QList<LDPolygon> data = fileInfo()->inlinePolygons(); |
|
415 |
|
416 for (LDPolygon& entry : data) |
|
417 { |
|
418 for (int i = 0; i < entry.numVertices(); ++i) |
|
419 entry.vertices[i].transform (transform(), position()); |
|
420 } |
|
421 |
|
422 return data; |
|
423 } |
|
424 |
|
425 // ============================================================================= |
|
426 // ----------------------------------------------------------------------------- |
|
427 long LDObject::lineNumber() const |
|
428 { |
|
429 if (document() != null) |
|
430 { |
|
431 for (int i = 0; i < document()->getObjectCount(); ++i) |
|
432 { |
|
433 if (document()->getObject (i) == this) |
|
434 return i; |
|
435 } |
|
436 } |
|
437 |
|
438 return -1; |
|
439 } |
|
440 |
|
441 // ============================================================================= |
|
442 // |
|
443 void LDObject::moveObjects (LDObjectList objs, const bool up) |
|
444 { |
|
445 if (objs.isEmpty()) |
|
446 return; |
|
447 |
|
448 // If we move down, we need to iterate the array in reverse order. |
|
449 long const start = up ? 0 : (objs.size() - 1); |
|
450 long const end = up ? objs.size() : -1; |
|
451 long const incr = up ? 1 : -1; |
|
452 LDObjectList objsToCompile; |
|
453 LDDocument* file = objs[0]->document(); |
|
454 |
|
455 for (long i = start; i != end; i += incr) |
|
456 { |
|
457 LDObject* obj = objs[i]; |
|
458 |
|
459 long const idx = obj->lineNumber(); |
|
460 long const target = idx + (up ? -1 : 1); |
|
461 |
|
462 if ((up and idx == 0) or (not up and idx == (long) file->objects().size() - 1l)) |
|
463 { |
|
464 // One of the objects hit the extrema. If this happens, this should be the first |
|
465 // object to be iterated on. Thus, nothing has changed yet and it's safe to just |
|
466 // abort the entire operation. |
|
467 assert (i == start); |
|
468 return; |
|
469 } |
|
470 |
|
471 objsToCompile << obj; |
|
472 objsToCompile << file->getObject (target); |
|
473 |
|
474 obj->swap (file->getObject (target)); |
|
475 } |
|
476 |
|
477 RemoveDuplicates (objsToCompile); |
|
478 |
|
479 // The objects need to be recompiled, otherwise their pick lists are left with |
|
480 // the wrong index colors which messes up selection. |
|
481 for (LDObject* obj : objsToCompile) |
|
482 g_win->R()->compileObject (obj); |
|
483 } |
|
484 |
|
485 // ============================================================================= |
|
486 // |
|
487 QString LDObject::typeName (LDObjectType type) |
|
488 { |
|
489 return LDObject::getDefault (type)->typeName(); |
|
490 } |
|
491 |
|
492 // ============================================================================= |
|
493 // |
|
494 QString LDObject::describeObjects (const LDObjectList& objs) |
|
495 { |
|
496 QString text; |
|
497 |
|
498 if (objs.isEmpty()) |
|
499 return "nothing"; // :) |
|
500 |
|
501 for (LDObjectType objType = OBJ_FirstType; objType < OBJ_NumTypes; ++objType) |
|
502 { |
|
503 int count = 0; |
|
504 |
|
505 for (LDObject* obj : objs) |
|
506 { |
|
507 if (obj->type() == objType) |
|
508 count++; |
|
509 } |
|
510 |
|
511 if (count == 0) |
|
512 continue; |
|
513 |
|
514 if (not text.isEmpty()) |
|
515 text += ", "; |
|
516 |
|
517 QString noun = format ("%1%2", typeName (objType), Plural (count)); |
|
518 |
|
519 // Plural of "vertex" is "vertices", correct that |
|
520 if (objType == OBJ_Vertex and count != 1) |
|
521 noun = "vertices"; |
|
522 |
|
523 text += format ("%1 %2", count, noun); |
|
524 } |
|
525 |
|
526 return text; |
|
527 } |
|
528 |
|
529 // ============================================================================= |
|
530 // |
|
531 LDObject* LDObject::topLevelParent() |
|
532 { |
|
533 LDObject* it; |
|
534 |
|
535 for (it = this; it->parent(); it = it->parent()) |
|
536 ; |
|
537 |
|
538 return it; |
|
539 } |
|
540 |
|
541 // ============================================================================= |
|
542 // |
|
543 LDObject* LDObject::next() const |
|
544 { |
|
545 int idx = lineNumber(); |
|
546 assert (idx != -1); |
|
547 |
|
548 if (idx == document()->getObjectCount() - 1) |
|
549 return nullptr; |
|
550 |
|
551 return document()->getObject (idx + 1); |
|
552 } |
|
553 |
|
554 // ============================================================================= |
|
555 // |
|
556 LDObject* LDObject::previous() const |
|
557 { |
|
558 int idx = lineNumber(); |
|
559 assert (idx != -1); |
|
560 |
|
561 if (idx == 0) |
|
562 return nullptr; |
|
563 |
|
564 return document()->getObject (idx - 1); |
|
565 } |
|
566 |
|
567 // ============================================================================= |
|
568 // |
|
569 bool LDObject::previousIsInvertnext (LDBFC*& ptr) |
|
570 { |
|
571 LDObject* prev = previous(); |
|
572 |
|
573 if (prev and prev->type() == OBJ_BFC and static_cast<LDBFC*> (prev)->statement() == BFCStatement::InvertNext) |
|
574 { |
|
575 ptr = static_cast<LDBFC*> (prev); |
|
576 return true; |
|
577 } |
|
578 |
|
579 return false; |
|
580 } |
|
581 |
|
582 // ============================================================================= |
|
583 // |
|
584 void LDObject::move (Vertex vect) |
|
585 { |
|
586 if (hasMatrix()) |
|
587 { |
|
588 LDMatrixObject* mo = static_cast<LDMatrixObject*> (this); |
|
589 mo->setPosition (mo->position() + vect); |
|
590 } |
|
591 else if (type() == OBJ_Vertex) |
|
592 { |
|
593 // ugh |
|
594 static_cast<LDVertex*> (this)->pos += vect; |
|
595 } |
|
596 else |
|
597 { |
|
598 for (int i = 0; i < numVertices(); ++i) |
|
599 setVertex (i, vertex (i) + vect); |
|
600 } |
|
601 } |
|
602 |
|
603 // ============================================================================= |
|
604 // |
|
605 LDObject* LDObject::getDefault (const LDObjectType type) |
|
606 { |
|
607 switch (type) |
|
608 { |
|
609 case OBJ_Comment: return LDSpawn<LDComment>(); |
|
610 case OBJ_BFC: return LDSpawn<LDBFC>(); |
|
611 case OBJ_Line: return LDSpawn<LDLine>(); |
|
612 case OBJ_CondLine: return LDSpawn<LDCondLine>(); |
|
613 case OBJ_Subfile: return LDSpawn<LDSubfile>(); |
|
614 case OBJ_Triangle: return LDSpawn<LDTriangle>(); |
|
615 case OBJ_Quad: return LDSpawn<LDQuad>(); |
|
616 case OBJ_Empty: return LDSpawn<LDEmpty>(); |
|
617 case OBJ_Error: return LDSpawn<LDError>(); |
|
618 case OBJ_Vertex: return LDSpawn<LDVertex>(); |
|
619 case OBJ_Overlay: return LDSpawn<LDOverlay>(); |
|
620 case OBJ_NumTypes: assert (false); |
|
621 } |
|
622 return nullptr; |
|
623 } |
|
624 |
|
625 // ============================================================================= |
|
626 // |
|
627 void LDObject::invert() {} |
|
628 void LDBFC::invert() {} |
|
629 void LDEmpty::invert() {} |
|
630 void LDComment::invert() {} |
|
631 void LDError::invert() {} |
|
632 |
|
633 // ============================================================================= |
|
634 // |
|
635 void LDTriangle::invert() |
|
636 { |
|
637 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1. |
|
638 // Thus, we swap 1 and 2. |
|
639 Vertex tmp = vertex (1); |
|
640 setVertex (1, vertex (2)); |
|
641 setVertex (2, tmp); |
|
642 |
|
643 return; |
|
644 } |
|
645 |
|
646 // ============================================================================= |
|
647 // |
|
648 void LDQuad::invert() |
|
649 { |
|
650 // Quad: 0 -> 1 -> 2 -> 3 |
|
651 // reversed: 0 -> 3 -> 2 -> 1 |
|
652 // Thus, we swap 1 and 3. |
|
653 Vertex tmp = vertex (1); |
|
654 setVertex (1, vertex (3)); |
|
655 setVertex (3, tmp); |
|
656 } |
|
657 |
|
658 // ============================================================================= |
|
659 // |
|
660 void LDSubfile::invert() |
|
661 { |
|
662 if (document() == nullptr) |
|
663 return; |
|
664 |
|
665 // Check whether subfile is flat |
|
666 int axisSet = (1 << X) | (1 << Y) | (1 << Z); |
|
667 LDObjectList objs = fileInfo()->inlineContents (true, false); |
|
668 |
|
669 for (LDObject* obj : objs) |
|
670 { |
|
671 for (int i = 0; i < obj->numVertices(); ++i) |
|
672 { |
|
673 Vertex const& vrt = obj->vertex (i); |
|
674 |
|
675 if (axisSet & (1 << X) and vrt.x() != 0.0) |
|
676 axisSet &= ~(1 << X); |
|
677 |
|
678 if (axisSet & (1 << Y) and vrt.y() != 0.0) |
|
679 axisSet &= ~(1 << Y); |
|
680 |
|
681 if (axisSet & (1 << Z) and vrt.z() != 0.0) |
|
682 axisSet &= ~(1 << Z); |
|
683 } |
|
684 |
|
685 if (axisSet == 0) |
|
686 break; |
|
687 } |
|
688 |
|
689 if (axisSet != 0) |
|
690 { |
|
691 // Subfile has all vertices zero on one specific plane, so it is flat. |
|
692 // Let's flip it. |
|
693 Matrix matrixModifier = IdentityMatrix; |
|
694 |
|
695 if (axisSet & (1 << X)) |
|
696 matrixModifier[0] = -1; |
|
697 |
|
698 if (axisSet & (1 << Y)) |
|
699 matrixModifier[4] = -1; |
|
700 |
|
701 if (axisSet & (1 << Z)) |
|
702 matrixModifier[8] = -1; |
|
703 |
|
704 setTransform (transform() * matrixModifier); |
|
705 return; |
|
706 } |
|
707 |
|
708 // Subfile is not flat. Resort to invertnext. |
|
709 int idx = lineNumber(); |
|
710 |
|
711 if (idx > 0) |
|
712 { |
|
713 LDBFC* bfc = dynamic_cast<LDBFC*> (previous()); |
|
714 |
|
715 if (bfc and bfc->statement() == BFCStatement::InvertNext) |
|
716 { |
|
717 // This is prefixed with an invertnext, thus remove it. |
|
718 bfc->destroy(); |
|
719 return; |
|
720 } |
|
721 } |
|
722 |
|
723 // Not inverted, thus prefix it with a new invertnext. |
|
724 document()->insertObj (idx, new LDBFC (BFCStatement::InvertNext)); |
|
725 } |
|
726 |
|
727 // ============================================================================= |
|
728 // |
|
729 void LDLine::invert() |
|
730 { |
|
731 // For lines, we swap the vertices. |
|
732 Vertex tmp = vertex (0); |
|
733 setVertex (0, vertex (1)); |
|
734 setVertex (1, tmp); |
|
735 } |
|
736 |
|
737 // ============================================================================= |
|
738 // |
|
739 void LDCondLine::invert() |
|
740 { |
|
741 // I don't think that a conditional line's control points need to be swapped, do they? |
|
742 Vertex tmp = vertex (0); |
|
743 setVertex (0, vertex (1)); |
|
744 setVertex (1, tmp); |
|
745 } |
|
746 |
|
747 void LDVertex::invert() {} |
|
748 |
|
749 // ============================================================================= |
|
750 // |
|
751 LDLine* LDCondLine::toEdgeLine() |
|
752 { |
|
753 LDLine* replacement = new LDLine; |
|
754 |
|
755 for (int i = 0; i < replacement->numVertices(); ++i) |
|
756 replacement->setVertex (i, vertex (i)); |
|
757 |
|
758 replacement->setColor (color()); |
|
759 replace (replacement); |
|
760 return replacement; |
|
761 } |
|
762 |
|
763 // ============================================================================= |
|
764 // |
|
765 LDObject* LDObject::fromID (int id) |
|
766 { |
|
767 auto it = g_allObjects.find (id); |
|
768 |
|
769 if (it != g_allObjects.end()) |
|
770 return *it; |
|
771 |
|
772 return nullptr; |
|
773 } |
|
774 |
|
775 // ============================================================================= |
|
776 // |
|
777 QString LDOverlay::asText() const |
|
778 { |
|
779 return format ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6", |
|
780 fileName(), camera(), x(), y(), width(), height()); |
|
781 } |
|
782 |
|
783 void LDOverlay::invert() {} |
|
784 |
|
785 // ============================================================================= |
|
786 // |
|
787 // Hook the set accessors of certain properties to this changeProperty function. |
|
788 // It takes care of history management so we can capture low-level changes, this |
|
789 // makes history stuff work out of the box. |
|
790 // |
|
791 template<typename T> |
|
792 static void changeProperty (LDObject* obj, T* ptr, const T& val) |
|
793 { |
|
794 int idx; |
|
795 |
|
796 if (*ptr == val) |
|
797 return; |
|
798 |
|
799 if (obj->document() and (idx = obj->lineNumber()) != -1) |
|
800 { |
|
801 QString before = obj->asText(); |
|
802 *ptr = val; |
|
803 QString after = obj->asText(); |
|
804 |
|
805 if (before != after) |
|
806 { |
|
807 obj->document()->addToHistory (new EditHistory (idx, before, after)); |
|
808 g_win->R()->compileObject (obj); |
|
809 CurrentDocument()->redoVertices(); |
|
810 } |
|
811 } |
|
812 else |
|
813 { |
|
814 *ptr = val; |
|
815 } |
|
816 } |
|
817 |
|
818 // ============================================================================= |
|
819 // |
|
820 void LDObject::setColor (LDColor const& val) |
|
821 { |
|
822 changeProperty (this, &m_color, val); |
|
823 } |
|
824 |
|
825 // ============================================================================= |
|
826 // |
|
827 const Vertex& LDObject::vertex (int i) const |
|
828 { |
|
829 return m_coords[i]; |
|
830 } |
|
831 |
|
832 // ============================================================================= |
|
833 // |
|
834 void LDObject::setVertex (int i, const Vertex& vert) |
|
835 { |
|
836 changeProperty (this, &m_coords[i], vert); |
|
837 } |
|
838 |
|
839 // ============================================================================= |
|
840 // |
|
841 void LDMatrixObject::setPosition (const Vertex& a) |
|
842 { |
|
843 changeProperty (this, &m_position, a); |
|
844 } |
|
845 |
|
846 // ============================================================================= |
|
847 // |
|
848 void LDMatrixObject::setTransform (const Matrix& val) |
|
849 { |
|
850 changeProperty (this, &m_transform, val); |
|
851 } |
|
852 |
|
853 // ============================================================================= |
|
854 // |
|
855 void LDObject::select() |
|
856 { |
|
857 if (document() != null) |
|
858 document()->addToSelection (this); |
|
859 } |
|
860 |
|
861 // ============================================================================= |
|
862 // |
|
863 void LDObject::deselect() |
|
864 { |
|
865 if (isSelected() and document() != null) |
|
866 { |
|
867 document()->removeFromSelection (this); |
|
868 |
|
869 // If this object is inverted with INVERTNEXT, deselect the INVERTNEXT as well. |
|
870 LDBFC* invertnext; |
|
871 |
|
872 if (previousIsInvertnext (invertnext)) |
|
873 invertnext->deselect(); |
|
874 } |
|
875 } |
|
876 |
|
877 // ============================================================================= |
|
878 // |
|
879 QString PreferredLicenseText() |
|
880 { |
|
881 return (cfg::UseCALicense ? CALicenseText : ""); |
|
882 } |
|
883 |
|
884 // ============================================================================= |
|
885 // |
|
886 LDObject* LDObject::createCopy() const |
|
887 { |
|
888 LDObject* copy = ParseLine (asText()); |
|
889 return copy; |
|
890 } |
|
891 |
|
892 // ============================================================================= |
|
893 // |
|
894 void LDSubfile::setFileInfo (LDDocument* const& a) |
|
895 { |
|
896 changeProperty (this, &m_fileInfo, a); |
|
897 |
|
898 // If it's an immediate subfile reference (i.e. this subfile belongs in an |
|
899 // explicit file), we need to pre-compile the GL polygons for the document |
|
900 // if they don't exist already. |
|
901 if (a != null and |
|
902 a->isImplicit() == false and |
|
903 a->polygonData().isEmpty()) |
|
904 { |
|
905 a->initializeCachedData(); |
|
906 } |
|
907 }; |
|
908 |
|
909 void LDObject::getVertices (QVector<Vertex>& verts) const |
|
910 { |
|
911 for (int i = 0; i < numVertices(); ++i) |
|
912 verts << vertex (i); |
|
913 } |
|
914 |
|
915 void LDSubfile::getVertices (QVector<Vertex>& verts) const |
|
916 { |
|
917 verts << fileInfo()->inlineVertices(); |
|
918 } |
|
919 |
|
920 void LDVertex::getVertices (QVector<Vertex>& verts) const |
|
921 { |
|
922 verts.append (pos); |
|
923 } |
|