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(unsigned w)
172 {
173 _layout._maxW = w;
174 _update_layout();
175 }
176
181 {
182 _layout._lineSpacing = k;
183 _update_layout();
184 }
185
187 {
188 return _layout;
189 }
190
194 void SetOrder(float order);
195
199 void SetColor(const Color& col, TextParam type = TextParam::NORMAL);
200
219 bool SetString(std::string_view str);
220
224 void SetFontId(std::size_t id);
225
229 void SetFontSize(unsigned size);
230
234 void SetOutline(unsigned outline, const Color& color = ColorDef::BLACK);
235
239 void SetShadow(unsigned shadow, const Color& color = ColorDef::BLACK);
240
245 void SetBackground(bool open, const Color& col = ColorDef::TEXT_BG);
246
248 void SetBackground(Text* b)
249 {
250 if (b->_sprBg)
251 SetBackground(true, b->_sprBg->GetColor());
252 else
253 SetBackground(false);
254 }
255
261 Coor* GetCoor() { return _coor; }
262
263 float GetOrder() { return _order; }
264
265 std::string GetString();
266
268 {
269 return _col[type];
270 }
271
278
283 {
284 return _fontSize[type];
285 }
286
289 void SetUI(bool ui) { _ui = ui; }
290
294 bool IsPickup();
295
300 size_t Pos2Index(const Position& pos);
312 unsigned GetNodeFontSize(size_t i);
313
319 size_t Pos2IndexOutRange(const Position& pos);
320
326 size_t Pos2CursorIndex(const Position& pos);
327
329
331private:
332 Text();
333
334 size_t _fontId;// 字体id
335 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSize;// 设置字体大小
336 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSizeReal;// 实际字体大小
337 ArrayEnum<Color, TextParam::MAX_NUM> _col;// 字体颜色
338 float _order;// 绘制顺序
339
340 Layout _layout;// 布局
341 Coor* _coor;// 坐标系
342 std::vector<TextNode> _allNode;// 所有节点
343 bool _bFormat;// 格式化
344 bool _ui; // 是否ui
345
346
347 Position _areaSize;// 占地大小(自己坐标系)
348 gcSprite _sprBg;// 背景精灵
349
350 // 缓存参数
351 std::string _str;
352
353
354 void _update_order();
355
356 void _update_spr_bg();
357
358 void _refresh_string();
359
360
362 void _update_layout();
363
365 bool _is_char_can_first(char ch);
366
367 // 计算scale
368 bool _update_scale();
369};
370
372}
373
枚举类数组
2d坐标系
void SetPosition(const Position &pos)
不提供其他类型重载,避免{}无法判断类型
引用计数对象,通过get_gc函数创建
内存池数据
Color GetColor(size_t i=0)
返回颜色
void SetFontSize(unsigned size)
设置字体大小
void SetMaxW(unsigned w)
设置自动换行宽度,0为不自动换行
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)
返回所在位置(本地坐标系)的字符下标
float GetOrder()
Color GetColor(TextParam type=TextParam::NORMAL)
size_t Pos2IndexOutRange(const Position &pos)
返回所在位置(本地坐标系)的字符下标
void SetUI(bool 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
单词必须在单行
unsigned _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()