X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fcontainer.cpp;h=a8cf0ad87ec45015ee5f0f73729ed7412b33a680;hb=9d59b5831000704a1ed39c22a6043ba658993159;hp=4fceefdac106829e6d0a939f5e89fc27b66f437e;hpb=fa8da664bfb749497b1b2d2991077af0554cc369;p=architektonas diff --git a/src/container.cpp b/src/container.cpp index 4fceefd..a8cf0ad 100644 --- a/src/container.cpp +++ b/src/container.cpp @@ -4,16 +4,19 @@ // (C) 2011 Underground Software // See the README and GPLv3 files for licensing and warranty information // -// JLH = James L. Hammons +// JLH = James Hammons // // WHO WHEN WHAT // --- ---------- ------------------------------------------------------------ // JLH 03/30/2011 Created this file +// JLH 06/02/2011 Added code to delete objects in this container when they go +// out of scope // #include "container.h" #include +#include "dimension.h" Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p), @@ -21,16 +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() { + 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; iDraw(painter); + } } + /*virtual*/ Vector Container::Center(void) { return position; @@ -49,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; @@ -109,8 +152,38 @@ Like so: { if (objects[i]->Collided(point)) { - objects.erase(objects.begin() + i); // Calls the destructor, (deletes the object, I presume... O_o) +#if 0 +Dimension * dimension = objects[i]->GetAttachedDimension(); +#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(); + Vector p2 = dimension->GetPoint2(); + for(int j=0; j<(int)objects.size(); j++) + { + Vector * objectP1 = objects[i]->GetPointAt(p1); + Vector * objectP2 = objects[i]->GetPointAt(p2); + + if (objectP1) + dimension->SetPoint1(objectP1); + + if (objectP2) + 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 + // delete one at a time... Need to come up with a better approach. } } } @@ -130,51 +203,30 @@ Like so: return collision; } -/*virtual*/ void Container::PointerMoved(Vector point) -{ - objectWasDragged = true; -#if 0 - if (dragging) - { - // Here we need to check whether or not we're dragging a handle or the object itself... -// Vector delta = point - oldPoint; -// position += delta; -// endpoint += delta; - radius = Vector(point - position).Magnitude(); - - oldPoint = point; - needUpdate = true; - } - else if (draggingHandle1) - { - Vector delta = point - oldPoint; - position += delta; - oldPoint = point; - needUpdate = true; - } -/* else if (draggingHandle2) - { - Vector delta = point - oldPoint; +// 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. - endpoint += delta; +// 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 +// every object for collision. +/*virtual*/ void Container::PointerMoved(Vector point) +{ +// objectWasDragged = true; +//printf("CONTAINER: PointerMoved()\n"); - oldPoint = point; - needUpdate = true; - }*/ - else - needUpdate = false; -#else for(int i=0; i<(int)objects.size(); i++) { - if (objects[i]->GetState() == OSSelected) +// if (objects[i]->GetState() == OSSelected) objects[i]->PointerMoved(point); } -#endif + // Generic container doesn't need this??? // needUpdate = false; } + /*virtual*/ void Container::PointerReleased(void) { dragging = false; @@ -195,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; iNeedsUpdate()) needUpdate = true; @@ -208,7 +261,36 @@ about keeping track of old states... return needUpdate; } -void Container::Add(Object * object) + +/*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; iEnumerate(file); + + if (parent != NULL) + fprintf(file, "ENDCONTAINER\n"); } +