Use unique IDs instead of file indices in picking lists, this is much less prone to error

Thu, 27 Jun 2013 14:55:32 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 27 Jun 2013 14:55:32 +0300
changeset 307
c731a22899a3
parent 306
fef00a6cbff0
child 308
4e2425bd4dc7

Use unique IDs instead of file indices in picking lists, this is much less prone to error

src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/ldtypes.h file | annotate | diff | comparison | revisions
--- a/src/gldraw.cpp	Thu Jun 27 14:12:12 2013 +0300
+++ b/src/gldraw.cpp	Thu Jun 27 14:55:32 2013 +0300
@@ -220,13 +220,11 @@
 		return;
 	
 	if (list == GL::PickList) {
-		// Make the color by the object's index color if we're picking, so we can
-		// make the index from the color we get from the picking results. Be sure
-		// to use the top level parent's index since inlinees don't have an index.
-		long i = obj->topLevelParent ()->getIndex (file ());
-		
-		// We should have the index now.
-		assert (i != -1);
+		// Make the color by the object's ID if we're picking, so we can make the
+		// ID again from the color we get from the picking results. Be sure to use
+		// the top level parent's index since we want a subfile's children point
+		// to the subfile itself.
+		long i = obj->topLevelParent()->id();
 		
 		// Calculate a color based from this index. This method caters for
 		// 16777216 objects. I don't think that'll be exceeded anytime soon. :)
@@ -1123,8 +1121,8 @@
 	
 	// Go through each pixel read and add them to the selection.
 	for (long i = 0; i < numpixels; ++i) {
-		uint32 idx =
-			(*(pixelptr) * 0x10000) +
+		long idx =
+			(*(pixelptr + 0) * 0x10000) +
 			(*(pixelptr + 1) * 0x00100) +
 			(*(pixelptr + 2) * 0x00001);
 		pixelptr += 4;
@@ -1132,7 +1130,7 @@
 		if (idx == 0xFFFFFF)
 			continue; // White is background; skip
 		
-		LDObject* obj = file ()->obj (idx);
+		LDObject* obj = LDObject::fromID( idx );
 		
 		// If this is an additive single pick and the object is currently selected,
 		// we remove it from selection instead.
--- a/src/gui.cpp	Thu Jun 27 14:12:12 2013 +0300
+++ b/src/gui.cpp	Thu Jun 27 14:55:32 2013 +0300
@@ -587,7 +587,7 @@
 		delete obj;
 	}
 	
-	fullRefresh ();
+	refresh ();
 }
 
 // =============================================================================
--- a/src/ldtypes.cpp	Thu Jun 27 14:12:12 2013 +0300
+++ b/src/ldtypes.cpp	Thu Jun 27 14:55:32 2013 +0300
@@ -76,6 +76,8 @@
 			id = obj->id() + 1;
 	
 	setID( id );
+	
+	g_LDObjects << this;
 }
 
 LDGibberish::LDGibberish () {}
@@ -215,12 +217,17 @@
 
 LDObject::~LDObject () {
 	// Remove this object from the selection array if it is there.
-	for (ulong i = 0; i < g_win->sel ().size(); ++i)
+	for (ulong i = 0; i < g_win->sel().size(); ++i)
 		if (g_win->sel ()[i] == this)
 			g_win->sel ().erase (i);
 	
 	// Delete the GL lists
-	GL::deleteLists (this);
+	GL::deleteLists( this );
+	
+	// Remove this object from the list of LDObjects
+	ulong pos = g_LDObjects.find( this );
+	if( pos < g_LDObjects.size())
+		g_LDObjects.erase( pos );
 }
 
 // =============================================================================
@@ -818,6 +825,15 @@
 	return repl;
 }
 
+LDObject* LDObject::fromID (int id) {
+	for( LDObject* obj : g_LDObjects )
+		if( obj->id() == id )
+			return obj;
+	
+	return null;
+}
+
+
 // =============================================================================
 template<class T> void changeProperty (LDObject* obj, T* ptr, const T& val) {
 	long idx;
--- a/src/ldtypes.h	Thu Jun 27 14:12:12 2013 +0300
+++ b/src/ldtypes.h	Thu Jun 27 14:55:32 2013 +0300
@@ -158,6 +158,7 @@
 	
 	static void moveObjects (vector<LDObject*> objs, const bool bUp);
 	static str objectListContents (const vector<LDObject*>& objs);
+	static LDObject* fromID( int id );
 	
 	// Object list entry for this object
 	QListWidgetItem* qObjListEntry;

mercurial