Added history handling for quad splitting.

Wed, 10 Apr 2013 13:24:35 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 10 Apr 2013 13:24:35 +0300
changeset 93
92682e6369e9
parent 92
586d294ca83f
child 94
a9e67f6e610e

Added history handling for quad splitting.

gui_editactions.cpp file | annotate | diff | comparison | revisions
history.cpp file | annotate | diff | comparison | revisions
history.h file | annotate | diff | comparison | revisions
--- 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<LDObject*> objs = g_ForgeWindow->getSelectedObjects ();
 	
+	vector<ulong> ulaIndices;
+	vector<LDQuad*> 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<LDQuad*> (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<LDTriangle*> triangles = static_cast<LDQuad*> (obj)->splitToTriangles ();
 		
@@ -176,6 +184,7 @@
 		delete obj;
 	}
 	
+	History::addEntry (new QuadSplitHistory (ulaIndices, paCopies));
 	g_ForgeWindow->refresh ();
 }
 
--- 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<LDTriangle*> (g_CurrentFile->objects[idx]),
+			*tri2 = static_cast<LDTriangle*> (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<LDQuad*> (g_CurrentFile->objects[idx]);
+		std::vector<LDTriangle*> 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
--- 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<ulong> ulaIndices;
+	std::vector<LDQuad*> paQuads;
+	
+	QuadSplitHistory (std::vector<ulong> ulaIndices, std::vector<LDQuad*> paQuads) :
+		ulaIndices (ulaIndices), paQuads (paQuads) {}
+};
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 namespace History {
 	extern std::vector<HistoryEntry*> entries;
 	

mercurial