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"
21 Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
22 dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
26 Container::~Container()
29 while (objects.size() > 0)
32 objects.erase(objects.begin());
36 /*virtual*/ void Container::Draw(Painter * painter)
38 for(int i=0; i<(int)objects.size(); i++)
39 objects[i]->Draw(painter);
42 /*virtual*/ Vector Container::Center(void)
48 We need at least *three* handles for this object:
53 We need to think about the intuitive way (if there is any) to grab and
54 manipulate a complex object like this... Need to think, "What should happen when
55 I click here and drag there?"
57 Also: should put the snap logic into the Object base class (as a static method)...
60 /*virtual*/ bool Container::Collided(Vector point)
62 objectWasDragged = false;
63 Vector v1 = position - point;
66 if (state == OSInactive)
68 //printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
69 //printf(" v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
70 //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);
72 //How to translate this into pixels from Document space???
73 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
74 //the caller knows about the zoom factor and all that good kinda crap
75 if (v1.Magnitude() < 10.0)
79 oldPoint = position; //maybe "position"?
80 draggingHandle1 = true;
83 else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
92 else if (state == OSSelected)
94 // Here we test for collision with handles as well! (SOON!)
97 if (v1.Magnitude() < 2.0) // Handle #1
98 else if (v2.Magnitude() < 2.0) // Handle #2
100 if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
103 // state = OSInactive;
110 bool collision = false;
112 // NOTE that this deletes the object on mouse down instead of mouse up. Have to
113 // check to see how it feels to do it that way...
116 for(int i=0; i<(int)objects.size(); i++)
118 if (objects[i]->Collided(point))
120 objects.erase(objects.begin() + i); // Calls the destructor, (deletes the object, I presume... O_o)
127 for(int i=0; i<(int)objects.size(); i++)
129 if (objects[i]->Collided(point))
135 // Do we decouple the state of the generic container from the objects inside??? Mebbe.
141 // The TLC is passing all mouse movement here, so we're doing the same here.
142 // Need to adjust all other objects to handle things correctly.
143 // One optimization that will need to be done eventually is to subdivide the screen
144 // into parts and keep subdividing until an acceptable number of objects lie within
145 // the slice. This way, the GUI will still be responsive and *not* have to test
146 // every object for collision.
147 /*virtual*/ void Container::PointerMoved(Vector point)
149 // objectWasDragged = true;
150 //printf("CONTAINER: PointerMoved()\n");
152 for(int i=0; i<(int)objects.size(); i++)
154 // if (objects[i]->GetState() == OSSelected)
155 objects[i]->PointerMoved(point);
158 // Generic container doesn't need this???
159 // needUpdate = false;
162 /*virtual*/ void Container::PointerReleased(void)
165 draggingHandle1 = false;
166 draggingHandle2 = false;
168 // Here we check for just a click: If object was clicked and dragged, then
169 // revert to the old state (OSInactive). Otherwise, keep the new state that
171 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
172 about keeping track of old states...
174 if (objectWasDragged)
176 //Note that the preceeding is unnecessary for a generic container!
178 for(int i=0; i<(int)objects.size(); i++)
179 objects[i]->PointerReleased();
182 /*virtual*/ bool Container::NeedsUpdate(void)
186 for(int i=0; i<(int)objects.size(); i++)
188 if (objects[i]->NeedsUpdate())
195 /*virtual*/ void Container::Add(Object * object)
197 objects.push_back(object);