]> Shamusworld >> Repos - architektonas/blobdiff - src/object.cpp
Fixes to accomodate object connections.
[architektonas] / src / object.cpp
index 8f9377bf7345624c61acffb6b9de11735a4ec656..925d9b4ceeeae7f6ef816da797d697bbf1327887 100644 (file)
@@ -16,6 +16,7 @@
 //
 
 #include "object.h"
+#include <stdlib.h>
 
 // Initialize static variables
 bool Object::fixedAngle = false;
@@ -28,19 +29,22 @@ bool Object::snapToGrid = true;
 
 
 Object::Object(): position(Vector(0, 0)), parent(0), state(OSInactive), oldState(OSInactive),
-       needUpdate(false), attachedDimension(0)
+       needUpdate(false)//, attachedDimension(0)
 {
 }
 
 
 Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v), parent(passedInParent),
-       state(OSInactive), oldState(OSInactive), needUpdate(false), attachedDimension(0)
+       state(OSInactive), oldState(OSInactive), needUpdate(false)//, attachedDimension(0)
 {
 }
 
 
 Object::~Object()
 {
+printf("Object: Destroyed!\n");
+       for(uint i=0; i<connected.size(); i++)
+               connected[i].object->DisconnectAll(this);
 }
 
 
@@ -113,6 +117,51 @@ Object::~Object()
 }
 
 
+// This returns a point on the object at 'parameter', which is between 0 and 1.
+// Default is to return the object's position.
+/*virtual*/ Vector Object::GetPointAtParameter(double)
+{
+       return position;
+}
+
+
+// Since these functions are pretty much non-object specific, we can implement
+// them here. :-)
+/*virtual*/ void Object::Connect(Object * obj, double parameter)
+{
+       connected.push_back(Connection(obj, parameter));
+}
+
+
+/*virtual*/ void Object::Disconnect(Object * obj, double parameter)
+{
+       for(uint i=0; i<connected.size(); i++)
+       {
+               if (connected[i].object == obj && connected[i].t == parameter)
+               {
+                       connected.erase(connected.begin() + i);
+                       return;
+               }
+       }
+}
+
+
+/*virtual*/ void Object::DisconnectAll(Object * obj)
+{
+       // According the std::vector docs, only items at position i and beyond are
+       // invalidated, everything before i is still valid. So we use that here.
+       for(uint i=0; i<connected.size();)
+       {
+               // If we found our object, erase it from the vector but don't advance
+               // the iterator. Otherwise, advance the iterator. :-)
+               if (connected[i].object == obj)
+                       connected.erase(connected.begin() + i);
+               else
+                       i++;
+       }
+}
+
+
 ObjectState Object::GetState(void)
 {
        return state;
@@ -125,10 +174,10 @@ void Object::Reparent(Object * newParent)
 }
 
 
-Dimension * Object::GetAttachedDimension(void)
+/*Dimension * Object::GetAttachedDimension(void)
 {
        return attachedDimension;
-}
+}*/
 
 
 // Class methods...