MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
attachable.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 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(Exception::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