dl
dl_text.h
浏览该文件的文档.
1
20#pragma once
21
22#include "base/dl_color.h"
23#include "base/dl_array_enum.h"
24#include "render/dl_sprite.h"
25
26namespace dl
27{
36
38{
39 char32_t _ch;
43 gcSprite _sprBg; // 默认没有
45 Float _advance1;// 现在\t使用它作为真正长度,而advance为设置的宽度比如2.0
46
47 Float _lineH;// 单行高度
48 Float _ascent;// 基线之上的高度
49 unsigned _shadow;
50
51 Quad _quad[4];// 原始大小
53
55 _advance1{ 0 },
56 _shadow{ 0 },
57 _xy{}
58 {
59 }
60
62 {
63 if (_spr)
64 _quad[0] = _spr->GetQuad();
65 if (_sprOutline)
66 _quad[1] = _sprOutline->GetQuad();
67 if (_sprShadow)
68 _quad[2] = _sprShadow->GetQuad();
69 if (_sprBg)
70 _quad[3] = _sprBg->GetQuad();
71 }
72
73 void Set(Float x, Float y)
74 {
75 _xy = { x, y };
76 if (_spr)
77 _spr->SetQuad(_quad[0] + Position{ x, y });
78 if (_sprOutline)
79 _sprOutline->SetQuad(_quad[1] + Position{ x, y });
80 if (_sprShadow)
81 _sprShadow->SetQuad(_quad[2] + Position{ x + _shadow, y + _shadow });
82 if (_sprBg)
83 _sprBg->SetQuad(_quad[3] + Position{ x, y });
84 }
85
87 {
88 return _xy + Position{ 0, -_ascent };
89 }
91 {
92 return _xy + Position{ _advance, -_ascent };
93 }
95 {
96 return _xy + Position{ _advance, _lineH - _ascent };
97 }
98};
99
100class Text
101{
102 friend class Factory;
103 friend class PoolList<Text>;
104public:
120
121
122
126 void Render();
127
131 void SetFormat(bool open)
132 {
133 if (open != _bFormat)
134 {
135 _bFormat = open;
136 _refresh_string();
137 }
138 }
139
144 {
145 _layout._alignHorizontalLine = dir_h;
146 _update_layout();
147 }
148
149
150
155 void SetPositionAlign(const Position& pos = {},
156 const Align::Direction2& dir = Align::LT)
157 {
159 _coor->SetPosition(pos + offset);
160 }
161
166 void SetCenter(const Position& pos = {});
167
171 void SetMaxW(float w)
172 {
173 _layout._maxW = w;
174 _update_layout();
175 }
176
180 {
181 _layout._lineSpacing = k;
182 _update_layout();
183 }
184
186 {
187 return _layout;
188 }
189
193 void SetOrder(float order);
194
198 void SetColor(const Color& col, TextParam type = TextParam::NORMAL);
199
218 bool SetString(std::string_view str);
219
223 void SetFontId(std::size_t id);
224
228 void SetFontSize(unsigned size);
229
233 void SetOutline(unsigned outline, const Color& color = ColorDef::BLACK);
234
238 void SetShadow(unsigned shadow, const Color& color = ColorDef::BLACK);
239
244 void SetBackground(bool open, const Color& col = ColorDef::TEXT_BG);
245
247 void SetBackground(Text* b)
248 {
249 if (b->_sprBg)
250 SetBackground(true, b->_sprBg->GetColor());
251 else
252 SetBackground(false);
253 }
254
260 Coor* GetCoor() { return _coor; }
261
262 float GetOrder() { return _order; }
263
264 std::string GetString();
265
267 {
268 return _col[type];
269 }
270
277
282 {
283 return _fontSize[type];
284 }
285
288 void SetUI(bool ui) { _ui = ui; }
289
293 bool IsPickup();
294
299 size_t Pos2Index(const Position& pos);
311 unsigned GetNodeFontSize(size_t i);
312
318 size_t Pos2IndexOutRange(const Position& pos);
319
325 size_t Pos2CursorIndex(const Position& pos);
326
328
330private:
331 Text();
332
333 size_t _fontId;// 字体id
334 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSize;// 设置字体大小
335 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSizeReal;// 实际字体大小
336 ArrayEnum<Color, TextParam::MAX_NUM> _col;// 字体颜色
337 float _order;// 绘制顺序
338
339 Layout _layout;// 布局
340 Coor* _coor;// 坐标系
341 std::vector<TextNode> _allNode;// 所有节点
342 bool _bFormat;// 格式化
343 bool _ui; // 是否ui
344
345
346 Position _areaSize;// 占地大小(自己坐标系)
347 gcSprite _sprBg;// 背景精灵
348
349 // 缓存参数
350 std::string _str;
351
352
353 void _update_order();
354
355 void _update_spr_bg();
356
357 void _refresh_string();
358
359
361 void _update_layout();
362
364 bool _is_char_can_first(char ch);
365
366 // 计算scale
367 bool _update_scale();
368};
369
371}
372
枚举类数组
2d坐标系
void SetPosition(const Position &pos)
不提供其他类型重载,避免{}无法判断类型
引用计数对象,通过get_gc函数创建
内存池数据
Color GetColor(size_t i=0)
返回颜色
void SetFontSize(unsigned size)
设置字体大小
unsigned GetFontSize(TextParam type=TextParam::NORMAL)
返回字体大小
void SetLineSpacing(Float k)
设置行间距,默认为1.0
void Render()
绘制
std::string GetString()
size_t Pos2CursorIndex(const Position &pos)
返回所在位置(本地坐标系)的光标下标
Position Index2Pos(size_t i)
下标转到 字符位置(本地坐标系、左上角)
bool SetString(std::string_view str)
设置内容
friend class Factory
Position GetNodeSize(size_t i)
获取某个字的包围盒大小
bool IsPickup()
是否被光标选中
const Layout & GetLayout()
unsigned GetNodeFontSize(size_t i)
获取某个字的字体大小
void SetBackground(Text *b)
注意传入gcText会调用上面的重载
void SetLayoutAlign(Align::Direction dir_h)
设置布局属性(多行内的单行)
void SetFormat(bool open)
设置是否要格式化字符串(默认为否)
void SetShadow(unsigned shadow, const Color &color=ColorDef::BLACK)
设置阴影属性
Coor * GetCoor()
返回坐标系
void SetColor(const Color &col, TextParam type=TextParam::NORMAL)
设置颜色
void SetOutline(unsigned outline, const Color &color=ColorDef::BLACK)
设置描边属性
void SetPositionAlign(const Position &pos={}, const Align::Direction2 &dir=Align::LT)
设置Coor位置,来整体对齐
size_t Pos2Index(const Position &pos)
返回所在位置(本地坐标系)的字符下标
void SetMaxW(float w)
设置自动换行宽度,0为不自动换行
float GetOrder()
Color GetColor(TextParam type=TextParam::NORMAL)
size_t Pos2IndexOutRange(const Position &pos)
返回所在位置(本地坐标系)的字符下标
void SetUI(bool ui)
设置是否是ui(影响穿透)
Position GetSize()
返回区域大小
void SetOrder(float order)
设置绘制顺序
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
void SetCenter(const Position &pos={})
设置Coor位置,并根据整体大小居中
void RenderDebug()
void SetFontId(std::size_t id)
设置字体id
array使用枚举类作为索引的封装
2d绘图元素
方向、对齐定义
Array< Direction, 2 > Direction2
constexpr Array< T, N > Size2Anchor(const Array< T, N > &size, const Array< Direction, N > &dir)
根据大小和对齐,计算锚点
constexpr Direction2 LT
constexpr Color BLACK
constexpr Color TEXT_BG
文本背景,半透明黑色
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
TextParam
文本参数
float Float
GC< Sprite > gcSprite
GC< Text > gcText
Array< float, 4 > Color
Array< Position, 4 > Quad
bool _isCantFirstPunct
行首不能为一些符号
Float _lineSpacing
行间距
bool _isWordInSingleLine
单词必须在单行
float _maxW
最大宽度,0为无限
Align::Direction _alignHorizontalLine
单行水平对齐
gcSprite _sprBg
gcSprite _spr
void Set(Float x, Float y)
unsigned _shadow
Position GetLeftTop()
gcSprite _sprOutline
void RecordQuad()
gcSprite _sprShadow
Position GetRightDown()
Position GetRightTop()