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"
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"
41#include "ui/dl_ui_window.h"
42#include "ui/dl_ui_extend.h"
43
45namespace dl::UI
46{
56
67
68 enum class WindowShowMode
69 {
73 };
74
77{
78public:
80 _bHide{ false },
81 _sizeStandard{},
82 _edtFocus{ nullptr },
83 _uiScale{ 1.0f },
84 _orderMin{ Z_DEFAULT_WIDNOW_MIN },
85 _orderMax{ Z_DEFAULT_WIDNOW_MAX }
86 {
87 _arrSizeType[SizeType::SMALL] = 32;
88 _arrSizeType[SizeType::NORMAL] = 50;
89 _arrSizeType[SizeType::LARGE] = 100;
90 _arrSizeType[SizeType::LONG] = 120;
91 //根面板
92 _panelRoot = new Panel;
93 _panelRoot->_node = _allPanel.AddRoot(_panelRoot);
94 }
95
101#ifdef NDEBUG
102 Panel* CreatePanel(Panel* parent)
103 {
104 if (!parent)
105 parent = _panelRoot;
106
107 Panel* p = new Panel;
108 p->_node = _allPanel.Add(p, parent);
109 p->_coor->SetParent(parent->_coor);
110 p->_refresh_order();
111 return p;
112 }
113 Panel* CreatePanel()
114 {
115 return CreatePanel(nullptr);
116 }
117 Panel* lua_CreatePanel()
118 {
119 return CreatePanel(nullptr);
120 }
121#else
123 {
125
126 if (!parent)
127 parent = _panelRoot;
128
129 Panel* p = new Panel;
130 p->_node = _allPanel.Add(p, parent);
131 p->_coor->SetParent(parent->_coor);
132 p->_refresh_order();
133 p->_debugName = fmt::format("{}({},{}):",
134 String::CvtPathSeparator(loc._file),
135 loc._line, loc._column);
136 return p;
137 }
143 {
144 return CreatePanel(nullptr, DL_SOURCE_LOCATION_CALL);
145 }
146#endif
151 void DeletePanel(Panel* panel, bool del_child = false)
152 {
153 if(del_child)
154 {
155 auto vec = panel->_node->GetChild();
156 for(auto& iter : vec)
157 {
158 DeletePanel(iter->GetValue(), true);
159 }
160 }
161 // 会导致父节点的子节点列表迭代器失效
162 _allPanel.Del(panel->_node);
163 delete panel;
164 }
165
169 void DeletePanelChild(Panel* panel, bool recursion = true)
170 {
171 auto vec = panel->_node->GetChild();
172 for (auto& iter : vec)
173 {
174 DeletePanel(iter->GetValue(), recursion);
175 }
176 }
177
180 void SetPanelParent(Panel* panel, Panel* parent)
181 {
182 _allPanel.SetParent(panel->_node, parent->_node);
183 panel->_coor->SetParent(parent->_coor);
184 panel->_refresh_order();
185 }
187 {
188 return _panelRoot;
189 }
190
191 //-----------------------------窗口管理器-----------------------------------------
192 // 一般步骤为
193 // 1.创建
194 // auto win = UI::XXXX::Create()
195 // win->ExFastSet();
196 // g_ui->AddWindow("my_name", win);
197 //
198 // 2.打开或关闭
199 // g_ui->ShowWindow("my_name");
200 //
201 // 3.释放
202 // delete win,或程序结束时自动删除
203
210 void AddWindow(std::string_view name, Window* win);
211
212 Window* GetWindow(std::string_view name);
213
215 void ShowWindow(std::string_view name, WindowShowMode mode = WindowShowMode::SHOW);
216
217 void SetWindowOrder(float z_min, float z_max)
218 {
219 _orderMin = z_min;
220 _orderMax = z_max;
222 }
223public:
224 void _on_window_show_change(Window* win, bool visible);
229public:
230 //-----------------------------已注册元素创建(不会返回失败)-------------------------
234 UI::Text* CreateText(std::string_view name);
238 gcText CreateTextOnly(std::string_view name);
242 ButtonSprite* CreateButtonSprite(std::string_view name);
246 Background* CreateBackground(std::string_view name);
250 Locator::SL CreateLocator(std::string_view name);
254 ListBox* CreateListBox(std::string_view name);
255
256 //-------------------------------默认(创建失败时会使用)---------------------------
257
261 UI::ListBox* CreateListBoxDefault(bool add_text = false);
262 UI::EditBox* CreateEditBoxDefault(bool single_line = true);
266
270 void Register(std::string_view json_path);
275
279 void RegisterAll(std::string_view path);
280
284 void SetScale (float s)
285 {
286 _uiScale = s;
287 _refresh_scale();
288 }
289
293 void SetStandardSize(const Size& size);
298 float GetScale()
299 {
300 return _uiScale;
301 }
302
304 {
305 return _allPanel;
306 }
307
309 {
310 for (auto& node : _allPanel)
311 {
312 node->GetValue()->_refresh_lang();
313 }
314 }
315
320 /*Components* CloneCpt(Components* cpt)
321 {
322 if (auto p = cpt->Cast<UI::Text>())
323 return p->Clone()->GetCpt();
324 if (auto p = cpt->Cast<UI::ButtonSprite>())
325 return p->Clone()->GetCpt();
326 assert(0);
327 return nullptr;
328 }*/
329
331 void SetHide(bool hide)
332 {
333 _bHide = hide;
334 }
335
337 bool IsHide()
338 {
339 return _bHide;
340 }
341
342 EditBox* GetFocusEditBox() { return _edtFocus; }
343
345 void SetFocusEditBox(EditBox* edt, bool focus = true);
346
351 void RenderDebugElementData(Coor* coor, const Cpt::ElementData& elem_data);
352
354
357 void _update();
358 void _render();
359
361 void _resize();
363
365 void _on_char(char32_t ch);
367 void _on_key(int action, bool ctrl);
368private:
370 NTree<Panel*> _allPanel;
372 Panel* _panelRoot;
373
375 Float _uiScale;
376 Size _sizeStandard;// 标准大小
377
378 bool _bDebug;// 调试绘图
379 bool _bHide;// 隐藏ui界面
380
381 EditBox* _edtFocus;// 焦点输入框
382
383 // 默认ui大小
385
386 //-------------------------Window-----------------------
387 std::unordered_map<std::string, Window*> _allWindow;
388 std::list<Window*> _listShowWindow; // 当前显示窗口,顺序决定order,尾部最小
389 float _orderMin;
390 float _orderMax;
391
392 //-------------------------模板--------------------------
394 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
395 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
396 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
397 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
398 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
399
400
402 struct RegKey
403 {
404 ElementType _type;
405 std::string _name;
406 json _json;
407
408 bool operator<(const RegKey& b) const
409 {
410 return _type < b._type;
411 }
412 };
413 std::list<RegKey> _allReg;
414
415 void _reg_text(json& j, const std::string& name);
416 void _reg_msg_box(json& j, const std::string& name);
417 void _reg_button(json& j, const std::string& name);
418 void _reg_background(json& j, const std::string& name);
419 void _reg_locator(json& j, std::string_view name);
420 void _reg_list_box(json& j, std::string_view name);
421
422 void _release()
423 {
425
426 for (auto& iter : _allText)
427 delete iter.second;
428 for (auto& iter : _allBtnSprite)
429 delete iter.second;
430 for (auto& iter : _allBackground)
431 delete iter.second;
432 for (auto& iter : _allListBox)
433 delete iter.second;
434 }
435
436 void _refresh_scale()
437 {
438 _panelRoot->_coor->SetScale(_uiScale);
440 }
441 void _refresh_standard_size();
442
443 void _debug_render();
444};
445
446
447}
448
449namespace dl
450{
451// panel可能是全局的,注意生命周期
452extern UI::Manager* g_ui;
453}
454
455
456#endif
枚举类数组
2d坐标系
void SetParent(Coor *coor)
void SetScale(const Position &scale)
元素通用属性(默认左上对齐,便于理解)
N叉树
精灵按钮(注意精灵没有Coor)
UI系统管理器
void ShowWindow(Window *win, WindowShowMode mode=WindowShowMode::SHOW)
ListBox * CreateListBox(std::string_view name)
创建一个ListBox
void _refresh_window_order()
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
void DeletePanelChild(Panel *panel, bool recursion=true)
删除一个Panel的子节点
UI::ListBox * CreateListBoxDefault(bool add_text=false)
void _on_char(char32_t ch)
输入时调用
void _update_pre()
system调用
void _update_window()
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)
按键操作
UI::ProgressBar * CreateProgressBarDefault()
void _release_window_all()
void _on_window_show_change(Window *win, bool visible)
void SetScale(float s)
设置ui缩放
void _resize()
窗口大小变化时调用
void AddWindow(std::string_view name, Window *win)
记录已创建的窗口
bool IsHide()
是否隐藏ui
UI::Background * CreateBackgroundDefault()
void SetWindowOrder(float z_min, float z_max)
void ShowWindow(std::string_view name, WindowShowMode mode=WindowShowMode::SHOW)
void RenderDebugElementData(Coor *coor, const Cpt::ElementData &elem_data)
Panel * CreatePanel(Panel *parent, DL_SOURCE_LOCATION)
创建一个Panel
Window * GetWindow(std::string_view name)
void _release_window(Window *win)
UI::ButtonSprite * CreateButtonSpriteDefault(ButtonStyle style=ButtonStyle::NORMAL)
gcText CreateTextOnly(std::string_view name)
创建一个Text,忽略UI::Text的额外的信息
void Register(std::string_view json_path)
注册一个东西
Panel * CreatePanel(DL_SOURCE_LOCATION)
ButtonSprite * CreateButtonSprite(std::string_view name)
创建一个ButtonSprite
void SetFocusEditBox(EditBox *edt, bool focus=true)
设置激活输入框
Locator::SL CreateLocator(std::string_view name)
创建一个Locator
void RenderDebug()
绘制调试信息
UI::Menu * CreateMenuDefault()
void _resize_panel_root()
Size GetStandardSize()
void RegisterEnd()
真正注册操作
UI::Tree * CreateTreeDefault()
UI::EditBox * CreateEditBoxDefault(bool single_line=true)
float GetScale()
返回ui缩放
UI::Text * CreateTextDefault()
组件实例
资源工厂
json格式相关
nlohmann::json json
2d定位器
#define DL_SOURCE_LOCATION
#define DL_SOURCE_LOCATION_CALL
#define DL_MAKE_SOURCE_LOCATION(loc)
N叉树
通用类型定义
作为绘图背景
九图精灵按钮
精灵变色按钮
文本变色按钮
UI之输入框
UI之拓展
UI之列表框
UI之菜单
ui之panel
UI之进度条
UI之精灵
UI之文本
UI之树
UI之窗口
WindowShowMode
窗口显示操作
ButtonStyle
默认按钮样式
SizeType
默认按钮大小
float Float
GC< Text > gcText
constexpr float Z_DEFAULT_WIDNOW_MAX
UI::Manager * g_ui
PointU Size
constexpr float Z_DEFAULT_WIDNOW_MIN