1 // container.cpp: Container object
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/30/2011 Created this file
12 // JLH 06/02/2011 Added code to delete objects in this container when they go
16 #include "container.h"
19 #include "dimension.h"
23 Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
24 dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
31 Container::Container(const Container & copy): Object(copy.position, copy.parent)
33 // Use overloaded assignment operator
39 Container::~Container()
45 // Assignment operator
46 Container & Container::operator=(const Container & from)
48 // Take care of self-assignment
54 // Small problem with this approach: if the copied object goes out of scope,
55 // all of the objects we copied in here will be deleted. D'oh!
56 for(uint i=0; i<from.objects.size(); i++)
58 Object * object = from.objects[i];
60 // Need to copy the object here...
62 objects.push_back(object);
69 /*virtual*/ void Container::Draw(Painter * painter)
73 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
74 // for(int i=0; i<(int)objects.size(); i++)
77 //printf("Container: About to draw (object = $%X)\n", objects[i]);
78 objects[i]->Draw(painter);
79 bounds = bounds.united(objects[i].RectangularExtents());
82 boundary = boundary.united((*i)->Extents());
86 if (state == OSSelected)
88 painter->SetPen(QPen(Qt::magenta, 2.0, Qt::DashLine));
89 painter->SetBrush(QBrush(Qt::NoBrush));
90 painter->DrawRect(boundary);
95 /*virtual*/ Vector Container::Center(void)
101 We need at least *three* handles for this object:
106 We need to think about the intuitive way (if there is any) to grab and
107 manipulate a complex object like this... Need to think, "What should happen when
108 I click here and drag there?"
110 Also: should put the snap logic into the Object base class (as a static method)...
114 /*virtual*/ bool Container::Collided(Vector point)
116 objectWasDragged = false;
117 Vector v1 = position - point;
120 if (state == OSInactive)
122 //printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
123 //printf(" v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
124 //printf(" point = %lf,%lf,%lf; p1 = %lf,%lf,%lf; p2 = %lf,%lf,%lf\n", point.x, point.y, point.z, position.x, position.y, position.z, endpoint.x, endpoint.y, endpoint.z);
126 //How to translate this into pixels from Document space???
127 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
128 //the caller knows about the zoom factor and all that good kinda crap
129 if (v1.Magnitude() < 10.0)
133 oldPoint = position; //maybe "position"?
134 draggingHandle1 = true;
137 else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
146 else if (state == OSSelected)
148 // Here we test for collision with handles as well! (SOON!)
151 if (v1.Magnitude() < 2.0) // Handle #1
152 else if (v2.Magnitude() < 2.0) // Handle #2
154 if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
157 // state = OSInactive;
164 bool collision = false;
166 // NOTE that this deletes the object on mouse down instead of mouse up. Have to
167 // check to see how it feels to do it that way...
170 for(int i=0; i<(int)objects.size(); i++)
172 if (objects[i]->Collided(point))
175 Dimension * dimension = objects[i]->GetAttachedDimension();
177 Object * objectToDelete = objects[i];
178 objects.erase(objects.begin() + i); // Calls the destructor, (deletes the object, I presume... O_o) [NOPE! SURE DOESN'T!]
179 delete objectToDelete;
181 // If this object had an attached dimension, reattach it to another object, if any...
182 // The only problem with that approach is if the object it gets attached to is deleted,
183 // it will call the dimension to use a NULL pointer and BLAMMO
187 Vector p1 = dimension->GetPoint1();
188 Vector p2 = dimension->GetPoint2();
189 for(int j=0; j<(int)objects.size(); j++)
191 Vector * objectP1 = objects[i]->GetPointAt(p1);
192 Vector * objectP2 = objects[i]->GetPointAt(p2);
195 dimension->SetPoint1(objectP1);
198 dimension->SetPoint2(objectP2);
202 // This only allows deleting objects one at a time...
204 // however, this way of doing things could be problematic if we don't
205 // delete one at a time... Need to come up with a better approach.
211 for(int i=0; i<(int)objects.size(); i++)
213 if (objects[i]->Collided(point))
219 // Do we decouple the state of the generic container from the objects inside??? Mebbe.
226 // The TLC is passing all mouse movement here, so we're doing the same here.
227 // Need to adjust all other objects to handle things correctly.
229 // One optimization that will need to be done eventually is to subdivide the screen
230 // into parts and keep subdividing until an acceptable number of objects lie within
231 // the slice. This way, the GUI will still be responsive and *not* have to test
232 // every object for collision.
233 /*virtual*/ void Container::PointerMoved(Vector point)
235 // objectWasDragged = true;
236 //printf("CONTAINER: PointerMoved()\n");
238 for(int i=0; i<(int)objects.size(); i++)
240 // if (objects[i]->GetState() == OSSelected)
241 objects[i]->PointerMoved(point);
244 // Generic container doesn't need this???
245 // needUpdate = false;
249 /*virtual*/ void Container::PointerReleased(void)
252 draggingHandle1 = false;
253 draggingHandle2 = false;
255 // Here we check for just a click: If object was clicked and dragged, then
256 // revert to the old state (OSInactive). Otherwise, keep the new state that
258 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
259 about keeping track of old states...
261 if (objectWasDragged)
263 //Note that the preceeding is unnecessary for a generic container!
265 for(int i=0; i<(int)objects.size(); i++)
266 objects[i]->PointerReleased();
270 /*virtual*/ bool Container::NeedsUpdate(void)
274 for(uint i=0; i<objects.size(); i++)
276 if (objects[i]->NeedsUpdate())
284 /*virtual*/ void Container::Add(Object * object)
286 objects.push_back(object);
287 printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
292 /*virtual*/ ObjectType Container::Type(void)
299 void Container::Delete(Object * objectToDelete)
303 for(unsigned int i=0; i<objects.size(); i++)
305 if (objects[i] == objectToDelete)
308 delete objectToDelete;
313 std::vector<Object *>::iterator i = objects.begin();
315 while (i != objects.end())
317 if (*i == objectToDelete)
320 delete objectToDelete;
330 void Container::Clear(void)
333 while (objects.size() > 0)
335 printf("Container: Deleting object ($%X)...\n", objects[0]);
337 objects.erase(objects.begin());
342 void Container::SelectAll(void)
344 for(unsigned int i=0; i<objects.size(); i++)
345 objects[i]->state = OSSelected;
349 void Container::DeselectAll(void)
351 for(unsigned int i=0; i<objects.size(); i++)
352 objects[i]->state = OSInactive;
356 int Container::ItemsSelected(void)
360 for(uint i=0; i<objects.size(); i++)
361 if (objects[i]->state == OSSelected)
368 /*ObjectType Container::SelectedItemType(unsigned int index)
370 if (index >= objects.size())
373 return objects[index]->Type();
377 Object * Container::SelectedItem(unsigned int index)
379 // if (index >= objects.size())
382 unsigned int selectedIndex = 0;
384 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
386 if ((*i)->state == OSSelected)
388 if (selectedIndex == index)
399 void Container::MoveContentsTo(Container * newContainer)
402 if (newContainer == NULL)
405 // Shuffle the contents of this container to the new one
406 for(unsigned int i=0; i<objects.size(); i++)
407 newContainer->Add(objects[i]);
409 // & clear our vector
414 void Container::MoveSelectedContentsTo(Container * newContainer)
417 if (newContainer == NULL)
420 // Shuffle the contents of this container to the new one
421 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
423 if ((*i)->state != OSSelected)
429 newContainer->Add(*i);
435 /*virtual*/ void Container::Enumerate(FILE * file)
437 // Only put "CONTAINER" markers if *not* the top level container
439 fprintf(file, "CONTAINER\n");
441 for(uint i=0; i<objects.size(); i++)
442 objects[i]->Enumerate(file);
445 fprintf(file, "ENDCONTAINER\n");