MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
billboardsetproxy.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 _graphicsbillboardsetproxy_cpp
41 #define _graphicsbillboardsetproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the World proxy wrapping billboard functionality.
45 
47 #include "Graphics/scenemanager.h"
48 #include "Graphics/billboard.h"
49 
50 #include "exception.h"
51 #include "serialization.h"
52 #include "stringtool.h"
53 
54 #include <Ogre.h>
55 
56 namespace
57 {
58  /// @internal
59  /// @brief Converts an Ogre BillboardOrigin enum value to it's corresponding Mezzanine value.
60  /// @param Origin The Ogre value to be converted.
61  /// @return Returns the Mezzanine BillboardOrigin corresponding to the provided Ogre value.
62  Mezzanine::Graphics::BillboardOrigin ConvertBillboardOrigin(const Ogre::BillboardOrigin Origin)
63  {
64  switch(Origin)
65  {
66  case Ogre::BBO_TOP_LEFT: return Mezzanine::Graphics::BBO_Top_Left; break;
67  case Ogre::BBO_TOP_CENTER: return Mezzanine::Graphics::BBO_Top_Center; break;
68  case Ogre::BBO_TOP_RIGHT: return Mezzanine::Graphics::BBO_Top_Right; break;
69  case Ogre::BBO_CENTER_LEFT: return Mezzanine::Graphics::BBO_Center_Left; break;
70  case Ogre::BBO_CENTER: return Mezzanine::Graphics::BBO_Center; break;
71  case Ogre::BBO_CENTER_RIGHT: return Mezzanine::Graphics::BBO_Center_Right; break;
72  case Ogre::BBO_BOTTOM_LEFT: return Mezzanine::Graphics::BBO_Bottom_Left; break;
73  case Ogre::BBO_BOTTOM_CENTER: return Mezzanine::Graphics::BBO_Bottom_Center; break;
74  case Ogre::BBO_BOTTOM_RIGHT: return Mezzanine::Graphics::BBO_Bottom_Right; break;
75  }
77  }
78  /// @internal
79  /// @brief Converts a Mezzanine BillboardOrigin enum value to it's corresponding Ogre value.
80  /// @param Origin The Mezzanine value to be converted.
81  /// @return Returns the Ogre BillboardOrigin corresponding to the provided Mezzanine value.
82  Ogre::BillboardOrigin ConvertBillboardOrigin(const Mezzanine::Graphics::BillboardOrigin Origin)
83  {
84  switch(Origin)
85  {
86  case Mezzanine::Graphics::BBO_Top_Left: return Ogre::BBO_TOP_LEFT; break;
87  case Mezzanine::Graphics::BBO_Top_Center: return Ogre::BBO_TOP_CENTER; break;
88  case Mezzanine::Graphics::BBO_Top_Right: return Ogre::BBO_TOP_RIGHT; break;
89  case Mezzanine::Graphics::BBO_Center_Left: return Ogre::BBO_CENTER_LEFT; break;
90  case Mezzanine::Graphics::BBO_Center: return Ogre::BBO_CENTER; break;
91  case Mezzanine::Graphics::BBO_Center_Right: return Ogre::BBO_CENTER_RIGHT; break;
92  case Mezzanine::Graphics::BBO_Bottom_Left: return Ogre::BBO_BOTTOM_LEFT; break;
93  case Mezzanine::Graphics::BBO_Bottom_Center: return Ogre::BBO_BOTTOM_CENTER; break;
94  case Mezzanine::Graphics::BBO_Bottom_Right: return Ogre::BBO_BOTTOM_RIGHT; break;
95  }
96  return Ogre::BBO_CENTER;
97  }
98 
99  /// @internal
100  /// @brief Converts an Ogre BillboardRotation enum value to it's corresponding Mezzanine type.
101  /// @param Rotation The Ogre type to be converted.
102  /// @return Returns the Mezzanine BillboardRotation corresponding to the provided Ogre type.
103  Mezzanine::Graphics::BillboardRotation ConvertBillboardRotation(const Ogre::BillboardRotationType Rotation)
104  {
105  switch(Rotation)
106  {
107  case Ogre::BBR_VERTEX: return Mezzanine::Graphics::BBR_Vertex; break;
108  case Ogre::BBR_TEXCOORD: return Mezzanine::Graphics::BBR_TexureCoord; break;
109  }
111  }
112  /// @internal
113  /// @brief Converts a Mezzanine BillboardRotation enum value to it's corresponding Ogre type.
114  /// @param Rotation The Mezzanine type to be converted.
115  /// @return Returns the Ogre BillboardRotation corresponding to the provided Mezzanine type.
116  Ogre::BillboardRotationType ConvertBillboardRotation(const Mezzanine::Graphics::BillboardRotation Rotation)
117  {
118  switch(Rotation)
119  {
120  case Mezzanine::Graphics::BBR_Vertex: return Ogre::BBR_VERTEX; break;
121  case Mezzanine::Graphics::BBR_TexureCoord: return Ogre::BBR_TEXCOORD; break;
122  }
123  return Ogre::BBR_TEXCOORD;
124  }
125 
126  /// @internal
127  /// @brief Converts an Ogre BillboardType enum value to it's corresponding Mezzanine type.
128  /// @param Type The Ogre type to be converted.
129  /// @return Returns the Mezzanine BillboardType corresponding to the provided Ogre type.
130  Mezzanine::Graphics::BillboardType ConvertBillboardType(const Ogre::BillboardType Type)
131  {
132  switch(Type)
133  {
134  case Ogre::BBT_POINT: return Mezzanine::Graphics::BBT_Point; break;
135  case Ogre::BBT_ORIENTED_COMMON: return Mezzanine::Graphics::BBT_Oriented_Common; break;
136  case Ogre::BBT_ORIENTED_SELF: return Mezzanine::Graphics::BBT_Oriented_Self; break;
137  case Ogre::BBT_PERPENDICULAR_COMMON: return Mezzanine::Graphics::BBT_Perpendicular_Common; break;
138  case Ogre::BBT_PERPENDICULAR_SELF: return Mezzanine::Graphics::BBT_Perpendicular_Self; break;
139  }
141  }
142  /// @internal
143  /// @brief Converts a Mezzanine BillboardType enum value to it's corresponding Ogre type.
144  /// @param Type The Mezzanine type to be converted.
145  /// @return Returns the Ogre BillboardType corresponding to the provided Mezzanine type.
146  Ogre::BillboardType ConvertBillboardType(const Mezzanine::Graphics::BillboardType Type)
147  {
148  switch(Type)
149  {
150  case Mezzanine::Graphics::BBT_Point: return Ogre::BBT_POINT; break;
151  case Mezzanine::Graphics::BBT_Oriented_Common: return Ogre::BBT_ORIENTED_COMMON; break;
152  case Mezzanine::Graphics::BBT_Oriented_Self: return Ogre::BBT_ORIENTED_SELF; break;
153  case Mezzanine::Graphics::BBT_Perpendicular_Common: return Ogre::BBT_PERPENDICULAR_COMMON; break;
154  case Mezzanine::Graphics::BBT_Perpendicular_Self: return Ogre::BBT_PERPENDICULAR_SELF; break;
155  }
156  return Ogre::BBT_POINT;
157  }
158 }
159 
160 namespace Mezzanine
161 {
162  namespace Graphics
163  {
164  BillboardSetProxy::BillboardSetProxy(const UInt32 InitialPoolSize, SceneManager* Creator) :
165  RenderableProxy(Creator)
166  { this->CreateBillboardSet(InitialPoolSize); }
167 
169  RenderableProxy(Creator)
170  {
171  this->CreateBillboardSet(20);
172  this->ProtoDeSerialize(SelfRoot);
173  }
174 
176  { this->DestroyBillboardSet(); }
177 
178  void BillboardSetProxy::CreateBillboardSet(const UInt32 InitialPoolSize)
179  {
180  this->GraphicsBillboardSet = this->Manager->_GetGraphicsWorldPointer()->createBillboardSet(InitialPoolSize);
181  this->GraphicsNode->attachObject( this->GraphicsBillboardSet );
182  this->GraphicsBillboardSet->MovableObject::setUserAny( Ogre::Any( static_cast<RenderableProxy*>( this ) ) );
183  this->GraphicsBillboardSet->setVisibilityFlags(0);
184  this->GraphicsBillboardSet->setQueryFlags(0);
185  }
186 
188  {
189  if( this->GraphicsBillboardSet ) {
190  this->GraphicsNode->detachObject( this->GraphicsBillboardSet );
191  this->Manager->_GetGraphicsWorldPointer()->destroyBillboardSet( this->GraphicsBillboardSet );
192  }
193  }
194 
195  ///////////////////////////////////////////////////////////////////////////////
196  // Utility
197 
199  {
200  return Mezzanine::PT_Graphics_BillboardSetProxy;
201  }
202 
203  ///////////////////////////////////////////////////////////////////////////////
204  // Billboard Management
205 
207  {
208  Billboard* NewBB = new Billboard( this->GraphicsBillboardSet->createBillboard(Location.GetOgreVector3(),Colour.GetOgreColourValue()) );
209  this->Billboards.push_back(NewBB);
210  return NewBB;
211  }
212 
214  {
215  BillboardIterator Ret = this->Billboards.begin();
216  if( Index == 0 )
217  return *Ret;
218 
219  while( Index-- )
220  ++Ret;
221 
222  return *Ret;
223  }
224 
226  {
227  return this->Billboards.size();
228  }
229 
231  {
232  this->GraphicsBillboardSet->removeBillboard( ToBeDestroyed->_GetGraphicsObject() );
233  for( BillboardIterator BillIt = this->Billboards.begin() ; BillIt != this->Billboards.end() ; ++BillIt )
234  {
235  if( (*BillIt) == ToBeDestroyed )
236  {
237  delete (*BillIt);
238  this->Billboards.erase(BillIt);
239  return;
240  }
241  }
242  }
243 
245  {
246  for( BillboardIterator BillIt = this->Billboards.begin() ; BillIt != this->Billboards.end() ; ++BillIt )
247  {
248  delete (*BillIt);
249  }
250  this->Billboards.clear();
251  this->GraphicsBillboardSet->clear();
252  }
253 
254  ///////////////////////////////////////////////////////////////////////////////
255  // BillboardSet Properties
256 
257  void BillboardSetProxy::SetAutoExtend(const Boolean AutoExtend)
258  { this->GraphicsBillboardSet->setAutoextend(AutoExtend); }
259 
261  { return this->GraphicsBillboardSet->getAutoextend(); }
262 
263  void BillboardSetProxy::SetAccurateFacing(const Boolean AccFace)
264  { this->GraphicsBillboardSet->setUseAccurateFacing(AccFace); }
265 
267  { return this->GraphicsBillboardSet->getUseAccurateFacing(); }
268 
270  { this->GraphicsBillboardSet->setPoolSize(Size); }
271 
273  { return this->GraphicsBillboardSet->getPoolSize(); }
274 
276  { this->GraphicsBillboardSet->setBillboardOrigin( ConvertBillboardOrigin(Origin) ); }
277 
279  { return ConvertBillboardOrigin( this->GraphicsBillboardSet->getBillboardOrigin() ); }
280 
282  { this->GraphicsBillboardSet->setBillboardRotationType( ConvertBillboardRotation(Rotation) ); }
283 
285  { return ConvertBillboardRotation( this->GraphicsBillboardSet->getBillboardRotationType() ); }
286 
288  { this->GraphicsBillboardSet->setBillboardType( ConvertBillboardType(Type) ); }
289 
291  { return ConvertBillboardType( this->GraphicsBillboardSet->getBillboardType() ); }
292 
293  void BillboardSetProxy::SetDefaultDimensions(const Real Width, const Real Height)
294  { this->GraphicsBillboardSet->setDefaultDimensions(Width,Height); }
295 
297  { this->GraphicsBillboardSet->setDefaultWidth(Width); }
298 
300  { return this->GraphicsBillboardSet->getDefaultWidth(); }
301 
303  { this->GraphicsBillboardSet->setDefaultHeight(Height); }
304 
306  { return this->GraphicsBillboardSet->getDefaultHeight(); }
307 
309  { this->GraphicsBillboardSet->setCommonDirection( Dir.GetOgreVector3() ); }
310 
312  { return Vector3( this->GraphicsBillboardSet->getCommonDirection() ); }
313 
315  { this->GraphicsBillboardSet->setCommonUpVector( UpDir.GetOgreVector3() ); }
316 
318  { return Vector3( this->GraphicsBillboardSet->getCommonUpVector() ); }
319 
321  { this->GraphicsBillboardSet->setMaterialName(MatName); }
322 
324  { return this->GraphicsBillboardSet->getMaterialName(); }
325 
326  ///////////////////////////////////////////////////////////////////////////////
327  // Serialization
328 
330  {
331  XML::Node SelfRoot = ParentNode.AppendChild(this->GetDerivedSerializableName());
332  if( !SelfRoot.AppendAttribute("InWorld").SetValue( this->IsInWorld() ? "true" : "false" ) ) {
333  SerializeError("Create XML Attribute Values",BillboardSetProxy::GetSerializableName(),true);
334  }
335 
336  this->ProtoSerializeProperties(SelfRoot);
337  this->ProtoSerializeBillboards(SelfRoot);
338  }
339 
341  {
343 
344  XML::Node PropertiesNode = SelfRoot.AppendChild( BillboardSetProxy::GetSerializableName() + "Properties" );
345 
346  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
347  PropertiesNode.AppendAttribute("AutoExtend").SetValue( this->GetAutoExtend() ? "true" : "false" ) &&
348  PropertiesNode.AppendAttribute("AccurateFacing").SetValue( this->GetAccurateFacing() ? "true" : "false" ) &&
349  PropertiesNode.AppendAttribute("PoolSize").SetValue( this->GetPoolSize() ) &&
350  PropertiesNode.AppendAttribute("BillboardOrigin").SetValue( static_cast<Whole>( this->GetBillboardOrigin() ) ) &&
351  PropertiesNode.AppendAttribute("BillboardRotation").SetValue( static_cast<Whole>( this->GetBillboardRotation() ) ) &&
352  PropertiesNode.AppendAttribute("BillboardType").SetValue( static_cast<Whole>( this->GetBillboardType() ) ) &&
353  PropertiesNode.AppendAttribute("DefaultWidth").SetValue( this->GetDefaultWidth() ) &&
354  PropertiesNode.AppendAttribute("DefaultHeight").SetValue( this->GetDefaultHeight() ) &&
355  PropertiesNode.AppendAttribute("MaterialName").SetValue( this->GetMaterialName() ) )
356  {
357  XML::Node CommonDirectionNode = PropertiesNode.AppendChild("CommonDirection");
358  this->GetCommonDirection().ProtoSerialize( CommonDirectionNode );
359  XML::Node CommonUpVectorNode = PropertiesNode.AppendChild("CommonUpVector");
360  this->GetCommonUpVector().ProtoSerialize( CommonUpVectorNode );
361 
362  return;
363  }else{
364  SerializeError("Create XML Attribute Values",BillboardSetProxy::GetSerializableName() + "Properties",true);
365  }
366  }
367 
369  {
370  XML::Node BillboardsNode = SelfRoot.AppendChild( BillboardSetProxy::GetSerializableName() + "Billboards" );
371 
372  if( BillboardsNode.AppendAttribute("Version").SetValue("1") )
373  {
374  for( ConstBillboardIterator BillIt = this->Billboards.begin() ; BillIt != this->Billboards.end() ; ++BillIt )
375  {
376  (*BillIt)->ProtoSerialize( BillboardsNode );
377  }
378  }else{
379  SerializeError("Create XML Attribute Values",BillboardSetProxy::GetSerializableName() + "Billboards",true);
380  }
381  }
382 
384  {
385  Boolean WasInWorld = false;
386  XML::Attribute InWorldAttrib = SelfRoot.GetAttribute("InWorld");
387  if( !InWorldAttrib.Empty() ) {
388  WasInWorld = StringTools::ConvertToBool( InWorldAttrib.AsString() );
389  }
390 
391  this->DestroyAllBillboards();
392  this->ProtoDeSerializeProperties(SelfRoot);
393  this->ProtoDeSerializeBillboards(SelfRoot);
394 
395  if( WasInWorld ) {
396  this->AddToWorld();
397  }
398  }
399 
401  {
403 
404  XML::Attribute CurrAttrib;
405  XML::Node PropertiesNode = SelfRoot.GetChild( BillboardSetProxy::GetSerializableName() + "Properties" );
406 
407  if( !PropertiesNode.Empty() ) {
408  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
409  CurrAttrib = PropertiesNode.GetAttribute("AutoExtend");
410  if( !CurrAttrib.Empty() )
411  this->SetAutoExtend( StringTools::ConvertToBool( CurrAttrib.AsString() ) );
412 
413  CurrAttrib = PropertiesNode.GetAttribute("AccurateFacing");
414  if( !CurrAttrib.Empty() )
415  this->SetAccurateFacing( StringTools::ConvertToBool( CurrAttrib.AsString() ) );
416 
417  CurrAttrib = PropertiesNode.GetAttribute("PoolSize");
418  if( !CurrAttrib.Empty() )
419  this->SetPoolSize( CurrAttrib.AsReal() );
420 
421  CurrAttrib = PropertiesNode.GetAttribute("BillboardOrigin");
422  if( !CurrAttrib.Empty() )
423  this->SetBillboardOrigin( static_cast<Graphics::BillboardOrigin>( CurrAttrib.AsWhole() ) );
424 
425  CurrAttrib = PropertiesNode.GetAttribute("BillboardRotation");
426  if( !CurrAttrib.Empty() )
427  this->SetBillboardRotation( static_cast<Graphics::BillboardRotation>( CurrAttrib.AsWhole() ) );
428 
429  CurrAttrib = PropertiesNode.GetAttribute("BillboardType");
430  if( !CurrAttrib.Empty() )
431  this->SetBillboardType( static_cast<Graphics::BillboardType>( CurrAttrib.AsWhole() ) );
432 
433  CurrAttrib = PropertiesNode.GetAttribute("DefaultWidth");
434  if( !CurrAttrib.Empty() )
435  this->SetDefaultWidth( CurrAttrib.AsReal() );
436 
437  CurrAttrib = PropertiesNode.GetAttribute("DefaultHeight");
438  if( !CurrAttrib.Empty() )
439  this->SetDefaultHeight( CurrAttrib.AsReal() );
440 
441  CurrAttrib = PropertiesNode.GetAttribute("MaterialName");
442  if( !CurrAttrib.Empty() )
443  this->SetMaterialName( CurrAttrib.AsString() );
444 
445  XML::Node CommonDirectionNode = PropertiesNode.GetChild("CommonDirection").GetFirstChild();
446  if( !CommonDirectionNode.Empty() ) {
447  Vector3 CommonDir(CommonDirectionNode);
448  this->SetCommonDirection(CommonDir);
449  }
450 
451  XML::Node CommonUpVectorNode = PropertiesNode.GetChild("CommonUpVector").GetFirstChild();
452  if( !CommonUpVectorNode.Empty() ) {
453  Vector3 CommonUpVec(CommonUpVectorNode);
454  this->SetCommonUpVector(CommonUpVec);
455  }
456  }else{
457  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (BillboardSetProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
458  }
459  }else{
460  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,BillboardSetProxy::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
461  }
462  }
463 
465  {
466  XML::Node BillboardsNode = SelfRoot.GetChild( BillboardSetProxy::GetSerializableName() + "Billboards" );
467 
468  if( !BillboardsNode.Empty() ) {
469  if(BillboardsNode.GetAttribute("Version").AsInt() == 1) {
470  for( XML::NodeIterator BillIt = BillboardsNode.begin() ; BillIt != BillboardsNode.end() ; ++BillIt )
471  {
472  Billboard* NewBB = this->CreateBillboard( Vector3() );
473  NewBB->ProtoDeSerialize( (*BillIt) );
474  }
475  }
476  }
477  }
478 
481 
483  { return "BillboardSetProxy"; }
484 
485  ///////////////////////////////////////////////////////////////////////////////
486  // Internal Methods
487 
488  Ogre::BillboardSet* BillboardSetProxy::_GetGraphicsObject() const
489  { return this->GraphicsBillboardSet; }
490 
491  Ogre::MovableObject* BillboardSetProxy::_GetBaseGraphicsObject() const
492  { return this->GraphicsBillboardSet; }
493  }//Graphics
494 }//Mezzanine
495 
496 #endif