Made overlay data be contained in the part files, still wip

Tue, 02 Jul 2013 21:39:47 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 02 Jul 2013 21:39:47 +0300
changeset 316
b4fefda58f43
parent 315
cb0c23f35e2b
child 317
e41ab239ed28

Made overlay data be contained in the part files, still wip

src/common.h file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gldraw.h 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/common.h	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/common.h	Tue Jul 02 21:39:47 2013 +0300
@@ -58,6 +58,8 @@
 # define devf(...)
 #endif // RELEASE
 
+#define elif else if
+
 extern File g_file_stdout;
 extern File g_file_stderr;
 
--- a/src/file.cpp	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/file.cpp	Tue Jul 02 21:39:47 2013 +0300
@@ -672,6 +672,20 @@
 					
 					return obj;
 				}
+				elif( tokens[2] == "OVERLAY" )
+				{
+					CHECK_TOKEN_COUNT( 8 );
+					CHECK_TOKEN_NUMBERS( 4, 7 )
+					
+					LDOverlay* obj = new LDOverlay;
+					obj->setFilename( tokens[2] );
+					obj->setCamera( tokens[3].toLong() );
+					obj->setX( tokens[4].toLong() );
+					obj->setY( tokens[5].toLong() );
+					obj->setWidth( tokens[6].toLong() );
+					obj->setHeight( tokens[7].toLong() );
+					return obj;
+				}
 			}
 			
 			// Just a regular comment:
@@ -699,7 +713,7 @@
 			
 			LDSubfile* obj = new LDSubfile;
 			obj->setColor( tokens[1].toLong() );
-			obj->setPosition( parseVertex(tokens, 2 )); // 2 - 4
+			obj->setPosition( parseVertex( tokens, 2 )); // 2 - 4
 			
 			matrix transform;
 			for( short i = 0; i < 9; ++i )
--- a/src/gldraw.cpp	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/gldraw.cpp	Tue Jul 02 21:39:47 2013 +0300
@@ -1405,6 +1405,8 @@
 		uint32 pixel = img->pixel (i, j);
 		img->setPixel (i, j, 0x80000000 | (pixel & 0x00FFFFFF));
 	}
