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:
29 class Filter
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);
152
153 void SetColor(const std::vector<Color>& mul_col)
154 {
155 _col.Set(mul_col);
156 _refresh_txt();
157 }
158
160
161 Coor* GetCoor() { return _panel->GetCoor(); }
162 std::string GetString()
163 {
164 return CodeCvt::Utf32ToUtf8(_str);
165 }
166 std::string GetStringSelect();
167 gcText GetText() { return _txt; }
168 UI::Panel* GetPanel() { return _panel; }
169
170 Components* GetCpt() { return &_cpt; }
171
172 void _on_char(char32_t ch);
173 void _on_backspace(); // 退格
174 void _on_delete(); // 删除
175 void _on_enter(); // 回车
176 void _on_string(std::string_view str);//
177 void _on_left(); // 左移光标
178 void _on_right(); // 右移光标
179 void _on_select_all(); // 全选
180
181 void _on_focus(bool focus);
182
184 {
185 }
186private:
187 EditBox() :
188 _cpt{ this },
189 _bSingleLine{ true },
190 _bPassword{ false },
191 _bChange{ false },
192 _maxSize{ 0 },
193 _iCursor{ 0 },
194 _iBeg{ 0 },
195 _iEnd{ 0 },
196 _bChangeSelect{ false }
197 {
198 _init_cpt();
199 }
200 std::u32string _str;
201 std::string _strTip;// 提示文本(为空时显示)
202 bool _bSingleLine; // 单行/多行
203 ColorSet _col; // 文本颜色
204 Color _colTip; // 提示文本颜色
205 Color _colCursor; // 光标颜色
206 Color _colSelect; // 提示文本颜色
207 bool _bClickLoseFocus;
208 bool _bPassword; // 密码模式
209
210 std::size_t _maxSize;// 最大字符数(0表示不限制)
211 std::size_t _iCursor;// 光标位置(0表示起始或第一个精灵(精灵左侧),size表示end)
212 std::size_t _iBeg;// 选中起始位置
213 std::size_t _iEnd;// 选中结束位置
214
215 TimerReverse _timerCursor;
216 bool _bChange; // 内容发生改变(单帧有效)
217 bool _bChangeSelect;// 拖拽中
218
219 Panel* _panel;
220 gcText _txt;
221 gcSprite _sprCursor;
222 std::vector<gcSprite> _vecSprSelect;
223
224 Components _cpt;
225 Cpt::DeleteThis _cptDeleteThis;
226 Cpt::SetOrder _cptSetOrder;
227 Cpt::Update _cptUpdate;
228 Cpt::Render _cptRender;
229 Cpt::GetCoor _cptGetCoor;
230 Cpt::Clone _cptClone;
231 Cpt::ElementData _cptElemData;
232
233 void _delete_this() { delete this; }
234
235 Components* _clone()
236 {
237 return Clone()->GetCpt();
238 }
239
240 void _init_cpt()
241 {
242 _cptDeleteThis = std::bind(&EditBox::_delete_this, this);
243 _cptSetOrder = std::bind(&EditBox::SetOrder, this, std::placeholders::_1);
244 _cptUpdate = std::bind(&EditBox::Update, this);
245 _cptRender = std::bind(&EditBox::Render, this);
246 _cptGetCoor = std::bind(&EditBox::GetCoor, this);
247 _cptClone = std::bind(&EditBox::_clone, this);
248
249 _cpt.PushBack(&_cptDeleteThis);
250 _cpt.PushBack(&_cptSetOrder);
251 _cpt.PushBack(&_cptUpdate);
252 _cpt.PushBack(&_cptRender);
253 _cpt.PushBack(&_cptGetCoor);
254 _cpt.PushBack(&_cptClone);
255
256 _cpt.PushBack(&_cptElemData);
257 }
258
259 void _init(gcText txt);
260 void _init_coor();
261
262 // 特殊逻辑传入focus
263 void _refresh_txt(bool* focus = nullptr);
264 void _refresh_cursor();
265 void _refresh_select();
266
267 // 未刷新重建
268 void _limit_range(); // 限制光标、选中为合法范围
269 void _delete_select();
270
271 bool _check_size(std::size_t add)
272 {
273 if (!_maxSize)
274 return true;
275 if (_str.size() + add <= _maxSize)
276 return true;
277 return false;
278 }
279
280 void _update();
281};
282}
283
284#endif
2d坐标系
元素通用属性(默认左上对齐,便于理解)
互逆计时器,t秒反转一次
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
Time * g_time
std::array< float, 4 > Color