Spinning Topp Logo BlackTopp Studios
inc
sprite.h
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 _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  /// @param SpriteName 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  /// @param ParentAtlas The TextureAtlas this sprite belongs to.
65  Sprite(const String& SpriteName, const Real UVTop, const Real UVLeft, const Real UVBottom, const Real UVRight, TextureAtlas* ParentAtlas) :
66  Atlas(ParentAtlas),
67  Name(SpriteName),
68  Top(UVTop),
69  Left(UVLeft),
70  Bottom(UVBottom),
71  Right(UVRight)
72  { }
73  /// @brief Class destructor.
75  { }
76 
77  ///////////////////////////////////////////////////////////////////////////////
78  // Public Data Members
79 
80  /// @brief The TextureAtlas this sprite belongs to.
82  /// @brief The name of this sprite.
83  const String Name;
84  /// @brief The top position of this sprite on the Atlas.
85  const Real Top;
86  /// @brief The left position of this sprite on the Atlas.
87  const Real Left;
88  /// @brief The bottom position of this sprite on the Atlas.
89  const Real Bottom;
90  /// @brief The right position of this sprite on the Atlas.
91  const Real Right;
92 
93  ///////////////////////////////////////////////////////////////////////////////
94  // Utility Methods
95 
96  /// @brief Gets the name of the atlas this sprite belongs to.
97  inline const String& GetAtlasName() const
98  { return this->Atlas->GetName(); }
99 
100  ///////////////////////////////////////////////////////////////////////////////
101  // Position and Size Methods
102 
103  /// @brief Gets the Top coordinate on the Atlas texture.
104  /// @return Returns a Real representing the top position of this sprite on the Atlas texture in pixels.
105  inline Real GetUVTop() const
106  { return this->Top * this->Atlas->GetTextureSize().Y; }
107  /// @brief Gets the Bottom coordinate on the Atlas texture.
108  /// @return Returns a Real representing the bottom position of this sprite on the Atlas texture in pixels.
109  inline Real GetUVBottom() const
110  { return this->Bottom * this->Atlas->GetTextureSize().Y; }
111  /// @brief Gets the Left coordinate on the Atlas texture.
112  /// @return Returns a Real representing the left position of this sprite on the Atlas texture in pixels.
113  inline Real GetUVLeft() const
114  { return this->Left * this->Atlas->GetTextureSize().X; }
115  /// @brief Gets the Right coordinate on the Atlas texture.
116  /// @return Returns a Real representing the right position of this sprite on the Atlas texture in pixels.
117  inline Real GetUVRight() const
118  { return this->Right * this->Atlas->GetTextureSize().X; }
119  /// @brief Gets the position of the sprite on the Atlas texture.
120  /// @return Returns a Vector2 containing the top-left position of this sprite on the Atlas texture in pixels.
121  inline Vector2 GetPosition() const
122  { return Vector2(this->Left,this->Top) * this->Atlas->GetTextureSize(); }
123  /// @brief Gets the size of the sprite on the Atlas texture.
124  /// @return Returns a Vector2 containing the size of this sprite on the Atlas Texture in pixels.
125  inline Vector2 GetSize() const
126  { return Vector2(this->GetWidth(),this->GetHeight()); }
127  /// @brief Gets the sprite's height on the Atlas texture.
128  /// @return Returns a Real representing the height of this sprite on the Atlas Texture in pixels.
129  inline Real GetHeight() const
130  { return this->GetUVBottom() - this->GetUVTop(); }
131  /// @brief Gets the sprite's width on the Atlas texture.
132  /// @return Returns a Real representing the width of this sprite on the Atlas Texture in pixels.
133  inline Real GetWidth() const
134  { return this->GetUVRight() - this->GetUVLeft(); }
135  /// @brief Gets the pixel position on the Atlas of a corner belonging to this Sprite.
136  /// @param Corner The corner to retrieve the coordinates for.
137  /// @return Returns a Vector2 containing the Atlas pixel position of the specific corner.
138  inline Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
139  {
140  switch(Corner)
141  {
142  case UI::QC_TopLeft: return Vector2(this->Left,this->Top) * this->Atlas->GetTextureSize(); break;
143  case UI::QC_TopRight: return Vector2(this->Right,this->Top) * this->Atlas->GetTextureSize(); break;
144  case UI::QC_BottomLeft: return Vector2(this->Left,this->Bottom) * this->Atlas->GetTextureSize(); break;
145  case UI::QC_BottomRight: return Vector2(this->Right,this->Bottom) * this->Atlas->GetTextureSize(); break;
146  default:
147  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Invalid QuadCorner value used to query Atlas Coordinates."); break; }
148  }
149  }
150  /// @brief Gets the relative position on the Atlas of a corner belonging to this Sprite.
151  /// @param Corner The corner to retrieve the coordinates for.
152  /// @return Returns a Vector2 containing the Atlas relative position of the specific corner.
153  inline Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
154  {
155  switch(Corner)
156  {
157  case UI::QC_TopLeft: return Vector2(this->Left,this->Top); break;
158  case UI::QC_TopRight: return Vector2(this->Right,this->Top); break;
159  case UI::QC_BottomLeft: return Vector2(this->Left,this->Bottom); break;
160  case UI::QC_BottomRight: return Vector2(this->Right,this->Bottom); break;
161  default:
162  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Invalid QuadCorner value used to query Atlas Coordinates."); break; }
163  }
164  }
165  };//Sprite
166  }//UI
167 }//Mezzanine
168 
169 #endif
Real GetHeight() const
Gets the sprite's height on the Atlas texture.
Definition: sprite.h:129
Vector2 GetTextureSize() const
Gets the size of the TextureAtlas.
Real GetWidth() const
Gets the sprite's width on the Atlas texture.
Definition: sprite.h:133
const String Name
The name of this sprite.
Definition: sprite.h:83
const String & GetName() const
Gets the name of this Texture Atlas.
const Real Right
The right position of this sprite on the Atlas.
Definition: sprite.h:91
const Real Bottom
The bottom position of this sprite on the Atlas.
Definition: sprite.h:89
Vector2 GetPosition() const
Gets the position of the sprite on the Atlas texture.
Definition: sprite.h:121
Real GetUVBottom() const
Gets the Bottom coordinate on the Atlas texture.
Definition: sprite.h:109
QuadCorner
Used by Sprites and Glyphs for tracking their placement on a TextureAtlas.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
This is a collection of smaller textures packed into a larger texture, intended to increase UI perfor...
Definition: textureatlas.h:71
TextureAtlas * Atlas
The TextureAtlas this sprite belongs to.
Definition: sprite.h:81
const Real Left
The left position of this sprite on the Atlas.
Definition: sprite.h:87
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
const Real Top
The top position of this sprite on the Atlas.
Definition: sprite.h:85
Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
Gets the pixel position on the Atlas of a corner belonging to this Sprite.
Definition: sprite.h:138
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Real GetUVRight() const
Gets the Right coordinate on the Atlas texture.
Definition: sprite.h:117
Real X
Coordinate on the X vector.
Definition: vector2.h:67
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
Gets the relative position on the Atlas of a corner belonging to this Sprite.
Definition: sprite.h:153
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
Sprite(const String &SpriteName, const Real UVTop, const Real UVLeft, const Real UVBottom, const Real UVRight, TextureAtlas *ParentAtlas)
Class constructor.
Definition: sprite.h:65
Real GetUVTop() const
Gets the Top coordinate on the Atlas texture.
Definition: sprite.h:105
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
Real GetUVLeft() const
Gets the Left coordinate on the Atlas texture.
Definition: sprite.h:113
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
~Sprite()
Class destructor.
Definition: sprite.h:74
Basic class used to describe a portion of a texture to be applied to a Quad.
Definition: sprite.h:55
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Vector2 GetSize() const
Gets the size of the sprite on the Atlas texture.
Definition: sprite.h:125
const String & GetAtlasName() const
Gets the name of the atlas this sprite belongs to.
Definition: sprite.h:97