dl
dl_ui_text.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15
16#include "render/dl_text.h"
17#include "misc/dl_language.h"
18
19namespace dl::UI
20{
21
23class Text
24{
25public:
26 static Text* Create(gcText txt)
27 {
28 Text* ret = new Text;
29 ret->_txt = txt;
32 return ret;
33 }
38 {
39 Text* ret = new Text;
40 ret->_txt = g_factory->CloneText(_txt, true);
41 ret->_cptElemData = _cptElemData;
42
43 //存在则复制 语言属性
44 if (Cpt::Lang* cpt = _cpt.Get<Cpt::Lang>())
45 {
46 ret->SetLang(cpt->_str[0], cpt->_str[1]);
47 }
48
49 return ret;
50 }
51
52 void Render()
53 {
54 _txt->Render();
55 }
56
57 bool IsPickup()
58 {
59 return _txt->IsPickup();
60 }
61
63 void SetOrder(float order)
64 {
65 _txt->SetOrder(order);
66 }
67
68 Coor* GetCoor() { return _txt->GetCoor(); }
69
74 void SetLang(std::string_view tag, std::string_view name)
75 {
76 _cptLang._str = { std::string{tag}, std::string{name} };
77 _cptLang._func = std::bind(&Text::_refresh_lang, this, std::placeholders::_1, std::placeholders::_2);
78 _cpt.PushBack(&_cptLang);
79
80 _refresh_lang(tag, name);
81 }
82
83 void SetString(std::string_view str)
84 {
85 _txt->SetString(str);
87 }
92 gcText GetText() { return _txt; }
93
95 {
96 _cptElemData._size = toPosition(_txt->GetSize());
97 _cptElemData._anchor = {};
98 _cptElemData._hv = hv;
99
100 //if (_panel)//在组内则更新位置
101 //_panel->OnChangeElementData(&_cpt);
102 }
103
105 {
106 SetElementData(_cptElemData._hv);
107 }
108
109 Components* GetCpt() { return &_cpt; }
110private:
111 Text():
112 _cpt{this}
113 {
114 _init_cpt();
115 }
116
117 gcText _txt;
118
119 Components _cpt;
120 Cpt::DeleteThis _cptDeleteThis;
121 Cpt::SetOrder _cptSetOrder;
122 Cpt::Render _cptRender;
123 Cpt::GetCoor _cptGetCoor;
124 Cpt::Lang _cptLang;
125 Cpt::ElementData _cptElemData;
126
127 void _delete_this() { delete this; }
128
129 void _init_cpt()
130 {
131 _cptDeleteThis = std::bind(&Text::_delete_this, this);
132 _cptSetOrder = std::bind(&Text::SetOrder, this, std::placeholders::_1);
133 _cptRender = std::bind(&Text::Render, this);
134 _cptGetCoor = std::bind(&Text::GetCoor, this);
135 _cptSetOrder = std::bind(&Text::SetOrder, this, std::placeholders::_1);
136
137 _cpt.PushBack(&_cptDeleteThis);
138 _cpt.PushBack(&_cptSetOrder);
139 _cpt.PushBack(&_cptRender);
140 _cpt.PushBack(&_cptGetCoor);
141 _cpt.PushBack(&_cptSetOrder);
142
143 _cpt.PushBack(&_cptElemData);
144 }
145 //回调
146 void _refresh_lang(std::string_view tag, std::string_view name)
147 {
148 SetString(lang(tag, name));
149 }
150};
151}
组件容器还需表示对象自己(比如UI::Text)
Definition: dl_component.h:191
T * Get(size_t index=0)
返回第n个类型对象,失败返回nullptr
Definition: dl_component.h:210
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:216
2d坐标系
Definition: dl_coor.h:27
元素通用属性(默认左中对齐,具体看类型)
Definition: dl_component.h:273
Align::Direction2 _hv
Definition: dl_component.h:288
语言本地化
Definition: dl_component.h:265
std::array< std::string, 2 > _str
Definition: dl_component.h:267
std::function< void(std::string_view, std::string_view)> _func
Definition: dl_component.h:268
gcText CloneText(gcText txt, bool copy_str=false)
复制一个文本
Definition: dl_factory.h:231
void Render()
绘制
Size GetSize()
返回区域大小
Definition: dl_text.h:253
bool IsPickup()
是否被光标选中
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:238
void SetOrder(float order)
设置绘制顺序
void SetString(std::string_view str)
设置内容
ui文本
Definition: dl_ui_text.h:24
bool IsPickup()
Definition: dl_ui_text.h:57
void Render()
Definition: dl_ui_text.h:52
Coor * GetCoor()
Definition: dl_ui_text.h:68
Components * GetCpt()
Definition: dl_ui_text.h:109
void SetOrder(float order)
设置绘制顺序
Definition: dl_ui_text.h:63
void RefreshElementData()
Definition: dl_ui_text.h:104
void SetLang(std::string_view tag, std::string_view name)
设置语言
Definition: dl_ui_text.h:74
void SetString(std::string_view str)
Definition: dl_ui_text.h:83
void SetElementData(const Align::Direction2 &hv)
Definition: dl_ui_text.h:94
static Text * Create(gcText txt)
Definition: dl_ui_text.h:26
Text * Clone()
复制一个,注意有些组件属性需要复制
Definition: dl_ui_text.h:37
gcText GetText()
返回文本对象,注意在大小变化后,不会更新ui的元素数据
Definition: dl_ui_text.h:92
本地化
文本绘制
std::array< Direction, 2 > Direction2
Definition: dl_align.h:31
constexpr Direction2 LC
Definition: dl_align.h:33
Factory * g_factory
constexpr float Z_DEFAULT_UI
Definition: dl_sprite.h:26
std::string_view lang(std::string_view tag, std::string_view name)
返回一个语言配置的字符串,传入 menu、start
Definition: dl_language.h:119
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)