MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textcursor.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 
41 #ifndef _uitextcursor_cpp
42 #define _uitextcursor_cpp
43 
44 #include "UI/textcursor.h"
45 #include "UI/textline.h"
46 #include "UI/character.h"
47 #include "mathtool.h"
48 #include "stringtool.h"
49 #include "XML/xml.h"
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
56  IndexPosition(0), Layer(Creator)
57  { this->CursorColour = ColourValue::Black(); }
58 
60  { }
61 
62  ///////////////////////////////////////////////////////////////////////////////
63  // Utility Methods
64 
65  void TextCursor::SetVisible(Boolean Visible)
66  { this->Visibility = Visible; }
67 
68  Boolean TextCursor::GetVisible() const
69  { return this->Visibility; }
70 
72  { this->IndexPosition = Index; }
73 
75  { return this->IndexPosition; }
76 
78  {
79  TextLayer::CharIndexPair ResultPair = this->Layer->GetIndexAtOffset(Offset);
80  if( ResultPair.first ) {
81  this->IndexPosition = ResultPair.second;
82  }
83  }
84 
86  { return this->Layer->GetOffsetAtIndex(this->IndexPosition); }
87 
89  {
90  Rect Ret;
91  if( this->IndexPosition < 0 || this->IndexPosition >= Layer->GetNumCharacters() ) {
93 
94  Ret.Position.Y = (*Last)->GetPositionOffset();
95  Ret.Position.X = (*Last)->GetOffsetAtIndex(-1);
96  Ret.Size.Y = (*Last)->GetLineHeight();
97  Ret.Size.X = MathTools::Floor( Ret.Size.Y * 0.1 ) + 1.0;
98  }else{
99  Integer IndexCount = 0;
100 
102  while( LineIt != Layer->EndTextLine() && IndexCount + (*LineIt)->GetNumCharacters() < this->IndexPosition )
103  {
104  IndexCount += (*LineIt)->GetNumCharacters();
105  ++LineIt;
106  }
107 
108  Ret.Position.Y = (*LineIt)->GetPositionOffset();
109  Ret.Position.X = (*LineIt)->GetOffsetAtIndex( this->IndexPosition - IndexCount );
110  Ret.Size.Y = (*LineIt)->GetLineHeight();
111  Ret.Size.X = MathTools::Floor( Ret.Size.Y * 0.1 ) + 1.0;
112  }
113  return Ret;
114  }
115 
116  void TextCursor::SetColour(const ColourValue& Colour)
117  {
118  if( this->CursorColour == Colour )
119  return;
120 
121  this->CursorColour = Colour;
122  this->Layer->_MarkDirty();
123  }
124 
126  {
127  return this->CursorColour;
128  }
129 
130  ///////////////////////////////////////////////////////////////////////////////
131  // Left and Right Methods
132 
134  { --(this->IndexPosition); }
135 
137  { ++(this->IndexPosition); }
138 
140  { this->Layer->InsertCharacterAtIndex(this->IndexPosition,GlyphID); }
141 
142  void TextCursor::InsertCharactersAtCursor(const Char8* Characters, const UInt32 BufSize)
143  { this->Layer->InsertCharactersAtIndex(this->IndexPosition,Characters,BufSize); }
144 
145  void TextCursor::InsertCharactersAtCursor(const UInt32* Characters, const UInt32 BufSize)
146  { this->Layer->InsertCharactersAtIndex(this->IndexPosition,Characters,BufSize); }
147 
149  {
150  UInt32 NumChars = this->Layer->GetNumCharacters();
151  if( NumChars > 0 ) {
152  if( this->IndexPosition < 0 || this->IndexPosition > this->Layer->GetNumCharacters() ) {
153  this->Layer->RemoveCharacterAtIndex( NumChars - 1 );
154  }else{
155  this->Layer->RemoveCharacterAtIndex(--(this->IndexPosition));
156  }
157  }
158  }
159 
161  {
162  UInt32 NumChars = this->Layer->GetNumCharacters();
163  if( NumChars > 0 ) {
164  if( this->IndexPosition < 0 || this->IndexPosition > NumChars ) {
165  // Nothing to be done, may fill out other logic later.
166  }else{
168  }
169  }
170  }
171 
172  ///////////////////////////////////////////////////////////////////////////////
173  // Serialization
174 
175  void TextCursor::ProtoSerialize(XML::Node& ParentNode) const
176  {
177  XML::Node SelfRoot = ParentNode.AppendChild( this->TextCursor::GetSerializableName() );
178 
179  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
180  SelfRoot.AppendAttribute("IndexPosition").SetValue(this->IndexPosition) &&
181  SelfRoot.AppendAttribute("Visibility").SetValue( ( this->Visibility ? "True" : "False" ) ) )
182  {
183  XML::Node CursorColourNode = SelfRoot.AppendChild("CursorColour");
184  this->CursorColour.ProtoSerialize( CursorColourNode );
185 
186  return;
187  }else{
188  SerializeError("Create XML Attribute Values",TextCursor::GetSerializableName(),true);
189  }
190  }
191 
193  {
194  XML::Attribute CurrAttrib;
195 
196  if( !SelfRoot.Empty() ) {
197  if( SelfRoot.GetAttribute("Version").AsInt() == 1 ) {
198  CurrAttrib = SelfRoot.GetAttribute("IndexPosition");
199  if( !CurrAttrib.Empty() )
200  this->IndexPosition = CurrAttrib.AsWhole();
201 
202  CurrAttrib = SelfRoot.GetAttribute("Visibility");
203  if( !CurrAttrib.Empty() )
204  this->Visibility = StringTools::ConvertToBool( CurrAttrib.AsString() );
205 
206  XML::Node CursorColourNode = SelfRoot.GetChild("CursorColour").GetFirstChild();
207  if( !CursorColourNode.Empty() )
208  this->CursorColour.ProtoDeSerialize(CursorColourNode);
209  }else{
210  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + TextCursor::GetSerializableName() + ": Not Version 1.");
211  }
212  }else{
213  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,TextCursor::GetSerializableName() + " was not found in the provided XML node, which was expected.");
214  }
215  }
216 
218  { return "TextCursor"; }
219  }//UI
220 }//Mezzanine
221 
222 #endif