dl
dl_ui_manager.h
浏览该文件的文档.
1
18#pragma once
19
20#ifndef DL_DISABLE_RENDER
22#include "base/dl_type_def.h"
23#include "misc/dl_locator.h"
24#include "base/dl_tree.h"
25#include "dl_ui_background.h"
26#include "render/dl_factory.h"
27#include "io/dl_json.h"
28#include "ui/dl_ui_panel.h"
29#include "ui/dl_ui_sprite.h"
30#include "ui/dl_ui_text.h"
31#include "ui/dl_ui_msg_box.h"
36#include "ui/dl_ui_list_box.h"
37#include "ui/dl_ui_edit_box.h"
38#include "ui/dl_ui_tree.h"
39#include "ui/dl_ui_menu.h"
40#include "ui/dl_ui_window.h"
41#include "ui/dl_ui_extend.h"
42
44namespace dl::UI
45{
54
57{
58public:
60 _bHide{ false },
61 _sizeStandard{},
62 _edtFocus{ nullptr },
63 _uiScale{1.0f}
64 {
65 _arrSizeType[SizeType::SMALL] = 32;
66 _arrSizeType[SizeType::NORMAL] = 50;
67 _arrSizeType[SizeType::LARGE] = 100;
68 _arrSizeType[SizeType::LONG] = 120;
69 //根面板
70 _panelRoot = new Panel;
71 _panelRoot->_node = _allPanel.AddRoot(_panelRoot);
72 }
73
79#ifdef NDEBUG
80 Panel* CreatePanel(Panel* parent)
81 {
82 if (!parent)
83 parent = _panelRoot;
84
85 Panel* p = new Panel;
86 p->_node = _allPanel.Add(p, parent);
87 p->_coor->SetParent(parent->_coor);
88 p->_refresh_order();
89 return p;
90 }
91 Panel* CreatePanel()
92 {
93 return CreatePanel(nullptr);
94 }
95#else
97 const std::source_location location = std::source_location::current())
98 {
99 if (!parent)
100 parent = _panelRoot;
101
102 Panel* p = new Panel;
103 p->_node = _allPanel.Add(p, parent);
104 p->_coor->SetParent(parent->_coor);
105 p->_refresh_order();
106 p->_debugName = fmt::format("{}({},{}):",
107 String::CvtPathSeparator(location.file_name()),
108 location.line(), location.column());
109 return p;
110 }
111 Panel* CreatePanel(const std::source_location location = std::source_location::current())
112 {
113 return CreatePanel(nullptr, location);
114 }
115#endif
117 {
118 return CreatePanel(nullptr);
119 }
120
124 void DeletePanel(Panel* panel, bool del_child = false)
125 {
126 if(del_child)
127 {
128 auto vec = panel->_node->GetChild();
129 for(auto& iter : vec)
130 {
131 DeletePanel(iter->GetValue(), true);
132 }
133 }
134 // 会导致父节点的子节点列表迭代器失效
135 _allPanel.Del(panel->_node);
136 delete panel;
137 }
138
142 void DeletePanelChild(Panel* panel, bool recursion = true)
143 {
144 auto vec = panel->_node->GetChild();
145 for (auto& iter : vec)
146 {
147 DeletePanel(iter->GetValue(), recursion);
148 }
149 }
150
153 void SetPanelParent(Panel* panel, Panel* parent)
154 {
155 _allPanel.SetParent(panel->_node, parent->_node);
156 panel->_coor->SetParent(parent->_coor);
157 panel->_refresh_order();
158 }
160 {
161 return _panelRoot;
162 }
163
164 //-----------------------------已注册元素创建(不会返回失败)-------------------------
168 UI::Text* CreateText(std::string_view name);
172 gcText CreateTextOnly(std::string_view name);
176 MsgBox* CreateMsgBox(std::string_view name);
180 ButtonSprite* CreateButtonSprite(std::string_view name);
184 Background* CreateBackground(std::string_view name);
188 Locator::SL CreateLocator(std::string_view name);
192 ListBox* CreateListBox(std::string_view name);
193
194 //-------------------------------默认(创建失败时会使用)---------------------------
205
210 ListBox* CreateListBoxDefault(bool add_text = false);
211 EditBox* CreateEditBoxDefault(bool single_line = true);
214
218 void Register(std::string_view json_path);
223
227 void RegisterAll(std::string_view path);
228
232 void SetScale (float s)
233 {
234 _uiScale = s;
235 _refresh_scale();
236 }
237
241 void SetStandardSize(const Size& size)
242 {
243 if (!g_system->IsWindowExist())
244 {
245 log_err("必须创建窗口之后调用!");
246 return;
247 }
248 _sizeStandard = size;
249 _refresh_standard_size();
250 }
252 {
253 return Any::Zero(_sizeStandard) ?
254 g_system->GetWindowSize() : _sizeStandard;
255 }
256
259 float GetScale()
260 {
261 return _uiScale;
262 }
263
265 {
266 return _allPanel;
267 }
268
270 {
271 for (auto& node : _allPanel)
272 {
273 node->GetValue()->_refresh_lang();
274 }
275 }
276
281 /*Components* CloneCpt(Components* cpt)
282 {
283 if (auto p = cpt->Cast<UI::Text>())
284 return p->Clone()->GetCpt();
285 if (auto p = cpt->Cast<UI::ButtonSprite>())
286 return p->Clone()->GetCpt();
287 assert(0);
288 return nullptr;
289 }*/
290
292 void SetHide(bool hide)
293 {
294 _bHide = hide;
295 }
296
298 bool IsHide()
299 {
300 return _bHide;
301 }
302
303 EditBox* GetFocusEditBox() { return _edtFocus; }
304
306 void SetFocusEditBox(EditBox* edt, bool focus = true);
307
312 void RenderDebugElementData(Coor* coor, const Cpt::ElementData& elem_data);
313
315 {
316 for (auto& node : _allPanel)
317 {
318 delete node->GetValue();
319 }
320 _release();
321 }
322
325 void _update();
326 void _render();
327
329 void _resize();
331
333 void _on_char(char32_t ch);
335 void _on_key(int action, bool ctrl);
336private:
338 NTree<Panel*> _allPanel;
340 Panel* _panelRoot;
341
343 Float _uiScale;
344 Size _sizeStandard;// 标准大小
345
346 bool _bDebug;// 调试绘图
347 bool _bHide;// 隐藏ui界面
348
349 EditBox* _edtFocus;// 焦点输入框
350
351 // 默认ui大小
353
354 //-------------------------模板--------------------------
356 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
357 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
358 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
359 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
360 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
361 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
362
363
365 struct RegKey
366 {
367 ElementType _type;
368 std::string _name;
369 json _json;
370
371 bool operator<(const RegKey& b) const
372 {
373 return _type < b._type;
374 }
375 };
376 std::list<RegKey> _allReg;
377
378 void _reg_text(json& j, const std::string& name);
379 void _reg_msg_box(json& j, const std::string& name);
380 void _reg_button(json& j, const std::string& name);
381 void _reg_background(json& j, const std::string& name);
382 void _reg_locator(json& j, std::string_view name);
383 void _reg_list_box(json& j, std::string_view name);
384
385 void _release()
386 {
387 for (auto& iter : _allText)
388 delete iter.second;
389 for (auto& iter : _allMsgBox)
390 delete iter.second;
391 for (auto& iter : _allBtnSprite)
392 delete iter.second;
393 for (auto& iter : _allBackground)
394 delete iter.second;
395 for (auto& iter : _allListBox)
396 delete iter.second;
397 }
398
399 void _refresh_scale()
400 {
401 _panelRoot->_coor->SetScale(_uiScale);
403 }
404 void _refresh_standard_size();
405
406 void _debug_render();
407};
408
409
410}
411
412namespace dl
413{
414// panel可能是全局的,注意生命周期
415extern UI::Manager* g_ui;
416}
417
418
419#endif
枚举类数组
2d坐标系
void SetParent(Coor *coor)
void SetScale(const Position &scale)
元素通用属性(默认左上对齐,便于理解)
N叉树
精灵按钮(注意精灵没有Coor)
UI系统管理器
MsgBox * CreateMsgBox(std::string_view name)
创建一个MsgBox
ListBox * CreateListBox(std::string_view name)
创建一个ListBox
UI::Text * CreateText(std::string_view name)
创建一个UI::Text
const NTree< Panel * > & GetPanelAll()
EditBox * GetFocusEditBox()
Background * CreateBackground(std::string_view name)
创建一个Background
void SetStandardSize(const Size &size)
设置ui标准大小,窗口缩放后,ui会自动缩放
void SetPanelParent(Panel *panel, Panel *parent)
设置panel的父panel
EditBox * CreateEditBoxDefault(bool single_line=true)
void DeletePanelChild(Panel *panel, bool recursion=true)
删除一个Panel的子节点
void _on_char(char32_t ch)
输入时调用
void _update_pre()
system调用
void SetHide(bool hide)
Components克隆
void RegisterAll(std::string_view path)
注册多个,传入目录。会调用RegisterEnd
void DeletePanel(Panel *panel, bool del_child=false)
删除一个Panel
void _on_key(int action, bool ctrl)
按键操作
ListBox * CreateListBoxDefault(bool add_text=false)
void SetScale(float s)
设置ui缩放
void _resize()
窗口大小变化时调用
Menu * CreateMenuDefault()
bool IsHide()
是否隐藏ui
Panel * CreatePanel(Panel *parent, const std::source_location location=std::source_location::current())
创建一个Panel
Background * CreateBackgroundDefault()
Panel * CreatePanel(const std::source_location location=std::source_location::current())
void RenderDebugElementData(Coor *coor, const Cpt::ElementData &elem_data)
gcText CreateTextOnly(std::string_view name)
创建一个Text,忽略UI::Text的额外的信息
void Register(std::string_view json_path)
注册一个东西
ButtonSprite * CreateButtonSprite(std::string_view name)
创建一个ButtonSprite
void SetFocusEditBox(EditBox *edt, bool focus=true)
设置激活输入框
MsgBox * CreateMsgBoxDefault()
Locator::SL CreateLocator(std::string_view name)
创建一个Locator
void RenderDebug()
绘制调试信息
ButtonSprite * CreateButtonSpriteDefault(ButtonStyle style=ButtonStyle::NORMAL)
void _resize_panel_root()
void RegisterEnd()
真正注册操作
float GetScale()
返回ui缩放
Tree * CreateTreeDefault()
UI::Text * CreateTextDefault()
组件实例
资源工厂
json格式相关
nlohmann::json json
2d定位器
#define log_err(...)
N叉树
通用类型定义
作为绘图背景
九图精灵按钮
文本变色按钮
UI之输入框
UI之拓展
UI之列表框
UI之菜单
ui之panel
UI之精灵
UI之文本
UI之树
UI之窗口
bool Zero(const C &container)
float Float
GC< Text > gcText
System * g_system
UI::Manager * g_ui
PointU Size