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"
37#include "ui/dl_ui_list_box.h"
38#include "ui/dl_ui_edit_box.h"
39#include "ui/dl_ui_tree.h"
40#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{
55
58{
59public:
61 _bHide{ false },
62 _sizeStandard{},
63 _edtFocus{ nullptr },
64 _uiScale{1.0f}
65 {
66 _arrSizeType[SizeType::SMALL] = 32;
67 _arrSizeType[SizeType::NORMAL] = 50;
68 _arrSizeType[SizeType::LARGE] = 100;
69 _arrSizeType[SizeType::LONG] = 120;
70 //根面板
71 _panelRoot = new Panel;
72 _panelRoot->_node = _allPanel.AddRoot(_panelRoot);
73 }
74
80#ifdef NDEBUG
81 Panel* CreatePanel(Panel* parent)
82 {
83 if (!parent)
84 parent = _panelRoot;
85
86 Panel* p = new Panel;
87 p->_node = _allPanel.Add(p, parent);
88 p->_coor->SetParent(parent->_coor);
89 p->_refresh_order();
90 return p;
91 }
92 Panel* CreatePanel()
93 {
94 return CreatePanel(nullptr);
95 }
96 Panel* lua_CreatePanel()
97 {
98 return CreatePanel(nullptr);
99 }
100#else
102 {
104
105 if (!parent)
106 parent = _panelRoot;
107
108 Panel* p = new Panel;
109 p->_node = _allPanel.Add(p, parent);
110 p->_coor->SetParent(parent->_coor);
111 p->_refresh_order();
112 p->_debugName = fmt::format("{}({},{}):",
113 String::CvtPathSeparator(loc._file),
114 loc._line, loc._column);
115 return p;
116 }
122 {
123 return CreatePanel(nullptr, DL_SOURCE_LOCATION_CALL);
124 }
125#endif
130 void DeletePanel(Panel* panel, bool del_child = false)
131 {
132 if(del_child)
133 {
134 auto vec = panel->_node->GetChild();
135 for(auto& iter : vec)
136 {
137 DeletePanel(iter->GetValue(), true);
138 }
139 }
140 // 会导致父节点的子节点列表迭代器失效
141 _allPanel.Del(panel->_node);
142 delete panel;
143 }
144
148 void DeletePanelChild(Panel* panel, bool recursion = true)
149 {
150 auto vec = panel->_node->GetChild();
151 for (auto& iter : vec)
152 {
153 DeletePanel(iter->GetValue(), recursion);
154 }
155 }
156
159 void SetPanelParent(Panel* panel, Panel* parent)
160 {
161 _allPanel.SetParent(panel->_node, parent->_node);
162 panel->_coor->SetParent(parent->_coor);
163 panel->_refresh_order();
164 }
166 {
167 return _panelRoot;
168 }
169
170 //-----------------------------已注册元素创建(不会返回失败)-------------------------
174 UI::Text* CreateText(std::string_view name);
178 gcText CreateTextOnly(std::string_view name);
182 MsgBox* CreateMsgBox(std::string_view name);
186 ButtonSprite* CreateButtonSprite(std::string_view name);
190 Background* CreateBackground(std::string_view name);
194 Locator::SL CreateLocator(std::string_view name);
198 ListBox* CreateListBox(std::string_view name);
199
200 //-------------------------------默认(创建失败时会使用)---------------------------
211
216 ListBox* CreateListBoxDefault(bool add_text = false);
217 EditBox* CreateEditBoxDefault(bool single_line = true);
220
224 void Register(std::string_view json_path);
229
233 void RegisterAll(std::string_view path);
234
238 void SetScale (float s)
239 {
240 _uiScale = s;
241 _refresh_scale();
242 }
243
247 void SetStandardSize(const Size& size)
248 {
249 if (!g_system->IsWindowExist())
250 {
251 log_err("必须创建窗口之后调用!");
252 return;
253 }
254 _sizeStandard = size;
255 _refresh_standard_size();
256 }
258 {
259 return Any::Zero(_sizeStandard) ?
260 g_system->GetWindowSize() : _sizeStandard;
261 }
262
265 float GetScale()
266 {
267 return _uiScale;
268 }
269
271 {
272 return _allPanel;
273 }
274
276 {
277 for (auto& node : _allPanel)
278 {
279 node->GetValue()->_refresh_lang();
280 }
281 }
282
287 /*Components* CloneCpt(Components* cpt)
288 {
289 if (auto p = cpt->Cast<UI::Text>())
290 return p->Clone()->GetCpt();
291 if (auto p = cpt->Cast<UI::ButtonSprite>())
292 return p->Clone()->GetCpt();
293 assert(0);
294 return nullptr;
295 }*/
296
298 void SetHide(bool hide)
299 {
300 _bHide = hide;
301 }
302
304 bool IsHide()
305 {
306 return _bHide;
307 }
308
309 EditBox* GetFocusEditBox() { return _edtFocus; }
310
312 void SetFocusEditBox(EditBox* edt, bool focus = true);
313
318 void RenderDebugElementData(Coor* coor, const Cpt::ElementData& elem_data);
319
321 {
322 _release();
323 for (auto& node : _allPanel)
324 {
325 delete node->GetValue();
326 }
327 }
328
331 void _update();
332 void _render();
333
335 void _resize();
337
339 void _on_char(char32_t ch);
341 void _on_key(int action, bool ctrl);
342private:
344 NTree<Panel*> _allPanel;
346 Panel* _panelRoot;
347
349 Float _uiScale;
350 Size _sizeStandard;// 标准大小
351
352 bool _bDebug;// 调试绘图
353 bool _bHide;// 隐藏ui界面
354
355 EditBox* _edtFocus;// 焦点输入框
356
357 // 默认ui大小
359
360 //-------------------------模板--------------------------
362 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
363 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
364 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
365 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
366 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
367 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
368
369
371 struct RegKey
372 {
373 ElementType _type;
374 std::string _name;
375 json _json;
376
377 bool operator<(const RegKey& b) const
378 {
379 return _type < b._type;
380 }
381 };
382 std::list<RegKey> _allReg;
383
384 void _reg_text(json& j, const std::string& name);
385 void _reg_msg_box(json& j, const std::string& name);
386 void _reg_button(json& j, const std::string& name);
387 void _reg_background(json& j, const std::string& name);
388 void _reg_locator(json& j, std::string_view name);
389 void _reg_list_box(json& j, std::string_view name);
390
391 void _release()
392 {
393 for (auto& iter : _allText)
394 delete iter.second;
395 for (auto& iter : _allMsgBox)
396 delete iter.second;
397 for (auto& iter : _allBtnSprite)
398 delete iter.second;
399 for (auto& iter : _allBackground)
400 delete iter.second;
401 for (auto& iter : _allListBox)
402 delete iter.second;
403 }
404
405 void _refresh_scale()
406 {
407 _panelRoot->_coor->SetScale(_uiScale);
409 }
410 void _refresh_standard_size();
411
412 void _debug_render();
413};
414
415
416}
417
418namespace dl
419{
420// panel可能是全局的,注意生命周期
421extern UI::Manager* g_ui;
422}
423
424
425#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
Background * CreateBackgroundDefault()
void RenderDebugElementData(Coor *coor, const Cpt::ElementData &elem_data)
Panel * CreatePanel(Panel *parent, DL_SOURCE_LOCATION)
创建一个Panel
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)
设置激活输入框
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 DL_SOURCE_LOCATION
#define DL_SOURCE_LOCATION_CALL
#define log_err(...)
#define DL_MAKE_SOURCE_LOCATION(loc)
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