Spinning Topp Logo BlackTopp Studios
inc
attachable.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _attachable_cpp
41 #define _attachable_cpp
42 
43 #include "attachable.h"
44 #include "exception.h"
45 
46 /// @file attachable.cpp
47 /// @brief Contains the Mezzanine::Attachable Class and Mezzanine::Attachable::AttachableElement enumeration implementations
48 
49 namespace Mezzanine
50 {
51  ///////////////////////////////////////////////////////////////////////////////
52  // AttachableBase Methods
53 
55  Updating(false)
56  { }
57 
59  { }
60 
62  { return (AB ? AB->Updating : false); }
63 
64  ///////////////////////////////////////////////////////////////////////////////
65  // Conversion Functions
66 
68  { return (this->GetOrientation() * Location * this->GetScaling()) + this->GetLocation(); }
69 
71  { return this->GetOrientation().GetInverse() * (Location - this->GetLocation()) / this->GetScaling(); }
72 
74  { return this->GetOrientation() * Orientation; }
75 
77  { return this->GetOrientation().GetInverse() * Orientation; }
78 
79  ///////////////////////////////////////////////////////////////////////////////
80  // Internal Methods
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // AttachableParent Methods
84 
86  { }
87 
89  { }
90 
91  ///////////////////////////////////////////////////////////////////////////////
92  // Attachment child management
93 
95  {
96  if((AttachableBase*)Target == (AttachableBase*)this) //don't be stoopid, can't attach to yourself
97  return;
98  /*if( ( this->GetType() >= Mezzanine::WSO_AEFirst && this->GetType() <= Mezzanine::WSO_AELast ) &&
99  ( Target->GetType() >= Mezzanine::WSO_AEFirst && Target->GetType() <= Mezzanine::WSO_AELast ) ) //do not permit AE's to attach to other AE's
100  Entresol::GetSingletonPtr()->LogAndThrow(Exception("Cannot attach AreaEffects to other AreaEffects.")); */
101  if(Target->Parent)
102  {
103  if(Target->Parent == this) return;
104  else MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Cannot attach object to multiple AttachableParent instances.");
105  }
106 
107  Attached.push_back(Target);
108  Target->Parent = this;
109 
110  Target->SetLocation( this->ConvertLocalToGlobal( Target->GetLocalLocation() ) );
111  Target->SetOrientation( this->ConvertLocalToGlobal( Target->GetLocalOrientation() ) );
112  }
113 
115  {
116  for( AttachableIterator it = Attached.begin() ; it != Attached.end() ; ++it )
117  {
118  if(Target == (*it))
119  {
120  Attached.erase(it);
121  return;
122  }
123  }
124  Target->Parent = NULL;
125  }
126 
128  { Attached.clear(); }
129 
131  { return Attached.size(); }
132 
134  { return Attached.at(Index); }
135 
137  { return Attached.begin(); }
138 
140  { return Attached.end(); }
141 
143  { return Attached.begin(); }
144 
146  { return Attached.end(); }
147 
148  ///////////////////////////////////////////////////////////////////////////////
149  // Internal Methods
150 
152  {
153  if( Attached.empty() )
154  return;
155  Updating = true;
156  for( AttachableIterator it = Attached.begin() ; it != Attached.end() ; ++it )
157  {
158  (*it)->_RecalculateGlobalTransform(true);
159  }
160  Updating = false;
161  }
162 
163  ///////////////////////////////////////////////////////////////////////////////
164  // AttachableChild Methods
165 
167  Parent(NULL),
168  LocalTransformDirty(false),
169  GlobalTransformDirty(false)
170  { LocalXform.SetIdentity(); }
171 
173  { }
174 
175  ///////////////////////////////////////////////////////////////////////////////
176  // Utility Functions
177 
179  { return Parent; }
180 
181  ///////////////////////////////////////////////////////////////////////////////
182  // Transform Functions
183 
185  { return LocalXform.Location; }
186 
188  { return LocalXform.Rotation; }
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Internal Methods
192 
194  {
195  // Set data for parent updates appropriately.
196  // Can't assign directly in case "FromParent" is false and we need an update, such as when SetLocation() is invoked directly.
197  if(FromParent)
198  GlobalTransformDirty = true;
199 
200  if(Parent && GlobalTransformDirty)
201  {
202  this->SetOrientation( Parent->ConvertLocalToGlobal(LocalXform.Rotation) );
203  this->SetLocation( Parent->ConvertLocalToGlobal(LocalXform.Location) );
204  }
205  GlobalTransformDirty = false;
206  }
207 
209  {
210  if(Parent && LocalTransformDirty)
211  {
212  this->LocalXform.Location = Parent->ConvertGlobalToLocal( this->GetLocation() );
213  this->LocalXform.Rotation = Parent->ConvertGlobalToLocal( this->GetOrientation() );
214  }
215  LocalTransformDirty = false;
216  }
217 
218  ///////////////////////////////////////////////////////////////////////////////
219  // Pure Virtual Functions
220 
221  // The coding that goes into pure virtual functions is not for inexperienced eyes such
222  // as yours. Take your training to the Mountaintop monanstary and learn the ancient ways of
223  // c++ from the Tibetan monks you find there. Godspeed.
224 }//Mezzanine
225 
226 #endif
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
AttachableBase()
Class constructor.
Definition: attachable.cpp:54
Boole GetUpdating(AttachableBase *AB) const
Gets the update status of another attachable.
Definition: attachable.cpp:61
virtual void SetOrientation(const Quaternion &Orientation)=0
Sets the orientation of this object.
Contains the Mezzanine::Attachable Class and Mezzanine::Attachable::AttachableElement enumeration dec...
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Base class for objects that can have attachables attached to them.
Definition: attachable.h:133
This class is the base class for objects that can be attached to AttachableParent.
Definition: attachable.h:204
virtual Whole GetNumAttached() const
Gets the number of elements attached to this object.
Definition: attachable.cpp:130
This class is the base class for other attachable classes and is responsible for transform updates to...
Definition: attachable.h:59
Quaternion GetInverse() const
Inverses this Quaternion.
Definition: quaternion.cpp:228
AttachableContainer::const_iterator ConstAttachableIterator
Const Iterator type for AttachableChild instances stored by this class.
Definition: attachable.h:141
virtual ~AttachableChild()
Class destructor.
Definition: attachable.cpp:172
This implements the exception hiearchy for Mezzanine.
virtual void SetLocation(const Vector3 &Location)=0
Sets the Location of this object.
AttachableIterator EndChild()
Get an AttachableIterator to one past the last object.
Definition: attachable.cpp:139
virtual void DetachObject(AttachableChild *Target)
Detaches an attachable element from this object.
Definition: attachable.cpp:114
AttachableParent()
Class constructor.
Definition: attachable.cpp:85
virtual void DetachAllChildren()
Detaches all attachables currently attached.
Definition: attachable.cpp:127
AttachableContainer Attached
A container storing all of the other attachable objects connected to this attachable.
Definition: attachable.h:145
AttachableIterator BeginChild()
Get an AttachableIterator to the first object.
Definition: attachable.cpp:136
virtual AttachableChild * GetAttached(const Whole &Index) const
Get a specific attached Item.
Definition: attachable.cpp:133
Quaternion Rotation
The Rotation or relative rotation is stored in a Quaternion.
Definition: transform.h:71
virtual ~AttachableParent()
Class destructor.
Definition: attachable.cpp:88
virtual Vector3 GetScaling() const =0
Gets the scale of this object.
virtual ~AttachableBase()
Class destructor.
Definition: attachable.cpp:58
AttachableContainer::iterator AttachableIterator
Iterator type for AttachableChild instances stored by this class.
Definition: attachable.h:139
virtual Vector3 GetLocation() const =0
Gets the Location of this object.
virtual void AttachObject(AttachableChild *Target)
Attaches an attachable element to this object.
Definition: attachable.cpp:94
void _RecalculateLocalTransform()
Recalculates this objects local transform based on it's current global position.
Definition: attachable.cpp:208
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
Vector3 GetLocalLocation() const
Gets the Location of this object in local space.
Definition: attachable.cpp:184
Vector3 Location
The location or relative location is stored in a Vector3.
Definition: transform.h:68
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
void _RecalculateGlobalTransform(Boole FromParent=false)
Recalculates objects global transform from parent.
Definition: attachable.cpp:193
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Vector3 ConvertGlobalToLocal(const Vector3 &Location) const
Converts a point in global space to the same point in local space.
Definition: attachable.cpp:70
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Vector3 ConvertLocalToGlobal(const Vector3 &Location) const
Converts a point in local space to the same point in global space.
Definition: attachable.cpp:67
AttachableChild()
Class constructor.
Definition: attachable.cpp:166
void SetIdentity()
Sets default construction values for all members.
Definition: transform.cpp:65
AttachableParent * GetParent() const
Gets the parent of this child.
Definition: attachable.cpp:178
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
void _RecalculateAllChildTransforms()
Recalculates the transforms of all children of this attachable.
Definition: attachable.cpp:151
Quaternion GetLocalOrientation() const
Gets the orientation of this object in local space.
Definition: attachable.cpp:187
virtual Quaternion GetOrientation() const =0
Gets the orientation of this object.