]> Shamusworld >> Repos - architektonas/blob - src/container.cpp
Added gratuitous About screen.
[architektonas] / src / container.cpp
1 // container.cpp: Container object
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/30/2011  Created this file
12 //
13
14 #include "container.h"
15
16 #include <QtGui>
17
18
19 Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
20         dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
21 {
22 }
23
24 Container::~Container()
25 {
26 }
27
28 /*virtual*/ void Container::Draw(QPainter * painter)
29 {
30         for(int i=0; i<(int)objects.size(); i++)
31                 objects[i]->Draw(painter);
32 }
33
34 /*virtual*/ Vector Container::Center(void)
35 {
36         return position;
37 }
38
39 /*
40  We need at least *three* handles for this object:
41  - one for moving
42  - one for resizing
43  - one for rotation
44
45 We need to think about the intuitive way (if there is any) to grab and
46 manipulate a complex object like this... Need to think, "What should happen when
47 I click here and drag there?"
48
49 Also: should put the snap logic into the Object base class (as a static method)...
50 */
51
52 /*virtual*/ bool Container::Collided(Vector point)
53 {
54         objectWasDragged = false;
55         Vector v1 = position - point;
56
57 #if 0
58         if (state == OSInactive)
59         {
60 //printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
61 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
62 //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);
63 //printf("      \n", );
64 //How to translate this into pixels from Document space???
65 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
66 //the caller knows about the zoom factor and all that good kinda crap
67                 if (v1.Magnitude() < 10.0)
68                 {
69                         oldState = state;
70                         state = OSSelected;
71                         oldPoint = position; //maybe "position"?
72                         draggingHandle1 = true;
73                         return true;
74                 }
75                 else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
76                 {
77                         oldState = state;
78                         state = OSSelected;
79                         oldPoint = point;
80                         dragging = true;
81                         return true;
82                 }
83         }
84         else if (state == OSSelected)
85         {
86                 // Here we test for collision with handles as well! (SOON!)
87 /*
88 Like so:
89                 if (v1.Magnitude() < 2.0) // Handle #1
90                 else if (v2.Magnitude() < 2.0) // Handle #2
91 */
92                 if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
93                 {
94                         oldState = state;
95 //                      state = OSInactive;
96                         oldPoint = point;
97                         dragging = true;
98                         return true;
99                 }
100         }
101 #else
102         bool collision = false;
103
104         // NOTE that this deletes the object on mouse down instead of mouse up. Have to
105         // check to see how it feels to do it that way...
106         if (deleteActive)
107         {
108                 for(int i=0; i<(int)objects.size(); i++)
109                 {
110                         if (objects[i]->Collided(point))
111                         {
112                                 objects.erase(objects.begin() + i);     // Calls the destructor, (deletes the object, I presume... O_o)
113                                 break;
114                         }
115                 }
116         }
117         else
118         {
119                 for(int i=0; i<(int)objects.size(); i++)
120                 {
121                         if (objects[i]->Collided(point))
122                                 collision = true;
123                 }
124         }
125 #endif
126
127         // Do we decouple the state of the generic container from the objects inside??? Mebbe.
128         state = OSInactive;
129 //      return false;
130         return collision;
131 }
132
133 // The TLC is passing all mouse movement here, so we're doing the same here.
134 // Need to adjust all other objects to handle things correctly.
135 // One optimization that will need to be done eventually is to subdivide the screen
136 // into parts and keep subdividing until an acceptable number of objects lie within
137 // the slice. This way, the GUI will still be responsive and *not* have to test
138 // every object for collision.
139 /*virtual*/ void Container::PointerMoved(Vector point)
140 {
141 //      objectWasDragged = true;
142 //printf("CONTAINER: PointerMoved()\n");
143
144         for(int i=0; i<(int)objects.size(); i++)
145         {
146 //              if (objects[i]->GetState() == OSSelected)
147                         objects[i]->PointerMoved(point);
148         }
149
150         // Generic container doesn't need this???
151 //      needUpdate = false;
152 }
153
154 /*virtual*/ void Container::PointerReleased(void)
155 {
156         dragging = false;
157         draggingHandle1 = false;
158         draggingHandle2 = false;
159
160         // Here we check for just a click: If object was clicked and dragged, then
161         // revert to the old state (OSInactive). Otherwise, keep the new state that
162         // we set.
163 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
164 about keeping track of old states...
165 */
166         if (objectWasDragged)
167                 state = oldState;
168 //Note that the preceeding is unnecessary for a generic container!
169
170         for(int i=0; i<(int)objects.size(); i++)
171                 objects[i]->PointerReleased();
172 }
173
174 /*virtual*/ bool Container::NeedsUpdate(void)
175 {
176         needUpdate = false;
177
178         for(int i=0; i<(int)objects.size(); i++)
179         {
180                 if (objects[i]->NeedsUpdate())
181                         needUpdate = true;
182         }
183
184         return needUpdate;
185 }
186
187 /*virtual*/ void Container::Add(Object * object)
188 {
189         objects.push_back(object);
190 }