JugiMap Framework
jmGuiTextWidgets.h
1 #ifndef JUGIMAP_GUI_TEXT_WIDGETS_H
2 #define JUGIMAP_GUI_TEXT_WIDGETS_H
3 
4 #include <assert.h>
5 #include <algorithm>
6 #include <cmath>
7 #include <vector>
8 #include <map>
9 #include <array>
10 #include <string>
11 #include "../utf8cpp/utf8.h"
12 
13 
14 #include "jmGuiCommon.h"
15 #include "jmGuiText.h"
16 #include "jmFont.h"
17 #include "jmCommon.h"
18 
19 
20 namespace jugimap{
21 
22 
23 
24 class TextLibrary;
25 class TextSprite;
26 class ComposedSprite;
27 class StandardSprite;
28 class TextSegment;
29 class TextSegmentWord;
30 class GuiSlider;
31 class TextFieldLineSubtext;
32 class TextFieldLine;
33 
34 
35 
38 
39 
41 class GuiTextField : public GuiWidget
42 {
43 public:
44 
50 
52  enum class Alignment
53  {
54 
56  LEFT,
57 
60  JUSTIFY
61  };
62 
64  enum class DrawingDelay
65  {
66  NONE,
67  BY_ROWS,
68  BY_WORDS,
70  };
71 
72 
75  enum class ScrollingMode
76  {
79  NORMAL,
80 
87  };
88 
89 
90 
94  GuiTextField(VectorShape * _vectorShape, Map* _map);
95 
96  ~GuiTextField();
97 
98 
99  void Update() override;
100  void SetDisabled(bool _disabled) override;
101  void SetVisible(bool _visible) override;
102 
103 
104 
106  TextSegment* GetTextSegment(){ return textSegment;}
107 
108 
113  int GetTextSegmentHeight(TextSegment *_textSegment);
114 
115 
119  std::string GetClickedButtonName(){ return clickedTextName; }
120 
121 
122 
124  void SetScrollingMode(ScrollingMode _scrollingMode);
125 
126 
128  ScrollingMode GetScrollingMode(){ return scrollingMode; }
129 
130 
136  void SetScrollingModeFixedStepProperties(int _numberOfLines, int _lineHeight, float _scrollDurationMS);
137 
138 
140  void SetDrawingDelay(DrawingDelay _drawingDelay, int _drawingdelayMS);
141 
142 
144  DrawingDelay GetDrawingDelay(){ return drawingDelay; }
145 
146 
148  void SetTextAlignment(Alignment _textAlignment);
149 
150 
152  Alignment GetTextAlignment(){ return alignment;}
153 
154 
156  void SetAlphaFadeAtMargin(bool _alphaFadeAtMargin){ alphaFadeAtMargin = _alphaFadeAtMargin;}
157 
158 
166  void SetTextRect(Recti _box);
167 
168 
170  Recti GetTextRect(){ return box; }
171 
172 
180  void SetVisibleRect(Recti _visibleBox);
181 
182 
184  Recti GetVisibleRect(){ return visibleBox; }
185 
186 
190  void SetClickableRect(Recti _clickableBox);
191 
192 
194  Recti GetClickableRect(){ return clickableBox; }
195 
196 
202  void SetLocalLinesSpacingFactor(float _spacingFactor){ localLinesSpacingFactor = _spacingFactor; }
203 
204 
208  void Start(TextSegment *_textSegment);
209 
210 
214  void Reset();
215 
216 
218  bool IsDelayedDrawingActive(){ return !delayedDrawingFinished; }
219 
220 
222  void SkipDelay();
223 
224 
229  bool ScrollTextUp();
230 
231 
236  bool ScrollTextDown();
237 
238 
239  float GetRelativeScrollPosition(){ return relScrollPosition; }
240 
241 
244 
245 
248 
249 
250 
251 
252 private:
253  // LINK POINTERS
254  SpriteLayer *layer = nullptr; // LINK to textLayer where text object are placed
255  TextSegment * textSegment = nullptr; // LINK to displayed text segment
256 
257  // OWNED DATA POINTERS
258  GuiSlider * slider = nullptr; // OWNED
259  std::vector<TextFieldLine*>Lines; // OWNED
260 
261  //---status flags
262  std::string clickedTextName;
263 
264  //---
265  std::vector<float>gapWords; // extra gap between words when align is set to JUSTIFY
266  float gapWordsMax = 5.0; // max extra gap
267  float localLinesSpacingFactor = 0.0;
268 
269  ScrollingMode scrollingMode = ScrollingMode::NORMAL;
270  Alignment alignment = Alignment::LEFT;
271  DrawingDelay drawingDelay = DrawingDelay::NONE;
272 
273  int drawingDelayMS = 0;
274  int drawingDelayStartMS = 0;
275 
276  Recti box;
277  Recti visibleBox;
278  Recti clickableBox;
279  int segmentHeight = 0;
280  int scrollingRange = 0;
281  float relScrollPosition = 0.0;
282  float relScrollStep = 0.25;
283 
284  bool delayedDrawingFinished = false;
285  bool alphaFadeAtMargin = true;
286  Tween scrollTween;
287 
288  std::vector<TextFieldLineSubtext*>clickableSubtexts; // LINKS
289 
290  // properties for FIXED STEP scrolling mode
291  int fsNumberOfLines = 5;
292  int fsLineHeight = 30;
293  int fsScrollDurationMS = 250;
294  int indexFirstLineInBox = -1;
295 
296 
297  void RebuildLines(bool _rebuildEngineTexts=true);
298  void RebuildGaps();
299 
300  void Update_DelayByRow();
301  void Update_DelayByWord();
302  void Update_DelayByCharacters();
303 
304  void UpdateVisibleRange(float offset);
305 
306  std::string GetClickedText();
307 
308 
309 };
310 
311 //-----------------------------------------------------------------------------------------------------------------------
312 
313 class TextFieldLineSubtext
314 {
315 private:
316  friend class GuiTextField;
317  friend class TextFieldLine;
318 
319  std::string text;
320  float xPos = 0;
321  ColorRGBA color;
322  Font* font = nullptr; // LINK
323  TextSprite * textSprite = nullptr; // LINK
324  int charCount = -1; // Used for drawing delay BY_LETTERS !
325  bool visibleViaDrawingDelay = true;
326  //bool inVisibleRange = true;
327 
328  ColorRGBA color_CO; // used for clickable texts
329  std::string button;
330 };
331 
332 //-----------------------------------------------------------------------------------------------------------------------
333 
334 class TextFieldLine
335 {
336 private:
337  friend class GuiTextField;
338 
339  std::vector<TextFieldLineSubtext>subtexts;
340  float height = 0; // pixel height
341  int yPos = 0;
342  bool inVisibleBox = false;
343  bool inBox = false;
344 
345 
346  ~TextFieldLine();
347 
348  void Add(const TextSegmentWord &_word, float textPos);
349  void AddAsSeparateWords(const TextSegmentWord &_word, float textPos);
350  void UpdateEngineText(int _subtextIndex, SpriteLayer *_layer, Font *_font, int _xBox, int _yPos);
351 
352 };
353 
354 
355 //========================================================================================================================
356 
357 
362 class GuiTextInput : public GuiWidget
363 {
364 public:
365 
366 
368  GuiTextInput(ComposedSprite* _rootSprite);
369 
370  //~GuiTextInput();
371 
372  void SetDisabled(bool _disabled) override;
373  void SetVisible(bool _visible) override;
374  void Update() override;
375 
376 
377 
379  void Start();
380 
382  void Start(const std::string &_text);
383 
384 
386  void SetText(const std::string &_text);
387 
389  std::string GetText(){ return text; }
390 
391 
393  void SetTextMaximumLength(int _maxLength){ maxLength = _maxLength; }
394 
395 
397  void SetAllowEscToAbort(bool _allowEscToAbort){ allowEscToAbort = _allowEscToAbort; }
398 
399 
401  void SetAllowEmptyText(bool _allowEmptyText){ allowEmptyText = _allowEmptyText; }
402 
403 
405  void SetBannedChars(const std::string &_bannedChars){ bannedChars = _bannedChars; }
406 
407 
409  void MakeCharsFileNameSafe();
410 
411 
413  void SetCursorFlashingSpeed(int _cursorFlashingMS);
414 
415 
416  void Finish();
417 
418 
419 private:
420  ComposedSprite* rootSprite = nullptr; // LINK
421  StandardSprite* inputBoxSprite = nullptr; // LINK
422  TextSprite *textSprite = nullptr; // LINK
423  TextSprite *cursorSprite = nullptr; // LINK
424 
425 
426  std::string text;
427  std::string defaultText;
428 
429  int maxLength = 12;
430  char currentChar = 0;
431  bool active = false;
432  ColorRGBA cursorColor{0,255,255};
433 
434  int cursorFlashingMS = 250;
435  int cursorFlashingStartMS = 0;
436 
437  bool allowEscToAbort = false;
438  bool allowEmptyText = true;
439  std::string bannedChars;
440 
441  std::vector<char>allowedAsciCodes;
442 
443 
444  // visual appearance for widget states
445  int indexInputBoxSprite_idle = 0;
446  int indexInputBoxSprite_CO = 0;
447  int indexInputBoxSprite_active = 0;
448  int indexInputBoxSprite_disabled = 0;
449 
450  ColorRGBA textColor_idle{255,255,255};
451  //ColorRGBA textColor_CO{255,255,255};
452  ColorRGBA textColor_active{255,255,255};
453  //ColorRGBA textColor_disabled{255,255,255};
454 
455 
456  void UpdateTextOnChange();
457 
458 
459 };
460 
461 
463 
464 
465 
466 }
467 
468 
469 
470 
471 #endif
jugimap::GuiTextField::IsDelayedDrawingActive
bool IsDelayedDrawingActive()
Returns true if a text segment is currently being drawn by the drawing delay; otherwise returns false...
Definition: jmGuiTextWidgets.h:218
jugimap::GuiTextField::ScrollTextDown
bool ScrollTextDown()
Scroll the segment text down and return true. If scrolling is not possible returns false.
Definition: jmGuiTextWidgets.cpp:942
jugimap::GuiTextInput::GetText
std::string GetText()
Returns the text.
Definition: jmGuiTextWidgets.h:389
jugimap::GuiTextField::SetScrollingMode
void SetScrollingMode(ScrollingMode _scrollingMode)
Set the scrolling mode.
Definition: jmGuiTextWidgets.cpp:295
jugimap::GuiWidget
The GuiWidget class is the base widget class.
Definition: jmGuiCommon.h:65
jugimap::GuiTextField::SetVisible
void SetVisible(bool _visible) override
Set the visibility of this widget.
Definition: jmGuiTextWidgets.cpp:288
jugimap::Rect< int >
jugimap::GuiTextField::ScrollingMode::NORMAL
jugimap::GuiTextField::SetClickableRect
void SetClickableRect(Recti _clickableBox)
Set the clickable area rectangle.
Definition: jmGuiTextWidgets.cpp:134
jugimap::GuiTextField::DrawingDelay::BY_ROWS
Text is displayed gradually by rows.
jugimap::GuiTextField::ScrollingMode::FIXED_STEP
jugimap::GuiTextField::Reset
void Reset()
Reset the text field.
Definition: jmGuiTextWidgets.cpp:326
jugimap::GuiTextField
The GuiTextField class provides a widget for displaying text.
Definition: jmGuiTextWidgets.h:41
jugimap::ColorRGBA
Color in RGBA color space - integer variant.
Definition: jmColor.h:15
jugimap::GuiTextField::SetScrollingModeFixedStepProperties
void SetScrollingModeFixedStepProperties(int _numberOfLines, int _lineHeight, float _scrollDurationMS)
Set the scrolling properties if scrolling mode is ScrollingMode::FIXED_STEP.
Definition: jmGuiTextWidgets.cpp:302
jugimap::GuiTextField::GuiTextField
GuiTextField(VectorShape *_vectorShape, Map *_map)
Construct a new GuiTextField from the given *_vectorShape* in the *_map*.
Definition: jmGuiTextWidgets.cpp:36
jugimap::GuiTextField::GetTextSegmentHeight
int GetTextSegmentHeight(TextSegment *_textSegment)
Returns the height of the given *_textSegment* in this text field.
Definition: jmGuiTextWidgets.cpp:141
jugimap::GuiTextField::IsTextSegmentFirstLineDisplayed
bool IsTextSegmentFirstLineDisplayed()
Returns true if the first line of the text segement is displayed; otherwise returns false.
Definition: jmGuiTextWidgets.cpp:1085
jugimap::GuiTextField::SetVisibleRect
void SetVisibleRect(Recti _visibleBox)
Set the visible area rectangle.
Definition: jmGuiTextWidgets.cpp:127
jugimap::GuiTextField::ScrollingMode
ScrollingMode
Definition: jmGuiTextWidgets.h:75
jugimap::GuiTextInput::SetVisible
void SetVisible(bool _visible) override
Set the visibility of this widget.
Definition: jmGuiTextWidgets.cpp:1405
jugimap::GuiTextField::Alignment
Alignment
The text alignment.
Definition: jmGuiTextWidgets.h:52
jugimap::GuiTextInput::SetAllowEmptyText
void SetAllowEmptyText(bool _allowEmptyText)
Set if empty text is allowed.
Definition: jmGuiTextWidgets.h:401
jugimap::GuiTextField::GetTextAlignment
Alignment GetTextAlignment()
Returns the text alignment of this text field.
Definition: jmGuiTextWidgets.h:152
jugimap::GuiTextField::Alignment::LEFT
Text lines are aligned to the left edge.
jugimap::StandardSprite
The StandardSprite class defines the sprite from JugiMap Editor.
Definition: jmSprites.h:787
jugimap::GuiTextField::GetClickedButtonName
std::string GetClickedButtonName()
Returns the name of the clicked button if such event happens; otherwise returns an empty string.
Definition: jmGuiTextWidgets.h:119
jugimap::GuiTextField::DrawingDelay::NONE
Text is displayed instantly.
jugimap::GuiTextField::SetDisabled
void SetDisabled(bool _disabled) override
Set the disabled state of this widget.
Definition: jmGuiTextWidgets.cpp:273
jugimap::GuiTextField::SetAlphaFadeAtMargin
void SetAlphaFadeAtMargin(bool _alphaFadeAtMargin)
Set to true if alpha fade at margin between box and visible box is used.
Definition: jmGuiTextWidgets.h:156
jugimap::GuiTextField::GetClickableRect
Recti GetClickableRect()
Returns the clickable area rectangle.
Definition: jmGuiTextWidgets.h:194
jugimap::GuiTextField::GetTextRect
Recti GetTextRect()
Returns the text area rectangle.
Definition: jmGuiTextWidgets.h:170
jugimap::GuiTextInput::MakeCharsFileNameSafe
void MakeCharsFileNameSafe()
Set a string of banned characters in file name.
Definition: jmGuiTextWidgets.cpp:1544
jugimap::GuiTextInput::SetText
void SetText(const std::string &_text)
Set the text.
Definition: jmGuiTextWidgets.cpp:1343
jugimap::Recti
Rect< int > Recti
Rect struct in integer precision.
Definition: jmCommon.h:256
jugimap::GuiTextInput::SetBannedChars
void SetBannedChars(const std::string &_bannedChars)
Set a string of characters which can not appear in the text.
Definition: jmGuiTextWidgets.h:405
jugimap::GuiTextField::IsTextSegmentLastLineDisplayed
bool IsTextSegmentLastLineDisplayed()
Returns true if the last line of the text segement is displayed; otherwise returns false.
Definition: jmGuiTextWidgets.cpp:1099
jugimap::GuiTextField::DrawingDelay
DrawingDelay
The drawing delay of displayed text.
Definition: jmGuiTextWidgets.h:64
jugimap::GuiTextField::GetDrawingDelay
DrawingDelay GetDrawingDelay()
Returns the drawing delay of this text field.
Definition: jmGuiTextWidgets.h:144
jugimap::TextSegment
The TextSegment class defines a segment of text which can be accessed and displayed separatelly from ...
Definition: jmGuiText.h:125
jugimap::GuiTextInput::SetCursorFlashingSpeed
void SetCursorFlashingSpeed(int _cursorFlashingMS)
Set the flashing interval of the text cursor in milliseconds.
Definition: jmGuiTextWidgets.cpp:1384
jugimap::GuiTextField::SetTextAlignment
void SetTextAlignment(Alignment _textAlignment)
Set the text alignment to the given *_textAlignment*.
Definition: jmGuiTextWidgets.cpp:319
jugimap::GuiTextField::DrawingDelay::BY_WORDS
Text is displayed gradually by words.
jugimap::GuiTextField::ScrollTextUp
bool ScrollTextUp()
Scroll the segment text up and returns true. If scrolling is not possible returns false.
Definition: jmGuiTextWidgets.cpp:896
jugimap::ComposedSprite
The ComposedSprite class defines the composed sprite from JugiMap Editor.
Definition: jmSprites.h:682
jugimap::GuiTextInput::Start
void Start()
Start the text editing.
Definition: jmGuiTextWidgets.cpp:1314
jugimap::GuiTextInput::SetAllowEscToAbort
void SetAllowEscToAbort(bool _allowEscToAbort)
Set if the escape key aborts editing.
Definition: jmGuiTextWidgets.h:397
jugimap::GuiTextInput::SetTextMaximumLength
void SetTextMaximumLength(int _maxLength)
Set the maximum number of characters of the text.
Definition: jmGuiTextWidgets.h:393
jugimap::GuiTextField::GetScrollingMode
ScrollingMode GetScrollingMode()
Returns the scrolling mode of this text field.
Definition: jmGuiTextWidgets.h:128
jugimap::GuiTextField::Alignment::JUSTIFY
jugimap::GuiTextInput
The GuiTextField class provides a widget for text input.
Definition: jmGuiTextWidgets.h:362
jugimap::GuiTextField::SetDrawingDelay
void SetDrawingDelay(DrawingDelay _drawingDelay, int _drawingdelayMS)
Set the drawing delay to the given *_drawingDelay* and duration to *_drawingdelayMS* (in milliseconds...
Definition: jmGuiTextWidgets.cpp:311
jugimap::GuiTextInput::GuiTextInput
GuiTextInput(ComposedSprite *_rootSprite)
Construct a new gui text input from the given *_rootSprite*.
Definition: jmGuiTextWidgets.cpp:1198
jugimap::GuiTextField::Start
void Start(TextSegment *_textSegment)
Start displaying the given *_textSegment*.
Definition: jmGuiTextWidgets.cpp:158
jugimap::GuiTextField::GetVisibleRect
Recti GetVisibleRect()
Returns the visible area rectangle.
Definition: jmGuiTextWidgets.h:184
jugimap::TextSprite
The TextSprite class defines the text sprite from JugiMap Editor.
Definition: jmSprites.h:889
jugimap::GuiTextField::SetLocalLinesSpacingFactor
void SetLocalLinesSpacingFactor(float _spacingFactor)
Set the local factor for vertical lines spacing.
Definition: jmGuiTextWidgets.h:202
jugimap::GuiTextField::DrawingDelay::BY_CHARACTERS
Text is displayed gradually by characters.
jugimap::GuiTextField::SetTextRect
void SetTextRect(Recti _box)
Set the text area rectangle.
Definition: jmGuiTextWidgets.cpp:120
jugimap::VectorShape
The VectorShape class defines the vector shape from JugiMap Editor.
Definition: jmVectorShapes.h:206
jugimap::GuiTextInput::SetDisabled
void SetDisabled(bool _disabled) override
Set the disabled state of this widget.
Definition: jmGuiTextWidgets.cpp:1390
jugimap::Map
The Map class defines the map element from JugiMap Editor.
Definition: jmMap.h:26
jugimap::GuiTextField::globalLinesSpacingFactor
static float globalLinesSpacingFactor
Global factor for vertical lines spacing.
Definition: jmGuiTextWidgets.h:49
jugimap::GuiTextField::SkipDelay
void SkipDelay()
Skip drawing delay of a text segment and show entire text immediately.
Definition: jmGuiTextWidgets.cpp:858
jugimap::GuiTextField::GetTextSegment
TextSegment * GetTextSegment()
Returns the text segment if exists; otherwise returns nullptr.
Definition: jmGuiTextWidgets.h:106