MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
singlelinetextlayer.cpp
1 //© Copyright 2010 - 2012 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 "mathtool.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  UInt32 Count = 0;
77  TextLine* Caption = GetLine();
79  {
80  for( CharacterIterator CharIt = Characters.begin() ; CharIt != Characters.end() ; ++CharIt )
81  {
82  if( Count < StartIndex )
83  continue;
84 
85  if( !Caption->AppendCharacter( (*CharIt) ) )
86  break;
87  }
88  }
90  {
91  for( ReverseCharacterIterator CharIt = Characters.rbegin() ; CharIt != Characters.rend() ; ++CharIt )
92  {
93  if( Count < StartIndex )
94  continue;
95 
96  if( !Caption->AppendCharacter( (*CharIt) ) )
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 || 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  {
124  return this->TextLines.at(0);
125  }
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Utility
129 
131  {
132  return RenderLayer::RLT_SingleLineText;
133  }
134 
135  ///////////////////////////////////////////////////////////////////////////////
136  // Text Methods
137 
139  {
140  this->GetLine()->SetAlignment(Align);
141  this->_MarkDirty();
142  }
143 
145  {
146  this->SetTextlineVerticalAlignment(Align);
147  // Previous line will mark dirty for us
148  }
149 
151  {
152  this->StartIndex = Index;
153  }
154 
156  {
157  return this->StartIndex;
158  }
159 
160  ///////////////////////////////////////////////////////////////////////////////
161  // Serialization
162 
164  {
166  XML::Node PropertiesNode = SelfRoot.AppendChild( SingleLineTextLayer::GetSerializableName() + "Properties" );
167 
168  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
169  PropertiesNode.AppendAttribute("StartIndex").SetValue(this->StartIndex) )
170  {
171  return;
172  }else{
173  SerializeError("Create XML Attribute Values",SingleLineTextLayer::GetSerializableName() + "Properties",true);
174  }
175  }
176 
178  {
180  XML::Attribute CurrAttrib;
181  XML::Node PropertiesNode = SelfRoot.GetChild( SingleLineTextLayer::GetSerializableName() + "Properties" );
182 
183  if( !PropertiesNode.Empty() ) {
184  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
185  CurrAttrib = PropertiesNode.GetAttribute("StartIndex");
186  if( !CurrAttrib.Empty() )
187  this->StartIndex = CurrAttrib.AsInt();
188  }else{
189  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (SingleLineTextLayer::GetSerializableName() + "Properties") + ": Not Version 1.");
190  }
191  }else{
192  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,SingleLineTextLayer::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
193  }
194  }
195 
197  {
199  }
200 
202  {
203  return "SingleLineTextLayer";
204  }
205  }//UI
206 }//Mezzanine
207 
208 #endif