Spinning Topp Logo BlackTopp Studios
inc
renderlayer.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 _uirenderlayer_cpp
41 #define _uirenderlayer_cpp
42 
43 #include "UI/renderlayer.h"
44 #include "UI/quadrenderable.h"
45 #include "UI/screen.h"
46 #include "MathTools/mathtools.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  Vector2 RotCenter( TopLeft.X + ( ( BottomRight.X - TopLeft.X ) * 0.5 ), TopLeft.Y + ( ( BottomRight.Y - TopLeft.Y ) * 0.5 ) );
85  this->RotationTransform( TopLeft, RotCenter );
86  this->RotationTransform( TopRight, RotCenter );
87  this->RotationTransform( BottomLeft, RotCenter );
88  this->RotationTransform( BottomRight, RotCenter );
89  }
90  }
91 
92  void RenderLayer::RotationTransform(Vector2& TopLeft, Vector2& TopRight, Vector2& BottomLeft, Vector2& BottomRight, const Vector2& RotationCenter)
93  {
94  if( 0 != this->RotAngle ) {
95  this->RotationTransform( TopLeft, RotationCenter );
96  this->RotationTransform( TopRight, RotationCenter );
97  this->RotationTransform( BottomLeft, RotationCenter );
98  this->RotationTransform( BottomRight, RotationCenter );
99  }
100  }
101 
103  {
104  if( 0 != this->RotAngle ) {
105  Vector2 RotCenter( RotRect[UI::QC_TopLeft].X + ( ( RotRect[UI::QC_BottomRight].X - RotRect[UI::QC_TopLeft].X ) * 0.5 ),
106  RotRect[UI::QC_TopLeft].Y + ( ( RotRect[UI::QC_BottomRight].Y - RotRect[UI::QC_TopLeft].Y ) * 0.5 ) );
107  this->RotationTransform( RotRect[UI::QC_TopLeft], RotCenter );
108  this->RotationTransform( RotRect[UI::QC_TopRight], RotCenter );
109  this->RotationTransform( RotRect[UI::QC_BottomLeft], RotCenter );
110  this->RotationTransform( RotRect[UI::QC_BottomRight], RotCenter );
111  }
112  }
113 
114  void RenderLayer::RotationTransform(Vector2* RotRect, const Vector2& RotationCenter)
115  {
116  if( 0 != this->RotAngle ) {
117  this->RotationTransform( RotRect[UI::QC_TopLeft], RotationCenter );
118  this->RotationTransform( RotRect[UI::QC_TopRight], RotationCenter );
119  this->RotationTransform( RotRect[UI::QC_BottomLeft], RotationCenter );
120  this->RotationTransform( RotRect[UI::QC_BottomRight], RotationCenter );
121  }
122  }
123 
124  ///////////////////////////////////////////////////////////////////////////////
125  // Utility
126 
128  {
129  return this->IndexID;
130  }
131 
133  {
134  /// @todo As settings are added that need may need to be modified, this method should be updated.
135  /// Prime candidate for this is Animations.
136  }
137 
139  {
140  /// @todo As settings are added that need may need to be modified, this method should be updated.
141  /// Prime candidate for this is Animations.
142  }
143 
145  {
146  Rect Area(this->Parent->GetActualPosition(),this->Parent->GetActualSize());
147  Area.ApplyScaling(this->Scale);
148  return Area;
149  }
150 
151  void RenderLayer::SetScale(const Vector2& Scaling)
152  {
153  if( this->Scale == Scaling )
154  return;
155 
156  this->Scale = Scaling;
157  this->_MarkDirty();
158  }
159 
161  {
162  return this->Scale;
163  }
164 
166  { return ( this->GetLayerType() == UI::RLT_Line ); }
167 
169  { return ( this->IsSingleImageLayer() || this->IsMultiImageLayer() ); }
170 
172  { return ( this->GetLayerType() == UI::RLT_SingleImage ); }
173 
175  { return ( this->GetLayerType() == UI::RLT_MultiImage ); }
176 
178  { return ( this->IsSingleLineTextLayer() || this->IsMultiLineTextLayer() ); }
179 
181  { return ( this->GetLayerType() == UI::RLT_SingleLineText ); }
182 
184  { return ( this->GetLayerType() == UI::RLT_MultiLineText ); }
185 
186  ///////////////////////////////////////////////////////////////////////////////
187  // Rotation Methods
188 
190  {
191  this->RotAngle = MathTools::DegreesToRadians(Degrees);
192  this->_MarkDirty();
193  }
194 
196  {
197  this->RotAngle = Radians;
198  this->_MarkDirty();
199  }
200 
202  { return MathTools::RadiansToDegrees(RotAngle); }
203 
205  { return this->RotAngle; }
206 
207  ///////////////////////////////////////////////////////////////////////////////
208  // Accessor Methods
209 
211  { return this->Parent; }
212 
214  { return this->Parent->GetScreen(); }
215 
216  ///////////////////////////////////////////////////////////////////////////////
217  // Serialization
218 
220  {
222  XML::Node PropertiesNode = SelfRoot.AppendChild( RenderLayer::GetSerializableName() + "Properties" );
223 
224  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
225  PropertiesNode.AppendAttribute("RotAngle").SetValue(this->RotAngle) )
226  {
227  XML::Node ScaleNode = PropertiesNode.AppendChild("Scale");
228  this->Scale.ProtoSerialize( ScaleNode );
229 
230  return;
231  }else{
232  SerializeError("Create XML Attribute Values",RenderLayer::GetSerializableName() + "Properties",true);
233  }
234  }
235 
237  {
239  XML::Attribute CurrAttrib;
240  XML::Node PropertiesNode = SelfRoot.GetChild( RenderLayer::GetSerializableName() + "Properties" );
241 
242  if( !PropertiesNode.Empty() ) {
243  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
244  CurrAttrib = PropertiesNode.GetAttribute("RotAngle");
245  if( !CurrAttrib.Empty() )
246  this->RotAngle = CurrAttrib.AsReal();
247 
248  XML::Node ScaleNode = PropertiesNode.GetChild("Scale").GetFirstChild();
249  if( !ScaleNode.Empty() )
250  this->Scale.ProtoDeSerialize(ScaleNode);
251  }else{
252  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (RenderLayer::GetSerializableName() + "Properties") + ": Not Version 1.");
253  }
254  }else{
255  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,RenderLayer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
256  }
257  }
258 
260  {
262  }
263 
265  {
266  return "RenderLayer";
267  }
268 
269  ///////////////////////////////////////////////////////////////////////////////
270  // Internal Methods
271 
273  {
274  this->Dirty = true;
275  this->Parent->_MarkDirty();
276  }
277 
279  {
280  this->IndexID = Index;
281  }
282  }//UI
283 }//Mezzanine
284 
285 #endif
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
virtual Boole IsMultiLineTextLayer() const
Gets whether or not this is a MultiLineTextLayer.
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
virtual Boole IsLineLayer() const
Gets whether or not this is an LineLayer.
virtual void NotifyActive()
Notifies this RenderLayer that the group this layer belongs to has become the active group...
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual Vector2 GetScale() const
Gets the scaling currently applied to this render layer.
virtual void RotationTransform(Vector2 &Point, const Vector2 &RotationCenter)
Applies rotation to a point in 2D space.
Definition: renderlayer.cpp:64
virtual Rect GetAreaRect() const
Gets a rect containing the actual position and size of this layer.
virtual Boole IsSingleImageLayer() const
Gets whether or not this is a SingleImageLayer.
virtual Whole GetIndex() const
Gets the index position of this RenderLayer in it's parent.
Thrown when the requested identity could not be found.
Definition: exception.h:94
virtual QuadRenderable * GetParent() const
Gets the parent of this render layer.
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
QuadRenderable * Parent
A pointer to the parent of this RenderLayer.
Definition: renderlayer.h:67
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
String PriAtlas
This contains the name of the atlas that will be used as default when one isn't specified.
virtual Boole IsSingleLineTextLayer() const
Gets whether or not this is a SingleLineTextLayer.
Rect & ApplyScaling(const Vector2 &Scaling)
Scales this rect.
Definition: rect.h:234
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Value representing a SingleLineTextLayer.
virtual void _UpdateIndex(const Whole Index)
Notifies this RenderLayer that it's index in the parent QuadRenderable has been updated.
Screen * GetScreen() const
Gets the parent screen of this renderable.
Definition: renderable.cpp:80
This class represents a box shaped area on the screen.
Definition: rect.h:55
virtual Screen * GetScreen() const
Gets the screen the parent renderable is being rendered on.
virtual Real GetRotationRadians() const
Gets the current rotation applied to this renderable in radians.
virtual void RotationTransform(Vector2 &TopLeft, Vector2 &TopRight, Vector2 &BottomLeft, Vector2 &BottomRight, const Vector2 &RotationCenter)
Applies rotation to a quad in 2D space.
Definition: renderlayer.cpp:92
virtual void _MarkDirty()
Marks this renderable as well as all parent objects as dirty.
bool Empty() const
Is this storing anything at all?
Value representing a MultiLineTextLayer.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
Vector2 Scale
The scaling applied to this RenderLayer.
Definition: renderlayer.h:64
bool SetValue(const Char8 *rhs)
Set the value of this.
Value representing a SingleImageLayer.
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
virtual Real GetRotationDegrees() const
Gets the current rotation applied to this renderable in degrees.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Real X
Coordinate on the X vector.
Definition: vector2.h:67
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Boole Dirty
This determines whether or not the vertices in this renderer need to be refreshed.
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool Empty() const
Is this storing anything at all?
virtual Vector2 GetActualSize() const
Gets the pixel size of this widget.
virtual void SetRotationDegrees(const Real &Degrees)
Sets the rotation to be applied to this renderable.
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
virtual Boole IsMultiImageLayer() const
Gets whether or not this is a MultiImageLayer.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
Definition: vector2.cpp:105
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this Renderable.
virtual void RotationTransform(Vector2 &TopLeft, Vector2 &TopRight, Vector2 &BottomLeft, Vector2 &BottomRight)
Applies rotation to a quad in 2D space.
Definition: renderlayer.cpp:81
virtual Boole IsTextLayer() const
Gets whether or not this is a TextLayer.
virtual Boole IsImageLayer() const
Gets whether or not this is an ImageLayer.
virtual void NotifyInactive()
Notifies this RenderLayer that the group this layer belongs to is no longer the active group...
virtual String GetPrimaryAtlas()
Gets the currently set primary atlas.
Definition: screen.cpp:880
virtual void SetScale(const Vector2 &Scaling)
Sets the scaling to be applied to this render layer.
RenderLayer(QuadRenderable *ParentRenderable)
Class constructor.
Definition: renderlayer.cpp:52
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector2.cpp:314
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void SetRotationRadians(const Real &Radians)
Sets the rotation to be applied to this renderable.
Value representing a LineLayer.
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
virtual Vector2 GetActualPosition() const
Gets the pixel position of this widget.
This represents a nestable quad for an object in a GUI layout.
virtual void _MarkDirty()
Marks this renderable as dirty, and informs other renderables if needed.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual ~RenderLayer()
Class destructor.
Definition: renderlayer.cpp:61
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
virtual UI::RenderLayerType GetLayerType() const =0
Gets the type of render layer this is.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
This class is a helper class for creating UI's. It is responsible for storing and keeping track of al...
Definition: screen.h:142
Value representing a MultiImageLayer.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector2.cpp:335
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Whole IndexID
Stores the index of this RenderLayer in it's parent QuadRenderable.
Definition: renderlayer.h:70
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
Real RotAngle
The amount of rotation to be applied to this RenderLayer in radians.
Definition: renderlayer.h:73