Added support for BFC types

Mon, 08 Apr 2013 14:45:28 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 08 Apr 2013 14:45:28 +0300
changeset 73
d20867ac03cc
parent 72
5755c02d89f0
child 74
1affec15e572

Added support for BFC types

file.cpp file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
--- a/file.cpp	Wed Mar 27 11:24:16 2013 +0200
+++ b/file.cpp	Mon Apr 08 14:45:28 2013 +0300
@@ -279,12 +279,31 @@
 	if (~tokens[0] != 1)
 		return new LDGibberish (zLine, "Illogical line code");
 	
-	char const c = tokens[0][0];
+	const char c = tokens[0][0];
 	switch (c - '0') {
 	case 0:
 		{
 			// Comment
-			str zComment = zLine.substr (2, -1);
+			str zComment;
+			for (uint i = 1; i < tokens.size(); ++i) {
+				zComment += tokens[i];
+				
+				if (i != tokens.size() - 1)
+					zComment += ' ';
+			}
+			
+			// Handle BFC statements
+			if (tokens.size() > 2 && tokens[1] == "BFC") {
+				for (short i = 0; i < NUM_BFCStatements; ++i)
+					if (zComment == str::mkfmt ("BFC %s", LDBFC::saStatements [i]))
+						return new LDBFC (i);
+				
+				// MLCAD is notorious for stuffing these statements in parts it
+				// creates. The above block only handles valid statements, so we
+				// need to handle MLCAD-style invertnext separately.
+				if (zComment == "BFC CERTIFY INVERTNEXT")
+					return new LDBFC (BFC_InvertNext);
+			}
 			
 			if (tokens.size() > 2 && tokens[1] == "!LDFORGE") {
 				// Handle LDForge-specific types, they're embedded into comments
--- a/gui.cpp	Wed Mar 27 11:24:16 2013 +0200
+++ b/gui.cpp	Mon Apr 08 14:45:28 2013 +0300
@@ -445,7 +445,25 @@
 		if (obj->getType() != OBJ_Quad)
 			continue;
 		
-		static_cast<LDQuad*> (obj)->splitToTriangles ();
+		// 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", this);
+			return;
+		}
+		
+		std::vector<LDTriangle*> triangles = static_cast<LDQuad*> (obj)->splitToTriangles ();
+		
+		// Replace the quad with the first triangle and add the second triangle
+		// after the first one.
+		g_CurrentFile->objects[lIndex] = triangles[0];
+		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + lIndex + 1, triangles[1]);
+		
+		// Delete this quad now, it has been split.
+		delete this;
 	}
 	
 	refresh ();
@@ -644,6 +662,13 @@
 			}
 			break;
 		
+		case OBJ_BFC:
+			{
+				LDBFC* bfc = static_cast<LDBFC*> (obj);
+				zText = LDBFC::saStatements[bfc->dStatement];
+			}
+			break;
+		
 		default:
 			zText = g_saObjTypeNames[obj->getType ()];
 			break;
@@ -655,7 +680,7 @@
 		
 		// Color gibberish red
 		if (obj->getType() == OBJ_Gibberish) {
-			item->setBackgroundColor (0, "#AA0000");
+			item->setBackground (0, QColor ("#AA0000"));
 			item->setForeground (0, QColor ("#FFAA00"));
 		} else if (lv_colorize &&
 			obj->dColor != -1 &&
--- a/ldtypes.cpp	Wed Mar 27 11:24:16 2013 +0200
+++ b/ldtypes.cpp	Mon Apr 08 14:45:28 2013 +0300
@@ -31,6 +31,7 @@
 	"triangle",
 	"quadrilateral",
 	"condline",
+	"bfc",
 	"vertex",
 };
 
@@ -44,7 +45,8 @@
 	"triangle",
 	"quad",
 	"condline",
-	"vertex"
+	"bfc",
+	"vertex",
 };
 
 // =============================================================================
@@ -106,6 +108,10 @@
 	commonInit ();
 }
 
