MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
billboard.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 _graphicsbillboard_cpp
41 #define _graphicsbillboard_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the Billboard class used by BillboardSetProxy.
45 
46 #include "Graphics/billboard.h"
47 
48 #include "exception.h"
49 #include "serialization.h"
50 #include "stringtool.h"
51 
52 #include <Ogre.h>
53 
54 namespace Mezzanine
55 {
56  namespace Graphics
57  {
58  Billboard::Billboard(Ogre::Billboard* InternalBillboard) :
59  GraphicsBillboard(InternalBillboard)
60  { }
61 
63  { }
64 
65  ///////////////////////////////////////////////////////////////////////////////
66  // Utility
67 
69  { return this->GraphicsBillboard->hasOwnDimensions(); }
70 
72  { this->GraphicsBillboard->resetDimensions(); }
73 
75  { return this->GraphicsBillboard->isUseTexcoordRect(); }
76 
77  ///////////////////////////////////////////////////////////////////////////////
78  // Billboard Properties
79 
81  { this->GraphicsBillboard->setPosition( Loc.GetOgreVector3() ); }
82 
84  { return Vector3( this->GraphicsBillboard->getPosition() ); }
85 
87  { this->GraphicsBillboard->mDirection = Dir.GetOgreVector3(); }
88 
90  { return Vector3( this->GraphicsBillboard->mDirection ); }
91 
92  void Billboard::SetRotation(const Real Rot)
93  { this->GraphicsBillboard->setRotation( Ogre::Radian(Rot) ); }
94 
96  { return this->GraphicsBillboard->getRotation().valueRadians(); }
97 
98  void Billboard::SetColour(const ColourValue& Colour)
99  { this->GraphicsBillboard->setColour( Colour.GetOgreColourValue() ); }
100 
102  { return ColourValue( this->GraphicsBillboard->getColour() ); }
103 
104  void Billboard::SetDimensions(const Real Width, const Real Height)
105  { this->GraphicsBillboard->setDimensions(Width,Height); }
106 
108  { return this->GraphicsBillboard->getOwnWidth(); }
109 
111  { return this->GraphicsBillboard->getOwnHeight(); }
112 
113  void Billboard::SetTextureCoordRect(const Real Left, const Real Top, const Real Right, const Real Bottom)
114  { this->GraphicsBillboard->setTexcoordRect(Left,Top,Right,Bottom); }
115 
117  { return this->GraphicsBillboard->getTexcoordRect().left; }
118 
120  { return this->GraphicsBillboard->getTexcoordRect().top; }
121 
123  { return this->GraphicsBillboard->getTexcoordRect().right; }
124 
126  { return this->GraphicsBillboard->getTexcoordRect().bottom; }
127 
128  ///////////////////////////////////////////////////////////////////////////////
129  // Serialization
130 
131  void Billboard::ProtoSerialize(XML::Node& ParentNode) const
132  {
133  XML::Node SelfRoot = SelfRoot.AppendChild( Billboard::GetSerializableName() );
134 
135  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
136  SelfRoot.AppendAttribute("Rotation").SetValue( this->GetRotation() ) )
137  {
138  if( this->HasOwnDimensions() ) {
139  if( !( SelfRoot.AppendAttribute("OwnWidth").SetValue( this->GetOwnWidth() ) &&
140  SelfRoot.AppendAttribute("OwnHeight").SetValue( this->GetOwnHeight() ) ) )
141  {
142  SerializeError("Create XML Attribute Values",Billboard::GetSerializableName(),true);
143  }
144  }
145 
146  if( this->IsUsingTextureCoordRect() ) {
147  if( !( SelfRoot.AppendAttribute("LeftTexCoord").SetValue( this->GetLeftTextureCoord() ) &&
148  SelfRoot.AppendAttribute("TopTexCoord").SetValue( this->GetTopTextureCoord() ) &&
149  SelfRoot.AppendAttribute("RightTexCoord").SetValue( this->GetRightTextureCoord() ) &&
150  SelfRoot.AppendAttribute("BottomTexCoord").SetValue( this->GetBottomTextureCoord() ) ) )
151  {
152  SerializeError("Create XML Attribute Values",Billboard::GetSerializableName(),true);
153  }
154  }
155 
156  XML::Node LocationNode = SelfRoot.AppendChild("Location");
157  this->GetLocation().ProtoSerialize( LocationNode );
158  XML::Node DirectionNode = SelfRoot.AppendChild("Direction");
159  this->GetDirection().ProtoSerialize( DirectionNode );
160  XML::Node ColourNode = SelfRoot.AppendChild("Colour");
161  this->GetColour().ProtoSerialize( ColourNode );
162 
163  return;
164  }else{
165  SerializeError("Create XML Attribute Values",Billboard::GetSerializableName(),true);
166  }
167  }
168 
170  {
171  XML::Attribute CurrAttrib;
172 
173  if( String( SelfRoot.Name() ) == Billboard::GetSerializableName() ) {
174  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
175  Real OwnWidth = -1.0, OwnHeight = -1.0;
176  Real LeftTexCoord = -1.0, TopTexCoord = -1.0, RightTexCoord = -1.0, BottomTexCoord = -1.0;
177 
178  CurrAttrib = SelfRoot.GetAttribute("Rotation");
179  if( !CurrAttrib.Empty() )
180  this->SetRotation( CurrAttrib.AsReal() );
181 
182  CurrAttrib = SelfRoot.GetAttribute("OwnWidth");
183  if( !CurrAttrib.Empty() )
184  OwnWidth = CurrAttrib.AsReal();
185 
186  CurrAttrib = SelfRoot.GetAttribute("OwnHeight");
187  if( !CurrAttrib.Empty() )
188  OwnHeight = CurrAttrib.AsReal();
189 
190  if( OwnWidth >= 0 && OwnHeight >= 0 ) {
191  this->SetDimensions(OwnWidth,OwnHeight);
192  }
193 
194  CurrAttrib = SelfRoot.GetAttribute("LeftTexCoord");
195  if( !CurrAttrib.Empty() )
196  LeftTexCoord = CurrAttrib.AsReal();
197 
198  CurrAttrib = SelfRoot.GetAttribute("TopTexCoord");
199  if( !CurrAttrib.Empty() )
200  TopTexCoord = CurrAttrib.AsReal();
201 
202  CurrAttrib = SelfRoot.GetAttribute("RightTexCoord");
203  if( !CurrAttrib.Empty() )
204  RightTexCoord = CurrAttrib.AsReal();
205 
206  CurrAttrib = SelfRoot.GetAttribute("BottomTexCoord");
207  if( !CurrAttrib.Empty() )
208  BottomTexCoord = CurrAttrib.AsReal();
209 
210  if( LeftTexCoord >= 0 && TopTexCoord >= 0 && RightTexCoord >= 0 && BottomTexCoord >= 0 ) {
211  this->SetTextureCoordRect(LeftTexCoord,TopTexCoord,RightTexCoord,BottomTexCoord);
212  }
213 
214  XML::Node LocationNode = SelfRoot.GetChild("Location").GetFirstChild();
215  if( !LocationNode.Empty() ) {
216  Vector3 Loc(LocationNode);
217  this->SetLocation(Loc);
218  }
219 
220  XML::Node DirectionNode = SelfRoot.GetChild("Direction").GetFirstChild();
221  if( !DirectionNode.Empty() ) {
222  Vector3 Dir(DirectionNode);
223  this->SetDirection(Dir);
224  }
225 
226  XML::Node ColourNode = SelfRoot.GetChild("Colour").GetFirstChild();
227  if( !ColourNode.Empty() ) {
228  ColourValue Colour(ColourNode);
229  this->SetColour(Colour);
230  }
231  }else{
232  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Billboard::GetSerializableName() + ": Not Version 1.");
233  }
234  }else{
235  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,Billboard::GetSerializableName() + " was not found in the provided XML node, which was expected.");
236  }
237  }
238 
240  { return "Billboard"; }
241 
242  ///////////////////////////////////////////////////////////////////////////////
243  // Internal Methods
244 
245  Ogre::Billboard* Billboard::_GetGraphicsObject() const
246  { return this->GraphicsBillboard; }
247  }//Graphics
248 }//Mezzanine
249 
250 #endif