dl
dl_ui_edit_box.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
16#include "misc/dl_timer.h"
17#include "ui/dl_ui_background.h"
18#include "ui/dl_ui_panel.h"
19#include "ui/dl_ui_color.h"
20#include "system/dl_time.h"
21#include "render/dl_sprite.h"
22
23namespace dl::UI
24{
26class EditBox
27{
28public:
31 {
32 public:
33 bool _number = true;
34 bool _letter = true;
35 bool _symbol = true;
36 bool _endl = false;
37 bool _space = true;
38 bool _tab = false;
39 bool _other = true;
40 };
41
45 static EditBox* Create(Panel* panel, gcText txt)
46 {
47 EditBox* ret = new EditBox;
48 ret->_panel = panel;
49 ret->_panel->SetOrderMode(Panel::FORCE);
50 ret->_init(txt);
51 ret->_refresh_txt();
53 return ret;
54 }
55 EditBox* Clone();
56
57 void SetFocus(bool focus);
58 void SetString(std::string_view str)
59 {
60 _str = CodeCvt::Utf8ToUtf32(str);
61 _refresh_txt();
62 _refresh_cursor();
63 _refresh_select();
64 }
65 void SetStringTip(std::string_view str)
66 {
67 if (str == _strTip)
68 return;
69 _strTip = str;
70 _refresh_txt();
71 }
72
73 bool IsFocus();
74 bool IsChange() { return _bChange; }
75
76 void Update()
77 {
78 size_t index = Find::FirstIndex(_str, 0);
79 if (index != -1)
80 {
81 log_warn("字符串出现空字符,将清除!");
82 _str.resize(index);
83
84 _bChange = true;
85
86 _refresh_txt();
87 _refresh_cursor();
88 _refresh_select();
89 }
90
91 _panel->Update();
92
93 if (g_input->KeyDown(MouseCode::LEFT))
94 {// 输入框都是按下就响应
95 bool focus = IsPickup();
96 SetFocus(focus);
97 }
98
99 if(IsFocus())
100 _update();
101 }
102
103 void Render()
104 {
105 _txt->Render();
106
107 if(IsFocus()
108 && _timerCursor.Run(g_time->GetDelta()))
109 _sprCursor->Render();
110
111 for (gcSprite& spr : _vecSprSelect)
112 spr->Render();
113
114 _bChange = false;
115 }
116
117 //-----------------------光标位置转换----------------------------
122 size_t Pos2Cursor(const Position& pos)
123 {
124 Position p = _txt->GetCoor()->ParentToThis(pos);
125 return _txt->Pos2CursorIndex(p);
126 }
127
132 {
133 Position p = _txt->Index2Pos(i);
134 return _txt->GetCoor()->ThisToParent(p);
135 }
136
137
138
139 bool IsPickup()
140 {
141 return _panel->IsPickup();
142 }
143
145 void SetOrder(float order)
146 {
147 _panel->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
148 _txt->SetOrder(Math::OffsetOrder(order, 0, -1, 0));
149 _sprCursor->SetOrder(Math::OffsetOrder(order, 0, -1, -1));
150 }
151
152 void SetSingleLine(bool single_line);
154 {
155 _inputMode = mode;
156 }
157
158 void SetColor(const std::vector<Color>& mul_col)
159 {
160 _col.Set(mul_col);
161 _refresh_txt();
162 }
163
165
166 Coor* GetCoor() { return _panel->GetCoor(); }
167 std::string GetString()
168 {
169 return CodeCvt::Utf32ToUtf8(_str);
170 }
171 InputMode GetInputMode() { return _inputMode; }
172
173 std::string GetStringSelect();
174 gcText GetText() { return _txt; }
175 UI::Panel* GetPanel() { return _panel; }
176 Cpt::ElementData& GetElementData() { return _cptElemData; }
177
178 Components* GetCpt() { return &_cpt; }
179
180 void _on_char(char32_t ch);
181 void _on_backspace(); // 退格
182 void _on_delete(); // 删除
183 void _on_enter(); // 回车
184 void _on_string(std::string_view str);//
185 void _on_left(); // 左移光标
186 void _on_right(); // 右移光标
187 void _on_select_all(); // 全选
188
189 void _on_focus(bool focus);
190
192 {
193 }
194private:
195 EditBox() :
196 _cpt{ this },
197 _bSingleLine{ true },
198 _bPassword{ false },
199 _bChange{ false },
200 _maxSize{ 0 },
201 _iCursor{ 0 },
202 _iBeg{ 0 },
203 _iEnd{ 0 },
204 _bChangeSelect{ false }
205 {
206 _init_cpt();
207 }
208 std::u32string _str;
209 std::string _strTip;// 提示文本(为空时显示)
210 bool _bSingleLine; // 单行/多行
211 ColorSet _col; // 文本颜色
212 Color _colTip; // 提示文本颜色
213 Color _colCursor; // 光标颜色
214 Color _colSelect; // 提示文本颜色
215 bool _bClickLoseFocus;
216 bool _bPassword; // 密码模式
217 InputMode _inputMode; // 输入模式
218
219 std::size_t _maxSize;// 最大字符数(0表示不限制)
220 std::size_t _iCursor;// 光标位置(0表示起始或第一个精灵(精灵左侧),size表示end)
221 std::size_t _iBeg;// 选中起始位置
222 std::size_t _iEnd;// 选中结束位置
223
224 TimerReverse _timerCursor;
225 bool _bChange; // 内容发生改变(单帧有效)
226 bool _bChangeSelect;// 拖拽中
227
228 Panel* _panel;
229 gcText _txt;
230 gcSprite _sprCursor;
231 std::vector<gcSprite> _vecSprSelect;
232
233 Components _cpt;
234 Cpt::DeleteThis _cptDeleteThis;
235 Cpt::SetOrder _cptSetOrder;
236 Cpt::Update _cptUpdate;
237 Cpt::Render _cptRender;
238 Cpt::GetCoor _cptGetCoor;
239 Cpt::Clone _cptClone;
240 Cpt::ElementData _cptElemData;
241
242 void _delete_this() { delete this; }
243
244 Components* _clone()
245 {
246 return Clone()->GetCpt();
247 }
248
249 void _init_cpt()
250 {
251 _cptDeleteThis = std::bind(&EditBox::_delete_this, this);
252 _cptSetOrder = std::bind(&EditBox::SetOrder, this, std::placeholders::_1);
253 _cptUpdate = std::bind(&EditBox::Update, this);
254 _cptRender = std::bind(&EditBox::Render, this);
255 _cptGetCoor = std::bind(&EditBox::GetCoor, this);
256 _cptClone = std::bind(&EditBox::_clone, this);
257
258 _cpt.PushBack(&_cptDeleteThis);
259 _cpt.PushBack(&_cptSetOrder);
260 _cpt.PushBack(&_cptUpdate);
261 _cpt.PushBack(&_cptRender);
262 _cpt.PushBack(&_cptGetCoor);
263 _cpt.PushBack(&_cptClone);
264
265 _cpt.PushBack(&_cptElemData);
266 }
267
268 void _init(gcText txt);
269 void _init_coor();
270
271 // 特殊逻辑传入focus
272 void _refresh_txt(bool* focus = nullptr);
273 void _refresh_cursor();
274 void _refresh_select();
275
276 // 未刷新重建
277 void _limit_range(); // 限制光标、选中为合法范围
278 void _delete_select();
279
280 bool _check_size(std::size_t add)
281 {
282 if (!_maxSize)
283 return true;
284 if (_str.size() + add <= _maxSize)
285 return true;
286 return false;
287 }
288
289 void _update();
290};
291}
292
293#endif
2d坐标系
元素通用属性(默认左上对齐,便于理解)
互逆计时器,t秒反转一次
void SetInputMode(InputMode mode)
void _on_string(std::string_view str)
static EditBox * Create(Panel *panel, gcText txt)
创建输入框
void _on_focus(bool focus)
void SetSingleLine(bool single_line)
void SetColor(const std::vector< Color > &mul_col)
Position Cursor2Pos(size_t i)
下标 -> 本地坐标
void SetFocus(bool focus)
void _on_select_all()
EditBox * Clone()
void _on_backspace()
void SetString(std::string_view str)
void _on_char(char32_t ch)
size_t Pos2Cursor(const Position &pos)
本地坐标 -> 选中下标
std::string GetStringSelect()
void SetOrder(float order)
设置绘制顺序
Cpt::ElementData & GetElementData()
void SetStringTip(std::string_view str)
@ FORCE
强行设置全局
void SetOrderMode(OrderMode mode)
设置order模式
组件实例
#define log_warn(...)
2d绘图元素
时间相关
计时器
作为绘图背景
ui相关的颜色定义
ui之panel
std::string Utf32ToUtf8(std::u32string_view str)
字符串转换:utf32 -> utf8
std::u32string Utf8ToUtf32(std::string_view str)
字符串转换:utf8 -> utf32
Function< std::function< void(float)>, str_uid("SetOrder")> SetOrder
设置order
Function< std::function< dl::Coor *()>, str_uid("GetCoor")> GetCoor
返回坐标系
Function< std::function< void()>, str_uid("Render")> Render
绘制
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
Function< std::function< void()>, str_uid("Update")> Update
逻辑
Function< std::function< Components *()>, str_uid("Clone")> Clone
克隆
size_t FirstIndex(const T &container, const V &v)
find,返回下标
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Position2 Position
Input * g_input
constexpr float Z_DEFAULT_UI
GC< Sprite > gcSprite
GC< Text > gcText
Array< float, 4 > Color
Time * g_time