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"
35#include "ui/dl_ui_list_box.h"
36#include "ui/dl_ui_edit_box.h"
37#include "ui/dl_ui_tree.h"
38#include "ui/dl_ui_menu.h"
39
41namespace dl::UI
42{
51
54{
55public:
57 _bHide{ false },
58 _sizeStandard{},
59 _edtFocus{ nullptr },
60 _uiScale{1.0f}
61 {
62 _arrSizeType[SizeType::SMALL] = 32;
63 _arrSizeType[SizeType::NORMAL] = 50;
64 _arrSizeType[SizeType::LARGE] = 100;
65 _arrSizeType[SizeType::LONG] = 120;
66 //根面板
67 _panelRoot = new Panel;
68 _panelRoot->_node = _allPanel.AddRoot(_panelRoot);
69 }
70
77 {
78 if (!parent)
79 parent = _panelRoot;
80
81 Panel* p = new Panel;
82 p->_node = _allPanel.Add(p, parent);
83 p->_coor->SetParent(parent->_coor);
84 p->_refresh_order();
85 return p;
86 }
88 {
89 return CreatePanel(nullptr);
90 }
91
95 void DeletePanel(Panel* panel, bool del_child = false)
96 {
97 if(del_child)
98 {
99 auto vec = panel->_node->GetChild();
100 for(auto& iter : vec)
101 {
102 DeletePanel(iter->GetValue(), true);
103 }
104 }
105 // 会导致父节点的子节点列表迭代器失效
106 _allPanel.Del(panel->_node);
107 delete panel;
108 }
109
113 void DeletePanelChild(Panel* panel, bool recursion = true)
114 {
115 auto vec = panel->_node->GetChild();
116 for (auto& iter : vec)
117 {
118 DeletePanel(iter->GetValue(), recursion);
119 }
120 }
121
124 void SetPanelParent(Panel* panel, Panel* parent)
125 {
126 _allPanel.SetParent(panel->_node, parent->_node);
127 panel->_coor->SetParent(parent->_coor);
128 panel->_refresh_order();
129 }
131 {
132 return _panelRoot;
133 }
134
135 //-----------------------------已注册元素创建(不会返回失败)-------------------------
139 UI::Text* CreateText(std::string_view name);
143 gcText CreateTextOnly(std::string_view name);
147 MsgBox* CreateMsgBox(std::string_view name);
151 ButtonSprite* CreateButtonSprite(std::string_view name);
155 Background* CreateBackground(std::string_view name);
159 Locator::SL CreateLocator(std::string_view name);
163 ListBox* CreateListBox(std::string_view name);
164
165 //-------------------------------默认(创建失败时会使用)---------------------------
176
181 ListBox* CreateListBoxDefault(bool add_text = false);
182 EditBox* CreateEditBoxDefault(bool single_line = true);
185
189 void Register(std::string_view json_path);
194
198 void RegisterAll(std::string_view path);
199
203 void SetScale (float s)
204 {
205 _uiScale = s;
206 _refresh_scale();
207 }
208
212 void SetStandardSize(const Size& size)
213 {
214 if (!g_system->IsWindowExist())
215 {
216 log_err("必须创建窗口之后调用!");
217 return;
218 }
219 _sizeStandard = size;
220 _refresh_standard_size();
221 }
223 {
224 return Any::Zero(_sizeStandard) ?
225 g_system->GetWindowSize() : _sizeStandard;
226 }
227
230 float GetScale()
231 {
232 return _uiScale;
233 }
234
236 {
237 return _allPanel;
238 }
239
241 {
242 for (auto& node : _allPanel)
243 {
244 node->GetValue()->_refresh_lang();
245 }
246 }
247
252 /*Components* CloneCpt(Components* cpt)
253 {
254 if (auto p = cpt->Cast<UI::Text>())
255 return p->Clone()->GetCpt();
256 if (auto p = cpt->Cast<UI::ButtonSprite>())
257 return p->Clone()->GetCpt();
258 assert(0);
259 return nullptr;
260 }*/
261
263 void SetHide(bool hide)
264 {
265 _bHide = hide;
266 }
267
269 bool IsHide()
270 {
271 return _bHide;
272 }
273
274 EditBox* GetFocusEditBox() { return _edtFocus; }
275
277 void SetFocusEditBox(EditBox* edt, bool focus = true);
278
283 void RenderDebugElementData(Coor* coor, const Cpt::ElementData& elem_data);
284
286 {
287 for (auto& node : _allPanel)
288 {
289 delete node->GetValue();
290 }
291 _release();
292 }
293
296 void _update();
297 void _render();
298
300 void _resize();
302
304 void _on_char(char32_t ch);
306 void _on_key(int action, bool ctrl);
307private:
309 NTree<Panel*> _allPanel;
311 Panel* _panelRoot;
312
314 Float _uiScale;
315 Size _sizeStandard;// 标准大小
316
317 bool _bDebug;// 调试绘图
318 bool _bHide;// 隐藏ui界面
319
320 EditBox* _edtFocus;// 焦点输入框
321
322 // 默认ui大小
324
325 //-------------------------模板--------------------------
327 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
328 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
329 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
330 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
331 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
332 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
333
334
336 struct RegKey
337 {
338 ElementType _type;
339 std::string _name;
340 json _json;
341
342 bool operator<(const RegKey& b) const
343 {
344 return _type < b._type;
345 }
346 };
347 std::list<RegKey> _allReg;
348
349 void _reg_text(json& j, const std::string& name);
350 void _reg_msg_box(json& j, const std::string& name);
351 void _reg_button(json& j, const std::string& name);
352 void _reg_background(json& j, const std::string& name);
353 void _reg_locator(json& j, std::string_view name);
354 void _reg_list_box(json& j, std::string_view name);
355
356 void _release()
357 {
358 for (auto& iter : _allText)
359 delete iter.second;
360 for (auto& iter : _allMsgBox)
361 delete iter.second;
362 for (auto& iter : _allBtnSprite)
363 delete iter.second;
364 for (auto& iter : _allBackground)
365 delete iter.second;
366 for (auto& iter : _allListBox)
367 delete iter.second;
368 }
369
370 void _refresh_scale()
371 {
372 _panelRoot->_coor->SetScale(_uiScale);
374 }
375 void _refresh_standard_size();
376
377 void _debug_render();
378};
379
380
381}
382
383namespace dl
384{
385// panel可能是全局的,注意生命周期
386extern UI::Manager* g_ui;
387}
388
389
390#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)
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()
Panel * CreatePanel(Panel *parent)
创建一个Panel
组件实例
资源工厂
json格式相关
nlohmann::json json
2d定位器
#define log_err(...)
N叉树
通用类型定义
作为绘图背景
文本变色按钮
UI之输入框
UI之列表框
UI之菜单
ui之panel
UI之精灵
UI之文本
UI之树
bool Zero(const C &container)
float Float
GC< Text > gcText
System * g_system
UI::Manager * g_ui
PointU Size