Update clean-workspaces.sh after SpiderMonkey's upgrade to 78.6.0 and 91.13.1.
[0ad.git] / source / gui / CGUIText.h
blob64f78ad08b6fe06725ed8603915ff8892dd0f04e
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. 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 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. 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.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_GUITEXT
19 #define INCLUDED_GUITEXT
21 #include "gui/CGUISprite.h"
22 #include "gui/SettingTypes/CGUIColor.h"
23 #include "gui/SettingTypes/EAlign.h"
24 #include "maths/Rect.h"
25 #include "maths/Size2D.h"
26 #include "maths/Vector2D.h"
27 #include "ps/CStrIntern.h"
29 #include <array>
30 #include <list>
31 #include <vector>
33 class CCanvas2D;
34 class CGUI;
35 class CGUIString;
36 class IGUIObject;
37 struct SGenerateTextImage;
39 using SGenerateTextImages = std::array<std::vector<SGenerateTextImage>, 2>;
41 /**
42 * An CGUIText object is a parsed string, divided into
43 * text-rendering components. Each component, being a
44 * call to the Renderer. For instance, if you by tags
45 * change the color, then the GUI will have to make
46 * individual calls saying it want that color on the
47 * text.
49 * For instance:
50 * "Hello [b]there[/b] bunny!"
52 * That without word-wrapping would mean 3 components.
53 * i.e. 3 calls to CRenderer. One drawing "Hello",
54 * one drawing "there" in bold, and one drawing "bunny!".
56 class CGUIText
58 public:
59 /**
60 * A sprite call to the CRenderer
62 struct SSpriteCall
64 // The CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache
65 NONCOPYABLE(SSpriteCall);
66 MOVABLE(SSpriteCall);
68 SSpriteCall() {}
70 /**
71 * Size and position of sprite
73 CRect m_Area;
75 /**
76 * Sprite from global GUI sprite database.
78 CGUISpriteInstance m_Sprite;
81 /**
82 * A text call to the CRenderer
84 struct STextCall
86 NONCOPYABLE(STextCall);
87 MOVABLE(STextCall);
89 STextCall() :
90 m_UseCustomColor(false),
91 m_Bold(false), m_Italic(false), m_Underlined(false),
92 m_pSpriteCall(nullptr) {}
94 /**
95 * Position
97 CVector2D m_Pos;
99 /**
100 * Size
102 CSize2D m_Size;
105 * The string that is suppose to be rendered.
107 CStrW m_String;
110 * Use custom color? If true then m_Color is used,
111 * else the color inputted will be used.
113 bool m_UseCustomColor;
116 * Color setup
118 CGUIColor m_Color;
121 * Font name
123 CStrIntern m_Font;
126 * Settings
128 bool m_Bold, m_Italic, m_Underlined;
131 * Tooltip text
133 CStrW m_Tooltip;
136 * *IF* an icon, then this is not nullptr.
138 std::list<SSpriteCall>::pointer m_pSpriteCall;
141 // The SSpriteCall CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache.
142 // Also take advantage of exchanging the containers directly with move semantics.
143 NONCOPYABLE(CGUIText);
144 MOVABLE(CGUIText);
147 * Generates empty text.
149 CGUIText() = default;
152 * Generate a CGUIText object from the inputted string.
153 * The function will break down the string and its
154 * tags to calculate exactly which rendering queries
155 * will be sent to the Renderer. Also, horizontal alignment
156 * is taken into acount in this method but NOT vertical alignment.
158 * @param string Text to generate CGUIText object from.
159 * @param font Default font, notice both Default color and default font
160 * can be changed by tags.
161 * @param width Width, 0 if no word-wrapping.
162 * @param bufferZone Space between text and edge, and space between text and images.
163 * @param align Horizontal alignment (left / center / right).
164 * @param pObject Optional parameter for error output. Used *only* if error parsing fails,
165 * and we need to be able to output which object the error occurred in to aid the user.
167 CGUIText(const CGUI& pGUI, const CGUIString& string, const CStrW& fontW, const float width, const float bufferZone, const EAlign align, const IGUIObject* pObject);
170 * Draw this CGUIText object
172 void Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor, const CVector2D& pos, CRect clipping) const;
174 const CSize2D& GetSize() const { return m_Size; }
176 const std::list<SSpriteCall>& GetSpriteCalls() const { return m_SpriteCalls; }
178 const std::vector<STextCall>& GetTextCalls() const { return m_TextCalls; }
180 // Helper functions of the constructor
181 bool ProcessLine(
182 const CGUI& pGUI,
183 const CGUIString& string,
184 const CStrIntern& font,
185 const IGUIObject* pObject,
186 const SGenerateTextImages& images,
187 const EAlign align,
188 const float prelimLineHeight,
189 const float width,
190 const float bufferZone,
191 bool& firstLine,
192 float& y,
193 int& i,
194 int& from);
196 void SetupSpriteCalls(
197 const CGUI& pGUI,
198 const std::array<std::vector<CStr>, 2>& feedbackImages,
199 const float y,
200 const float width,
201 const float bufferZone,
202 const int i,
203 const int posLastImage,
204 SGenerateTextImages& images);
206 float GetLineOffset(
207 const EAlign align,
208 const float widthRangeFrom,
209 const float widthRangeTo,
210 const CSize2D& lineSize) const;
212 void ComputeLineRange(
213 const SGenerateTextImages& images,
214 const float y,
215 const float width,
216 const float prelimLineHeight,
217 float& widthRangeFrom,
218 float& widthRangeTo) const;
220 void ComputeLineSize(
221 const CGUI& pGUI,
222 const CGUIString& string,
223 const CStrIntern& font,
224 const bool firstLine,
225 const float width,
226 const float widthRangeFrom,
227 const float widthRangeTo,
228 const int i,
229 const int tempFrom,
230 CSize2D& lineSize) const;
232 bool AssembleCalls(
233 const CGUI& pGUI,
234 const CGUIString& string,
235 const CStrIntern& font,
236 const IGUIObject* pObject,
237 const bool firstLine,
238 const float width,
239 const float widthRangeTo,
240 const float dx,
241 const float y,
242 const int tempFrom,
243 const int i,
244 int& from);
247 * List of TextCalls, for instance "Hello", "there!"
249 std::vector<STextCall> m_TextCalls;
252 * List of sprites, or "icons" that should be rendered
253 * along with the text.
255 std::list<SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
256 // so that we can point to elements.
258 * Width and height of the whole output, used when setting up
259 * scrollbars and such.
261 CSize2D m_Size;
264 struct SGenerateTextImage
266 // The image's starting location in Y
267 float m_YFrom;
269 // The image's end location in Y
270 float m_YTo;
272 // The image width in other words
273 float m_Indentation;
275 void SetupSpriteCall(
276 const bool left, CGUIText::SSpriteCall& spriteCall, const float width, const float y,
277 const CSize2D& size, const CStr& textureName, const float bufferZone);
280 #endif // INCLUDED_GUITEXT