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{
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
90 {// 输入框都是按下就响应
91 bool focus = IsPickup();
92 SetFocus(focus);
93 }
94 }
95
96 void Render()
97 {
98 _panel->Render();
99 _txt->Render();
100
101 if(IsFocus()
102 && _timerCursor.Run(g_time->GetDelta()))
103 _sprCursor->Render();
104
105 _bChange = false;
106 }
107
108 //-----------------------光标位置转换----------------------------
113 size_t Pos2Cursor(const Position& pos)
114 {
115 return _txt->Pos2Index(pos);
116 }
122 {
123 return _txt->Index2Pos(i);
124 }
125
126
127
128 bool IsPickup()
129 {
130 return _panel->IsPickup();
131 }
132
134 void SetOrder(float order)
135 {
136 _panel->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
137 _txt->SetOrder(Math::OffsetOrder(order, 0, -1, 0));
138 _sprCursor->SetOrder(Math::OffsetOrder(order, 0, -1, -1));
139 }
140
141 void SetSingleLine(bool single_line);
142
143 void SetColor(const std::vector<Color>& mul_col)
144 {
145 _col.Set(mul_col);
146 _refresh_txt();
147 }
148 Coor* GetCoor() { return _panel->GetCoor(); }
149 std::string GetString()
150 {
151 return CodeCvt::Utf32ToUtf8(_str);
152 }
153 gcText GetText() { return _txt; }
154
155 Components* GetCpt() { return &_cpt; }
156
157 void _on_char(char32_t ch);
158 void _on_backspace();// 退格
159 void _on_enter();// 回车
160 void _on_string(std::string_view str);//
161 void _on_left();// 左移光标
162 void _on_right();// 右移光标
163
164 void _on_focus(bool focus);
165
167 {
168 }
169private:
170 EditBox() :
171 _cpt{ this },
172 _bSingleLine{ true },
173 _bPassword{ false },
174 _bChange{ false },
175 _maxSize{ 0 },
176 _iCursor{ 0 },
177 _iBeg{ 0 },
178 _iEnd{ 0 }
179 {
180 _init_cpt();
181 }
182 std::u32string _str;
183 std::string _strTip;// 提示文本(为空时显示)
184 bool _bSingleLine; // 单行/多行
185 ColorSet _col; // 文本颜色
186 Color _colTip; // 提示文本颜色
187 Color _colCursor; // 光标颜色
188 Color _colSelect; // 提示文本颜色
189 bool _bClickLoseFocus;
190 bool _bPassword; // 密码模式
191
192 std::size_t _maxSize;// 最大字符数(0表示不限制)
193 std::size_t _iCursor;// 光标位置(0表示起始或第一个精灵(精灵左侧),size表示end)
194 std::size_t _iBeg;// 选中起始位置
195 std::size_t _iEnd;// 选中结束位置
196
197 TimerReverse _timerCursor;
198 bool _bChange; // 内容发生改变(单帧有效)
199
200 Panel* _panel;
201 gcText _txt;
202 gcSprite _sprCursor;
203 std::vector<gcSprite> _vecSprSelect;
204
205 Components _cpt;
206 Cpt::DeleteThis _cptDeleteThis;
207 Cpt::SetOrder _cptSetOrder;
208 Cpt::Update _cptUpdate;
209 Cpt::Render _cptRender;
210 Cpt::GetCoor _cptGetCoor;
211 Cpt::ElementData _cptElemData;
212
213 void _delete_this() { delete this; }
214
215 void _init_cpt()
216 {
217 _cptDeleteThis = std::bind(&EditBox::_delete_this, this);
218 _cptSetOrder = std::bind(&EditBox::SetOrder, this, std::placeholders::_1);
219 _cptUpdate = std::bind(&EditBox::Update, this);
220 _cptRender = std::bind(&EditBox::Render, this);
221 _cptGetCoor = std::bind(&EditBox::GetCoor, this);
222
223 _cpt.PushBack(&_cptDeleteThis);
224 _cpt.PushBack(&_cptSetOrder);
225 _cpt.PushBack(&_cptUpdate);
226 _cpt.PushBack(&_cptRender);
227 _cpt.PushBack(&_cptGetCoor);
228
229 _cpt.PushBack(&_cptElemData);
230 }
231
232 void _init();
233
234 // 特殊逻辑传入focus
235 void _refresh_txt(bool* focus = nullptr);
236 void _refresh_cursor();
237 void _refresh_select();
238
239 void _limit_range(); // 限制光标、选中为合法范围
240 void _delete_select();
241
242 bool _check_size(std::size_t add)
243 {
244 if (!_maxSize)
245 return true;
246 if (_str.size() + add <= _maxSize)
247 return true;
248 return false;
249 }
250};
251}
252
253#endif
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:246
2d坐标系
Definition: dl_coor.h:27
元素通用属性(默认左上对齐,便于理解)
Definition: dl_component.h:362
bool KeyDown(KeyCode key_code)
检测一个按键按下了,松开后再按下才会再次触发,只1帧有效。
Definition: dl_input.h:250
void SetOrder(float order)
设置绘制顺序,默认为 Constant::Z_DEFAULT @order 要设置的order,越小越靠近屏幕,范围[0, 1)
void Render()
绘制
void Render()
绘制
Position Index2Pos(size_t i)
下标转到 字符位置(在父坐标系)
size_t Pos2Index(const Position &pos)
返回所在位置(在父坐标系)的字符下标
void SetOrder(float order)
设置绘制顺序
FloatTime GetDelta()
返回帧间隔
Definition: dl_time.h:41
互逆计时器,t秒反转一次
Definition: dl_timer.h:49
bool Run(FloatTime dt)
Definition: dl_timer.h:60
void Set(const std::vector< Color > &mul_col)
Definition: dl_ui_color.h:38
ui输入框
void _on_string(std::string_view str)
std::string GetString()
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)
Components * GetCpt()
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)
本地坐标 -> 选中下标
void SetOrder(float order)
设置绘制顺序
void SetStringTip(std::string_view str)
void Update()
执行逻辑 @node 默认情况会自动调用
void SetOrder(float order)
设置order
Definition: dl_ui_panel.h:148
bool IsPickup()
是否被鼠标选中
Definition: dl_ui_panel.h:355
Coor * GetCoor()
返回坐标系
Definition: dl_ui_panel.h:200
组件系统
#define log_warn(...)
Definition: dl_log.h:195
时间相关
计时器
作为绘图背景
ui相关的颜色定义
ui之panel
std::string Utf32ToUtf8(std::u32string_view str)
字符串转换:utf32 -> utf8
std::u32string Utf8ToUtf32(std::string_view str)
字符串转换:utf8 -> utf32
size_t FirstIndex(const T &container, const V &v)
find,返回下标
Definition: dl_range_find.h:24
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Definition: dl_math.h:490
Input * g_input
Time * g_time
std::array< float, 4 > Color
Definition: dl_type_def.h:49
Position2 Position
Definition: dl_type_def.h:43