+	
+	updateOverlayObjects();
 }
 
 void GLRenderer::clearOverlay () {
@@ -1414,6 +1416,8 @@
 	overlayMeta& info = m_overlays[camera ()];
 	delete info.img;
 	info.img = null;
+	
+	updateOverlayObjects();
 }
 
 void GLRenderer::setDepthValue (double depth) {
@@ -1563,4 +1567,126 @@
 	LDObject* obj = g_win->sel ()[0];
 	AddObjectDialog::staticDialog (obj->getType (), obj);
 	ev->accept ();
+}
+
+LDOverlay* GLRenderer::findOverlayObject( GLRenderer::Camera cam )
+{
+	LDOverlay* ovlobj = null;
+	
+	for( LDObject* obj : *file() )
+	{
+		if( obj->getType() == LDObject::Overlay && static_cast<LDOverlay*>( obj )->camera() == cam )
+		{
+			ovlobj = static_cast<LDOverlay*>( obj );
+			break;
+		}
+	}
+	
+	return ovlobj;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+// Read in overlays from the current file and update overlay info accordingly.
+// =============================================================================
+void GLRenderer::overlaysFromObjects()
+{
+	for( Camera cam : g_Cameras )
+	{
+		if( cam == Free )
+			continue;
+		
+		overlayMeta& meta = m_overlays[cam];
+		LDOverlay* ovlobj = findOverlayObject( cam );
+		
+		if( !ovlobj && meta.img )
+		{
+			delete meta.img;
+			meta.img = null;
+		}
+		elif( ovlobj && !meta.img )
+		{
+			
+		}
+	}
+}
+
+// =============================================================================
+void GLRenderer::updateOverlayObjects()
+{
+	for( Camera cam : g_Cameras )
+	{
+		if( cam == Free )
+			continue;
+		
+		overlayMeta& meta = m_overlays[cam];
+		LDOverlay* ovlobj = findOverlayObject( cam );
+		
+		if( !meta.img && ovlobj )
+		{
+			// If this is the last overlay image, we need to remove the empty space after it as well.
+			LDObject* nextobj = ovlobj->next();
+			if( nextobj && nextobj->getType() == LDObject::Empty )
+			{
+				m_file->forgetObject( nextobj );
+				delete nextobj;
+			}
+			
+			// If the overlay object was there and the overlay itself is
+			// not, remove the object.
+			m_file->forgetObject( ovlobj );
+			delete ovlobj;
+		}
+		else if( meta.img && !ovlobj )
+		{
+			// Inverse case: image is there but the overlay object is
+			// not, thus create the object.
+			ovlobj = new LDOverlay;
+			
+			// Find a suitable position to place this object. We want to place
+			// this into the header, which is everything up to the first scemantic
+			// object. If we find another overlay object, place this object after
+			// the last one found. Otherwise, place it before the first schemantic
+			// object and put an empty object after it (though don't do this if
+			// there was no schemantic elements at all)
+			ulong i, lastOverlay = -1u;
+			bool found = false;
+			
+			for( i = 0; i < file()->numObjs(); ++i )
+			{
+				LDObject* obj = file()->obj( i );
+				if( obj->isScemantic() )
+				{
+					found = true;
+					break;
+				}
+				
+				if( obj->getType() == LDObject::Overlay )
+					lastOverlay = i;
+			}
+			
+			if( lastOverlay != -1u )
+				file()->insertObj( lastOverlay + 1, ovlobj );
+			else
+			{
+				file()->insertObj( i, ovlobj );
+				
+				if( found )
+					file()->insertObj( i + 1, new LDEmpty );
+			}
+		}
+		
+		if( meta.img && ovlobj )
+		{
+			ovlobj->setCamera( cam );
+			ovlobj->setFilename( meta.fname );
+			ovlobj->setX( meta.ox );
+			ovlobj->setY( meta.oy );
+			ovlobj->setWidth( meta.lw );
+			ovlobj->setHeight( meta.lh );
+		}
+	}
+	
+	if( g_win->R() == this )
+		g_win->refresh();
 }
\ No newline at end of file
--- a/src/gldraw.h	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/gldraw.h	Tue Jul 02 21:39:47 2013 +0300
@@ -45,6 +45,9 @@
 	QImage* img;
 };
 
+// Alias for short namespaces
+typedef GLRenderer GL;
+
 // =============================================================================
 // GLRenderer
 // 
@@ -81,6 +84,7 @@
 	overlayMeta&   getOverlay          (int newcam);
 	void           hardRefresh         ();
 	void           initGLData          ();
+	void           overlaysFromObjects ();
 	void           refresh             ();
 	void           resetAngles         ();
 	uchar*         screencap           (ushort& w, ushort& h);
@@ -88,6 +92,7 @@
 	void           setCamera           (const Camera cam);
 	void           setDepthValue       (double depth);
 	void           setupOverlay        ();
+	void           updateOverlayObjects();
 	void           zoomNotch           (bool inward);
 	void           zoomToFit           ();
 	
@@ -146,7 +151,7 @@
 	void           compileVertex        (const vertex& vrt);                     // Compile a single vertex to a list
 	vertex         coordconv2_3         (const QPoint& pos2d, bool snap) const;  // Convert a 2D point to a 3D point
 	QPoint         coordconv3_2         (const vertex& pos3d) const;             // Convert a 3D point to a 2D point
-	void           buildKnownVertBlips  ();
+	LDOverlay*     findOverlayObject    ( GL::Camera cam );
 	void           updateRectVerts      ();
 	void           pick                 (uint mouseX, uint mouseY);              // Perform object selection
 	void           setObjectColor       (LDObject* obj, const ListType list);    // Set the color to an object list
@@ -155,9 +160,6 @@
 	void           slot_toolTipTimer    ();
 };
 
