Spinning Topp Logo BlackTopp Studios
inc
worldproxy.cpp
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 _worldproxy_cpp
41 #define _worldproxy_cpp
42 
43 #include "worldproxy.h"
44 #include "worldobject.h"
45 
46 #include "enumerations.h"
47 #include "exception.h"
48 #include "serialization.h"
49 #include "stringtool.h"
50 
51 namespace Mezzanine
52 {
54  ParentObject(NULL),
55  ProxyID(0)
56  { }
57 
59  ParentObject(NULL),
60  ProxyID(ID)
61  { }
62 
64  { }
65 
67  { this->ProtoSerializeProperties(SelfRoot); }
68 
70  { this->ProtoDeSerializeProperties(SelfRoot); }
71 
72  ///////////////////////////////////////////////////////////////////////////////
73  // Utility
74 
76  { return this->ParentObject; }
77 
79  { return this->ProxyID; }
80 
81  ///////////////////////////////////////////////////////////////////////////////
82  // Serialization
83 
84  void WorldProxy::ProtoSerialize(XML::Node& ParentNode) const
85  {
86  XML::Node SelfRoot = ParentNode.AppendChild(this->GetDerivedSerializableName());
87  if( !SelfRoot.AppendAttribute("InWorld").SetValue( this->IsInWorld() ? "true" : "false" ) ) {
88  SerializeError("Create XML Attribute Values",WorldProxy::GetSerializableName(),true);
89  }
90 
91  this->ProtoSerializeImpl(SelfRoot);
92  }
93 
95  {
96  XML::Node PropertiesNode = SelfRoot.AppendChild( WorldProxy::GetSerializableName() + "Properties" );
97 
98  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
99  PropertiesNode.AppendAttribute("ProxyID").SetValue(this->ProxyID) )
100  {
101  XML::Node LocationNode = PropertiesNode.AppendChild("Location");
102  this->GetLocation().ProtoSerialize( LocationNode );
103  XML::Node OrientationNode = PropertiesNode.AppendChild("Orientation");
104  this->GetOrientation().ProtoSerialize( OrientationNode );
105  XML::Node ScaleNode = PropertiesNode.AppendChild("Scale");
106  this->GetScale().ProtoSerialize( ScaleNode );
107 
108  return;
109  }else{
110  SerializeError("Create XML Attribute Values",WorldProxy::GetSerializableName() + "Properties",true);
111  }
112  }
113 
115  {
116  Boole WasInWorld = false;
117  XML::Attribute InWorldAttrib = SelfRoot.GetAttribute("InWorld");
118  if( !InWorldAttrib.Empty() ) {
119  WasInWorld = StringTools::ConvertToBool( InWorldAttrib.AsString() );
120  }
121 
122  this->ProtoDeSerializeImpl(SelfRoot);
123 
124  if( WasInWorld ) {
125  this->AddToWorld();
126  }
127  }
128 
130  {
131  XML::Attribute CurrAttrib;
132  XML::Node PropertiesNode = SelfRoot.GetChild( WorldProxy::GetSerializableName() + "Properties" );
133 
134  if( !PropertiesNode.Empty() ) {
135  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
136  CurrAttrib = PropertiesNode.GetAttribute("ProxyID");
137  if( !CurrAttrib.Empty() )
138  this->ProxyID = static_cast<UInt32>( CurrAttrib.AsUint() );
139 
140  XML::Node PositionNode = PropertiesNode.GetChild("Location").GetFirstChild();
141  if( !PositionNode.Empty() ) {
142  Vector3 Loc(PositionNode);
143  this->SetLocation(Loc);
144  }
145 
146  XML::Node OrientationNode = PropertiesNode.GetChild("Orientation").GetFirstChild();
147  if( !PositionNode.Empty() ) {
148  Quaternion Rot(OrientationNode);
149  this->SetOrientation(Rot);
150  }
151 
152  XML::Node ScaleNode = PropertiesNode.GetChild("Scale").GetFirstChild();
153  if( !PositionNode.Empty() ) {
154  Vector3 Scale(ScaleNode);
155  this->SetScale(Scale);
156  }
157  }else{
158  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (WorldProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
159  }
160  }else{
161  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,WorldProxy::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
162  }
163  }
164 
166  { return WorldProxy::GetSerializableName(); }
167 
169  { return "WorldProxy"; }
170 
171  ///////////////////////////////////////////////////////////////////////////////
172  // Internal Methods
173 
175  {
176  if( ParentObject != NewParent ) {
177  ParentObject = NewParent;
178  /// @todo Notify something? Perhaps use the new event system?
179  }
180  }
181 }//Mezzanine
182 
183 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual Vector3 GetLocation() const =0
Gets this objects current location.
virtual UInt32 GetProxyID() const
Gets the unique ID of this proxy.
Definition: worldproxy.cpp:78
Thrown when the requested identity could not be found.
Definition: exception.h:94
virtual void ProtoSerializeImpl(XML::Node &SelfRoot) const
Implementation method for serializing additional sets of data.
Definition: worldproxy.cpp:66
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: worldproxy.cpp:114
virtual Quaternion GetOrientation() const =0
Gets this objects current orientation.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: worldproxy.cpp:129
Any global enumerations shared between multiple classes is to be declared here.
bool Empty() const
Is this storing anything at all?
virtual void SetScale(const Vector3 &Sc)=0
Sets the scaling to be applied to this object.
This implements the exception hiearchy for Mezzanine.
virtual void Scale(const Vector3 &Scale)=0
Scales the object from it's current size.
virtual void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: worldproxy.cpp:84
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldProxy.
Definition: worldproxy.cpp:165
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
This is the base class from which classes that are insertable into the physical world.
Definition: worldobject.h:60
Boole ConvertToBool(const String &ToConvert, const Boole Default)
Converts a string into a Boole.
Definition: stringtool.cpp:379
virtual Vector3 GetScale() const =0
Gets the scaling currently being applied to this object.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
virtual void SetOrientation(const Quaternion &Ori)=0
Sets the orientation of this object in parent space.
unsigned int AsUint(unsigned int def=0) const
Attempts to convert the value of the attribute to an unsigned int and returns the results...
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: worldproxy.cpp:94
bool Empty() const
Is this storing anything at all?
void _Bind(WorldObject *NewParent)
Binds this proxy to a WorldObject.
Definition: worldproxy.cpp:174
virtual void AddToWorld()=0
Performs all the necessary task to ensure this object is connected to it's respective world and ready...
WorldObject * ParentObject
Pointer to the Object this proxy belongs to.
Definition: worldproxy.h:58
UInt32 ProxyID
The unique ID assigned to the type of proxy an instance is.
Definition: worldproxy.h:61
virtual Boole IsInWorld() const =0
Gets whether or not this object is inside of it's world.
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
Definition: worldproxy.cpp:168
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
virtual ~WorldProxy()
Class destructor.
Definition: worldproxy.cpp:63
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual WorldObject * GetParentObject() const
Gets a pointer to the parent object controlling this proxy.
Definition: worldproxy.cpp:75
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: quaternion.cpp:502
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
WorldProxy()
Blank constructor.
Definition: worldproxy.cpp:53
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector3.cpp:588
virtual void SetLocation(const Vector3 &Loc)=0
Sets the location of this object in parent space.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
virtual void ProtoDeSerializeImpl(const XML::Node &SelfRoot)
Implementation method for deseriailizing additional sets of data.
Definition: worldproxy.cpp:69