JugiMap Framework
jmGuiText.h
1 #ifndef JUGIMAP_GUI_TEXT_H
2 #define JUGIMAP_GUI_TEXT_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 "jmFont.h"
16 #include "jmCommon.h"
17 
18 
19 namespace jugimap{
20 
21 
22 
23 class TextLibrary;
24 class TextBook;
25 class TextSequence;
26 class TextSprite;
27 class ComposedSprite;
28 class StandardSprite;
29 
30 
31 
32 class UnicodeString
33 {
34 public:
35 
36  //std::string s;
37 
38  UnicodeString(){}
39  UnicodeString(const std::string &_s);
40 
41  void Set(const std::string &_s);
42  void Append(const std::string &_s);
43  void Append(const UnicodeString &_us);
44  void Append(int _code);
45  void Clear();
46  bool IsEmpty(){ return codes.empty(); }
47  int GetLength(){ return codes.size(); }
48 
49  std::string ToStdString();
50  std::string ToStdString(int _startIndex, int _endIndex=-1);
51 
52 
53  std::vector<int> & GetCodes(){ return codes; }
54 
55 private:
56  std::vector<int>codes;
57 
58 };
59 
60 
61 void VerifyAndFixUtf8string(std::string& str);
62 
63 
64 std::string GetSubstring(const std::string &text, int indexStart, int indexEnd, bool &endReached);
65 
66 
67 void GetStringUnicodes(const std::string &text, std::vector<int> &unicodes);
68 
69 
70 int GetStringNumberOfCharacters(const std::string &text);
71 
72 
73 
74 class TextSegmentWord
75 {
76 public:
77  std::string word;
78  int nCharacters = 0;
79 
80  float width = -1; // width in pixels
81  float height = -1;
82 
83  //std::string replacementKeyword;
84  std::string originalWord;
85  ColorRGBA color{255,255,255};
86  Font * font = nullptr; // LINK
87  float fFontHeight = 1.0;
88 
89  std::string button;
90  //ColorRGBA buttonColor{255,255,255};
91  ColorRGBA buttonColor_CO{200,0,0};
92  //int offset = 0;
93 
94 
95  //int horSpacing = 0;
96  //int verSpacing = 0;
97 
98 
99 
100 
101  enum Content{
102  TEXT,
103  //HOR_OFFSET,
104  OFFSET,
105  VER_SPACING,
106  INDENT,
107  TAB_SPACING
108  };
109 
110  Content content = TEXT;
111 
112 
113  void UpdateSize(bool _forceUpdate = false);
114 
115 };
116 
117 
118 
126 {
127 public:
128  friend class TextLibrary;
129  friend class TextBook;
130  friend class TextSequence;
131 
132 
133  TextSegment(int _id) : id(_id)
134  {
135  currentColor = defaultColor; // index 0
136  currentFont = defaultFont; // index 0
137  currentIndent = defaultIndent;
138  currentTabSpacing = defaultTabSpacing;
139 
140  }
141 
142  ~TextSegment();
143 
144 
146  int GetId(){ return id; }
147 
148 
150  std::vector<Parameter>& GetParameters() { return parameters; }
151 
152 
159  bool Replace(const std::string &_searchedWord, const std::string &newWord);
160 
161 
166  bool Contains(const std::string &_word, bool compareWithOriginalWord = false);
167 
168 
169  void AddParagraph(const std::string &_text, ColorsLibrary &_colors, FontsLibrary &_fonts);
170 
171 
172  std::vector<TextSegmentWord*> &GetWords(){ return words; }
173  void SetWordSizesIfNeeded();
174 
175 
176 private:
177  int id = 0;
178  std::vector<TextSegmentWord*> words;
179  std::vector<Parameter> parameters;
180 
181 
182 
183 static ColorRGBA defaultColor;
184 static Font *defaultFont;
185 static ColorRGBA currentColor;
186 static Font *currentFont;
187 static std::string currentButton;
188 static ColorRGBA currentButtonColor;
189 static ColorRGBA currentButtonColor_CO;
190 //static int currentOffset;
191 static int defaultIndent;
192 static int defaultTabSpacing;
193 static int currentIndent;
194 static int currentTabSpacing;
195 
196 
197  void ProcessAddingWord(TextSegmentWord *word, ColorsLibrary &colors, FontsLibrary &fonts);
198 
199 };
200 
201 
210 {
211 public:
212 
213  TextSequence(){}
214  TextSequence(int _id) : id(_id){}
215  ~TextSequence();
216 
217 
219  int GetId(){ return id; }
220 
221 
223  std::vector<TextSegment*>& GetTextSegments(){ return TextSegments; }
224 
225 
227  std::vector<Parameter>& GetParameters() { return parameters; }
228 
229 
230 private:
231  int id = 0;
232  std::vector<TextSegment*>TextSegments; // OWNED pointers
233  std::vector<Parameter> parameters;
234 
235 };
236 
237 
238 //=======================================================================================
239 
240 
245 class TextBook
246 {
247 public:
248 
252  TextBook(const std::string &_name) : name(_name){}
253 
254  TextBook(const TextBook &_src) = delete;
255  TextBook(const TextBook &&_src) = delete;
256  TextBook& operator=(const TextBook &_src) = delete;
257 
258 
260  ~TextBook();
261 
262 
264  std::string GetName(){ return name; }
265 
266 
273  void LoadContent(const std::string &_filePath, ColorsLibrary &_colors, FontsLibrary &_fonts);
274 
275 
279  bool AddTextSegment(TextSegment *_textSegment);
280 
281 
285  bool AddTextSequence(TextSequence *_textSequence);
286 
287 
289  std::vector<TextSegment*>& GetTextSegments(){ return TextSegments; }
290 
291 
293  std::vector<TextSequence*>& GetTextSequences(){ return TextSequences; }
294 
295 
296 
297 private:
298  std::string name;
299  std::vector<TextSegment*>TextSegments; // OWNED pointers
300  std::vector<TextSequence*>TextSequences; // OWNED pointers
301 
302 
303  void _ParseParametersIfDefined(std::string &text, std::vector<Parameter>&_parameters);
304 
305 };
306 
307 
308 
309 //========================================================================================================================
310 
311 
317 {
318 public:
319  static std::string path;
320  static std::string pathPrefix;
321 
322 
323  ~TextLibrary();
324 
325 
330  TextBook* CreateNewBook(const std::string &_bookName);
331 
332 
336  TextBook *FindBook(const std::string &_bookName);
337 
338 
345  TextSegment *GetTextSegmentFromBook(const std::string &_bookName, int _segmentID);
346 
347 
348 private:
349 
350  std::vector<TextBook*>books;
351 
352 };
353 
354 
356 extern TextLibrary textLibrary;
357 
358 
359 
360 
361 }
362 
363 
364 
365 
366 #endif
jugimap::TextBook::AddTextSegment
bool AddTextSegment(TextSegment *_textSegment)
Add the given *_textSegment* to this text book.
Definition: jmGuiText.cpp:204
jugimap::ColorsLibrary
The ColorsLibrary defines storage for ColorRGBA objects.
Definition: jmColor.h:108
jugimap::TextSequence::GetTextSegments
std::vector< TextSegment * > & GetTextSegments()
Returns a reference to the vector of stored text segments.
Definition: jmGuiText.h:223
jugimap::TextBook::GetTextSegments
std::vector< TextSegment * > & GetTextSegments()
Returns a reference to the vector of stored text segments.
Definition: jmGuiText.h:289
jugimap::TextSequence
The TextSequence class defines a storage for text segments.
Definition: jmGuiText.h:209
jugimap::TextLibrary::GetTextSegmentFromBook
TextSegment * GetTextSegmentFromBook(const std::string &_bookName, int _segmentID)
Returns a pointer link to the text segment within a book.
Definition: jmGuiText.cpp:941
jugimap::TextSequence::GetId
int GetId()
Returns the identification number of this text segment.
Definition: jmGuiText.h:219
jugimap::TextBook::GetName
std::string GetName()
Returns the name of this text book.
Definition: jmGuiText.h:264
jugimap::TextSegment::GetParameters
std::vector< Parameter > & GetParameters()
Returns a reference to the vector of stored custom parameter.
Definition: jmGuiText.h:150
jugimap::TextBook::LoadContent
void LoadContent(const std::string &_filePath, ColorsLibrary &_colors, FontsLibrary &_fonts)
Load text segments and text sequences from a text file.
Definition: jmGuiText.cpp:236
jugimap::TextLibrary::CreateNewBook
TextBook * CreateNewBook(const std::string &_bookName)
Create a new text book with the given *_bookName* and returns a pointer link.
Definition: jmGuiText.cpp:911
jugimap::TextSequence::GetParameters
std::vector< Parameter > & GetParameters()
Returns a reference to the vector of stored custom parameters.
Definition: jmGuiText.h:227
jugimap::TextSegment::Contains
bool Contains(const std::string &_word, bool compareWithOriginalWord=false)
Returns true if this text segment contain the given *_word*; otherwise it returns false.
Definition: jmGuiText.cpp:820
jugimap::TextSegment::Replace
bool Replace(const std::string &_searchedWord, const std::string &newWord)
Replace the given word *_searchedWord* with the newWord.
Definition: jmGuiText.cpp:849
jugimap::TextLibrary
The TextLibrary class defines a storage for text books.
Definition: jmGuiText.h:316
jugimap::TextBook::AddTextSequence
bool AddTextSequence(TextSequence *_textSequence)
Add the given *_textSequence* to this text book.
Definition: jmGuiText.cpp:220
jugimap::TextSegment
The TextSegment class defines a segment of text which can be accessed and displayed separatelly from ...
Definition: jmGuiText.h:125
jugimap::TextBook::~TextBook
~TextBook()
Destructor.
Definition: jmGuiText.cpp:194
jugimap::FontsLibrary
The FontsLibrary class defines storage for Font objects.
Definition: jmFont.h:83
jugimap::TextBook::TextBook
TextBook(const std::string &_name)
Constructor.
Definition: jmGuiText.h:252
jugimap::TextBook
The TextBook class defines a storage for text segments and text sequences.
Definition: jmGuiText.h:245
jugimap::TextLibrary::FindBook
TextBook * FindBook(const std::string &_bookName)
Returns a pointer link to the text book with the given *_bookName*.
Definition: jmGuiText.cpp:928
jugimap::SpriteKind::TEXT
Text sprite - TextSprite object.
jugimap::TextSegment::GetId
int GetId()
Returns the identification number of this text segment.
Definition: jmGuiText.h:146
jugimap::TextBook::GetTextSequences
std::vector< TextSequence * > & GetTextSequences()
Returns a reference to the vector of stored text sequences.
Definition: jmGuiText.h:293