Spinning Topp Logo BlackTopp Studios
inc
textline.h
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 _uitextline_h
42 #define _uitextline_h
43 
44 #include "character.h"
45 
46 namespace Mezzanine
47 {
48  namespace UI
49  {
50  class TextLayer;
51  ///////////////////////////////////////////////////////////////////////////////
52  /// @brief This represents a single line of text to be rendered by a TextLayer.
53  /// @details While many of the utilities of the TextLine class are exposed in public interfaces,
54  /// it is important to note that this class as a whole is not intended to be used directly by game
55  /// programmers. These interfaces are available for those that need that level of control but it
56  /// should be noted that manual manipulation of this class may disrupt the behaviors of not only this
57  /// class but it's parent layer as well. Query methods are, of course, safe from this.
58  ///////////////////////////////////////
60  {
61  public:
62  /// @brief Basic container type for @ref Character storage by this class.
63  typedef std::list<Character*> CharacterContainer;
64  /// @brief Iterator type for @ref Character instances stored by this class.
65  typedef CharacterContainer::iterator CharacterIterator;
66  /// @brief Const Iterator type for @ref Character instances stored by this class.
67  typedef CharacterContainer::const_iterator ConstCharacterIterator;
68  /// @brief Reverse Iterator type for @ref Character instances stored by this class.
69  typedef CharacterContainer::reverse_iterator ReverseCharacterIterator;
70  /// @brief Const Reverse Iterator type for @ref Character instances stored by this class.
71  typedef CharacterContainer::const_reverse_iterator ReverseConstCharacterIterator;
72  /// @brief An std::pair storing two CharacterIterators, usually for expressing a range.
73  typedef std::pair<CharacterIterator,CharacterIterator> CharacterIteratorPair;
74  /// @brief An std::pair storing a Character and it's offset position (relative to the parent layer).
75  typedef std::pair<Character*,Vector2> OffsetResultPair;
76  protected:
77  /// @internal
78  /// @brief Vector containing all the characters belonging to this TextLine.
79  CharacterContainer Characters;
80  /// @internal
81  /// @brief The parent layer this text line belongs to.
83  /// @internal
84  /// @brief The current length of this TextLine.
86  /// @internal
87  /// @brief The size of the largest glyph on the Y axis.
89  /// @internal
90  /// @brief The offset in pixels from the parent layer on the Y axis.
92  /// @internal
93  /// @brief The alignment of the text on the X axis in this line.
95 
96  /// @internal
97  /// @brief Gets the character before the last character.
98  /// @return Returns a pointer to the requested character.
99  virtual Character* GetSecondFromLastCharacter() const = 0;
100  /// @internal
101  /// @brief Recalculates the offset for every character in this line.
102  virtual void RecalculateOffsets() = 0;
103  /// @internal
104  /// @brief Adds a character such that it becomes the last in the sequence.
105  /// @param ToAppend The character to be appended.
106  virtual void AppendToBack(Character* ToAppend) = 0;
107  /// @internal
108  /// @brief Adds a series of characters to the end of this line.
109  /// @param First The first Character in a range to be appended.
110  /// @param Last The last Character in a range to be appended.
111  virtual void AppendToBack(CharacterIterator First, CharacterIterator Last) = 0;
112  public:
113  /// @brief Class constructor.
114  /// @param ParentLayer The TextLayer that this TextLine belongs to.
115  TextLine(TextLayer* ParentLayer);
116  /// @brief Class destructor.
117  virtual ~TextLine();
118 
119  ///////////////////////////////////////////////////////////////////////////////
120  // Utility
121 
122  /// @brief Sets the current alignment for the text in this line of text.
123  /// @param Align The enum value representing the horizontal alignment to be set.
124  void SetAlignment(const UI::LinearAlignment Align);
125  /// @brief Gets the current alignment for the text in this line of text.
126  /// @return Returns a TextAlignment value representing the alignment the text in this line will have.
127  UI::LinearAlignment GetAlignment() const;
128 
129  /// @brief Gets the character at the provided offset as well as the nearest valid position the cursor can take.
130  /// @param Offset The offset from the left side of this TextLine to use when finding the result.
131  /// @return Returns a OffsetResultPair with the "first" member containing a pointer to the character at the offset,
132  /// or NULL if there isn't one and the "second" member containing the closest valid position the cursor can be placed at
133  /// in the form of an offset from this TextLines left edge.
134  OffsetResultPair GetCharacterAndCursorPositionAtOffset(const Real& Offset) const;
135  /// @brief Gets the index of the character at the provided offset.
136  /// @note The index returned is only among the characters in this line and is not indicative of the overall index among all characters in the parent layer.
137  /// @param Offset The offset from the left side of this TextLine to use when finding the result.
138  /// @return Returns an Integer representing the index of the character at the given offset.
139  Integer GetIndexAtOffset(const Real& Offset) const;
140  /// @brief Gets the Offset position of the character at the specified index.
141  /// @note The index provided is only among the characters in this line and is not indicative of the overall index among all characters in the parent layer.
142  /// @param Index The index of the character in this line to get the offset of.
143  /// @return Returns a Real representing the distance in pixels the character at the provided index is from the left side of the TextLine.
144  Real GetOffsetAtIndex(const Integer& Index) const;
145 
146  /// @brief Gets the offset position of the left-most part of this TextLine.
147  /// @return Returns the left-most offset from the left side of this TextLine where the cursor can be placed.
148  Real GetLeftMostCursorPosition() const;
149  /// @brief Gets the offset position of the right-most part of this TextLine.
150  /// @return Returns the right-most offset from the right side of this TextLine where the cursor can be placed.
151  Real GetRightMostCursorPosition() const;
152  /// @brief Gets the offset position of the cursor based on the text order of the layer this text line belongs to.
153  /// @return Returns the left most cursor position if text is ordered left to right, and vice versa.
154  virtual Real GetCursorStartPosition() const = 0;
155  /// @brief Gets the closest valid cursor position to the offset provided.
156  /// @param Offset The offset from the left side of the TextLine.
157  /// @return Returns a Real containing the closest valid offset to the one provided.
158  Real GetClosestCursorPosition(const Real& Offset);
159 
160  ///////////////////////////////////////////////////////////////////////////////
161  // Transform Related Methods
162 
163  /// @brief Sets the offset on the Y axis from the parent layer.
164  /// @warning Adjusting this value manually can create visual artifacts. Only call this manually if you know what you are doing.
165  /// @param Offset The offset in pixels from the top of the parent layer to be this TextLines position.
166  void SetPositionOffset(const Real& Offset);
167  /// @brief Gets the offset on the Y axis from the parent layer.
168  /// @return Returns a Real representing the offset in pixels from the parent layer.
169  Real GetPositionOffset() const;
170  /// @brief Calculates the current length of this text line.
171  /// @note This method will return the length of the non-clipped characters.
172  /// @return Returns a Real representing the current length of this text line.
173  Real GetCurrentLength() const;
174  /// @brief Gets the size of this TextLine on the Y axis.
175  /// @return Returns a Real representing the height of this TextLine.
176  Real GetLineHeight() const;
177 
178  ///////////////////////////////////////////////////////////////////////////////
179  // Character Management
180 
181  /// @brief Adds a Character to the end of this TextLine.
182  /// @param ToAdd The Character to be added.
183  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
184  /// @return Returns true if the character was successfully added, false otherwise.
185  Boole AppendCharacter(Character* ToAdd, const Real MaxWidth);
186  /// @brief Adds a series of Characters to the end of this TextLine.
187  /// @param ToAdd A container storing all Characters to attempt to add.
188  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
189  /// @return Returns true if all characters were successfully added, false otherwise.
190  Boole AppendCharacters(CharacterContainer& ToAdd, const Real MaxWidth);
191  /// @brief Adds a series of Characters to the end of this TextLine.
192  /// @param Pair An std::pair containing iterators to both the first and last Characters in a sequence to attempt to append.
193  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
194  /// @return Returns true if all characters were successfully added, false otherwise.
195  Boole AppendCharacters(CharacterIteratorPair Pair, const Real MaxWidth);
196  /// @brief Adds a series of Characters to the end of this TextLine.
197  /// @param First Iterator to the first Character in the series to be added.
198  /// @param Last Iterator to one passed the last Character in the series to be added.
199  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
200  /// @return Returns true if all characters were successfully added, false otherwise.
201  virtual Boole AppendCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth) = 0;
202  /// @brief Adds as many Characters in a range as will fit to this TextLine.
203  /// @param ToAdd A container storing all Characters to attempt to add.
204  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
205  /// @return Returns a CharacterIterator to the first character that was not added in the range. This can be the left-most non-added Character when added to
206  /// Left-To-Right TextLines, or the right-most non-added Character in Right-To-Left TextLines.
207  CharacterIterator AppendFittingCharacters(CharacterContainer& ToAdd, const Real MaxWidth);
208  /// @brief Adds as many Characters in a range as will fit to this TextLine.
209  /// @param Pair An std::pair containing iterators to both the first and last Characters in a sequence to attempt to append.
210  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
211  /// @return Returns a CharacterIterator to the first character that was not added in the range. This can be the left-most non-added Character when added to
212  /// Left-To-Right TextLines, or the right-most non-added Character in Right-To-Left TextLines.
213  CharacterIterator AppendFittingCharacters(CharacterIteratorPair Pair, const Real MaxWidth);
214  /// @brief Adds as many Characters in a range as will fit to this TextLine.
215  /// @param First Iterator to the first Character in the series to be added.
216  /// @param Last Iterator to one passed the last Character in the series to be added.
217  /// @param MaxWidth The maximum line width to assume for all text lines that are to be populated.
218  /// @return Returns a CharacterIterator to the first character that was not added in the range. This can be the left-most non-added Character when added to
219  /// Left-To-Right TextLines, or the right-most non-added Character in Right-To-Left TextLines.
220  virtual CharacterIterator AppendFittingCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth) = 0;
221 
222  /// @brief Gets the character in this textline at the specified index.
223  /// @param Index The index of the character to retrieve.
224  /// @return Returns a pointer to the character at the requested index, or NULL if out of bounds.
225  Character* GetCharacterAtIndex(const UInt32& Index);
226  /// @brief Gets the character in this textline at the specified offset.
227  /// @param Offset The offset from the parent position to use when checking characters.
228  /// @return Returns a pointer to the character at the requested offset, or NULL of none exists.
229  Character* GetCharacterAtOffset(const Real& Offset);
230  /// @brief Gets the number of characters in this TextLine.
231  /// @return Returns a UInt32 containing the number of characters in this TextLine.
232  UInt32 GetNumCharacters() const;
233  /// @brief Removes all characters from this TextLine.
234  void RemoveAllCharacters();
235 
236  /// @brief Gets the first character in this TextLine.
237  /// @note The first character is based on the order in which the characters are read. If the characters are
238  /// meant to be read left-to-right then this will be the left most character. If the characters are meant to
239  /// be read right-to-left, this will be the right most character.
240  /// @return Returns an iterator to the first character in this TextLine.
241  virtual CharacterIterator GetFirstCharacter() = 0;
242  /// @brief Gets the last character in this TextLine.
243  /// @note The last character is based on the order in which the characters are read. If the characters are
244  /// meant to be read left-to-right then this will be the right most character. If the characters are meant to
245  /// be read right-to-left, this will be the left most character.
246  /// @return Returns an iterator to the last character in this TextLine.
247  virtual CharacterIterator GetLastCharacter() = 0;
248  /// @brief Gets an iterator to the next Character.
249  /// @param Current An iterator to a valid Character.
250  /// @return Returns an iterator to the next Character in the sequence.
251  virtual CharacterIterator GetNextCharacter(CharacterIterator Current) = 0;
252  /// @brief Removes the character at the start of this TextLine.
253  virtual void RemoveFirstCharacter() = 0;
254  /// @brief Removes the character at the end of this TextLine.
255  virtual void RemoveLastCharacter() = 0;
256 
257  /// @brief Gets an iterator to the first Character.
258  /// @return Returns an iterator to the first Character being stored by this TextLine.
259  CharacterIterator BeginCharacter();
260  /// @brief Gets an iterator to one passed the last Character.
261  /// @return Returns an iterator to one passed the last Character being stored by this TextLine.
262  CharacterIterator EndCharacter();
263  /// @brief Gets a const iterator to the first Character.
264  /// @return Returns a const iterator to the first Character being stored by this TextLine.
265  ConstCharacterIterator BeginCharacter() const;
266  /// @brief Gets an iterator to one passed the last Character.
267  /// @return Returns an iterator to one passed the last Character being stored by this TextLine.
268  ConstCharacterIterator EndCharacter() const;
269  };//TextLine
270 
271  ///////////////////////////////////////////////////////////////////////////////
272  /// @brief This is a TextLine specialization class for text read from the left to the right.
273  /// @details
274  ///////////////////////////////////////
276  {
277  protected:
278  /// @copydoc TextLine::GetSecondFromLastCharacter()
279  Character* GetSecondFromLastCharacter() const;
280  /// @copydoc TextLine::RecalculateOffsets()
281  void RecalculateOffsets();
282  /// @copydoc TextLine::AppendToBack(Character*)
283  void AppendToBack(Character* ToAppend);
284  /// @copydoc TextLine::AppendToBack(CharacterIterator, CharacterIterator)
285  void AppendToBack(CharacterIterator First, CharacterIterator Last);
286  public:
287  /// @brief Class constructor.
288  /// @param ParentLayer The TextLayer that this TextLine belongs to.
289  LeftToRightTextLine(TextLayer* ParentLayer);
290  /// @brief Class destructor.
291  virtual ~LeftToRightTextLine();
292 
293  ///////////////////////////////////////////////////////////////////////////////
294  // Utility
295 
296  /// @copydoc TextLine::GetCursorStartPosition() const
297  Real GetCursorStartPosition() const;
298 
299  ///////////////////////////////////////////////////////////////////////////////
300  // Character Management
301 
302  /// @copydoc TextLine::AppendCharacters(CharacterIterator, CharacterIterator, const Real)
303  Boole AppendCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth);
304  /// @copydoc TextLine::AppendFittingCharacters(CharacterIterator, CharacterIterator, const Real)
305  CharacterIterator AppendFittingCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth);
306 
307  /// @copydoc TextLine::GetFirstCharacter()
308  CharacterIterator GetFirstCharacter();
309  /// @copydoc TextLine::GetLastCharacter()
310  CharacterIterator GetLastCharacter();
311  /// @copydoc TextLine::GetNextCharacter(CharacterIterator Current)
312  CharacterIterator GetNextCharacter(CharacterIterator Current);
313  /// @copydoc TextLine::RemoveFirstCharacter()
314  void RemoveFirstCharacter();
315  /// @copydoc TextLine::RemoveLastCharacter()
316  void RemoveLastCharacter();
317  };//LeftToRightTextLine
318 
319  ///////////////////////////////////////////////////////////////////////////////
320  /// @brief This is a TextLine specialization class for text read from the right to the left.
321  /// @details
322  ///////////////////////////////////////
324  {
325  protected:
326  /// @copydoc TextLine::GetSecondFromLastCharacter()
327  Character* GetSecondFromLastCharacter() const;
328  /// @copydoc TextLine::RecalculateOffsets()
329  void RecalculateOffsets();
330  /// @copydoc TextLine::AppendToBack(Character*)
331  void AppendToBack(Character* ToAppend);
332  /// @copydoc TextLine::AppendToBack(CharacterIterator, CharacterIterator)
333  void AppendToBack(CharacterIterator First, CharacterIterator Last);
334  public:
335  /// @brief Class constructor.
336  /// @param ParentLayer The TextLayer that this TextLine belongs to.
337  RightToLeftTextLine(TextLayer* ParentLayer);
338  /// @brief Class destructor.
339  virtual ~RightToLeftTextLine();
340 
341  ///////////////////////////////////////////////////////////////////////////////
342  // Utility
343 
344  /// @copydoc TextLine::GetCursorStartPosition() const
345  Real GetCursorStartPosition() const;
346 
347  ///////////////////////////////////////////////////////////////////////////////
348  // Character Management
349 
350  /// @copydoc TextLine::AppendCharacters(CharacterIterator, CharacterIterator, const Real)
351  Boole AppendCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth);
352  /// @copydoc TextLine::AppendFittingCharacters(CharacterIterator, CharacterIterator, const Real)
353  CharacterIterator AppendFittingCharacters(CharacterIterator First, CharacterIterator Last, const Real MaxWidth);
354 
355  /// @copydoc TextLine::GetFirstCharacter()
356  CharacterIterator GetFirstCharacter();
357  /// @copydoc TextLine::GetLastCharacter()
358  CharacterIterator GetLastCharacter();
359  /// @copydoc TextLine::GetNextCharacter(CharacterIterator Current)
360  CharacterIterator GetNextCharacter(CharacterIterator Current);
361  /// @copydoc TextLine::RemoveFirstCharacter()
362  void RemoveFirstCharacter();
363  /// @copydoc TextLine::RemoveLastCharacter()
364  void RemoveLastCharacter();
365  };//RightToLeftTextLine
366  }//UI
367 }//Mezzanine
368 
369 #endif
TextLayer * Parent
The parent layer this text line belongs to.
Definition: textline.h:82
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Real PositionOffset
The offset in pixels from the parent layer on the Y axis.
Definition: textline.h:91
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Real CurrLength
The current length of this TextLine.
Definition: textline.h:85
This represents a single line of text to be rendered by a TextLayer.
Definition: textline.h:59
This is a TextLine specialization class for text read from the right to the left. ...
Definition: textline.h:323
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...
std::list< Character * > CharacterContainer
Basic container type for Character storage by this class.
Definition: textline.h:63
This is a TextLine specialization class for text read from the left to the right. ...
Definition: textline.h:275
CharacterContainer::const_reverse_iterator ReverseConstCharacterIterator
Const Reverse Iterator type for Character instances stored by this class.
Definition: textline.h:71
std::pair< Character *, Vector2 > OffsetResultPair
An std::pair storing a Character and it's offset position (relative to the parent layer)...
Definition: textline.h:75
This class creates and encapsultes a character that can be used in text renders.
Definition: character.h:59
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
Real TallestHeight
The size of the largest glyph on the Y axis.
Definition: textline.h:88
UI::LinearAlignment Alignment
The alignment of the text on the X axis in this line.
Definition: textline.h:94
std::pair< CharacterIterator, CharacterIterator > CharacterIteratorPair
An std::pair storing two CharacterIterators, usually for expressing a range.
Definition: textline.h:73
CharacterContainer::iterator CharacterIterator
Iterator type for Character instances stored by this class.
Definition: textline.h:65
CharacterContainer::reverse_iterator ReverseCharacterIterator
Reverse Iterator type for Character instances stored by this class.
Definition: textline.h:69
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
CharacterContainer::const_iterator ConstCharacterIterator
Const Iterator type for Character instances stored by this class.
Definition: textline.h:67
This is a base class for render layers that render text.
Definition: textlayer.h:64
CharacterContainer Characters
Vector containing all the characters belonging to this TextLine.
Definition: textline.h:79