MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multilinetextlayer.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 _uimultilinetextlayer_cpp
41 #define _uimultilinetextlayer_cpp
42 
43 #include "UI/multilinetextlayer.h"
44 #include "UI/uimanager.h"
45 #include "UI/textline.h"
46 #include "UI/character.h"
47 #include "countedptr.h"
48 
49 #include <algorithm>
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
56  TextLayer(ParentRenderable)
57  { }
58 
59  MultiLineTextLayer::MultiLineTextLayer(const String& FontName, QuadRenderable* ParentRenderable) :
60  TextLayer(FontName,ParentRenderable)
61  { }
62 
63  MultiLineTextLayer::MultiLineTextLayer(const Real& LineHeight, QuadRenderable* ParentRenderable) :
64  TextLayer(LineHeight,ParentRenderable)
65  { }
66 
68  { }
69 
71  {
72  UInt32 LineIndex = 0;
73  bool NewLineDetected = false;
74  TextLine* CurrLine = this->GetOrCreateTextLine(LineIndex);
76  {
77  CharacterIterator Start = this->Characters.begin();
78  CharacterIterator End = this->Characters.begin();
79  const CharacterIterator CharEnd = this->Characters.end();
80 
81  do{
82  if( (*Start)->IsWhiteSpace() ) {
83  // Find the next non-whitespace character
84  while( End != CharEnd )
85  {
86  if( (*End)->IsNewLine() )
87  {
88  NewLineDetected = true;
89  ++End;
90  break;
91  }else if( !(*End)->IsWhiteSpace() ) {
92  break;
93  }
94 
95  ++End;
96  }
97 
98  // We got our range, append what we can
99  CharacterIterator Result = CurrLine->AppendFittingCharacters(Start,End);
100  if( Result != End || NewLineDetected )
101  {
102  CurrLine = this->GetOrCreateTextLine(++LineIndex);
103  }
104  }else{
105  // Find the next whitespace character
106  while( End != CharEnd )
107  {
108  if( (*End)->IsWhiteSpace() )
109  break;
110 
111  ++End;
112  }
113 
114  // We got our range, so lets try to insert it
115  if( !CurrLine->AppendCharacters(Start,End) )
116  {
117  // If we failed to insert, get the next line and try again
118  CurrLine = this->GetOrCreateTextLine(++LineIndex);
119  if( !CurrLine->AppendCharacters(Start,End) )
120  {
121  End = CurrLine->AppendFittingCharacters(Start,End);
122  }
123  }
124  }
125  Start = End;
126  }while( Start != CharEnd );
127  }
129  {
130  CharacterIterator Start = --this->Characters.end();
131  CharacterIterator End = --this->Characters.end();
132  const CharacterIterator CharEnd = --this->Characters.begin();
133 
134  CharacterIteratorPair AppendPair;
135 
136  do{
137  if( (*End)->IsWhiteSpace() ) {
138  // Find the next non-whitespace character
139  while( Start != CharEnd )
140  {
141  if( (*Start)->IsNewLine() )
142  {
143  NewLineDetected = true;
144  ++Start;
145  break;
146  }else if( !(*Start)->IsWhiteSpace() ) {
147  break;
148  }
149 
150  ++Start;
151  }
152 
153  AppendPair.first = Start;
154  AppendPair.second = End;
155  ++AppendPair.first;
156  ++AppendPair.second;
157 
158  // We got our range, append what we can
159  CharacterIterator Result = CurrLine->AppendFittingCharacters(AppendPair);
160  if( Result != Start || NewLineDetected )
161  {
162  CurrLine = this->GetOrCreateTextLine(++LineIndex);
163  }
164  }else{
165  // Find the next whitespace character
166  while( Start != CharEnd )
167  {
168  if( (*Start)->IsWhiteSpace() )
169  break;
170 
171  ++Start;
172  }
173 
174  AppendPair.first = Start;
175  AppendPair.second = End;
176  ++AppendPair.first;
177  ++AppendPair.second;
178 
179  // We got our range, so lets try to insert it
180  if( !CurrLine->AppendCharacters(AppendPair) )
181  {
182  // If we failed to insert, get the next line and try again
183  CurrLine = this->GetOrCreateTextLine(++LineIndex);
184  if( !CurrLine->AppendCharacters(AppendPair) )
185  {
186  Start = CurrLine->AppendFittingCharacters(AppendPair);
187  }
188  }
189  }
190  End = Start;
191  }while( CharEnd != CharEnd );
192  }
193 
194  // Clean up unused text lines
195  if( this->TextLines.size() > 1 && this->TextLines.size() >= LineIndex )
196  {
197  TextLines.erase(this->TextLines.begin() + LineIndex, this->TextLines.end());
198  }
199  }
200 
202  {
203  CharIndexPair Ret(true,0);
204  TextLineIterator LineIt = this->TextLines.begin();
205  // Check if we're too far to the top to get anything
206  if( Offset.Y < (*LineIt)->GetPositionOffset() - (*LineIt)->GetLineHeight() )
207  return CharIndexPair(false,0);
208 
209  Integer IndexCount = 0;
210  while( LineIt != TextLines.end() && Offset.Y < (*LineIt)->GetPositionOffset() )
211  {
212  IndexCount += static_cast<Integer>( (*LineIt)->GetNumCharacters() );
213  ++LineIt;
214  }
215 
216  // Check if we're too low to get anything
217  if( LineIt == (--TextLines.end()) && Offset.Y > (*LineIt)->GetPositionOffset() )
218  return CharIndexPair(false,0);
219 
220  Ret.second = IndexCount + (*LineIt)->GetIndexAtOffset(Offset.X);
221  return Ret;
222  }
223 
225  {
226  CharOffsetPair Ret;
227  Ret.first = true;
228  if( Index < 0 || Index >= this->Characters.size() ) {
229  TextLineIterator Last = --this->TextLines.end();
230  Ret.second.Y = (*Last)->GetPositionOffset();
231  Ret.second.X = (*Last)->GetOffsetAtIndex(-1);
232  return Ret;
233  }else{
234  Integer IndexCount = 0;
235 
236  TextLineIterator LineIt = this->TextLines.begin();
237  while( LineIt != this->TextLines.end() && IndexCount + (*LineIt)->GetNumCharacters() < Index )
238  {
239  IndexCount += (*LineIt)->GetNumCharacters();
240  ++LineIt;
241  }
242  Ret.second.Y = (*LineIt)->GetPositionOffset();
243  Ret.second.X = (*LineIt)->GetOffsetAtIndex( Index - IndexCount );
244  return Ret;
245  }
246  }
247 
249  {
250  if( Index >= this->TextLines.size() ) return this->CreateTextLine();
251  else return this->TextLines.at(Index);
252  }
253 
254  ///////////////////////////////////////////////////////////////////////////////
255  // Utility
256 
258  {
259  return RenderLayer::RLT_MultiLineText;
260  }
261 
262  ///////////////////////////////////////////////////////////////////////////////
263  // Text Methods
264 
265  ///////////////////////////////////////////////////////////////////////////////
266  // Serialization
267 
269  {
271  }
272 
274  {
276  }
277 
279  {
281  }
282 
284  {
285  return "MultiLineTextLayer";
286  }
287  }//UI
288 }//Mezzanine
289 
290 #endif