]> Shamusworld >> Repos - architektonas/blob - src/object.cpp
Added 1st stab at grouping capability.
[architektonas] / src / object.cpp
1 //
2 // object.cpp: Base class for all CAD objects
3 //
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  03/22/2011  Created this file
13 // JLH  04/01/2011  Added constructor to allow derived objects to have empty
14 //                  constructor bodies, added state querying
15 // JLH  04/02/2001  Added static methods for global states (fixed angle, etc)
16 //
17
18 #include "object.h"
19 #include <stdlib.h>
20
21 // Initialize static variables
22 bool Object::fixedAngle = false;
23 bool Object::fixedLength = false;
24 QFont * Object::font = 0;
25 int Object::viewportHeight = 0;
26 bool Object::deleteActive = false;
27 bool Object::dimensionActive = false;
28 bool Object::snapToGrid = true;
29 //snapToPoints all well here?
30 bool Object::ignoreClicks = false;
31 bool Object::dontMove = false;
32
33
34 Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject),
35         state(OSInactive), oldState(OSInactive), needUpdate(false)
36         //, attachedDimension(0)
37 {
38 }
39
40
41 Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v), parent(passedInParent),
42         state(OSInactive), oldState(OSInactive), needUpdate(false)//, attachedDimension(0)
43 {
44 }
45
46
47 Object::~Object()
48 {
49 printf("Object: Destroyed!\n");
50         for(uint i=0; i<connected.size(); i++)
51                 connected[i].object->DisconnectAll(this);
52 }
53
54
55 /*virtual*/ void Object::Draw(Painter *)
56 {
57 }
58
59
60 /*virtual*/ Vector Object::Center(void)
61 {
62         return Vector();
63 }
64
65
66 /*virtual*/ bool Object::Collided(Vector)
67 {
68         return false;
69 }
70
71
72 /*virtual*/ void Object::PointerMoved(Vector)
73 {
74 }
75
76
77 /*virtual*/ void Object::PointerReleased(void)
78 {
79 }
80
81
82 /*virtual*/ bool Object::NeedsUpdate(void)
83 {
84         return needUpdate;
85 }
86
87
88 // This is intended to be overridden by the Container class, for object morphing
89 /*virtual*/ void Object::Transmute(Object *, Object *)
90 {
91 }
92
93
94 /*virtual*/ Object * Object::GetParent(void)
95 {
96         return parent;
97 }
98
99
100 /*virtual*/ void Object::Add(Object *)
101 {
102 }
103
104
105 // This returns a pointer to the point passed in, if it coincides. Otherwise returns NULL.
106 /*virtual*/ Vector * Object::GetPointAt(Vector)
107 {
108         return 0;
109 }
110
111
112 // This is meant for writing object data to a file.
113 /*virtual*/ void Object::Enumerate(FILE *)
114 {
115 }
116
117
118 /*virtual*/ Object * Object::Copy(void)
119 {
120         return new Object(position, parent);
121 }
122
123
124 // This returns a point on the object at 'parameter', which is between 0 and 1.
125 // Default is to return the object's position.
126 /*virtual*/ Vector Object::GetPointAtParameter(double)
127 {
128         return position;
129 }
130
131
132 // Since these functions are pretty much non-object specific, we can implement
133 // them here. :-)
134 /*virtual*/ void Object::Connect(Object * obj, double parameter)
135 {
136         connected.push_back(Connection(obj, parameter));
137 }
138
139
140 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
141 {
142         for(uint i=0; i<connected.size(); i++)
143         {
144                 if (connected[i].object == obj && connected[i].t == parameter)
145                 {
146                         connected.erase(connected.begin() + i);
147                         return;
148                 }
149         }
150 }
151
152
153 /*virtual*/ void Object::DisconnectAll(Object * obj)
154 {
155         // According the std::vector docs, only items at position i and beyond are
156         // invalidated, everything before i is still valid. So we use that here.
157         for(uint i=0; i<connected.size();)
158         {
159                 // If we found our object, erase it from the vector but don't advance
160                 // the iterator. Otherwise, advance the iterator. :-)
161                 if (connected[i].object == obj)
162                         connected.erase(connected.begin() + i);
163                 else
164                         i++;
165         }
166 }
167
168
169 /*virtual*/ QRectF Object::Extents(void)
170 {
171         // Generic object returns an empty rect...
172         return QRectF();
173 }
174
175
176 #if 0
177 /*virtual*/ ObjectType Object::Type(void)
178 {
179         return OTObject;
180 }
181 #endif
182
183
184 ObjectState Object::GetState(void)
185 {
186         return state;
187 }
188
189
190 void Object::Reparent(Object * newParent)
191 {
192         parent = newParent;
193 }
194
195
196 /*Dimension * Object::GetAttachedDimension(void)
197 {
198         return attachedDimension;
199 }*/
200
201
202 // Class methods...
203
204 void Object::SetFixedAngle(bool state/*= true*/)
205 {
206         fixedAngle = state;
207 }
208
209
210 void Object::SetFixedLength(bool state/*= true*/)
211 {
212         fixedLength = state;
213 }
214
215
216 void Object::SetFont(QFont * f)
217 {
218         font = f;
219 }
220
221
222 void Object::SetViewportHeight(int height)
223 {
224         viewportHeight = height;
225 }
226
227
228 void Object::SetDeleteActive(bool state/*= true*/)
229 {
230         deleteActive = state;
231 }
232
233
234 void Object::SetDimensionActive(bool state/*= true*/)
235 {
236         dimensionActive = state;
237 }
238
239
240 void Object::SetSnapMode(bool state/*= true*/)
241 {
242         snapToGrid = state;
243 }