Sat, 16 Mar 2013 16:59:16 +0200
Added split-quads-to-triangles function
gui.cpp | file | annotate | diff | comparison | revisions | |
gui.h | file | annotate | diff | comparison | revisions | |
icons/quad-split.png | file | annotate | diff | comparison | revisions | |
ldtypes.cpp | file | annotate | diff | comparison | revisions | |
ldtypes.h | file | annotate | diff | comparison | revisions | |
pointer.cpp | file | annotate | diff | comparison | revisions | |
pointer.h | file | annotate | diff | comparison | revisions |
--- a/gui.cpp Sat Mar 16 15:45:20 2013 +0200 +++ b/gui.cpp Sat Mar 16 16:59:16 2013 +0200 @@ -63,6 +63,7 @@ MAKE_ACTION (about, sAboutText, "about", "Shows information about " APPNAME_DISPLAY ".") MAKE_ACTION (aboutQt, "About Qt", "aboutQt", "Shows information about Qt.") + MAKE_ACTION (splitQuads, "Split Quads", "quad-split", "Split quads into triangles.") MAKE_ACTION (setContents, "Set Contents", "set-contents", "Set the raw code of this object.") MAKE_ACTION (newSubfile, "New Subfile", "add-subfile", "Creates a new subfile reference.") @@ -130,6 +131,7 @@ qEditMenu->addAction (qAct_copy); // Copy qEditMenu->addAction (qAct_paste); // Paste qEditMenu->addSeparator (); // ----- + qEditMenu->addAction (qAct_splitQuads); // Split Quads qEditMenu->addAction (qAct_setContents); // Set Contents // Help menu @@ -161,6 +163,7 @@ qEditToolBar->addAction (qAct_cut); qEditToolBar->addAction (qAct_copy); qEditToolBar->addAction (qAct_paste); + qEditToolBar->addAction (qAct_splitQuads); qEditToolBar->addAction (qAct_setContents); addToolBar (qEditToolBar); } @@ -276,6 +279,38 @@ } +void LDForgeWindow::slot_splitQuads () { + if (qObjList->selectedItems().size() == 0) + return; + + const QList<QTreeWidgetItem*> qaItems = qObjList->selectedItems(); + + for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { + LDObject* obj = g_CurrentFile->objects[i]; + + // Don't even consider non-quads + if (obj->getType() != OBJ_Quad) + continue; + + bool bIsSelected = false; + + for (long j = 0; j < qaItems.size(); ++j) { + if (qaItems[j] == obj->qObjListEntry) { + bIsSelected = true; + break; + } + } + + if (!bIsSelected) + continue; // Was not selected + + static_cast<LDQuad*> (obj)->splitToTriangles (); + } + + R->hardRefresh (); + buildObjList (); +} + void LDForgeWindow::slot_setContents () { if (qObjList->selectedItems().size() != 1) return;
--- a/gui.h Sat Mar 16 15:45:20 2013 +0200 +++ b/gui.h Sat Mar 16 16:59:16 2013 +0200 @@ -34,7 +34,7 @@ QAction* qAct_cut, *qAct_copy, *qAct_paste; QAction* qAct_newSubfile, *qAct_newLine, *qAct_newTriangle, *qAct_newQuad; QAction* qAct_newCondLine, *qAct_newComment, *qAct_newVector, *qAct_newVertex; - QAction* qAct_setContents; + QAction* qAct_splitQuads, *qAct_setContents; QAction* qAct_about, *qAct_aboutQt; LDForgeWindow (); @@ -67,6 +67,7 @@ void slot_newVector (); void slot_newVertex (); + void slot_splitQuads (); void slot_setContents (); void slot_cut ();
--- a/ldtypes.cpp Sat Mar 16 15:45:20 2013 +0200 +++ b/ldtypes.cpp Sat Mar 16 16:59:16 2013 +0200 @@ -2,6 +2,7 @@ #include "ldtypes.h" #include "io.h" #include "misc.h" +#include "pointer.h" const char* g_saObjTypeNames[] = { "unidentified", @@ -157,4 +158,50 @@ str LDEmpty::getContents () { return str (); +} + +void LDQuad::splitToTriangles () { + // Find the index of this quad + ulong ulIndex; + for (ulIndex = 0; ulIndex < g_CurrentFile->objects.size(); ++ulIndex) + if (g_CurrentFile->objects[ulIndex].ptr == this) + break; + + if (ulIndex >= g_CurrentFile->objects.size()) { + // couldn't find it? + logf (LOG_Error, "LDQuad::splitToTriangles: Couldn't find quad %p in " + "current object list!!\n", this); + return; + } + + // Create the two triangles based on this quadrilateral: + // 0---3 0---3 3 + // | | | / /| + // | | = | / / | + // | | |/ / | + // 1---2 1 1---2 + LDTriangle* tri1 = new LDTriangle; + tri1->vaCoords[0] = vaCoords[0]; + tri1->vaCoords[1] = vaCoords[1]; + tri1->vaCoords[2] = vaCoords[3]; + + LDTriangle* tri2 = new LDTriangle; + tri2->vaCoords[0] = vaCoords[1]; + tri2->vaCoords[1] = vaCoords[2]; + tri2->vaCoords[2] = vaCoords[3]; + + // The triangles also inherit the quad's color + tri1->dColor = tri2->dColor = dColor; + + // Replace the quad with the first triangle + objPointer::replacePointers (this, tri1); + + // Add the second triangle after the first one. + objPointer ptr (tri2); + ptr.serialize (); + g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + ulIndex, ptr); + + // Delete this quad now, it has been split. `delete this` isn't exactly + // safe in my experience so we'll tell objPointer to delete the quad. + objPointer::deleteObj (this); } \ No newline at end of file
--- a/ldtypes.h Sat Mar 16 15:45:20 2013 +0200 +++ b/ldtypes.h Sat Mar 16 16:59:16 2013 +0200 @@ -181,6 +181,9 @@ short dColor; vertex vaCoords[4]; + + // Split this quad into two triangles + void splitToTriangles (); }; // =============================================================================
--- a/pointer.cpp Sat Mar 16 15:45:20 2013 +0200 +++ b/pointer.cpp Sat Mar 16 16:59:16 2013 +0200 @@ -2,28 +2,12 @@ vector<objPointer*> g_pObjectPointers; -// #define POINTER_DEBUG - -#ifdef POINTER_DEBUG -#include <stdio.h> -#define DEBUGMESSAGE printf -#else -#define DEBUGMESSAGE -#endif - void objPointer::replacePointers (LDObject* old, LDObject* repl) { - DEBUGMESSAGE ("replacing %p with %p\n", old, repl); - DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size()); - for (ulong i = 0; i < g_pObjectPointers.size(); ++i) { objPointer* ptrptr = g_pObjectPointers[i]; - DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old); - if ((*ptrptr).ptr == old) { - DEBUGMESSAGE ("\treplacing... %p -> %p\n", (*ptrptr).ptr, repl); + + if ((*ptrptr).ptr == old) (*ptrptr).ptr = repl; - - DEBUGMESSAGE ("%lu. (%p <-> %p)\n", i, (*ptrptr).ptr, old); - } } } @@ -40,15 +24,16 @@ } void objPointer::serialize () { - DEBUGMESSAGE ("Adding %p to pointers (ptr = %p)... ", this, ptr); g_pObjectPointers.push_back (this); - DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size()); } void objPointer::unSerialize () { - DEBUGMESSAGE ("Removing %p from pointers... ", this); for (ulong i = 0; i < g_pObjectPointers.size(); ++i) if (g_pObjectPointers[i] == this) g_pObjectPointers.erase (g_pObjectPointers.begin() + i); - DEBUGMESSAGE ("%u objects in pointers\n", g_pObjectPointers.size()); +} + +void objPointer::deleteObj (LDObject* obj) { + replacePointers (obj, nullptr); + delete obj; } \ No newline at end of file