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