Spinning Topp Logo BlackTopp Studios
inc
singlelinetextlayer.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 _uisinglelinetextlayer_cpp
41 #define _uisinglelinetextlayer_cpp
42 
43 #include "UI/singlelinetextlayer.h"
44 #include "UI/quadrenderable.h"
45 #include "UI/screen.h"
46 #include "UI/glyph.h"
47 #include "UI/textline.h"
48 #include "UI/character.h"
49 #include "UI/uimanager.h"
50 #include "MathTools/mathtools.h"
51 
52 namespace Mezzanine
53 {
54  namespace UI
55  {
57  TextLayer(ParentRenderable),
58  StartIndex(0)
59  { }
60 
61  SingleLineTextLayer::SingleLineTextLayer(const String& FontName, QuadRenderable* ParentRenderable) :
62  TextLayer(FontName,ParentRenderable),
63  StartIndex(0)
64  { }
65 
66  SingleLineTextLayer::SingleLineTextLayer(const Real& LineHeight, QuadRenderable* ParentRenderable) :
67  TextLayer(LineHeight,ParentRenderable),
68  StartIndex(0)
69  { }
70 
72  { }
73 
75  {
76  Integer Count = 0;
77  TextLine* Caption = this->GetLine();
79  {
80  for( CharacterIterator CharIt = this->Characters.begin() ; CharIt != this->Characters.end() ; ++CharIt )
81  {
82  if( Count < StartIndex )
83  continue;
84 
85  if( !Caption->AppendCharacter((*CharIt),MaxWidth) )
86  break;
87  }
88  }
90  {
91  for( ReverseCharacterIterator CharIt = this->Characters.rbegin() ; CharIt != this->Characters.rend() ; ++CharIt )
92  {
93  if( Count < StartIndex )
94  continue;
95 
96  if( !Caption->AppendCharacter((*CharIt),MaxWidth) )
97  break;
98  }
99  }
100  }
101 
103  {
104  TextLine* Line = this->GetLine();
105 
106  if( Offset.Y < Line->GetPositionOffset() - Line->GetLineHeight() || Offset.Y > Line->GetPositionOffset() )
107  return CharIndexPair(false,0);
108 
109  return CharIndexPair(true,this->StartIndex + Line->GetIndexAtOffset(Offset.X));
110  }
111 
113  {
114  TextLine* Line = this->GetLine();
115 
116  if( Index < this->StartIndex || static_cast<Whole>( Index ) > this->StartIndex + Line->GetNumCharacters() )
117  return CharOffsetPair(false,Vector2(0,0));
118 
119  return CharOffsetPair(true,Vector2(Line->GetOffsetAtIndex(Index - this->StartIndex),Line->GetPositionOffset()));
120  }
121 
123  { return this->TextLines.at(0); }
124 
125  ///////////////////////////////////////////////////////////////////////////////
126  // Utility
127 
129  { return UI::RLT_SingleLineText; }
130 
131  ///////////////////////////////////////////////////////////////////////////////
132  // Text Methods
133 
135  { this->SetTextLineHorizontalAlignment(Align); }
136 
138  { this->SetTextLineVerticalAlignment(Align); }
139 
141  { this->StartIndex = Index; }
142 
144  { return this->StartIndex; }
145 
146  ///////////////////////////////////////////////////////////////////////////////
147  // Serialization
148 
150  {
152  XML::Node PropertiesNode = SelfRoot.AppendChild( SingleLineTextLayer::GetSerializableName() + "Properties" );
153 
154  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
155  PropertiesNode.AppendAttribute("StartIndex").SetValue(this->StartIndex) )
156  {
157  return;
158  }else{
159  SerializeError("Create XML Attribute Values",SingleLineTextLayer::GetSerializableName() + "Properties",true);
160  }
161  }
162 
164  {
166  XML::Attribute CurrAttrib;
167  XML::Node PropertiesNode = SelfRoot.GetChild( SingleLineTextLayer::GetSerializableName() + "Properties" );
168 
169  if( !PropertiesNode.Empty() ) {
170  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
171  CurrAttrib = PropertiesNode.GetAttribute("StartIndex");
172  if( !CurrAttrib.Empty() )
173  this->StartIndex = CurrAttrib.AsInt();
174  }else{
175  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (SingleLineTextLayer::GetSerializableName() + "Properties") + ": Not Version 1.");
176  }
177  }else{
178  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,SingleLineTextLayer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
179  }
180  }
181 
183  { return this->SingleLineTextLayer::GetSerializableName(); }
184 
186  { return "SingleLineTextLayer"; }
187  }//UI
188 }//Mezzanine
189 
190 #endif
Characters/New Lines originate from the left, and advance to the right.
std::pair< Boole, Vector2 > CharOffsetPair
An std::pair type used as a return for index-offset conversions.
Definition: textlayer.h:88
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: textlayer.cpp:942
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual void SetTextLineVerticalAlignment(const UI::LinearAlignment Align)
Sets the alignment used to determine the start position of the textlines in this layer.
Definition: textlayer.cpp:732
Real GetPositionOffset() const
Gets the offset on the Y axis from the parent layer.
Definition: textline.cpp:225
virtual ~SingleLineTextLayer()
Class destructor.
CharacterContainer Characters
Container with all this layers Characters.
Definition: textlayer.h:104
Thrown when the requested identity could not be found.
Definition: exception.h:94
UInt32 GetNumCharacters() const
Gets the number of characters in this TextLine.
Definition: textline.cpp:304
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual void VerticallyAlign(const UI::LinearAlignment Align)
Aligns the text of the caption on the Y axis.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual CharIndexPair GetIndexAtOffsetImpl(const Vector2 &Offset)
Gets the index of the character at the specified offset position.
Value representing a SingleLineTextLayer.
virtual void SetStartIndex(const Integer &Index)
Sets the index from the beginning at which characters will start to be rendered.
Real GetOffsetAtIndex(const Integer &Index) const
Gets the Offset position of the character at the specified index.
Definition: textline.cpp:146
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
SingleLineTextLayer(QuadRenderable *ParentRenderable)
No-Font constructor.
bool Empty() const
Is this storing anything at all?
This represents a single line of text to be rendered by a TextLayer.
Definition: textline.h:59
virtual UI::RenderLayerType GetLayerType() const
Gets the type of render layer this is.
virtual Integer GetStartIndex() const
Gets the set index for the character that's at the start of the characters being rendered.
Boole AppendCharacter(Character *ToAdd, const Real MaxWidth)
Adds a Character to the end of this TextLine.
Definition: textline.cpp:237
std::pair< Boole, Integer > CharIndexPair
An std::pair type used as a return for index-offset conversions.
Definition: textlayer.h:86
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
LinearAlignment
Used by various UI classes to determine the alignment of their child objects, such as text in text li...
Characters/New Lines originate from the right, and advance to the left.
CharacterContainer::reverse_iterator ReverseCharacterIterator
Reverse Iterator type for Character instances stored by this class.
Definition: textlayer.h:74
bool SetValue(const Char8 *rhs)
Set the value of this.
virtual CharOffsetPair GetOffsetAtIndexImpl(const Integer Index)
Gets the offset position of the character at the provided index.
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Integer GetIndexAtOffset(const Real &Offset) const
Gets the index of the character at the provided offset.
Definition: textline.cpp:119
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. ...
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?
Real GetLineHeight() const
Gets the size of this TextLine on the Y axis.
Definition: textline.cpp:231
virtual void HorizontallyAlign(const UI::LinearAlignment Align)
Aligns the text of the caption on the X axis.
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this Renderable.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: textlayer.cpp:1007
virtual TextLine * GetLine() const
Gets a pointer to the only textline in this layer.
virtual void SetTextLineHorizontalAlignment(const UI::LinearAlignment Align)
Sets the horizontal alignment of every textline in this layer.
Definition: textlayer.cpp:726
Integer StartIndex
The index at which this text layer will start rendering characters.
RenderLayerType
This enum describes the type of RenderLayer this is for use in casting.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void PopulateTextLinesImpl(const Real MaxWidth)
Clears and then places characters belonging to this layer in the appropriate text lines...
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
TextLineContainer TextLines
Container with all this layers TextLines.
Definition: textlayer.h:101
This represents a nestable quad for an object in a GUI layout.
UI::TextOrdering HorizontalOrder
The order text will have in TextLines.
Definition: textlayer.h:143
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
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
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
CharacterContainer::iterator CharacterIterator
Iterator type for Character instances stored by this class.
Definition: textlayer.h:70
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.