]> Shamusworld >> Repos - architektonas/blobdiff - src/fileio.cpp
Changes to make containers behave like a first-class object.
[architektonas] / src / fileio.cpp
index 1002dca87fecf22b79cbb2883253e9660fe6c34b..ace5bfa247713a3adc4aabee61f0dc3871a4c0aa 100644 (file)
@@ -19,6 +19,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
+#include "applicationwindow.h"
+#include "drawingview.h"
+#include "global.h"
 #include "structs.h"
 
 /*
@@ -125,12 +128,14 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
           virtual Object function that reports the object by itself if it's a non-
           Container, otherwise it enumerates all objects within itself. */
 
-       fprintf(file, "ARCHITEKTONAS DRAWING V1.1\n");
-#if 0
-       object->Enumerate(file);
-#else
+       fprintf(file, "ARCHITEKTONAS DRAWING V1.2\n");
+       fprintf(file, "LAYERS %i\n", Global::numLayers);
+
+       for(int i=0; i<Global::numLayers; i++)
+               fprintf(file, "%i %i \"%s\"\n", (Global::layerHidden[i] ? 1 : 0), (Global::layerLocked[i] ? 1 : 0), Global::layerName[i].c_str());
+
+       fprintf(file, "ACTIVE %i\n", Global::activeLayer);
        WriteObjectToFile(file, (Object *)c);
-#endif
        fprintf(file, "END\n");
        return true;
 }
@@ -140,83 +145,97 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
 {
        float version;
 
-       fscanf(file, "ARCHITEKTONAS DRAWING V%f", &version);
+       fscanf(file, "ARCHITEKTONAS DRAWING V%f\n", &version);
 
-//printf("Load: version = %f\n", version);
-//     if (version != 1.0)
-//             return false;
+       // Clear out layer vectors
+       Global::layerHidden.clear();
+       Global::layerLocked.clear();
+       Global::layerName.clear();
 
-       if (version == 1.0)
+       if (version == 1.0f)
                return LoadVersion1_0(file, drawing);
-       else if (version == 1.1)
+       else if (version == 1.1f)
                return LoadVersion1_1(file, drawing);
+       else if (version == 1.2f)
+               return LoadVersion1_2(file, drawing);
 
+//printf("LoadAtnsFile: Could not locate version! (version=%f)\n", version);
        return false;
-       /* Approach: read each object in the file, one by one. If the object is a
-          Container, add objects to it until an "endContainer" marker is found.
-          This will require a stack to maintain the current Container. */
+}
+
+
+/*static*/ void FileIO::ResetLayerVectors(void)
+{
+       // Set up layer vectors
+       Global::layerHidden.insert(Global::layerHidden.begin(), false);
+       Global::layerLocked.insert(Global::layerLocked.begin(), false);
+       Global::layerName.insert(Global::layerName.begin(), "Background");
+       Global::numLayers = 1;
+       Global::activeLayer = 0;
+}
+
 
