Spinning Topp Logo BlackTopp Studios
inc
character.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 
41 #ifndef _uicharacter_cpp
42 #define _uicharacter_cpp
43 
44 #include "UI/character.h"
45 #include "UI/textlayer.h"
46 
47 #include "MathTools/mathtools.h"
48 
49 namespace Mezzanine
50 {
51  namespace UI
52  {
53  ///////////////////////////////////////////////////////////////////////////////
54  // Character Methods
55 
56  Character::Character(const UInt32& GlyphID, const CharacterTraits& Traits, TextLayer* Creator) :
57  CustomSize(-1,-1),
58  Layer(Creator),
59  CharSprite(NULL),
60  LengthOffset(0),
61  Highlighted(false)
62  {
63  // Assign the style
64  this->CharTraits = Traits;
65 
66  if( CharTraits.CharFont == NULL ) {
67  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"A Font is not defined in CharacterTraits when it is expected to be during Character initialization.");
68  }
69  this->CharGlyph = CharTraits.CharFont->GetGlyph(GlyphID);
70  }
71 
72  Character::Character(Glyph* CharacterGlyph, TextLayer* Creator) :
73  CustomSize(-1,-1),
74  Layer(Creator),
75  CharGlyph(CharacterGlyph),
76  CharSprite(NULL),
77  LengthOffset(0),
78  Highlighted(false)
79  {
80  // Since none was specified, create a default
83  }
84 
85  Character::Character(Glyph* CharacterGlyph, const CharacterTraits& Traits, TextLayer* Creator) :
86  CustomSize(-1,-1),
87  Layer(Creator),
88  CharGlyph(CharacterGlyph),
89  CharSprite(NULL),
90  LengthOffset(0),
91  Highlighted(false)
92  {
93  // Assign the style
94  this->CharTraits = Traits;
95 
96  if( CharTraits.CharFont != CharGlyph->Font ) {
97  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Font provided in traits does not match the font defined in provided glyph during Character initialization.");
98  }
99  }
100 
101  Character::Character(Sprite* CharacterSprite, TextLayer* Creator) :
102  CustomSize(-1,-1),
103  Layer(Creator),
104  CharGlyph(NULL),
105  CharSprite(CharacterSprite),
106  LengthOffset(0),
107  Highlighted(false)
108  {
109  // Since none was specified, create a default
110  this->CharTraits.CharFont = NULL;
112  }
113 
114  Character::Character(Sprite* CharacterSprite, const CharacterTraits& Traits, TextLayer* Creator) :
115  CustomSize(-1,-1),
116  Layer(Creator),
117  CharGlyph(NULL),
118  CharSprite(CharacterSprite),
119  LengthOffset(0),
120  Highlighted(false)
121  {
122  // Assign the style
123  this->CharTraits = Traits;
124  }
125 
126  ///////////////////////////////////////////////////////////////////////////////
127  // Utility
128 
130  {
131  if( this->IsCustomSizeSet() ) {
132  return MathTools::Floor( this->CustomSize.X );
133  }else{
134  Real Desired = this->Layer->GetDesiredLineHeight();
135  if( Desired > 0 ) {
136  return MathTools::Floor( ( this->GetUnscaledCharacterAdvance(Prev) * ( Desired / this->GetUnscaledLineHeight() ) ) * this->Layer->GetManualTextScale().X );
137  }else{
138  return MathTools::Floor( this->GetUnscaledCharacterAdvance(Prev) * this->Layer->GetManualTextScale().X );
139  }
140  }
141  }
142 
144  {
145  if( this->IsCustomSizeSet() ) {
146  return MathTools::Floor( this->CustomSize.X );
147  }else if( this->IsGlyph() ) {
148  return this->CharGlyph->GlyphAdvance + ( Prev != NULL ? this->CharGlyph->GetKerning(Prev->GlyphID) : 0 );
149  }else if( this->IsSprite() ) {
150  return this->CharSprite->GetWidth();
151  }
152  return 0;
153  }
154 
156  {
157  Real Desired = this->Layer->GetDesiredLineHeight();
158  if( Desired > 0 ) {
159  return MathTools::Floor( Desired * this->Layer->GetManualTextScale().X );
160  }
161  return MathTools::Floor( this->GetUnscaledLineHeight() * this->Layer->GetManualTextScale().X );
162  }
163 
165  {
166  if( this->IsCustomSizeSet() ) {
167  return this->CustomSize.Y;
168  }else if( this->IsGlyph() ) {
169  return this->CharGlyph->Font->GetLineHeight();
170  }else if( this->IsSprite() ) {
171  return this->CharSprite->GetHeight();
172  }
173  return 0;
174  }
175 
177  {
178  Real Desired = this->Layer->GetDesiredLineHeight();
179  if( Desired > 0 ) {
180  return MathTools::Floor( ( this->GetUnscaledVerticalOffset() * ( Desired / this->GetUnscaledLineHeight() ) ) * this->Layer->GetManualTextScale().X );
181  }
182  return MathTools::Floor( this->GetUnscaledVerticalOffset() * this->Layer->GetManualTextScale().X );
183  }
184 
186  {
187  // If this is a glyph, combine the manual vertical offset for low hanging letters as well as the difference between the baseline and lineheight.
188  // In almost all cases the combined offset should be a positive number, elevating the normal letters and thus making everything more centered.
189  /// @todo If we want to change how normal text is aligned on the Y axis, we need to update this.
190  if( this->IsGlyph() ) {
191  return this->CharGlyph->VerticalOffset;// + (this->CharGlyph->Font->GetLineHeight() - this->CharGlyph->Font->GetBaseLine());
192  }
193  return 0;
194  }
195 
197  {
198  if( this->IsGlyph() ) {
199  return this->CharGlyph->Atlas;
200  }else if( this->IsSprite() ) {
201  return this->CharSprite->Atlas;
202  }else{
203  return NULL;
204  }
205  }
206 
208  {
209  if( this->IsGlyph() ) return this->CharGlyph->GetAtlasName();
210  else if( this->IsSprite() ) return this->CharSprite->GetAtlasName();
211  else return String();
212  }
213 
215  {
216  return this->GetAtlas()->GetWhitePixel();
217  }
218 
220  {
221  if( this->IsGlyph() ) return CharGlyph->GetAtlasCoords(Corner);
222  else if( this->IsSprite() ) return CharSprite->GetAtlasCoords(Corner);
223  else return Vector2();
224  }
225 
227  {
228  if( this->IsGlyph() ) return CharGlyph->GetRelativeAtlasCoords(Corner);
229  else if( this->IsSprite() ) return CharSprite->GetRelativeAtlasCoords(Corner);
230  else return Vector2();
231  }
232 
234  {
235  if( this->CharTraits.CharColour != Colour ) {
236  this->CharTraits.CharColour = Colour;
237 
238  if( !Highlighted )
239  this->Layer->_MarkDirty();
240  }
241  }
242 
244  {
245  return this->CharTraits.CharColour;
246  }
247 
249  {
250  Boole ChangeCheck = ( this->IsHighlightable() && Highlight );
251  if( this->Highlighted != ChangeCheck ) {
252  this->Highlighted = ChangeCheck;
253  this->Layer->_MarkDirty();
254  }
255  }
256 
258  {
259  return this->Highlighted;
260  }
261 
263  {
264  if( this->CharTraits != Traits ) {
265  this->CharTraits = Traits;
266  this->Layer->_MarkDirty();
267  }
268  }
269 
271  {
272  return this->CharTraits;
273  }
274 
276  {
277  return (NULL != this->CharGlyph);
278  }
279 
281  {
282  return (NULL != this->CharSprite);
283  }
284 
286  {
287  if( this->CharGlyph && this->CharGlyph->IsWhitespace() ) return false;
288  else return true;
289  }
290 
292  {
293  if( this->CharGlyph && this->CharGlyph->IsNewLine() ) return false;
294  else return true;
295  }
296 
298  {
299  if( this->CharGlyph && this->CharGlyph->IsWhitespace() ) return true;
300  else return false;
301  }
302 
304  {
305  if( this->CharGlyph && this->CharGlyph->IsNewLine() ) return true;
306  else return false;
307  }
308 
309  ///////////////////////////////////////////////////////////////////////////////
310  // Transform Related Methods
311 
312  void Character::SetLengthOffset(const Real& Offset)
313  {
314  this->LengthOffset = Offset;
315  }
316 
318  {
319  return this->LengthOffset;
320  }
321 
323  {
324  return this->LengthOffset + this->GetCharacterSize().X;
325  }
326 
328  {
329  if( this->CustomSize != Size ) {
330  this->CustomSize = Size;
331  this->Layer->_MarkDirty();
332  }
333  }
334 
336  {
337  const Vector2 Test(-1,-1);
338  return (this->CustomSize != Test);
339  }
340 
342  {
343  Vector2 Ret;
344  if( this->IsCustomSizeSet() ) {
345  Ret = this->CustomSize;
346  }else{
347  Real Desired = this->Layer->GetDesiredLineHeight();
348  if( Desired > 0 ) {
349  Ret = ( ( this->GetUnscaledCharacterSize() * ( Desired / this->GetUnscaledCharacterSize().Y ) ) * this->Layer->GetManualTextScale() );
350  }else{
351  Ret = ( this->GetUnscaledCharacterSize() * this->Layer->GetManualTextScale() );
352  }
353  }
354  MathTools::Floor( Ret.X );
355  MathTools::Floor( Ret.Y );
356  return Ret;
357  }
358 
360  {
361  if( this->IsCustomSizeSet() ) {
362  return this->CustomSize;
363  }else if( this->IsGlyph() ) {
364  return this->CharGlyph->GetSize();
365  }else if( this->IsSprite() ) {
366  return this->CharSprite->GetSize();
367  }else{
368  return Vector2();
369  }
370  }
371 
372  ///////////////////////////////////////////////////////////////////////////////
373  // Fetch Methods
374 
376  { return this->CharGlyph; }
377 
379  { return this->CharSprite; }
380  }//UI
381 }//Mezzanine
382 
383 #endif
Real GetHeight() const
Gets the sprite's height on the Atlas texture.
Definition: sprite.h:129
Real GetUnscaledVerticalOffset() const
Gets the vertical adjustment for this character without scaling applied.
Definition: character.cpp:185
Boole Highlighted
Stores whether or not this character is being highlighted.
Definition: character.h:82
Real GetWidth() const
Gets the sprite's width on the Atlas texture.
Definition: sprite.h:133
const CharacterTraits & GetTraits() const
Gets this Characters sharable traits.
Definition: character.cpp:270
bool IsWhitespace() const
Checks if this glyph is not renderable.
Definition: glyph.h:169
Real GetLineHeight() const
Gets the height of a line of text in this font.
Definition: font.cpp:82
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
const String & GetAtlasName() const
Gets the name of the atlas this glyph belongs to.
Definition: glyph.h:120
Real LengthOffset
The X-axis offset from the left side where this character is being rendered relative to the TextLine ...
Definition: character.h:79
Class used to describe a single glyph or character available for text operations. ...
Definition: glyph.h:59
Boole IsWhiteSpace() const
Gets if this character is a whitespace glyph.
Definition: character.cpp:297
void SetTraits(const CharacterTraits &Traits)
Sets this Characters sharable traits.
Definition: character.cpp:262
Sprite * GetCharSprite() const
Gets the sprite being rendered by this character.
Definition: character.cpp:378
Vector2 GetUnscaledCharacterSize() const
Gets the rendered size of this character without any scaling applied.
Definition: character.cpp:359
Vector2 GetSize() const
Gets the size of the glyph on the Texture.
Definition: glyph.h:197
Boole IsHighlightable() const
Gets if this character can be highlighted.
Definition: character.cpp:291
Real GetRightEdgeLengthOffset() const
Gets the length offset of this Characters right side from its parent TextLine.
Definition: character.cpp:322
QuadCorner
Used by Sprites and Glyphs for tracking their placement on a TextureAtlas.
Boole IsCustomSizeSet() const
Whether or not the custom size has been set.
Definition: character.cpp:335
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Boole IsRenderable() const
Gets if this character contains valid data for rendering.
Definition: character.cpp:285
Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
Gets the relative atlas coordinates of a quad corner.
Definition: character.cpp:226
static ColourValue White()
Creates a ColourValue representing the colour White.
Real GetLengthOffset() const
Gets the length offset of this Character from it's parent TextLine.
Definition: character.cpp:317
TextureAtlas * GetAtlas() const
Gets the atlas this character is using for rendering.
Definition: character.cpp:196
This is a collection of smaller textures packed into a larger texture, intended to increase UI perfor...
Definition: textureatlas.h:71
Glyph * CharGlyph
A pointer to the Glyph this character is being rendered as, or NULL if it is a Sprite.
Definition: character.h:73
Vector2 GetCharacterSize() const
Gets the rendered size of this character.
Definition: character.cpp:341
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
virtual void _MarkDirty()
Marks this renderable as well as all parent objects as dirty.
This class stores common data for determining the look of a Character.
Real GetVerticalOffset() const
Gets the vertical adjustment for this character.
Definition: character.cpp:176
TextureAtlas * Atlas
The TextureAtlas this sprite belongs to.
Definition: sprite.h:81
FontData * CharFont
The font this Character belongs to.
Real GetUnscaledLineHeight() const
Gets the height a line needs to be to support rendering this character without scaling applied...
Definition: character.cpp:164
Boole IsNewLine() const
Gets if this character is a line break character.
Definition: character.cpp:303
Sprite * CharSprite
A pointer to the Sprite this character is being rendered as, or NULL if it is a Glyph.
Definition: character.h:76
Real VerticalOffset
The amount of pixels the glyph is to be adjusted on the Y axis.
Definition: glyph.h:109
Real GlyphAdvance
The number of pixels to advance the cursor for the next glyph.
Definition: glyph.h:107
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
Character(const UInt32 &GlyphID, const CharacterTraits &Traits, TextLayer *Creator)
GlyphID Constructor.
Definition: character.cpp:56
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
Vector2 GetAtlasWhitePixel() const
Gets the coordinates to the white pixel on the atlas this character belongs to.
Definition: character.cpp:214
Real GetCharacterAdvance(Glyph *Prev) const
Gets the amount of pixels to advance the cursor after placing this character.
Definition: character.cpp:129
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
void SetHighlighted(Boole Highlight)
Sets whether or not this character should be highlighted.
Definition: character.cpp:248
bool IsNewLine() const
Checks if this glyph marks a new line.
Definition: glyph.h:166
FontData * Font
The collection of glyphs this glyph belongs to.
Definition: glyph.h:103
Real X
Coordinate on the X vector.
Definition: vector2.h:67
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
Boole GetHighlighted() const
Gets if this character is being highlighted.
Definition: character.cpp:257
Vector2 CustomSize
The forced size of this character, if it is set.
Definition: character.h:67
virtual Real GetDesiredLineHeight() const
Gets the height in pixels this layer is configured to render it's text.
Definition: textlayer.cpp:378
TextLayer * Layer
A pointer to the layer this character is being rendered by.
Definition: character.h:70
Boole IsSprite() const
Gets whether or not this character is a sprite.
Definition: character.cpp:280
String GetAtlasName() const
Gets the name of the atlas this character is using for rendering.
Definition: character.cpp:207
void SetCharacterColour(const ColourValue &Colour)
Sets the fill colour for this character.
Definition: character.cpp:233
Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
Gets the atlas coordinates of a quad corner.
Definition: character.cpp:219
const ColourValue & GetCharacterColour() const
Gets the fill colour of this character.
Definition: character.cpp:243
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
Real GetUnscaledCharacterAdvance(Glyph *Prev) const
Gets the amount of pixels to advance the cursor after placing this character without scaling applied...
Definition: character.cpp:143
TextureAtlas * Atlas
The TextureAtlas this glyph belongs to.
Definition: glyph.h:101
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
Real GetLineHeight() const
Gets the height a line needs to be to support rendering this character.
Definition: character.cpp:155
void SetLengthOffset(const Real &Offset)
Sets the offset from the position of the parent TextLine.
Definition: character.cpp:312
virtual const Vector2 & GetManualTextScale() const
Gets the scaling currently being applied to the rendered text.
Definition: textlayer.cpp:461
ColourValue CharColour
The colour to render this Character as.
Real GetKerning(const UInt32 Previous) const
Convenience function for getting the Kerning information for a given Glyph.
Definition: glyph.h:126
Glyph * GetCharGlyph() const
Gets the glyph being rendered by this character.
Definition: character.cpp:375
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void SetCustomSize(const Vector2 &Size)
Sets the custom size this character is to be resized to.
Definition: character.cpp:327
Boole IsGlyph() const
Gets whether or not this character is a glyph.
Definition: character.cpp:275
Vector2 GetWhitePixel() const
Gets the location of the WhitePixel on the TextureAtlas.
Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
Gets the relative position on the Atlas of a corner belonging to this glyph.
Definition: glyph.h:220
CharacterTraits CharTraits
The rendering traits of this character.
Definition: character.h:64
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
This is a base class for render layers that render text.
Definition: textlayer.h:64
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
UInt32 GlyphID
The Character this glyph information represents.
Definition: glyph.h:105
Glyph * GetGlyph(const UInt32 &GlyphID) const
Gets the glyph corresponding to the provided characters UTF-8 code.
Definition: font.cpp:100
Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
Gets the pixel position on the Atlas of a corner belonging to this glyph.
Definition: glyph.h:206