-// Alias for short namespaces
-typedef GLRenderer GL;
-
 static const GLRenderer::ListType g_glListTypes[] = {
 	GL::NormalList,
 	GL::PickList,
--- a/src/gui.cpp	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/gui.cpp	Tue Jul 02 21:39:47 2013 +0300
@@ -661,6 +661,14 @@
 			descr = LDBFC::statements[static_cast<LDBFC*> (obj)->type];
 		break;
 		
+		case LDObject::Overlay:
+			{
+				LDOverlay* ovl = static_cast<LDOverlay*>( obj );
+				descr = fmt( "[%1] %2 (%3, %4), %5 x %6", g_CameraNames[ovl->camera()],
+					basename( ovl->filename() ), ovl->x(), ovl->y(), ovl->width(), ovl->height() );
+			}
+			break;
+		
 		default:
 			descr = g_saObjTypeNames[obj->getType ()];
 			break;
--- a/src/ldtypes.cpp	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/ldtypes.cpp	Tue Jul 02 21:39:47 2013 +0300
@@ -32,6 +32,7 @@
 	"condline",
 	"vertex",
 	"bfc",
+	"overlay",
 	"comment",
 	"unknown",
 	"empty",
@@ -47,6 +48,7 @@
 	"condline",
 	"vertex",
 	"bfc",
+	"overlay",
 	"comment",
 	"error",
 	"empty",
@@ -513,6 +515,7 @@
 	CHECK_FOR_OBJ (BFC)
 	CHECK_FOR_OBJ (Gibberish)
 	CHECK_FOR_OBJ (Vertex)
+	CHECK_FOR_OBJ (Overlay)
 	return null;
 }
 
@@ -609,6 +612,15 @@
 	return null;
 }
 
+// =============================================================================
+str LDOverlay::raw()
+{
+	return fmt( "0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6",
+		filename(), camera(), x(), y(), width(), height() );
+}
+
+void LDOverlay::move( vertex vect ) { Q_UNUSED( vect ) }
+void LDOverlay::invert() {}
 
 // =============================================================================
 template<class T> void changeProperty (LDObject* obj, T* ptr, const T& val) {
--- a/src/ldtypes.h	Tue Jul 02 01:42:43 2013 +0300
+++ b/src/ldtypes.h	Tue Jul 02 21:39:47 2013 +0300
@@ -92,18 +92,19 @@
 public:
 	// Object type codes. Codes are sorted in order of significance.
 	enum Type {
-		Subfile,		// Object represents a sub-file reference
-		Quad,			// Object represents a quadrilateral
-		Triangle,		// Object represents a triangle
-		Line,			// Object represents a line
-		CondLine,		// Object represents a conditional line
-		Vertex,			// Object is a vertex, LDForge extension object
-		BFC,			// Object represents a BFC statement
-		Comment,		// Object represents a comment
-		Gibberish,		// Object is the result of failed parsing
-		Empty,			// Object represents an empty line
-		Unidentified,	// Object is an uninitialized (SHOULD NEVER HAPPEN)
-		NumTypes		// Amount of object types
+		Subfile,        // Object represents a sub-file reference
+		Quad,           // Object represents a quadrilateral
+		Triangle,       // Object represents a triangle
+		Line,           // Object represents a line
+		CondLine,       // Object represents a conditional line
+		Vertex,         // Object is a vertex, LDForge extension object
+		BFC,            // Object represents a BFC statement
+		Overlay,        // Object contains meta-info about an overlay image.
+		Comment,        // Object represents a comment
+		Gibberish,      // Object is the result of failed parsing
+		Empty,          // Object represents an empty line
+		Unidentified,   // Object is an uninitialized (SHOULD NEVER HAPPEN)
+		NumTypes        // Amount of object types
 	};
 	
 	LDObject ();
@@ -389,6 +390,28 @@
 };
 
 // =============================================================================
+// LDOverlay
+// 
+// Overlay image meta, stored in the header of parts so as to preserve overlay
+// information.
+// =============================================================================
+class LDOverlay : public LDObject
+{
+public:
+	LDOBJ( Overlay )
+	LDOBJ_VERTICES( 0 )
+	LDOBJ_UNCOLORED
+	LDOBJ_NON_SCEMANTIC
+	LDOBJ_NO_MATRIX
+	PROPERTY( int, camera, setCamera )
+	PROPERTY( int, x, setX )
+	PROPERTY( int, y, setY )
+	PROPERTY( int, width, setWidth )
+	PROPERTY( int, height, setHeight )
+	PROPERTY( str, filename, setFilename )
+};
+
+// =============================================================================
 // Object type names. Pass the return value of getType as the index to get a
 // string representation of the object's type.
 extern const char* g_saObjTypeNames[];

mercurial