1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsPrerequisites.h"
6#include "2D/BsSprite.h"
7#include "Text/BsTextData.h"
8#include "Image/BsColor.h"
9#include "Math/BsVector2.h"
10#include "Allocators/BsStaticAlloc.h"
11
12namespace bs
13{
14 /** @addtogroup 2D
15 * @{
16 */
17
18 /** Specifies how is text horizontally aligned within its bounds. */
19 enum BS_SCRIPT_EXPORT(m:GUI) TextHorzAlign
20 {
21 THA_Left BS_SCRIPT_EXPORT(n:Left),
22 THA_Center BS_SCRIPT_EXPORT(n:Center),
23 THA_Right BS_SCRIPT_EXPORT(n:Right)
24 };
25
26 /** Specifies how is text vertically aligned within its bounds. */
27 enum BS_SCRIPT_EXPORT(m:GUI) TextVertAlign
28 {
29 TVA_Top BS_SCRIPT_EXPORT(n:Top),
30 TVA_Center BS_SCRIPT_EXPORT(n:Center),
31 TVA_Bottom BS_SCRIPT_EXPORT(n:Bottom)
32 };
33
34 /** Text sprite description structure used for initializing or updating a text sprite. */
35 struct TEXT_SPRITE_DESC
36 {
37 TEXT_SPRITE_DESC() = default;
38
39 UINT32 width = 0; /**< Width of the bounds to render the text within, in pixels. */
40 UINT32 height = 0; /**< Height of the bounds to render the text within, in pixels. */
41 SpriteAnchor anchor = SA_TopLeft; /**< Determines how to anchor the text within the bounds. */
42
43 String text; /**< UTF-8 encoded text to generate geometry for. */
44 HFont font; /**< Font containing the data about character glyphs. */
45 UINT32 fontSize = 0; /**< Size of the font to use when displaying the text. */
46 Color color; /**< Color tint of the text. */
47 TextHorzAlign horzAlign = THA_Left; /**< Specifies how is text horizontally aligned within its bounds. */
48 TextVertAlign vertAlign = TVA_Top; /**< Specifies how is text vertically aligned within its bounds. */
49 bool wordWrap = false; /**< If true the text will word wrap when it doesn't fit, otherwise it will overflow. */
50 bool wordBreak = true; /**< If enabled together with word wrap it will allow words to be broken if they don't fit. */
51 };
52
53 /** A sprite consisting of a quads representing a text string. */
54 class BS_EXPORT TextSprite : public Sprite
55 {
56 public:
57 TextSprite() = default;
58 ~TextSprite();
59
60 /**
61 * Recreates internal sprite data according the specified description structure.
62 *
63 * @param[in] desc Describes the geometry and material of the sprite.
64 * @param[in] groupId Group identifier that forces different materials to be used for different groups (for
65 * example you don't want the sprites to share the same group if they use different world
66 * transform matrices).
67 */
68 void update(const TEXT_SPRITE_DESC& desc, UINT64 groupId);
69
70 /**
71 * Calculates and returns offset for each individual text line. The offsets provide information on how much to
72 * offset the lines within provided bounds.
73 *
74 * @param[in] textData Text data to generate offsets for.
75 * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
76 * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
77 * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
78 * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
79 * @param[out] output Pre-allocated buffer to output the results in. Buffer must have an element
80 * for every line in @p textData.
81 */
82 static void getAlignmentOffsets(const TextDataBase& textData,
83 UINT32 width, UINT32 height, TextHorzAlign horzAlign, TextVertAlign vertAlign, Vector2I* output);
84
85 /**
86 * Calculates text quads you may use for text rendering, based on the specified text data. Only generates quads for
87 * the specified page.
88 *
89 * @param[in] page Font page to generate the data for.
90 * @param[in] textData Text data to generate offsets for.
91 * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
92 * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
93 * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
94 * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
95 * @param[in] anchor Determines how to anchor the text within the bounds.
96 * @param[out] vertices Output buffer containing quad positions. Must be allocated and of adequate size.
97 * @param[out] uv Output buffer containing quad UV coordinates. Must be allocated and of adequate
98 * size. Can be null.
99 * @param[out] indices Output buffer containing quad indices. Must be allocated and of adequate size. Can
100 * be null.
101 * @param[in] bufferSizeQuads Size of the output buffers, in number of quads.
102 * @return Number of generated quads.
103 */
104 static UINT32 genTextQuads(UINT32 page, const TextDataBase& textData, UINT32 width, UINT32 height,
105 TextHorzAlign horzAlign, TextVertAlign vertAlign, SpriteAnchor anchor, Vector2* vertices, Vector2* uv, UINT32* indices,
106 UINT32 bufferSizeQuads);
107
108 /**
109 * Calculates text quads you may use for text rendering, based on the specified text data. Generates quads for all
110 * pages.
111 *
112 * @param[in] textData Text data to generate offsets for.
113 * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
114 * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
115 * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
116 * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
117 * @param[in] anchor Determines how to anchor the text within the bounds.
118 * @param[out] vertices Output buffer containing quad positions. Must be allocated and of adequate size.
119 * @param[out] uv Output buffer containing quad UV coordinates. Must be allocated and of adequate
120 * size. Can be null.
121 * @param[out] indices Output buffer containing quad indices. Must be allocated and of adequate size. Can
122 * be null.
123 * @param[in] bufferSizeQuads Size of the output buffers, in number of quads.
124 * @return Number of generated quads.
125 */
126 static UINT32 genTextQuads(const TextDataBase& textData, UINT32 width, UINT32 height,
127 TextHorzAlign horzAlign, TextVertAlign vertAlign, SpriteAnchor anchor, Vector2* vertices, Vector2* uv, UINT32* indices,
128 UINT32 bufferSizeQuads);
129
130 private:
131 static const int STATIC_CHARS_TO_BUFFER = 25;
132 static const int STATIC_BUFFER_SIZE = STATIC_CHARS_TO_BUFFER * (4 * (2 * sizeof(Vector2)) + (6 * sizeof(UINT32)));
133
134 /** Clears internal geometry buffers. */
135 void clearMesh();
136
137 mutable StaticAlloc<STATIC_BUFFER_SIZE> mAlloc;
138 };
139
140 /** @} */
141}