dl
dl_text.h
浏览该文件的文档.
1
20#pragma once
21
22#include "base/dl_color.h"
23#include "base/dl_array_enum.h"
24#include "dl_sprite.h"
25
26namespace dl
27{
29enum class TextParam
30{
31 NORMAL,
32 OUTLINE,
33 SHADOW,
35};
38{
39 char32_t _ch;
44 Float _advance1;//现在\t使用它作为真正长度,而advance为设置的宽度比如2.0
45
46 Float _lineH;// 单行高度
47 Float _ascent;// 基线之上的高度
48 unsigned _shadow;
49
52
54 _advance1{ 0 },
55 _shadow{ 0 }
56 {
57 }
58
60 {
61 if (_spr)
62 _quad[0] = _spr->GetQuad();
63 if (_sprOutline)
65 if (_sprShadow)
66 _quad[2] = _sprShadow->GetQuad();
67 }
68
69 void Set(Float x, Float y)
70 {
71 _xy = { x, y };
72 if (_spr)
73 _spr->SetQuad(_quad[0] + Position{ x, y });
74 if (_sprOutline)
75 _sprOutline->SetQuad(_quad[1] + Position{ x, y });
76 if (_sprShadow)
78 }
79
81 {
82 return _xy + Position{ 0, -_ascent };
83 }
85 {
86 return _xy + Position{ _advance, -_ascent };
87 }
88};
89
90class Text
91{
92 friend class Factory;
93 friend class PoolList<Text>;
94public:
96 struct Layout
97 {
99 unsigned _maxW;
104 _alignHorizontalLine(Align::Direction::BACK),
105 _maxW(0),
106 _lineSpacing(1.0),
107 _isCantFirstPunct(false),
108 _isWordInSingleLine(false) {}
109 };
110
111
112
116 void Render();
117
121 void SetFormat(bool open)
122 {
123 if (open != _bFormat)
124 {
125 _bFormat = open;
126 _refresh_string();
127 }
128 }
129
134 {
135 _layout._alignHorizontalLine = dir_h;
136 _update_layout();
137 }
138
139
140
145 void SetPositionAlign(const Position& pos = {},
146 const Align::Direction2& dir = Align::LT)
147 {
149 _coor->SetPosition(pos + offset);
150 }
151
156 void SetCenter(const Position& pos = {});
157
161 void SetMaxW(unsigned w)
162 {
163 _layout._maxW = w;
164 _update_layout();
165 }
166
171 {
172 _layout._lineSpacing = k;
173 _update_layout();
174 }
175
177 {
178 return _layout;
179 }
180
184 void SetOrder(float order);
185
189 void SetColor(const Color& col, TextParam type = TextParam::NORMAL);
190
208 bool SetString(std::string_view str);
209
213 void SetFontId(std::size_t id);
214
218 void SetFontSize(unsigned size);
219
223 void SetOutline(unsigned outline, const Color& color = ColorDef::BLACK);
224
228 void SetShadow(unsigned shadow, const Color& color = ColorDef::BLACK);
229
234 void SetBackground(bool open, const Color& col = ColorDef::TEXT_BG);
235
238 {
239 if (b->_sprBg)
240 SetBackground(true, b->_sprBg->GetColor());
241 else
242 SetBackground(false);
243 }
244
250 Coor* GetCoor() { return _coor; }
251
252 float GetOrder() { return _order; }
253
254 std::string GetString();
255
257 {
258 return _col[type];
259 }
260
267
272 {
273 return _fontSize[type];
274 }
275
276
280 bool IsPickup();
281
286 size_t Pos2Index(const Position& pos);
291
293private:
294 Text();
295
296 size_t _fontId;// 字体id
297 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSize;// 设置字体大小
298 ArrayEnum<unsigned, TextParam::MAX_NUM> _fontSizeReal;// 实际字体大小
299 ArrayEnum<Color, TextParam::MAX_NUM> _col;// 字体颜色
300 float _order;// 绘制顺序
301
302 Layout _layout;// 布局
303 Coor* _coor;// 坐标系
304 std::vector<TextNode> _allNode;// 所有节点
305 bool _bFormat;// 格式化
306
307
308 Position _areaSize;// 占地大小(自己坐标系)
309 gcSprite _sprBg;// 背景精灵
310
311 // 缓存参数
312 std::string _str;
313
314
315 void _update_order();
316
317 void _update_spr_bg();
318
319 void _refresh_string();
320
321
323 void _update_layout();
324
326 bool _is_char_can_first(char ch);
327
328 // 计算scale
329 bool _update_scale();
330};
331
333}
334
2d坐标系
Definition: dl_coor.h:27
void SetPosition(const Position &pos)
资源工厂 @info 加载、管理、创建各种资源
Definition: dl_factory.h:42
void SetQuad(const Quad &quad)
Quad GetQuad() const
Definition: dl_sprite.h:72
Color GetColor(size_t i=0)
返回颜色
Definition: dl_sprite.h:128
void SetFontSize(unsigned size)
设置字体大小
void SetMaxW(unsigned w)
设置自动换行宽度,0为不自动换行
Definition: dl_text.h:161
unsigned GetFontSize(TextParam type=TextParam::NORMAL)
返回字体大小
Definition: dl_text.h:271
void SetLineSpacing(Float k)
设置行间距,默认为1.0
Definition: dl_text.h:170
void Render()
绘制
std::string GetString()
Position Index2Pos(size_t i)
下标转到 字符位置(在父坐标系)
bool SetString(std::string_view str)
设置内容
bool IsPickup()
是否被光标选中
const Layout & GetLayout()
Definition: dl_text.h:176
void SetBackground(Text *b)
注意传入gcText会调用上面的重载
Definition: dl_text.h:237
void SetLayoutAlign(Align::Direction dir_h)
设置布局属性(单行而已)
Definition: dl_text.h:133
void SetFormat(bool open)
设置是否要格式化字符串(默认为否)
Definition: dl_text.h:121
void SetShadow(unsigned shadow, const Color &color=ColorDef::BLACK)
设置阴影属性
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:250
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位置,来整体对齐
Definition: dl_text.h:145
size_t Pos2Index(const Position &pos)
返回所在位置(在父坐标系)的字符下标
float GetOrder()
Definition: dl_text.h:252
Color GetColor(TextParam type=TextParam::NORMAL)
Definition: dl_text.h:256
Position GetSize()
返回区域大小
void SetOrder(float order)
设置绘制顺序
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
void SetCenter(const Position &pos={})
设置Coor位置,并根据整体大小居中
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:166
constexpr Direction2 LT
Definition: dl_align.h:33
constexpr Color BLACK
Definition: dl_color.h:251
constexpr Color TEXT_BG
文本背景,半透明黑色
Definition: dl_color.h:260
Definition: dl_array2.h:18
TextParam
文本参数
Definition: dl_text.h:30
@ 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)
转换到Position,已知长度
Position2 Position
Definition: dl_type_def.h:43
std::array< Position, 4 > Quad
Definition: dl_type_def.h:46
布局属性
Definition: dl_text.h:97
bool _isCantFirstPunct
行首不能为一些符号
Definition: dl_text.h:101
Float _lineSpacing
行间距
Definition: dl_text.h:100
bool _isWordInSingleLine
单词必须在单行
Definition: dl_text.h:102
unsigned _maxW
最大宽度,0为无限
Definition: dl_text.h:99
Align::Direction _alignHorizontalLine
单行水平对齐
Definition: dl_text.h:98
单个字符
Definition: dl_text.h:38
Position _xy
Definition: dl_text.h:51
Float _lineH
Definition: dl_text.h:46
gcSprite _spr
Definition: dl_text.h:40
Float _ascent
Definition: dl_text.h:47
void Set(Float x, Float y)
Definition: dl_text.h:69
Quad _quad[3]
Definition: dl_text.h:50
unsigned _shadow
Definition: dl_text.h:48
Position GetLeftTop()
Definition: dl_text.h:80
gcSprite _sprOutline
Definition: dl_text.h:41
void RecordQuad()
Definition: dl_text.h:59
gcSprite _sprShadow
Definition: dl_text.h:42
Float _advance
Definition: dl_text.h:43
Float _advance1
Definition: dl_text.h:44
char32_t _ch
Definition: dl_text.h:39
Position GetRightTop()
Definition: dl_text.h:84