# HG changeset patch # User Santeri Piippo # Date 1365589475 -10800 # Node ID 92682e6369e9793d6b1e1e0dc314688ebd968831 # Parent 586d294ca83f2a9f04848b84a67e9f71fd698c63 Added history handling for quad splitting. diff -r 586d294ca83f -r 92682e6369e9 gui_editactions.cpp --- a/gui_editactions.cpp Wed Apr 10 04:34:19 2013 +0300 +++ b/gui_editactions.cpp Wed Apr 10 13:24:35 2013 +0300 @@ -151,6 +151,18 @@ ACTION (splitQuads, "Split Quads", "quad-split", "Split quads into triangles.", (0)) { vector objs = g_ForgeWindow->getSelectedObjects (); + vector ulaIndices; + vector paCopies; + + // Store stuff first for history archival + for (LDObject* obj : objs) { + if (obj->getType() != OBJ_Quad) + continue; + + ulaIndices.push_back (obj->getIndex (g_CurrentFile)); + paCopies.push_back (static_cast (obj)->clone ()); + } + for (LDObject* obj : objs) { if (obj->getType() != OBJ_Quad) continue; @@ -158,12 +170,8 @@ // Find the index of this quad long lIndex = obj->getIndex (g_CurrentFile); - if (lIndex == -1) { - // couldn't find it? - logf (LOG_Error, "Couldn't find quad %p in " - "current object list!!\n", obj); + if (lIndex == -1) return; - } std::vector triangles = static_cast (obj)->splitToTriangles (); @@ -176,6 +184,7 @@ delete obj; } + History::addEntry (new QuadSplitHistory (ulaIndices, paCopies)); g_ForgeWindow->refresh (); } diff -r 586d294ca83f -r 92682e6369e9 history.cpp --- a/history.cpp Wed Apr 10 04:34:19 2013 +0300 +++ b/history.cpp Wed Apr 10 13:24:35 2013 +0300 @@ -199,4 +199,49 @@ } g_ForgeWindow->refresh (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +QuadSplitHistory::~QuadSplitHistory () { + for (LDQuad* pQuad : paQuads) + delete pQuad; +} + +void QuadSplitHistory::undo () { + for (ulong i = 0; i < paQuads.size(); ++i) { + // The quad was replaced by the first triangle and a second one was + // added after it. Thus, we remove the second one here and replace + // the first with a copy of the quad. + ulong idx = ulaIndices[i]; + printf ("%lu (%lu)\n", i, idx); + + LDTriangle* tri1 = static_cast (g_CurrentFile->objects[idx]), + *tri2 = static_cast (g_CurrentFile->objects[idx + 1]); + LDQuad* pCopy = paQuads[i]->clone (); + + tri1->replace (pCopy); + g_CurrentFile->forgetObject (tri2); + delete tri2; + } + + g_ForgeWindow->refresh (); +} + +void QuadSplitHistory::redo () { + for (long i = paQuads.size() - 1; i >= 0; --i) { + ulong idx = ulaIndices[i]; + + printf ("redo: %ld (%lu)\n", i, idx); + + LDQuad* pQuad = static_cast (g_CurrentFile->objects[idx]); + std::vector paTriangles = pQuad->splitToTriangles (); + + g_CurrentFile->objects[idx] = paTriangles[0]; + g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx + 1, paTriangles[1]); + delete pQuad; + } + + g_ForgeWindow->refresh (); } \ No newline at end of file diff -r 586d294ca83f -r 92682e6369e9 history.h --- a/history.h Wed Apr 10 04:34:19 2013 +0300 +++ b/history.h Wed Apr 10 13:24:35 2013 +0300 @@ -20,6 +20,7 @@ #define HISTORY_H #include "common.h" +#include "ldtypes.h" #define IMPLEMENT_HISTORY_TYPE(N) \ virtual ~N##History (); \ @@ -112,6 +113,20 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +class QuadSplitHistory : public HistoryEntry { +public: + IMPLEMENT_HISTORY_TYPE (QuadSplit) + + std::vector ulaIndices; + std::vector paQuads; + + QuadSplitHistory (std::vector ulaIndices, std::vector paQuads) : + ulaIndices (ulaIndices), paQuads (paQuads) {} +}; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= namespace History { extern std::vector entries;