2 // object.cpp: Base class for all CAD objects
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James Hammons <jlhamm@acm.org>
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)
22 // Initialize static variables
23 bool Object::fixedAngle = false;
24 bool Object::fixedLength = false;
25 QFont * Object::font = 0;
26 int Object::viewportHeight = 0;
27 bool Object::deleteActive = false;
28 bool Object::dimensionActive = false;
29 bool Object::snapToGrid = true;
30 //snapToPoints all well here?
31 bool Object::ignoreClicks = false;
32 bool Object::dontMove = false;
33 bool Object::selectionInProgress = false;
34 QRectF Object::selection;
35 double Object::gridSpacing;
36 int Object::currentLayer = 0;
37 Point Object::snapPoint;
38 bool Object::snapPointIsValid = false;
41 Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject),
42 state(OSInactive), layer(0), oldState(OSInactive), needUpdate(false)
43 //, attachedDimension(0)
48 Object::Object(Vector v, Object * passedInParent/*= 0*/): position(v),
49 parent(passedInParent), state(OSInactive), layer(0), oldState(OSInactive),
50 needUpdate(false)//, attachedDimension(0)
57 printf("Object: Destroyed!\n");
58 for(uint i=0; i<connected.size(); i++)
59 connected[i].object->DisconnectAll(this);
63 /*virtual*/ void Object::Draw(Painter *)
68 /*virtual*/ Vector Object::Center(void)
74 /*virtual*/ bool Object::Collided(Vector)
80 /*virtual*/ bool Object::PointerMoved(Vector)
86 /*virtual*/ void Object::PointerReleased(void)
91 /*virtual*/ bool Object::NeedsUpdate(void)
97 /*virtual*/ bool Object::HitTest(Point)
103 // This is intended to be overridden by the Container class, for object morphing
104 /*virtual*/ void Object::Transmute(Object *, Object *)
109 /*virtual*/ Object * Object::GetParent(void)
115 /*virtual*/ void Object::Add(Object *)
120 // This returns a pointer to the point passed in, if it coincides. Otherwise returns NULL.
121 /*virtual*/ Vector * Object::GetPointAt(Vector)
127 // This is meant for writing object data to a file.
128 /*virtual*/ void Object::Enumerate(FILE *)
133 /*virtual*/ Object * Object::Copy(void)
135 return new Object(position, parent);
139 // This returns a point on the object at 'parameter', which is between 0 and 1.
140 // Default is to return the object's position.
141 /*virtual*/ Vector Object::GetPointAtParameter(double)
147 // Since these functions are pretty much non-object specific, we can implement
149 /*virtual*/ void Object::Connect(Object * obj, double parameter)
151 // Check to see if this connection is already in our list...
152 Connection c(obj, parameter);
153 std::vector<Connection>::iterator i;
155 for(i=connected.begin(); i!=connected.end(); i++)
157 // Bail out if this connection is already present...
162 // Connection is a new one, add it in...
163 connected.push_back(c);
167 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
169 std::vector<Connection>::iterator i;
171 for(i=connected.begin(); i!=connected.end(); i++)
173 if (((*i).object == obj) && ((*i).t == parameter))
182 /*virtual*/ void Object::DisconnectAll(Object * obj)
184 std::vector<Connection>::iterator i;
186 for(i=connected.begin(); i!=connected.end(); )
188 if ((*i).object == obj)
196 /*virtual*/ QRectF Object::Extents(void)
198 // Generic object returns an empty rect...
204 /*virtual*/ ObjectType Object::Type(void)
211 /*virtual*/ void Object::Translate(Vector amount)
217 /*virtual*/ void Object::Rotate(Point, double)
222 /*virtual*/ void Object::Scale(Point, double)
227 /*virtual*/ void Object::Mirror(Point, Point)
232 /*virtual*/ void Object::Save(void)
234 oldPosition = position;
238 /*virtual*/ void Object::Restore(void)
240 position = oldPosition;
244 ObjectState Object::GetState(void)
250 void Object::Reparent(Object * newParent)
256 /*Dimension * Object::GetAttachedDimension(void)
258 return attachedDimension;
264 void Object::SetFixedAngle(bool state/*= true*/)
270 void Object::SetFixedLength(bool state/*= true*/)
276 void Object::SetFont(QFont * f)
282 void Object::SetViewportHeight(int height)
284 viewportHeight = height;
288 void Object::SetDeleteActive(bool state/*= true*/)
290 deleteActive = state;
294 void Object::SetDimensionActive(bool state/*= true*/)
296 dimensionActive = state;
300 void Object::SetSnapMode(bool state/*= true*/)
307 // This looks strange, but it's really quite simple: We want a point that's
308 // more than half-way to the next grid point to snap there while conversely we
309 // want a point that's less than half-way to to the next grid point then snap
310 // to the one before it. So we add half of the grid spacing to the point, then
311 // divide by it so that we can remove the fractional part, then multiply it
312 // back to get back to the correct answer.
314 Vector Object::SnapPointToGrid(Vector point)
316 point += gridSpacing / 2.0; // *This* adds to Z!!!
317 point /= gridSpacing;
318 point.x = floor(point.x);//need to fix this for negative numbers...
319 point.y = floor(point.y);
320 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
321 point *= gridSpacing;