dl
dl_ui_edit_box.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
15#include "misc/dl_component.h"
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)
45 {
46 EditBox* ret = new EditBox;
47 ret->_panel = panel;
48 ret->_init();
49 ret->_refresh_txt();
50 return ret;
51 }
52
53 void SetFocus(bool focus);
54 void SetString(std::string_view str)
55 {
56 _str = CodeCvt::Utf8ToUtf32(str);
57 _refresh_txt();
58 _refresh_cursor();
59 _refresh_select();
60 }
61 void SetStringTip(std::string_view str)
62 {
63 if (str == _strTip)
64 return;
65 _strTip = str;
66 _refresh_txt();
67 }
68
69 bool IsFocus();
70 bool IsChange() { return _bChange; }
71
72 void Update()
73 {
74 size_t index = Find::FirstIndex(_str, 0);
75 if (index != -1)
76 {
77 log_warn("字符串出现空字符,将清除!");
78 _str.resize(index);
79
80 _bChange = true;
81
82 _refresh_txt();
83 _refresh_cursor();
84 _refresh_select();
85 }
86
87 _panel->Update();
88
89 if (g_input->KeyDown(MouseCode::LEFT))
90 {// 输入框都是按下就响应
91 bool focus = IsPickup();
92 SetFocus(focus);
93 }
94
95 if(IsFocus())
96 _update();
97 }
98
99 void Render()
100 {
101 //_panel->Render();
102 _txt->Render();
103
104 if(IsFocus()
105 && _timerCursor.Run(g_time->GetDelta()))
106 _sprCursor->Render();
107
108 for (gcSprite& spr : _vecSprSelect)
109 spr->Render();
110
111 _bChange = false;
112 }
113
114 //-----------------------光标位置转换----------------------------
119 size_t Pos2Cursor(const Position& pos)
120 {
121 Position p = _txt->GetCoor()->ParentToThis(pos);
122 return _txt->Pos2CursorIndex(p);
123 }
124
129 {
130 Position p = _txt->Index2Pos(i);
131 return _txt->GetCoor()->ThisToParent(p);
132 }
133
134
135
136 bool IsPickup()
137 {
138 return _panel->IsPickup();
139 }
140
142 void SetOrder(float order)
143 {
144 _panel->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
145 _txt->SetOrder(Math::OffsetOrder(order, 0, -1, 0));
146 _sprCursor->SetOrder(Math::OffsetOrder(order, 0, -1, -1));
147 }
148
149 void SetSingleLine(bool single_line);
150
151 void SetColor(const std::vector<Color>& mul_col)
152 {
153 _col.Set(mul_col);
154 _refresh_txt();
155 }
156
158
159 Coor* GetCoor() { return _panel->GetCoor(); }
160 std::string GetString()
161 {
162 return CodeCvt::Utf32ToUtf8(_str);
163 }
164 std::string GetStringSelect();
165 gcText GetText() { return _txt; }
166
167 Components* GetCpt() { return &_cpt; }
168
169 void _on_char(char32_t ch);
170 void _on_backspace(); // 退格
171 void _on_delete(); // 删除
172 void _on_enter(); // 回车
173 void _on_string(std::string_view str);//
174 void _on_left(); // 左移光标
175 void _on_right(); // 右移光标
176 void _on_select_all(); // 全选
177
178 void _on_focus(bool focus);
179
181 {
182 }
183private:
184 EditBox() :
185 _cpt{ this },
186 _bSingleLine{ true },
187 _bPassword{ false },
188 _bChange{ false },
189 _maxSize{ 0 },
190 _iCursor{ 0 },
191 _iBeg{ 0 },
192 _iEnd{ 0 },
193 _bChangeSelect{ false }
194 {
195 _init_cpt();
196 }
197 std::u32string _str;
198 std::string _strTip;// 提示文本(为空时显示)
199 bool _bSingleLine; // 单行/多行
200 ColorSet _col; // 文本颜色
201 Color _colTip; // 提示文本颜色
202 Color _colCursor; // 光标颜色
203 Color _colSelect; // 提示文本颜色
204 bool _bClickLoseFocus;
205 bool _bPassword; // 密码模式
206
207 std::size_t _maxSize;// 最大字符数(0表示不限制)
208 std::size_t _iCursor;// 光标位置(0表示起始或第一个精灵(精灵左侧),size表示end)
209 std::size_t _iBeg;// 选中起始位置
210 std::size_t _iEnd;// 选中结束位置
211
212 TimerReverse _timerCursor;
213 bool _bChange; // 内容发生改变(单帧有效)
214 bool _bChangeSelect;// 拖拽中
215
216 Panel* _panel;
217 gcText _txt;
218 gcSprite _sprCursor;
219 std::vector<gcSprite> _vecSprSelect;
220
221 Components _cpt;
222 Cpt::DeleteThis _cptDeleteThis;
223 Cpt::SetOrder _cptSetOrder;
224 Cpt::Update _cptUpdate;
225 Cpt::Render _cptRender;
226 Cpt::GetCoor _cptGetCoor;
227 Cpt::ElementData _cptElemData;
228
229 void _delete_this() { delete this; }
230
231 void _init_cpt()
232 {
233 _cptDeleteThis = std::bind(&EditBox::_delete_this, this);
234 _cptSetOrder = std::bind(&EditBox::SetOrder, this, std::placeholders::_1);
235 _cptUpdate = std::bind(&EditBox::Update, this);
236 _cptRender = std::bind(&EditBox::Render, this);
237 _cptGetCoor = std::bind(&EditBox::GetCoor, this);
238
239 _cpt.PushBack(&_cptDeleteThis);
240 _cpt.PushBack(&_cptSetOrder);
241 _cpt.PushBack(&_cptUpdate);
242 _cpt.PushBack(&_cptRender);
243 _cpt.PushBack(&_cptGetCoor);
244
245 _cpt.PushBack(&_cptElemData);
246 }
247
248 void _init();
249
250 // 特殊逻辑传入focus
251 void _refresh_txt(bool* focus = nullptr);
252 void _refresh_cursor();
253 void _refresh_select();
254
255 // 未刷新重建
256 void _limit_range(); // 限制光标、选中为合法范围
257 void _delete_select();
258
259 bool _check_size(std::size_t add)
260 {
261 if (!_maxSize)
262 return true;
263 if (_str.size() + add <= _maxSize)
264 return true;
265 return false;
266 }
267
268 void _update();
269};
270}
271
272#endif
void PushBack(AnyType any)
添加一个任意对象
2d坐标系
元素通用属性(默认左上对齐,便于理解)
互逆计时器,t秒反转一次
void _on_string(std::string_view str)
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()
void _on_backspace()
static EditBox * Create(Panel *panel)
创建列表框
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)
组件系统
#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
逻辑
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
GC< Sprite > gcSprite
GC< Text > gcText
Time * g_time
std::array< float, 4 > Color