TDME2  1.9.200
GUIStyledTextNode.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
8 #include <tdme/gui/fwd-tdme.h>
11 #include <tdme/gui/nodes/GUINode.h>
16 #include <tdme/utilities/Integer.h>
20 
21 using std::string;
22 using std::vector;
23 
43 
44 /**
45  * GUI styled text node
46  * @author Andreas Drewke
47  */
49  : public GUINode
50 {
51  friend class tdme::gui::GUIParser;
54 
55 private:
56  struct TextStyle {
57  int startIdx;
58  int endIdx;
61  string url;
63  int textureId;
64  int width;
65  int height;
68  };
69 
70  bool editable;
72  GUIFont* font { nullptr };
73  int size;
76 
77  int autoWidth { 0 };
78  int autoHeight { 0 };
79 
86  int widthLast;
88 
89  vector<TextStyle> styles;
90  int startTextStyleIdx { -1 };
91 
92  struct Line {
94  int binaryIdx,
95  int charIdx,
96  float width,
97  float height,
98  float lineHeight,
99  float baseLine,
100  bool spaceWrap
101  ):
103  charIdx(charIdx),
104  width(width),
105  height(height),
109  {}
111  int charIdx;
112  float width;
113  float height;
114  float lineHeight;
115  float baseLine;
116  bool spaceWrap;
117  };
118 
119  int tabSize { 4 };
120  string spaceString { " " };
121  string newLine { "\n" };
122  string line;
123  vector<int> lineCharBinaryIdxs;
124  vector<int> lineCharIdxs;
125  vector<Line> lineConstraints;
126 
127  struct URLArea {
129  int left,
130  int top,
131  int width,
132  int height,
133  const string& url
134  ):
135  left(left),
136  top(top),
137  width(width),
138  height(height),
139  url(url)
140  {}
141  int left;
142  int top;
143  int width;
144  int height;
145  string url;
146  };
147  vector<URLArea> urlAreas;
148 
149  static constexpr int MOUSEPOSITION_NONE { Integer::MIN_VALUE };
150  int indexPositionX { 0 };
151  int indexPositionY { 0 };
156 
158 
159  bool editMode { false };
160 
161  /**
162  * @return edit mode
163  */
164  inline bool isEditMode() {
165  return editMode;
166  }
167 
168  /**
169  * @return edit mode
170  */
171  inline void setEditMode(bool editMode) {
172  this->editMode = editMode;
173  }
174 
175  /**
176  * Unset index mouse position
177  */
179 
180  /**
181  * Set index mouse position
182  */
183  void setIndexMousePosition(int x, int y);
184 
185  /**
186  * Unset selection index mouse position
187  */
189 
190  /**
191  * Set selection index mouse position
192  */
193  void setSelectionIndexMousePosition(int x, int y);
194 
195  /**
196  * @return URL areas
197  */
198  inline const vector<URLArea>& getURLAreas() {
199  return urlAreas;
200  }
201 
202  /**
203  * Do page up
204  * @return cursor index
205  */
206  int doPageUp();
207 
208  /**
209  * Do page down
210  * @return cursor index
211  */
212  int doPageDown();
213 
214  /**
215  * Get text style for
216  * @param lineCharIdxs line character indices
217  * @param lineCharIdx line character idx
218  * @param textStyleIdx text style to start looking up with, will also be written
219  * @return text style
220  */
221  inline TextStyle* getTextStyle(const vector<int>& lineCharIdxs, int lineCharIdx, int& textStyleIdx) {
222  if (styles.empty() == true) return nullptr;
223  TextStyle* textStyle = nullptr;
224  // find style to start with, aligned with last line start, if we do not have a start yet
225  if (textStyleIdx == -1) {
226  textStyleIdx = 0;
227  for (auto l = 0; l < styles.size(); l++) {
228  auto textStyle = &styles[l];
229  if (textStyle->startIdx > lineCharIdxs[lineCharIdx]) {
230  textStyleIdx = l - 1;
231  break;
232  }
233  }
234  }
235  // ok proceed to find correct style for character in text, based on our text style index
236  auto _textStyle = textStyleIdx < styles.size()?&styles[textStyleIdx]:nullptr;
237  if (_textStyle != nullptr && lineCharIdxs[lineCharIdx] >= _textStyle->startIdx) {
238  if (lineCharIdxs[lineCharIdx] > _textStyle->endIdx) {
239  // invalid text style, check next text style
240  textStyleIdx++;
241  _textStyle = textStyleIdx < styles.size()?&styles[textStyleIdx]:nullptr;
242  if (_textStyle != nullptr && lineCharIdxs[lineCharIdx] >= _textStyle->startIdx) {
243  if (lineCharIdxs[lineCharIdx] <= _textStyle->endIdx) {
244  // valid text style
245  textStyle = _textStyle;
246  }
247  }
248  } else
249  if (lineCharIdxs[lineCharIdx] <= _textStyle->endIdx) {
250  // valid text style
251  textStyle = _textStyle;
252  }
253  }
254  return textStyle;
255  }
256 
257  /**
258  * Determine next line constraints
259  * @param u8It UTF 8 character iterator
260  * @param charEndIdx character end index
261  * @param textStyleIdx text style index to start with
262  */
263  void determineNextLineConstraints(UTF8CharacterIterator& u8It, int charEndIdx, int textStyleIdx);
264 
265  /**
266  * Compute content alignment internal
267  */
269 
270 protected:
271  // forbid class copy
273 
274  /**
275  * Constructor
276  * @param screenNode screen node
277  * @param parentNode parent node
278  * @param id id
279  * @param flow flow
280  * @param alignments alignments
281  * @param requestedConstraints requested constraints
282  * @param backgroundColor background color
283  * @param backgroundImage background image
284  * @param backgroundImageScale9Grid background image scale 9 grid
285  * @param backgroundImageEffectColorMul background image effect color mul
286  * @param backgroundImageEffectColorAdd background image effect color add
287  * @param border border
288  * @param padding padding
289  * @param showOn show on
290  * @param hideOn hide on
291  * @param tooltip tooltip
292  * @param preformatted preformatted
293  * @param font font
294  * @param size font size
295  * @param color color
296  * @param text text
297  * @param editable editable
298  * @throws tdme::gui::GUIParserException
299  */
303  const string& id,
307  const GUIColor& backgroundColor,
308  const string& backgroundImage,
312  const GUINode_Border& border,
313  const GUINode_Padding& padding,
314  const GUINodeConditions& showOn,
315  const GUINodeConditions& hideOn,
316  const string& tooltip,
317  bool editable,
318  bool preformatted,
319  const string& font,
320  int size,
321  const string& color,
322  const MutableString& text
323  );
324 
325  // overridden methods
326  const string getNodeType() override;
327  bool isContentNode() override;
328 
329 public:
330  // overridden methods
331  int getContentWidth() override;
332  int getContentHeight() override;
333  void computeContentAlignment() override;
334  void dispose() override;
335  void render(GUIRenderer* guiRenderer) override;
336 
337  /**
338  * @return if is editable
339  */
340  inline bool isEditable() {
341  return editable;
342  }
343 
344  /**
345  * @return index position x
346  */
347  inline int getIndexPositionX() {
348  return indexPositionX;
349  }
350 
351  /**
352  * @return index position x
353  */
354  inline int getIndexPositionY() {
355  return indexPositionY;
356  }
357 
358  /**
359  * @return text
360  */
361  inline const MutableString& getText() const {
362  return text;
363  }
364 
365  /**
366  * Get previous new line
367  * @param index index
368  */
369  inline int getPreviousNewLine(int index) {
370  // find index of previous newline and store difference
371  auto previousNewLineIndex = index;
372  while (previousNewLineIndex >= 0 && text.getCharAt(previousNewLineIndex) != '\n') previousNewLineIndex--;
373  previousNewLineIndex = Math::max(previousNewLineIndex, 0);
374  return previousNewLineIndex;
375  }
376 
377  /**
378  * Get previous new line using Utf8 indices
379  * @param index index
380  */
381  inline int getPreviousNewLineUtf8(int index) {
382  // find index of previous newline and store difference
383  auto previousNewLineIndex = index;
384  while (previousNewLineIndex >= 0 && text.getUTF8CharAt(previousNewLineIndex) != '\n') previousNewLineIndex--;
385  previousNewLineIndex = Math::max(previousNewLineIndex, 0);
386  return previousNewLineIndex;
387  }
388 
389  /**
390  * Get next newline using
391  * @param index index
392  */
393  inline int getNextNewLine(int index) {
394  // find index of next newline
395  auto nextNewLineIndex = index;
396  auto textSize = text.size();
397  while (nextNewLineIndex < textSize && text.getCharAt(nextNewLineIndex) != '\n') nextNewLineIndex++;
398  nextNewLineIndex = Math::min(nextNewLineIndex, text.size() - 1);
399  return nextNewLineIndex;
400  }
401 
402  /**
403  * Get next newline using Utf8 indices
404  * @param index index
405  */
406  inline int getNextNewLineUtf8(int index) {
407  // find index of next newline
408  auto nextNewLineIndex = index;
409  auto textLength = text.length();
410  while (nextNewLineIndex < textLength && text.getUTF8CharAt(nextNewLineIndex) != '\n') nextNewLineIndex++;
411  nextNewLineIndex = Math::min(nextNewLineIndex, text.size() - 1);
412  return nextNewLineIndex;
413  }
414 
415  /**
416  * Get previous delimiter
417  * @param index index
418  * @param delimiters delimiters
419  */
420  inline int getPreviousDelimiter(int index, const string& delimiters) {
421  // find index of previous newline and store difference
422  auto previousDelimiterIndex = index;
423  while (previousDelimiterIndex >= 0 && delimiters.find(text.getCharAt(previousDelimiterIndex)) == string::npos) previousDelimiterIndex--;
424  previousDelimiterIndex = Math::max(previousDelimiterIndex, 0);
425  return previousDelimiterIndex;
426  }
427 
428  /**
429  * Get previous delimiter using Utf8 indices
430  * @param index index
431  * @param delimiters delimiters
432  */
433  inline int getPreviousDelimiterUtf8(int index, const string& delimiters) {
434  // find index of previous newline and store difference
435  auto previousDelimiterIndex = index;
436  while (previousDelimiterIndex >= 0 && delimiters.find(text.getUTF8CharAt(previousDelimiterIndex)) == string::npos) previousDelimiterIndex--;
437  previousDelimiterIndex = Math::max(previousDelimiterIndex, 0);
438  return previousDelimiterIndex;
439  }
440 
441  /**
442  * Get next delimiter
443  * @param index index
444  * @param delimiters
445  */
446  inline int getNextDelimiter(int index, const string& delimiters) {
447  // find index of next newline
448  auto nextDelimiterIndex = index;
449  auto textSize = text.size();
450  while (nextDelimiterIndex < textSize && delimiters.find(text.getUTF8CharAt(nextDelimiterIndex)) == string::npos) nextDelimiterIndex++;
451  nextDelimiterIndex = Math::min(nextDelimiterIndex, text.size() - 1);
452  return nextDelimiterIndex;
453  }
454 
455  /**
456  * Get next delimiter using Utf8 indices
457  * @param index index
458  * @param delimiters
459  */
460  inline int getNextDelimiterUtf8(int index, const string& delimiters) {
461  // find index of next newline
462  auto nextDelimiterIndex = index;
463  auto textLength = text.length();
464  while (nextDelimiterIndex < textLength && delimiters.find(text.getUTF8CharAt(nextDelimiterIndex)) == string::npos) nextDelimiterIndex++;
465  nextDelimiterIndex = Math::min(nextDelimiterIndex, text.size() - 1);
466  return nextDelimiterIndex;
467  }
468 
469  /**
470  * @return text size
471  */
472  inline int getTextLength() {
473  return text.length();
474  }
475 
476  /**
477  * Remove characters at idx with given length
478  * @param idx idx
479  * @param count length
480  * @return this mutable string
481  */
482  void removeText(int32_t idx, int32_t count);
483 
484  /**
485  * Insert character c at idx
486  * @param idx index
487  * @param c char
488  * @return this mutable string
489  */
490  void insertText(int32_t idx, int c);
491 
492  /**
493  * Insert string at idx
494  * @param idx index
495  * @param s string
496  * @return this mutable string
497  */
498  void insertText(int32_t idx, const string& s);
499 
500  /**
501  * Unset/dispose styles
502  */
503  void unsetStyles();
504 
505  /**
506  * Set text
507  * @param text text
508  */
509  void setText(const MutableString& text);
510 
511  /**
512  * Unset text style
513  * @param startIdx text start index
514  * @param endIdx text end index
515  */
516  void unsetTextStyle(int startIdx, int endIdx);
517 
518  /**
519  * Unset text style using Utf8 indices
520  * @param startIdx text start index
521  * @param endIdx text end index
522  */
523  inline void unsetTextStyleUtf8(int startIdx, int endIdx) {
525  }
526 
527  /**
528  * Set text style
529  * @param startIdx text start index
530  * @param endIdx text end index
531  * @param color color
532  * @param font font
533  * @param url url
534  */
535  void setTextStyle(int startIdx, int endIdx, const GUIColor& color, const string& font = string(), int size = -1, const string& url = string());
536 
537  /**
538  * Set text style using Utf8 indices
539  * @param startIdx text start index
540  * @param endIdx text end index
541  * @param color color
542  * @param font font
543  * @param url url
544  */
545  inline void setTextStyleUtf8(int startIdx, int endIdx, const GUIColor& color, const string& font = string(), int size = -1, const string& url = string()) {
547  }
548 
549  /**
550  * Set text style
551  * @param startIdx text start index
552  * @param endIdx text end index
553  * @param font font
554  * @param url url
555  */
556  void setTextStyle(int startIdx, int endIdx, const string& font, int size, const string& url = string());
557 
558  /**
559  * Set text style using Utf8 indices
560  * @param startIdx text start index
561  * @param endIdx text end index
562  * @param font font
563  * @param url url
564  */
565  inline void setTextStyleUtf8(int startIdx, int endIdx, const string& font, int size, const string& url = string()) {
567  }
568 
569  /**
570  * Set image
571  * @param idx index
572  * @param image image
573  * @param url url
574  * @param width width or -1 for original image width
575  * @param height height or -1 for original image height
576  * @param horizontalScale horizontal scale as factor
577  * @param verticalScale vertical scale as factor
578  * @param effectColorMul effect color mul
579  * @param effectColorAdd effect color add
580  */
581  void setImage(
582  int idx,
583  const string& image,
584  const string& url = string(),
585  int width = -1,
586  int height = -1,
587  float horizontalScale = 1.0f,
588  float verticalScale = 1.0f,
589  const GUIColor& effectColorMul = GUIColor::GUICOLOR_EFFECT_COLOR_MUL,
590  const GUIColor& effectColorAdd = GUIColor::GUICOLOR_EFFECT_COLOR_ADD
591  );
592 
593  /**
594  * Set image using Utf8 index
595  * @param idx index
596  * @param image image
597  * @param url url
598  * @param width width or -1 for original image width
599  * @param height height or -1 for original image height
600  * @param horizontalScale horizontal scale as factor
601  * @param verticalScale vertical scale as factor
602  * @param effectColorMul effect color mul
603  * @param effectColorAdd effect color add
604  */
605  inline void setImageUtf8(
606  int idx,
607  const string& image,
608  const string& url = string(),
609  int width = -1,
610  int height = -1,
611  float horizontalScale = 1.0f,
612  float verticalScale = 1.0f,
613  const GUIColor& effectColorMul = GUIColor::GUICOLOR_EFFECT_COLOR_MUL,
614  const GUIColor& effectColorAdd = GUIColor::GUICOLOR_EFFECT_COLOR_ADD
615  ) {
616  setImage(
618  image,
619  url,
620  width,
621  height,
622  horizontalScale,
623  verticalScale,
624  effectColorMul,
625  effectColorAdd
626  );
627  }
628 
629  /**
630  * Set scroll to index
631  */
632  void scrollToIndex(int index);
633 
634  /**
635  * Set scroll to index
636  */
637  void scrollToIndex();
638 
639  /**
640  * Set scroll to selection index
641  */
642  void scrollToSelectionIndex();
643 
644 };
Texture entity.
Definition: Texture.h:24
GUI parser.
Definition: GUIParser.h:40
static STATIC_DLL_IMPEXT GUIColor GUICOLOR_EFFECT_COLOR_ADD
Definition: GUIColor.h:36
static STATIC_DLL_IMPEXT GUIColor GUICOLOR_EFFECT_COLOR_MUL
Definition: GUIColor.h:35
GUI element node conditions.
GUI node base class.
Definition: GUINode.h:64
GUINodeConditions hideOn
Definition: GUINode.h:162
GUIColor backgroundImageEffectColorMul
Definition: GUINode.h:157
GUINode_Border border
Definition: GUINode.h:160
GUINode_Scale9Grid backgroundImageScale9Grid
Definition: GUINode.h:156
GUIColor backgroundImageEffectColorAdd
Definition: GUINode.h:158
GUIParentNode * parentNode
Definition: GUINode.h:148
GUINode_Padding padding
Definition: GUINode.h:159
GUIScreenNode * screenNode
Definition: GUINode.h:147
GUINode_RequestedConstraints requestedConstraints
Definition: GUINode.h:151
GUINode_Alignments alignments
Definition: GUINode.h:150
GUINodeConditions showOn
Definition: GUINode.h:161
GUINode_Flow * flow
Definition: GUINode.h:86
GUI parent node base class thats supporting child nodes.
Definition: GUIParentNode.h:42
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
void unsetTextStyleUtf8(int startIdx, int endIdx)
Unset text style using Utf8 indices.
void setImage(int idx, const string &image, const string &url=string(), int width=-1, int height=-1, float horizontalScale=1.0f, float verticalScale=1.0f, const GUIColor &effectColorMul=GUIColor::GUICOLOR_EFFECT_COLOR_MUL, const GUIColor &effectColorAdd=GUIColor::GUICOLOR_EFFECT_COLOR_ADD)
Set image.
int getNextDelimiter(int index, const string &delimiters)
Get next delimiter.
void dispose() override
Dispose node.
void setTextStyleUtf8(int startIdx, int endIdx, const GUIColor &color, const string &font=string(), int size=-1, const string &url=string())
Set text style using Utf8 indices.
void insertText(int32_t idx, int c)
Insert character c at idx.
void unsetTextStyle(int startIdx, int endIdx)
Unset text style.
int getPreviousNewLineUtf8(int index)
Get previous new line using Utf8 indices.
void setIndexMousePosition(int x, int y)
Set index mouse position.
void setSelectionIndexMousePosition(int x, int y)
Set selection index mouse position.
void setText(const MutableString &text)
Set text.
GUIStyledTextNode(GUIScreenNode *screenNode, GUIParentNode *parentNode, const string &id, GUINode_Flow *flow, const GUINode_Alignments &alignments, const GUINode_RequestedConstraints &requestedConstraints, const GUIColor &backgroundColor, const string &backgroundImage, const GUINode_Scale9Grid &backgroundImageScale9Grid, const GUIColor &backgroundImageEffectColorMul, const GUIColor &backgroundImageEffectColorAdd, const GUINode_Border &border, const GUINode_Padding &padding, const GUINodeConditions &showOn, const GUINodeConditions &hideOn, const string &tooltip, bool editable, bool preformatted, const string &font, int size, const string &color, const MutableString &text)
Constructor.
int getPreviousDelimiter(int index, const string &delimiters)
Get previous delimiter.
int getNextNewLine(int index)
Get next newline using.
void setTextStyleUtf8(int startIdx, int endIdx, const string &font, int size, const string &url=string())
Set text style using Utf8 indices.
int getNextNewLineUtf8(int index)
Get next newline using Utf8 indices.
void unsetStyles()
Unset/dispose styles.
void setImageUtf8(int idx, const string &image, const string &url=string(), int width=-1, int height=-1, float horizontalScale=1.0f, float verticalScale=1.0f, const GUIColor &effectColorMul=GUIColor::GUICOLOR_EFFECT_COLOR_MUL, const GUIColor &effectColorAdd=GUIColor::GUICOLOR_EFFECT_COLOR_ADD)
Set image using Utf8 index.
int getPreviousNewLine(int index)
Get previous new line.
void setTextStyle(int startIdx, int endIdx, const GUIColor &color, const string &font=string(), int size=-1, const string &url=string())
Set text style.
void removeText(int32_t idx, int32_t count)
Remove characters at idx with given length.
void scrollToSelectionIndex()
Set scroll to selection index.
void computeContentAlignment() override
Do content alignment.
void unsetIndexMousePosition()
Unset index mouse position.
void render(GUIRenderer *guiRenderer) override
Render.
void unsetSelectionIndexMousePosition()
Unset selection index mouse position.
const vector< URLArea > & getURLAreas()
int getPreviousDelimiterUtf8(int index, const string &delimiters)
Get previous delimiter using Utf8 indices.
TextStyle * getTextStyle(const vector< int > &lineCharIdxs, int lineCharIdx, int &textStyleIdx)
Get text style for.
void scrollToIndex()
Set scroll to index.
void computeContentAlignmentInternal()
Compute content alignment internal.
int getNextDelimiterUtf8(int index, const string &delimiters)
Get next delimiter using Utf8 indices.
const MutableString & getText() const
void determineNextLineConstraints(UTF8CharacterIterator &u8It, int charEndIdx, int textStyleIdx)
Determine next line constraints.
GUI font class.
Definition: GUIFont.h:41
Integer class.
Definition: Integer.h:25
Mutable utf8 aware string class.
Definition: MutableString.h:23
char getCharAt(int32_t idx) const
Get char at given binary index.
Definition: MutableString.h:78
int getUtf8BinaryIndex(int idx) const
int getUTF8CharAt(int32_t idx) const
Definition: MutableString.h:86
String tools class.
Definition: StringTools.h:22
UTF8 string character iterator.
std::exception Exception
Exception base class.
Definition: Exception.h:18
GUI node border entity.
GUI node padding entity.
GUI node scale 9 grid entity.
Line(int binaryIdx, int charIdx, float width, float height, float lineHeight, float baseLine, bool spaceWrap)
URLArea(int left, int top, int width, int height, const string &url)
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6