dl
dl_text.h
浏览该文件的文档.
1
17#pragma once
18
19#include "base/dl_color.h"
20#include "base/dl_array_enum.h"
21#include "dl_sprite.h"
22
23namespace dl
24{
26enum class TextParam
27{
28 NORMAL,
29 OUTLINE,
30 SHADOW,
32};
35{
36 char32_t _ch;
41 Float _advance1;//现在\t使用它作为真正长度,而advance为设置的宽度比如2.0
42 Float _h;//字体高度
43 Float _base;//基线之上的高度
44 unsigned _shadow;
45
47
48 TextNode(char32_t ch, Float advance, Float h, Float base, unsigned shadow) :
49 _ch(ch), _advance(advance), _advance1{ 0 }, _h(h), _base(base), _shadow(shadow)
50 {
51 }
52
54 {
55 if (_spr)
56 _quad[0] = _spr->GetQuad();
57 if (_sprOutline)
59 if (_sprShadow)
60 _quad[2] = _sprShadow->GetQuad();
61 }
62
63 void Set(Float x, Float y)
64 {
65 if (_spr)
66 _spr->SetQuad(_quad[0] + Position{ x, y });
67 if (_sprOutline)
68 _sprOutline->SetQuad(_quad[1] + Position{ x, y });
69 if (_sprShadow)
71 }
72};
73
74class Text
75{
76 friend class Factory;
77 friend class PoolList<Text>;
78public:
80 struct Layout
81 {
83 unsigned _maxW;
88 _alignHorizontalLine(Align::Direction::BACK),
89 _maxW(0),
90 _lineSpacing(1.0),
91 _isCantFirstPunct(false),
92 _isWordInSingleLine(false) {}
93 };
94
95
96
100 void Render();
101
105 void SetFormat(bool open)
106 {
107 if (open != _bFormat)
108 {
109 _bFormat = open;
110 _set_string(&_str);//更新设置一下
111 }
112 }
113
118 {
119 _layout._alignHorizontalLine = dir_h;
120 _update_layout();
121 }
122
123
124
129 void SetPositionAlign(const Position& pos = {},
131 {
133 _coor->SetPosition(pos + offset);
134 }
135
137 {
139 _coor->SetPosition(pos + offset );
140 }
141
146 void SetCenter(const Position& pos = {});
147
151 void SetMaxW(unsigned w)
152 {
153 _layout._maxW = w;
154 _update_layout();
155 }
156
161 {
162 _layout._lineSpacing = k;
163 _update_layout();
164 }
165
167 {
168 return _layout;
169 }
170
174 void SetOrder(float order);
175
179 void SetColor(const Color& col, TextParam type = TextParam::NORMAL);
180
197 void SetString(std::string_view str);
198
202 void SetFontId(std::size_t id);
203
207 void SetFontSize(unsigned size);
208
212 void SetOutline(unsigned outline, const Color& color = ColorDef::BLACK);
213
217 void SetShadow(unsigned shadow, const Color& color = ColorDef::BLACK);
218
223 void SetBackground(bool open, const Color& col = ColorDef::TEXT_BG);
224
227 {
228 if (b->_sprBg)
229 SetBackground(true, b->_sprBg->GetColor());
230 else
231 SetBackground(false);
232 }
233
238 Coor* GetCoor() { return _coor; }
239
240 float GetOrder() { return _order; }
241
242 std::string GetString();
243
245 {
246 return _col[type];
247 }
248
253 Size GetSize() { return _areaSize; }
254
259 {
260 return _fontSize[type];
261 }
262
263
267 bool IsPickup();
268
270private:
271 Text();
272
273 size_t _fontId;//字体id
274 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSize;//字体大小
275 ArrayEnum<Color, TextParam::MAX_NUM> _col;//字体颜色
276 float _order;//绘制顺序
277
278 Layout _layout;//布局
279 Coor* _coor;//坐标系
280 std::vector<TextNode> _allNode;//所有节点
281 bool _bFormat;//格式化
282
283 Size _areaSize;//占地大小
284 gcSprite _sprBg;//背景精灵
285
286 //缓存参数
287 std::string _str;
288
289
290 void _update_order();
291
292 void _update_spr_bg();
293
294 //传入第2个参数时,可以不传入第1个参数
295 void _set_string(const std::string* str, const std::u32string* str_utf = nullptr);
296
297
299 void _update_layout();
300
302 bool _is_char_can_first(char ch);
303};
304
306}
307
2d坐标系
Definition: dl_coor.h:27
void SetPosition(const Position &pos)
资源工厂 @info 加载、管理、创建各种资源
Definition: dl_factory.h:41
void SetQuad(const Quad &quad)
Quad GetQuad() const
Definition: dl_sprite.h:72
Color GetColor(size_t i=0)
返回颜色
Definition: dl_sprite.h:127
void SetFontSize(unsigned size)
设置字体大小
void SetMaxW(unsigned w)
设置自动换行宽度,0为不自动换行
Definition: dl_text.h:151
unsigned GetFontSize(TextParam type=TextParam::NORMAL)
返回字体大小
Definition: dl_text.h:258
void SetLineSpacing(Float k)
设置行间距,默认为1.0
Definition: dl_text.h:160
void Render()
绘制
std::string GetString()
void SetPositionAlign(const Position &pos, Align::Direction dir_v)
Definition: dl_text.h:136
Size GetSize()
返回区域大小
Definition: dl_text.h:253
bool IsPickup()
是否被光标选中
const Layout & GetLayout()
Definition: dl_text.h:166
void SetBackground(Text *b)
注意传入gcText会调用上面的重载
Definition: dl_text.h:226
void SetLayoutAlign(Align::Direction dir_h)
设置布局属性(单行而已)
Definition: dl_text.h:117
void SetFormat(bool open)
设置是否要格式化字符串(默认为否)
Definition: dl_text.h:105
void SetPositionAlign(const Position &pos={}, const Align::Direction2 &dir={ Align::Direction::BACK, Align::Direction::BACK })
设置Coor位置,来整体对齐
Definition: dl_text.h:129
void SetShadow(unsigned shadow, const Color &color=ColorDef::BLACK)
设置阴影属性
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:238
void SetColor(const Color &col, TextParam type=TextParam::NORMAL)
设置颜色
void SetOutline(unsigned outline, const Color &color=ColorDef::BLACK)
设置描边属性
float GetOrder()
Definition: dl_text.h:240
Color GetColor(TextParam type=TextParam::NORMAL)
Definition: dl_text.h:244
void SetOrder(float order)
设置绘制顺序
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
void SetCenter(const Position &pos={})
设置Coor位置,并根据整体大小居中
void SetString(std::string_view str)
设置内容
void SetFontId(std::size_t id)
设置字体id
array使用枚举类作为索引的封装
2d绘图元素
std::array< Direction, 2 > Direction2
Definition: dl_align.h:31
constexpr std::array< T, N > Size2Anchor(const std::array< T, N > &size, const std::array< Direction, N > &dir)
垂直位置
Definition: dl_align.h:157
constexpr Color BLACK
Definition: dl_color.h:251
constexpr Color TEXT_BG
文本背景,半透明黑色
Definition: dl_color.h:260
Definition: dl_array2.h:17
PointU Size
Definition: dl_type_def.h:34
TextParam
文本参数
Definition: dl_text.h:27
@ NORMAL
正常
@ OUTLINE
描边
@ SHADOW
阴影
float Float
Definition: dl_type_def.h:37
std::array< float, 4 > Color
Definition: dl_type_def.h:49
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
Position2 Position
Definition: dl_type_def.h:43
std::array< Position, 4 > Quad
Definition: dl_type_def.h:46
布局属性
Definition: dl_text.h:81
bool _isCantFirstPunct
行首不能为一些符号
Definition: dl_text.h:85
Float _lineSpacing
行间距
Definition: dl_text.h:84
bool _isWordInSingleLine
单词必须在单行
Definition: dl_text.h:86
unsigned _maxW
最大宽度,0为无限
Definition: dl_text.h:83
Align::Direction _alignHorizontalLine
单行水平对齐
Definition: dl_text.h:82
单个字符
Definition: dl_text.h:35
gcSprite _spr
Definition: dl_text.h:37
void Set(Float x, Float y)
Definition: dl_text.h:63
Float _base
Definition: dl_text.h:43
Quad _quad[3]
Definition: dl_text.h:46
unsigned _shadow
Definition: dl_text.h:44
TextNode(char32_t ch, Float advance, Float h, Float base, unsigned shadow)
Definition: dl_text.h:48
gcSprite _sprOutline
Definition: dl_text.h:38
void RecordQuad()
Definition: dl_text.h:53
gcSprite _sprShadow
Definition: dl_text.h:39
Float _advance
Definition: dl_text.h:40
Float _advance1
Definition: dl_text.h:41
char32_t _ch
Definition: dl_text.h:36
Float _h
Definition: dl_text.h:42