MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
renderlayer.cpp
1 //© Copyright 2010 - 2012 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 _uirenderlayer_cpp
41 #define _uirenderlayer_cpp
42 
43 #include "UI/renderlayer.h"
44 #include "UI/quadrenderable.h"
45 #include "UI/screen.h"
46 #include "mathtool.h"
47 
48 namespace Mezzanine
49 {
50  namespace UI
51  {
53  Parent(ParentRenderable),
54  IndexID(0),
55  RotAngle(0)
56  {
57  this->PriAtlas = this->Parent->GetScreen()->GetPrimaryAtlas();
58  this->Scale.SetValues(1,1);
59  }
60 
62  { }
63 
64  void RenderLayer::RotationTransform(Vector2& Point, const Vector2& RotationCenter)
65  {
66  if(0 != this->RotAngle)
67  {
68  Real RotCos = MathTools::Cos(RotAngle);
69  Real RotSin = MathTools::Sin(RotAngle);
70 
71  Point -= RotationCenter;
72 
73  Vector2 Copy = Point;
74  Point.X = Copy.X * RotCos - Copy.Y * RotSin;
75  Point.Y = Copy.X * RotSin + Copy.Y * RotCos;
76 
77  Point += RotationCenter;
78  }
79  }
80 
81  void RenderLayer::RotationTransform(Vector2& TopLeft, Vector2& TopRight, Vector2& BottomLeft, Vector2& BottomRight)
82  {
83  if( 0 == this->RotAngle )
84  return;
85 
86  Vector2 RotCenter( TopLeft.X + ((BottomRight.X - TopLeft.X) * 0.5), TopLeft.Y + ((BottomRight.Y - TopLeft.Y) * 0.5) );
87  this->RotationTransform(TopLeft,RotCenter);
88  this->RotationTransform(TopRight,RotCenter);
89  this->RotationTransform(BottomLeft,RotCenter);
90  this->RotationTransform(BottomRight,RotCenter);
91  }
92 
93  ///////////////////////////////////////////////////////////////////////////////
94  // Utility
95 
97  {
98  return this->IndexID;
99  }
100 
102  {
103  /// @todo As settings are added that need may need to be modified, this method should be updated.
104  /// Prime candidate for this is Animations.
105  }
106 
108  {
109  /// @todo As settings are added that need may need to be modified, this method should be updated.
110  /// Prime candidate for this is Animations.
111  }
112 
114  {
115  Rect Area(this->Parent->GetActualPosition(),this->Parent->GetActualSize());
116  Area.ApplyScaling(this->Scale);
117  return Area;
118  }
119 
120  void RenderLayer::SetScale(const Vector2& Scaling)
121  {
122  if( this->Scale == Scaling )
123  return;
124 
125  this->Scale = Scaling;
126  this->_MarkDirty();
127  }
128 
130  {
131  return this->Scale;
132  }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Rotation Methods
136 
138  {
139  this->RotAngle = MathTools::DegreesToRadians(Degrees);
140  this->_MarkDirty();
141  }
142 
144  {
145  this->RotAngle = Radians;
146  this->_MarkDirty();
147  }
148 
150  {
152  }
153 
155  {
156  return this->RotAngle;
157  }
158 
159  ///////////////////////////////////////////////////////////////////////////////
160  // Accessor Methods
161 
163  {
164  return this->Parent;
165  }
166 
168  {
169  return this->Parent->GetScreen();
170  }
171 
172  ///////////////////////////////////////////////////////////////////////////////
173  // Serialization
174 
176  {
178  XML::Node PropertiesNode = SelfRoot.AppendChild( RenderLayer::GetSerializableName() + "Properties" );
179 
180  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
181  PropertiesNode.AppendAttribute("RotAngle").SetValue(this->RotAngle) )
182  {
183  XML::Node ScaleNode = PropertiesNode.AppendChild("Scale");
184  this->Scale.ProtoSerialize( ScaleNode );
185 
186  return;
187  }else{
188  SerializeError("Create XML Attribute Values",RenderLayer::GetSerializableName() + "Properties",true);
189  }
190  }
191 
193  {
195  XML::Attribute CurrAttrib;
196  XML::Node PropertiesNode = SelfRoot.GetChild( RenderLayer::GetSerializableName() + "Properties" );
197 
198  if( !PropertiesNode.Empty() ) {
199  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
200  CurrAttrib = PropertiesNode.GetAttribute("RotAngle");
201  if( !CurrAttrib.Empty() )
202  this->RotAngle = CurrAttrib.AsReal();
203 
204  XML::Node ScaleNode = PropertiesNode.GetChild("Scale").GetFirstChild();
205  if( !ScaleNode.Empty() )
206  this->Scale.ProtoDeSerialize(ScaleNode);
207  }else{
208  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (RenderLayer::GetSerializableName() + "Properties") + ": Not Version 1.");
209  }
210  }else{
211  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,RenderLayer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
212  }
213  }
214 
216  {
218  }
219 
221  {
222  return "RenderLayer";
223  }
224 
225  ///////////////////////////////////////////////////////////////////////////////
226  // Internal Methods
227 
229  {
230  this->Dirty = true;
231  this->Parent->_MarkDirty();
232  }
233 
235  {
236  this->IndexID = Index;
237  }
238  }//UI
239 }//Mezzanine
240 
241 #endif