MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
entityproxy.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 _graphicsentityproxy_cpp
41 #define _graphicsentityproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the World proxy wrapping basic entity(mesh) functionality.
45 
46 #include "Graphics/entityproxy.h"
47 #include "Graphics/scenemanager.h"
48 #include "Graphics/meshmanager.h"
49 #include "Graphics/mesh.h"
50 
51 #include "enumerations.h"
52 #include "exception.h"
53 #include "serialization.h"
54 #include "stringtool.h"
55 
56 #include <Ogre.h>
57 
58 namespace Mezzanine
59 {
60  namespace Graphics
61  {
63  RenderableProxy(Creator),
64  GraphicsEntity(NULL),
65  ProxyMesh(NULL),
66  RenderDist(0),
67  LightMask(0xFFFFFFFF),
68  SceneVisible(true),
69  CanCastShadows(true)
70  { this->CreateEntity(NULL); }
71 
73  RenderableProxy(Creator),
74  GraphicsEntity(NULL),
75  ProxyMesh(NULL),
76  RenderDist(0),
77  LightMask(0xFFFFFFFF),
78  SceneVisible(true),
79  CanCastShadows(true)
80  { this->CreateEntity(TheMesh); }
81 
82  EntityProxy::EntityProxy(const String& MeshName, const String& GroupName, SceneManager* Creator) :
83  RenderableProxy(Creator),
84  GraphicsEntity(NULL),
85  ProxyMesh(NULL),
86  RenderDist(0),
87  LightMask(0xFFFFFFFF),
88  SceneVisible(true),
89  CanCastShadows(true)
90  { this->CreateEntity(MeshName,GroupName); }
91 
92  EntityProxy::EntityProxy(const XML::Node& SelfRoot, SceneManager* Creator) :
93  RenderableProxy(Creator),
94  GraphicsEntity(NULL),
95  ProxyMesh(NULL),
96  RenderDist(0),
97  LightMask(0xFFFFFFFF),
98  SceneVisible(true),
99  CanCastShadows(true)
100  { this->ProtoDeSerialize(SelfRoot); }
101 
103  { this->DestroyEntity(); }
104 
105  void EntityProxy::CreateEntity(Mesh* ObjectMesh)
106  {
107  if( ObjectMesh != NULL ) {
108  this->GraphicsEntity = this->Manager->_GetGraphicsWorldPointer()->createEntity( ObjectMesh->_GetInternalMesh() );
109  this->GraphicsNode->attachObject( this->GraphicsEntity );
110  this->GraphicsEntity->setUserAny( Ogre::Any( static_cast<RenderableProxy*>( this ) ) );
111  this->GraphicsEntity->setVisibilityFlags(0);
112  this->GraphicsEntity->setQueryFlags(0);
113  }
114 
115  this->ProxyMesh = ObjectMesh;
116  }
117 
118  void EntityProxy::CreateEntity(const String& MeshName, const String& GroupName)
119  {
120  Mesh* TheMesh = MeshManager::GetSingletonPtr()->LoadMesh(MeshName,GroupName);
121  this->CreateEntity(TheMesh);
122  }
123 
125  {
126  if( this->GraphicsEntity ) {
127  this->GraphicsNode->detachObject( this->GraphicsEntity );
128  this->Manager->_GetGraphicsWorldPointer()->destroyEntity( this->GraphicsEntity );
129  }
130  }
131 
132  ///////////////////////////////////////////////////////////////////////////////
133  // Utility
134 
136  {
137  return Mezzanine::PT_Graphics_EntityProxy;
138  }
139 
141  {
142  if( this->GraphicsEntity && !this->InWorld ) {
143  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
144  this->GraphicsEntity->setQueryFlags( this->QueryMask );
145  this->InWorld = true;
146  }
147  }
148 
150  {
151  if( this->GraphicsEntity && this->InWorld ) {
152  this->GraphicsEntity->setVisibilityFlags(0);
153  this->GraphicsEntity->setQueryFlags(0);
154  this->InWorld = false;
155  }
156  }
157 
158  ///////////////////////////////////////////////////////////////////////////////
159  // Mesh Management
160 
161  void EntityProxy::SetMesh(const String& MeshName, const String& Group)
162  {
163  if( this->GraphicsEntity ) {
164  this->DestroyEntity();
165  }
166  this->CreateEntity(MeshName,Group);
167 
168  this->GraphicsEntity->setVisible( this->SceneVisible );
169  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
170  this->GraphicsEntity->setLightMask( this->LightMask );
171  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
172  this->GraphicsEntity->setQueryFlags( this->QueryMask );
173  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
174  }
175 
176  void EntityProxy::SetMesh(Mesh* ObjectMesh)
177  {
178  if( this->GraphicsEntity ) {
179  this->DestroyEntity();
180  }
181  this->CreateEntity(ObjectMesh);
182 
183  this->GraphicsEntity->setVisible( this->SceneVisible );
184  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
185  this->GraphicsEntity->setLightMask( this->LightMask );
186  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
187  this->GraphicsEntity->setQueryFlags( this->QueryMask );
188  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
189  }
190 
192  {
193  return this->ProxyMesh;
194  }
195 
196  ///////////////////////////////////////////////////////////////////////////////
197  // RenderableProxy Properties
198 
199  void EntityProxy::SetVisible(const Boolean Visible)
200  {
201  this->SceneVisible = Visible;
202  if( this->GraphicsEntity ) {
203  this->GraphicsEntity->setVisible( this->SceneVisible );
204  }
205  }
206 
207  Boolean EntityProxy::GetVisible() const
208  {
209  return this->SceneVisible;
210  }
211 
212  void EntityProxy::SetCastShadows(const Boolean CastShadows)
213  {
214  this->CanCastShadows = CastShadows;
215  if( this->GraphicsEntity ) {
216  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
217  }
218  }
219 
221  {
222  return this->CanCastShadows;
223  }
224 
226  {
227  return ( this->GraphicsEntity ? this->GraphicsEntity->getReceivesShadows() : false );
228  }
229 
231  {
232  this->LightMask = Mask;
233  if( this->GraphicsEntity ) {
234  this->GraphicsEntity->setLightMask( this->LightMask );
235  }
236  }
237 
239  {
240  return this->LightMask;
241  }
242 
244  {
245  this->VisibilityMask = Mask;
246  if( this->GraphicsEntity && this->InWorld ) {
247  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
248  }
249  }
250 
252  {
253  return this->VisibilityMask;
254  }
255 
257  {
258  this->QueryMask = Mask;
259  if( this->GraphicsEntity && this->InWorld ) {
260  this->GraphicsEntity->setQueryFlags( this->QueryMask );
261  }
262  }
263 
265  {
266  return this->QueryMask;
267  }
268 
270  {
271  this->RenderDist = Distance;
272  if( this->GraphicsEntity ) {
273  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
274  }
275  }
276 
278  {
279  return this->RenderDist;
280  }
281 
282  ///////////////////////////////////////////////////////////////////////////////
283  // Entity Properties
284 
285  ///////////////////////////////////////////////////////////////////////////////
286  // Serialization
287 
288  void EntityProxy::ProtoSerialize(XML::Node& ParentNode) const
289  {
290  XML::Node SelfRoot = ParentNode.AppendChild(this->GetDerivedSerializableName());
291  if( !SelfRoot.AppendAttribute("InWorld").SetValue( this->IsInWorld() ? "true" : "false" ) ) {
292  SerializeError("Create XML Attribute Values",EntityProxy::GetSerializableName(),true);
293  }
294 
295  this->ProtoSerializeProperties(SelfRoot);
296  this->ProtoSerializeMesh(SelfRoot);
297  }
298 
300  {
302  }
303 
305  {
306  XML::Node MeshNode = SelfRoot.AppendChild( EntityProxy::GetSerializableName() + "Mesh" );
307 
308  if( MeshNode.AppendAttribute("Version").SetValue("1") &&
309  MeshNode.AppendAttribute("ProxyMeshName").SetValue( this->ProxyMesh ? this->ProxyMesh->GetName() : "" ) &&
310  MeshNode.AppendAttribute("ProxyMeshGroup").SetValue( this->ProxyMesh ? this->ProxyMesh->GetGroup() : "" ) )
311  {
312  return;
313  }else{
314  SerializeError("Create XML Attribute Values",EntityProxy::GetSerializableName() + "Mesh",true);
315  }
316  }
317 
319  {
320  Boolean WasInWorld = false;
321  XML::Attribute InWorldAttrib = SelfRoot.GetAttribute("InWorld");
322  if( !InWorldAttrib.Empty() ) {
323  WasInWorld = StringTools::ConvertToBool( InWorldAttrib.AsString() );
324  }
325 
326  this->ProtoDeSerializeMesh(SelfRoot);
327  this->ProtoDeSerializeProperties(SelfRoot);
328 
329  if( WasInWorld ) {
330  this->AddToWorld();
331  }
332  }
333 
335  {
337  }
338 
340  {
341  XML::Attribute CurrAttrib;
342  XML::Node MeshNode = SelfRoot.GetChild( EntityProxy::GetSerializableName() + "Mesh" );
343 
344  if( !MeshNode.Empty() ) {
345  if(MeshNode.GetAttribute("Version").AsInt() == 1) {
346  String MeshName, MeshGroup = Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME;
347 
348  CurrAttrib = MeshNode.GetAttribute("ProxyMeshName");
349  if( !CurrAttrib.Empty() )
350  MeshName = CurrAttrib.AsString();
351 
352  CurrAttrib = MeshNode.GetAttribute("ProxyMeshGroup");
353  if( !CurrAttrib.Empty() )
354  MeshGroup = CurrAttrib.AsString();
355 
356  if( !MeshName.empty() && !MeshGroup.empty() ) {
357  Mesh* NewMesh = MeshManager::GetSingletonPtr()->LoadMesh( MeshName, MeshGroup );
358  this->SetMesh( NewMesh );
359  }else{
360  this->SetMesh( NULL );
361  }
362  }
363  }
364  }
365 
368 
370  { return "EntityProxy"; }
371 
372  ///////////////////////////////////////////////////////////////////////////////
373  // Internal Methods
374 
375  Ogre::Entity* EntityProxy::_GetGraphicsObject() const
376  { return this->GraphicsEntity; }
377 
378  Ogre::MovableObject* EntityProxy::_GetBaseGraphicsObject() const
379  { return this->GraphicsEntity; }
380  }//Graphics
381 }//Mezzanine
382 
383 #endif