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"
37 struct SGenerateTextImage
;
39 using SGenerateTextImages
= std::array
<std::vector
<SGenerateTextImage
>, 2>;
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
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!".
60 * A sprite call to the CRenderer
64 // The CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache
65 NONCOPYABLE(SSpriteCall
);
71 * Size and position of sprite
76 * Sprite from global GUI sprite database.
78 CGUISpriteInstance m_Sprite
;
82 * A text call to the CRenderer
86 NONCOPYABLE(STextCall
);
90 m_UseCustomColor(false),
91 m_Bold(false), m_Italic(false), m_Underlined(false),
92 m_pSpriteCall(nullptr) {}
105 * The string that is suppose to be rendered.
110 * Use custom color? If true then m_Color is used,
111 * else the color inputted will be used.
113 bool m_UseCustomColor
;
128 bool m_Bold
, m_Italic
, m_Underlined
;
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
);
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
183 const CGUIString
& string
,
184 const CStrIntern
& font
,
185 const IGUIObject
* pObject
,
186 const SGenerateTextImages
& images
,
188 const float prelimLineHeight
,
190 const float bufferZone
,
196 void SetupSpriteCalls(
198 const std::array
<std::vector
<CStr
>, 2>& feedbackImages
,
201 const float bufferZone
,
203 const int posLastImage
,
204 SGenerateTextImages
& images
);
208 const float widthRangeFrom
,
209 const float widthRangeTo
,
210 const CSize2D
& lineSize
) const;
212 void ComputeLineRange(
213 const SGenerateTextImages
& images
,
216 const float prelimLineHeight
,
217 float& widthRangeFrom
,
218 float& widthRangeTo
) const;
220 void ComputeLineSize(
222 const CGUIString
& string
,
223 const CStrIntern
& font
,
224 const bool firstLine
,
226 const float widthRangeFrom
,
227 const float widthRangeTo
,
230 CSize2D
& lineSize
) const;
234 const CGUIString
& string
,
235 const CStrIntern
& font
,
236 const IGUIObject
* pObject
,
237 const bool firstLine
,
239 const float widthRangeTo
,
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.
264 struct SGenerateTextImage
266 // The image's starting location in Y
269 // The image's end location in Y
272 // The image width in other words
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