+LDBFC::LDBFC () {
+	commonInit ();
+}
+
 // =============================================================================
 str LDComment::getContents () {
 	return str::mkfmt ("0 %s", zText.chars ());
@@ -173,17 +179,23 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void LDQuad::splitToTriangles () {
-	// Find the index of this quad
-	long lIndex = getIndex (g_CurrentFile);
-	
-	if (lIndex == -1) {
-		// couldn't find it?
-		logf (LOG_Error, "LDQuad::splitToTriangles: Couldn't find quad %p in "
-			"current object list!!\n", this);
-		return;
-	}
-	
+const char* LDBFC::saStatements[] = {
+	"CCW",
+	"CW",
+	"CERTIFY CCW",
+	"CERTIFY CW",
+	"NOCERTIFY",
+	"INVERTNEXT",
+};
+
+str LDBFC::getContents () {
+	return str::mkfmt ("0 BFC %s", LDBFC::saStatements[dStatement]);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+vector<LDTriangle*> LDQuad::splitToTriangles () {
 	// Create the two triangles based on this quadrilateral:
 	// 0---3     0---3    3
 	// |   |     |  /    /|
@@ -203,13 +215,10 @@
 	// The triangles also inherit the quad's color
 	tri1->dColor = tri2->dColor = dColor;
 	
-	// Replace the quad with the first triangle and add the second triangle
-	// after the first one.
-	g_CurrentFile->objects[lIndex] = tri1;
-	g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + lIndex + 1, tri2);
-	
-	// Delete this quad now, it has been split.
-	delete this;
+	vector<LDTriangle*> triangles;
+	triangles.push_back (tri1);
+	triangles.push_back (tri2);
+	return triangles;
 }
 
 // =============================================================================
@@ -243,6 +252,7 @@
 LDSubfile::~LDSubfile () {}
 LDTriangle::~LDTriangle () {}
 LDVertex::~LDVertex () {}
+LDBFC::~LDBFC () {}
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--- a/ldtypes.h	Wed Mar 27 11:24:16 2013 +0200
+++ b/ldtypes.h	Mon Apr 08 14:45:28 2013 +0300
@@ -50,6 +50,7 @@
 	OBJ_Triangle,		// Object represents a triangle (LDTriangle, code: 3)
 	OBJ_Quad,			// Object represents a quadrilateral (LDQuad, code: 4)
 	OBJ_CondLine,		// Object represents a conditional line (LDCondLine, code: 5)
+	OBJ_BFC,			// Object represents a BFC statement
 	OBJ_Vertex			// Object is a vertex, LDForge extension object (LDVertex)
 };
 
@@ -141,6 +142,37 @@
 };
 
 // =============================================================================
+// LDBFC
+// 
+// Represents a 0 BFC statement in the LDraw code. eStatement contains the type
+// of this statement.
+// =============================================================================
+class LDBFC : public LDComment {
+public:
+	IMPLEMENT_LDTYPE (BFC)
+	LDBFC (const int dType) : dStatement (dType) {}
+	
+	// Statement strings
+	static const char* saStatements[];
+	
+	static str statementString (short dValue);
+	short dStatement;
+};
+
+
+// -----------------------------------------------------------------------------
+// Enumerator for LDBFC's dStatement
+enum LDBFCType_e {
+	BFC_CertifyCCW,
+	BFC_CCW,
+	BFC_CertifyCW,
+	BFC_CW,
+	BFC_NoCertify,	// Winding becomes disabled (0 BFC NOCERTIFY)
+	BFC_InvertNext,	// Winding is inverted for next object (0 BFC INVERTNEXT)
+	NUM_BFCStatements
+};
+
+// =============================================================================
 // LDSubfile
 //
 // Represents a single code-1 subfile reference.
@@ -219,7 +251,7 @@
 	vertex vaCoords[4];
 	
 	// Split this quad into two triangles
-	void splitToTriangles ();
+	vector<LDTriangle*> splitToTriangles ();
 };
 
 // =============================================================================

mercurial