]> Shamusworld >> Repos - architektonas/blobdiff - src/container.cpp
Initial stab at text object. Nonfunctional ATM.
[architektonas] / src / container.cpp
index c867eaf80224c59609925d49eed1ec35683d3103..a8cf0ad87ec45015ee5f0f73729ed7412b33a680 100644 (file)
@@ -24,22 +24,55 @@ Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
 {
 }
 
+
+// Copy constructor
+Container::Container(const Container & copy): Object(copy.position, copy.parent)
+{
+       // Use overloaded assignment operator
+       *this = copy;
+}
+
+
 Container::~Container()
 {
-       // No memory leaks!
-       while (objects.size() > 0)
+       Clear();
+}
+
+
+// Assignment operator
+Container & Container::operator=(const Container & from)
+{
+       // Take care of self-assignment
+       if (this == &from)
+               return *this;
+
+       Clear();
+
+       // Small problem with this approach: if the copied object goes out of scope,
+       // all of the objects we copied in here will be deleted. D'oh!
+       for(uint i=0; i<from.objects.size(); i++)
        {
-               delete objects[0];
-               objects.erase(objects.begin());
+               Object * object = from.objects[i];
+
+               // Need to copy the object here...
+
+               objects.push_back(object);
        }
+
+       return *this;
 }
 
+
 /*virtual*/ void Container::Draw(Painter * painter)
 {
        for(int i=0; i<(int)objects.size(); i++)
+       {
+//printf("Container: About to draw (object = $%X)\n", objects[i]);
                objects[i]->Draw(painter);
+       }
 }
 
+
 /*virtual*/ Vector Container::Center(void)
 {
        return position;
@@ -58,6 +91,7 @@ I click here and drag there?"
 Also: should put the snap logic into the Object base class (as a static method)...
 */
 
+
 /*virtual*/ bool Container::Collided(Vector point)
 {
        objectWasDragged = false;
@@ -118,13 +152,17 @@ Like so:
                {
                        if (objects[i]->Collided(point))
                        {
+#if 0
 Dimension * dimension = objects[i]->GetAttachedDimension();
-
-                               objects.erase(objects.begin() + i);     // Calls the destructor, (deletes the object, I presume... O_o)
+#endif
+                               Object * objectToDelete = objects[i];
+                               objects.erase(objects.begin() + i);     // Calls the destructor, (deletes the object, I presume... O_o) [NOPE! SURE DOESN'T!]
+                               delete objectToDelete;
 
 // If this object had an attached dimension, reattach it to another object, if any...
 // The only problem with that approach is if the object it gets attached to is deleted,
 // it will call the dimension to use a NULL pointer and BLAMMO
+#if 0
 if (dimension)
 {
        Vector p1 = dimension->GetPoint1();
@@ -141,7 +179,7 @@ if (dimension)
                        dimension->SetPoint2(objectP2);
        }
 }
-
+#endif
                                // This only allows deleting objects one at a time...
                                break;
                                // however, this way of doing things could be problematic if we don't
@@ -165,8 +203,10 @@ if (dimension)
        return collision;
 }
 
+
 // The TLC is passing all mouse movement here, so we're doing the same here.
 // Need to adjust all other objects to handle things correctly.
+
 // One optimization that will need to be done eventually is to subdivide the screen
 // into parts and keep subdividing until an acceptable number of objects lie within
 // the slice. This way, the GUI will still be responsive and *not* have to test
@@ -186,6 +226,7 @@ if (dimension)
 //     needUpdate = false;
 }
 
+
 /*virtual*/ void Container::PointerReleased(void)
 {
        dragging = false;
@@ -206,11 +247,12 @@ about keeping track of old states...
                objects[i]->PointerReleased();
 }
 
+
 /*virtual*/ bool Container::NeedsUpdate(void)
 {
        needUpdate = false;
 
-       for(int i=0; i<(int)objects.size(); i++)
+       for(uint i=0; i<objects.size(); i++)
        {
                if (objects[i]->NeedsUpdate())
                        needUpdate = true;
@@ -219,7 +261,36 @@ about keeping track of old states...
        return needUpdate;
 }
 
+
 /*virtual*/ void Container::Add(Object * object)
 {
        objects.push_back(object);
+printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
+}
+
+
+void Container::Clear(void)
+{
+       // No memory leaks!
+       while (objects.size() > 0)
+       {
+printf("Container: Deleting object ($%X)...\n", objects[0]);
+               delete objects[0];
+               objects.erase(objects.begin());
+       }
 }
+
+
+/*virtual*/ void Container::Enumerate(FILE * file)
+{
+       // Only put "CONTAINER" markers if *not* the top level container
+       if (parent != NULL)
+               fprintf(file, "CONTAINER\n");
+
+       for(uint i=0; i<objects.size(); i++)
+               objects[i]->Enumerate(file);
+
+       if (parent != NULL)
+               fprintf(file, "ENDCONTAINER\n");
+}
+