]> Shamusworld >> Repos - architektonas/blob - src/object.cpp
Fixes to accomodate object connections.
[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
30
31 Object::Object(): position(Vector(0, 0)), parent(0), state(OSInactive), oldState(OSInactive),
32         needUpdate(false)//, attachedDimension(0)
33 {
34 }
35
36
37 Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v), parent(passedInParent),
38         state(OSInactive), oldState(OSInactive), needUpdate(false)//, attachedDimension(0)
39 {
40 }
41
42
43 Object::~Object()
44 {
45 printf("Object: Destroyed!\n");
46         for(uint i=0; i<connected.size(); i++)
47                 connected[i].object->DisconnectAll(this);
48 }
49
50
51 /*virtual*/ void Object::Draw(Painter *)
52 {
53 }
54
55
56 /*virtual*/ Vector Object::Center(void)
57 {
58         return Vector();
59 }
60
61
62 /*virtual*/ bool Object::Collided(Vector)
63 {
64         return false;
65 }
66
67
68 /*virtual*/ void Object::PointerMoved(Vector)
69 {
70 }
71
72
73 /*virtual*/ void Object::PointerReleased(void)
74 {
75 }
76
77
78 /*virtual*/ bool Object::NeedsUpdate(void)
79 {
80         return needUpdate;
81 }
82
83
84 // This is intended to be overridden by the Container class, for object morphing
85 /*virtual*/ void Object::Transmute(Object *, Object *)
86 {
87 }
88
89
90 /*virtual*/ Object * Object::GetParent(void)
91 {
92         return parent;
93 }
94
95
96 /*virtual*/ void Object::Add(Object *)
97 {
98 }
99
100
101 // This returns a pointer to the point passed in, if it coincides. Otherwise returns NULL.
102 /*virtual*/ Vector * Object::GetPointAt(Vector)
103 {
104         return 0;
105 }
106
107
108 // This is meant for writing object data to a file.
109 /*virtual*/ void Object::Enumerate(FILE *)
110 {
111 }
112
113
114 /*virtual*/ Object * Object::Copy(void)
115 {
116         return new Object(position, parent);
117 }
118
119
120 // This returns a point on the object at 'parameter', which is between 0 and 1.
121 // Default is to return the object's position.
122 /*virtual*/ Vector Object::GetPointAtParameter(double)
123 {
124         return position;
125 }
126
127
128 // Since these functions are pretty much non-object specific, we can implement
129 // them here. :-)
130 /*virtual*/ void Object::Connect(Object * obj, double parameter)
131 {
132         connected.push_back(Connection(obj, parameter));
133 }
134
135
136 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
137 {
138         for(uint i=0; i<connected.size(); i++)
139         {
140                 if (connected[i].object == obj && connected[i].t == parameter)
141                 {
142                         connected.erase(connected.begin() + i);
143                         return;
144                 }
145         }
146 }
147
148
149 /*virtual*/ void Object::DisconnectAll(Object * obj)
150 {
151         // According the std::vector docs, only items at position i and beyond are
152         // invalidated, everything before i is still valid. So we use that here.
153         for(uint i=0; i<connected.size();)
154         {
155                 // If we found our object, erase it from the vector but don't advance
156                 // the iterator. Otherwise, advance the iterator. :-)
157                 if (connected[i].object == obj)
158                         connected.erase(connected.begin() + i);
159                 else
160                         i++;
161         }
162 }
163
164
165 ObjectState Object::GetState(void)
166 {
167         return state;
168 }
169
170
171 void Object::Reparent(Object * newParent)
172 {
173         parent = newParent;
174 }
175
176
177 /*Dimension * Object::GetAttachedDimension(void)
178 {
179         return attachedDimension;
180 }*/
181
182
183 // Class methods...
184
185 void Object::SetFixedAngle(bool state/*= true*/)
186 {
187         fixedAngle = state;
188 }
189
190
191 void Object::SetFixedLength(bool state/*= true*/)
192 {
193         fixedLength = state;
194 }
195
196
197 void Object::SetFont(QFont * f)
198 {
199         font = f;
200 }
201
202
203 void Object::SetViewportHeight(int height)
204 {
205         viewportHeight = height;
206 }
207
208
209 void Object::SetDeleteActive(bool state/*= true*/)
210 {
211         deleteActive = state;
212 }
213
214
215 void Object::SetDimensionActive(bool state/*= true*/)
216 {
217         dimensionActive = state;
218 }
219
220
221 void Object::SetSnapMode(bool state/*= true*/)
222 {
223         snapToGrid = state;
224 }