MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sprite.h
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 _uisprite_h
41 #define _uisprite_h
42 
43 #include "vector2.h"
44 #include "UI/uienumerations.h"
45 #include "UI/textureatlas.h"
46 
47 namespace Mezzanine
48 {
49  namespace UI
50  {
51  ///////////////////////////////////////////////////////////////////////////////
52  /// @brief Basic class used to describe a portion of a texture to be applied to a Quad.
53  /// @details
54  ///////////////////////////////////////
56  {
57  public:
58  /// @brief Class constructor.
59  /// @brief The name to be given to this sprite.
60  /// @param UVTop The top position of this sprite on the Atlas.
61  /// @param UVLeft The left position of this sprite on the Atlas.
62  /// @param UVBottom The bottom position of this sprite on the Atlas.
63  /// @param UVRight The right position of this sprite on the Atlas.
64  Sprite(const String& SpriteName, const Real UVTop, const Real UVLeft, const Real UVBottom, const Real UVRight) :
65  Atlas(NULL),
66  Name(SpriteName),
67  Top(UVTop),
68  Left(UVLeft),
69  Bottom(UVBottom),
70  Right(UVRight)
71  { }
72  /// @brief Class destructor.
74  { }
75 
76  ///////////////////////////////////////////////////////////////////////////////
77  // Public Data Members
78 
79  /// @brief The TextureAtlas this sprite belongs to.
81  /// @brief The name of this sprite.
82  const String Name;
83  /// @brief The top position of this sprite on the Atlas.
84  const Real Top;
85  /// @brief The left position of this sprite on the Atlas.
86  const Real Left;
87  /// @brief The bottom position of this sprite on the Atlas.
88  const Real Bottom;
89  /// @brief The right position of this sprite on the Atlas.
90  const Real Right;
91 
92  ///////////////////////////////////////////////////////////////////////////////
93  // Utility Methods
94 
95  /// @brief Gets the name of the atlas this sprite belongs to.
96  inline const String& GetAtlasName() const
97  { return Atlas->GetName(); }
98 
99  ///////////////////////////////////////////////////////////////////////////////
100  // Position and Size Methods
101 
102  /// @brief Gets the Top coordinate on the Atlas texture.
103  /// @return Returns a Real representing the top position of this sprite on the Atlas texture in pixels.
104  inline Real GetUVTop() const
105  { return this->Top * Atlas->GetTextureSize().Y; }
106  /// @brief Gets the Bottom coordinate on the Atlas texture.
107  /// @return Returns a Real representing the bottom position of this sprite on the Atlas texture in pixels.
108  inline Real GetUVBottom() const
109  { return this->Bottom * Atlas->GetTextureSize().Y; }
110  /// @brief Gets the Left coordinate on the Atlas texture.
111  /// @return Returns a Real representing the left position of this sprite on the Atlas texture in pixels.
112  inline Real GetUVLeft() const
113  { return this->Left * Atlas->GetTextureSize().X; }
114  /// @brief Gets the Right coordinate on the Atlas texture.
115  /// @return Returns a Real representing the right position of this sprite on the Atlas texture in pixels.
116  inline Real GetUVRight() const
117  { return this->Right * Atlas->GetTextureSize().X; }
118  /// @brief Gets the position of the sprite on the Atlas texture.
119  /// @return Returns a Vector2 containing the top-left position of this sprite on the Atlas texture in pixels.
120  inline Vector2 GetPosition() const
121  { return Vector2(this->Left,this->Top) * Atlas->GetTextureSize(); }
122  /// @brief Gets the size of the sprite on the Atlas texture.
123  /// @return Returns a Vector2 containing the size of this sprite on the Atlas Texture in pixels.
124  inline Vector2 GetSize() const
125  { return Vector2(this->GetWidth(),this->GetHeight()); }
126  /// @brief Gets the sprite's height on the Atlas texture.
127  /// @return Returns a Real representing the height of this sprite on the Atlas Texture in pixels.
128  inline Real GetHeight() const
129  { return GetUVBottom() - GetUVTop(); }
130  /// @brief Gets the sprite's width on the Atlas texture.
131  /// @return Returns a Real representing the width of this sprite on the Atlas Texture in pixels.
132  inline Real GetWidth() const
133  { return GetUVRight() - GetUVLeft(); }
134  /// @brief Gets the pixel position on the Atlas of a corner belonging to this Sprite.
135  /// @param Corner The corner to retrieve the coordinates for.
136  /// @return Returns a Vector2 containing the Atlas pixel position of the specific corner.
137  inline Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
138  {
139  switch(Corner)
140  {
141  case UI::QC_TopLeft: return Vector2(Left,Top) * Atlas->GetTextureSize(); break;
142  case UI::QC_TopRight: return Vector2(Right,Top) * Atlas->GetTextureSize(); break;
143  case UI::QC_BottomLeft: return Vector2(Left,Bottom) * Atlas->GetTextureSize(); break;
144  case UI::QC_BottomRight: return Vector2(Right,Bottom) * Atlas->GetTextureSize(); break;
145  }
146  }
147  /// @brief Gets the relative position on the Atlas of a corner belonging to this Sprite.
148  /// @param Corner The corner to retrieve the coordinates for.
149  /// @return Returns a Vector2 containing the Atlas relative position of the specific corner.
150  inline Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
151  {
152  switch(Corner)
153  {
154  case UI::QC_TopLeft: return Vector2(Left,Top); break;
155  case UI::QC_TopRight: return Vector2(Right,Top); break;
156  case UI::QC_BottomLeft: return Vector2(Left,Bottom); break;
157  case UI::QC_BottomRight: return Vector2(Right,Bottom); break;
158  }
159  }
160  };//Sprite
161  }//UI
162 }//Mezzanine
163 
164 #endif