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 }
222
225 float GetScale()
226 {
227 return _uiScale;
228 }
229
231 {
232 return _allPanel;
233 }
234
236 {
237 for (auto& node : _allPanel)
238 {
239 node->GetValue()->_refresh_lang();
240 }
241 }
242
247 /*Components* CloneCpt(Components* cpt)
248 {
249 if (auto p = cpt->Cast<UI::Text>())
250 return p->Clone()->GetCpt();
251 if (auto p = cpt->Cast<UI::ButtonSprite>())
252 return p->Clone()->GetCpt();
253 assert(0);
254 return nullptr;
255 }*/
256
258 void SetHide(bool hide)
259 {
260 _bHide = hide;
261 }
262
264 bool IsHide()
265 {
266 return _bHide;
267 }
268
269 EditBox* GetFocusEditBox() { return _edtFocus; }
270
272 void SetFocusEditBox(EditBox* edt, bool focus = true);
273
278 void RenderDebugElementData(Coor* coor, const Cpt::ElementData& elem_data);
279
281 {
282 for (auto& node : _allPanel)
283 {
284 delete node->GetValue();
285 }
286 _release();
287 }
288
291 void _update();
292 void _render();
293
295 void _resize();
297
299 void _on_char(char32_t ch);
301 void _on_key(int action, bool ctrl);
302private:
304 NTree<Panel*> _allPanel;
306 Panel* _panelRoot;
307
309 Float _uiScale;
310 Size _sizeStandard;// 标准大小
311
312 bool _bDebug;// 调试绘图
313 bool _bHide;// 隐藏ui界面
314
315 EditBox* _edtFocus;// 焦点输入框
316
317 // 默认ui大小
319
320 //-------------------------模板--------------------------
322 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
323 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
324 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
325 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
326 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
327 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
328
329
331 struct RegKey
332 {
333 ElementType _type;
334 std::string _name;
335 json _json;
336
337 bool operator<(const RegKey& b) const
338 {
339 return _type < b._type;
340 }
341 };
342 std::list<RegKey> _allReg;
343
344 void _reg_text(json& j, const std::string& name);
345 void _reg_msg_box(json& j, const std::string& name);
346 void _reg_button(json& j, const std::string& name);
347 void _reg_background(json& j, const std::string& name);
348 void _reg_locator(json& j, std::string_view name);
349 void _reg_list_box(json& j, std::string_view name);
350
351 void _release()
352 {
353 for (auto& iter : _allText)
354 delete iter.second;
355 for (auto& iter : _allMsgBox)
356 delete iter.second;
357 for (auto& iter : _allBtnSprite)
358 delete iter.second;
359 for (auto& iter : _allBackground)
360 delete iter.second;
361 for (auto& iter : _allListBox)
362 delete iter.second;
363 }
364
365 void _refresh_scale()
366 {
367 _panelRoot->_coor->SetScale(_uiScale);
369 }
370 void _refresh_standard_size();
371
372 void _debug_render();
373};
374
375
376}
377
378namespace dl
379{
380// panel可能是全局的,注意生命周期
381extern UI::Manager* g_ui;
382}
383
384
385#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之树
float Float
GC< Text > gcText
System * g_system
UI::Manager * g_ui
PointU Size