-#if 0
+/*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing)
+{
+       // Approach: read each object in the file, one by one. If the object is a
+       // Container, add objects to it until an "endContainer" marker is found.
+       // This will require a stack to maintain the current Container.
        std::vector<Container *> containerStack;
-       Container * currentTopContainer = drawing;//new Container(Vector(0, 0));
-       Object * object;
-//     ObjectType objectType;
-       int objectType;
+       Container * currentTopContainer = drawing;
+       ResetLayerVectors();
 
        while (!feof(file))
        {
-               if (FileIO::GetObjectFromFile(file, currentTopContainer, &object, &objectType) == false)
-                       return false;
-
-               // object->type down below can be replaced with objType.
-               // Above could be: bool FileIO::GetObjectFromFile(FILE *, Object *,
-               // ObjectType *); where the return value tells if it's a valid object,
-               // Object * returns the reconstructed object and ObjectType * returns
-               // the object type.
+               // Reconstruct the object
+               Object * obj = GetObjectFromFile(file);
 
-               if (objectType == OTFEndOfFile)
-               {
-//printf("Load: container size = %li\n", drawing->objects.size());
-                       return true;
-               }
-               else if (objectType == OTFContainer)
+               // objectFileType is set in GetObjectFromFile()...
+               if (objectFileType == OTFObject)
                {
-                       containerStack.push_back(currentTopContainer);
-                       currentTopContainer = new Container(Vector(0, 0), currentTopContainer);
+                       if (obj == NULL)
+                               return false;
+
+                       currentTopContainer->objects.push_back(obj);
+//printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
+
+                       // If the object is a container, push current TLC on the stack and
+                       // set it as the new TLC
+                       if (obj->type == OTContainer)
+                       {
+                               containerStack.push_back(currentTopContainer);
+                               currentTopContainer = (Container *)obj;
+                       }
                }
-               else if (objectType == OTFContainerEnd)
+               else if (objectFileType == OTFContainerEnd)
                {
-                       Container * containerToAdd = currentTopContainer;
+                       // Container is done, so pop the stack to get back the previous TLC
                        currentTopContainer = containerStack.back();
                        containerStack.pop_back();
-                       currentTopContainer->Add(containerToAdd);
                }
-               else
+               else if (objectFileType == OTFEndOfFile)
                {
-                       currentTopContainer->Add(object);
-//printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
+//printf("Load: container size = %li\n", drawing->objects.size());
+                       return true;
                }
        }
 
        return false;
-#else
-       return false;
-#endif
 }
 
 
-/*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing)
+/*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing)
 {
        // Approach: read each object in the file, one by one. If the object is a
        // Container, add objects to it until an "endContainer" marker is found.
        // This will require a stack to maintain the current Container.
        std::vector<Container *> containerStack;
        Container * currentTopContainer = drawing;
+       ResetLayerVectors();
 
        while (!feof(file))
        {
-               // Reconstruct the object
-               Object * obj = GetObjectFromFile(file);
+               // Reconstruct the object (extended format!)
+               Object * obj = GetObjectFromFile(file, true);
 
                // objectFileType is set in GetObjectFromFile()...
                if (objectFileType == OTFObject)
@@ -237,6 +256,12 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
                }
                else if (objectFileType == OTFContainerEnd)
                {
+                       // Add the extents of the current container
+                       Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer);
+                       currentTopContainer->p[0] = r.TopLeft();
+                       currentTopContainer->p[1] = r.BottomRight();
+//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b);
+
                        // Container is done, so pop the stack to get back the previous TLC
                        currentTopContainer = containerStack.back();
                        containerStack.pop_back();
@@ -252,8 +277,24 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
 }
 
 
-/*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing)
+/*static*/ bool FileIO::LoadVersion1_2(FILE * file, Container * drawing)
 {
+       int hidden, locked;
+       char textBuffer[65536];
+
+       // Load layer information first
+       fscanf(file, "LAYERS %i\n", &Global::numLayers);
+
+       for(int i=0; i<Global::numLayers; i++)
+       {
+               fscanf(file, "%i %i \"%[^\"]\"\n", &hidden, &locked, textBuffer);
+               Global::layerHidden.push_back(hidden ? true : false);
+               Global::layerLocked.push_back(locked ? true : false);
+               Global::layerName.push_back(textBuffer);
+       }
+
+       fscanf(file, "ACTIVE %i\n", &Global::activeLayer);
+
        // Approach: read each object in the file, one by one. If the object is a
        // Container, add objects to it until an "endContainer" marker is found.
        // This will require a stack to maintain the current Container.
@@ -284,6 +325,12 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
                }
                else if (objectFileType == OTFContainerEnd)
                {
+                       // Add the extents of the current container
+                       Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer);
+                       currentTopContainer->p[0] = r.TopLeft();
+                       currentTopContainer->p[1] = r.BottomRight();
+//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b);
+
                        // Container is done, so pop the stack to get back the previous TLC
                        currentTopContainer = containerStack.back();
                        containerStack.pop_back();
@@ -302,6 +349,7 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
 /*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/)
 {
        char buffer[256];
+       char textBuffer[65536];
        int foundLayer = 0;
        /*int num =*/ fscanf(file, "%s", buffer);
        Object * obj = NULL;
@@ -336,6 +384,7 @@ if (errno)
 }
 
        // Need to add pen attributes as well... do that for v1.1 :-P
+       // And fill attributes... do that for v1.3 (1.2 added layers)
        if (strcmp(buffer, "LINE") == 0)
        {
                Point p1, p2;
@@ -356,6 +405,12 @@ if (errno)
                fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2);
                obj = (Object *)new Arc(p, r, a1, a2);
        }
+       else if (strcmp(buffer, "TEXT") == 0)
+       {
+               Point p;
+               fscanf(file, "(%lf,%lf) \"%[^\"]\"", &p.x, &p.y, textBuffer);
+               obj = (Object *)new Text(p, textBuffer);
+       }
        else if (strcmp(buffer, "DIMENSION") == 0)
        {
                Point p1, p2;
@@ -376,6 +431,8 @@ if (errno)
        {
                objectFileType = OTFEndOfFile;
        }
+       else
+               printf("Unknown object type '%s'...\n", buffer);
 
        if (obj != NULL)
        {