dl
dl_ui_manager.h
浏览该文件的文档.
1
17#pragma once
18
19#ifndef DL_DISABLE_RENDER
20#include "misc/dl_component.h"
21#include "base/dl_type_def.h"
22#include "misc/dl_locator.h"
23#include "base/dl_tree.h"
24#include "dl_ui_background.h"
25#include "render/dl_factory.h"
26#include "io/dl_json.h"
27#include "ui/dl_ui_panel.h"
28#include "ui/dl_ui_sprite.h"
29#include "ui/dl_ui_text.h"
30#include "ui/dl_ui_msg_box.h"
34#include "ui/dl_ui_list_box.h"
35#include "ui/dl_ui_edit_box.h"
36#include "ui/dl_ui_tree.h"
37
38namespace dl::UI
39{
48
50{
51public:
53 _bHide{ false },
54 _sizeStandard{},
55 _edtFocus{ nullptr },
56 _uiScale{1.0f}
57 {
58 _arrSizeType[SizeType::SMALL] = 32;
59 _arrSizeType[SizeType::NORMAL] = 50;
60 _arrSizeType[SizeType::LARGE] = 100;
61 _arrSizeType[SizeType::LONG] = 120;
62 //根面板
63 _panelRoot = new Panel;
64 _panelRoot->_node = _allPanel.AddRoot(_panelRoot);
65 }
66
73 {
74 if (!parent)
75 parent = _panelRoot;
76
77 Panel* p = new Panel;
78 p->_order = Math::OffsetOrder(parent->_order, 0, -1);
79 p->_node = _allPanel.Add(p, parent);
80 p->_coor->SetParent(parent->_coor);
81 return p;
82 }
84 {
85 return CreatePanel(nullptr);
86 }
87
91 void DeletePanel(Panel* panel, bool del_child = false)
92 {
93 if(del_child)
94 {
95 auto vec = panel->_node->GetChild();
96 for(auto& iter : vec)
97 {
98 DeletePanel(iter->GetValue(), true);
99 }
100 }
101 // 会导致父节点的子节点列表迭代器失效
102 _allPanel.Del(panel->_node);
103 delete panel;
104 }
105
109 void DeletePanelChild(Panel* panel, bool recursion = true)
110 {
111 auto vec = panel->_node->GetChild();
112 for (auto& iter : vec)
113 {
114 DeletePanel(iter->GetValue(), recursion);
115 }
116 }
117
120 void SetPanelParent(Panel* panel, Panel* parent)
121 {
122 _allPanel.SetParent(panel->_node, parent->_node);
123 panel->_coor->SetParent(parent->_coor);
124 }
126 {
127 return _panelRoot;
128 }
129
130 //-----------------------------已注册元素创建(不会返回失败)-------------------------
134 UI::Text* CreateText(std::string_view name);
138 gcText CreateTextOnly(std::string_view name);
142 MsgBox* CreateMsgBox(std::string_view name);
146 ButtonSprite* CreateButtonSprite(std::string_view name);
150 Background* CreateBackground(std::string_view name);
154 Locator::SL CreateLocator(std::string_view name);
158 ListBox* CreateListBox(std::string_view name);
159
160 //-------------------------------默认(创建失败时会使用)---------------------------
169
174 ListBox* CreateListBoxDefault(bool add_text = false);
175 EditBox* CreateEditBoxDefault(bool single_line = true);
177
181 void Register(std::string_view json_path);
186
190 void RegisterAll(std::string_view path);
191
195 void SetOrderPickup(float order) { _orderPickup = order; }
199 void SetScale (float s)
200 {
201 _uiScale = s;
202 _refresh_scale();
203 }
204
208 void SetStandardSize(const Size& size)
209 {
210 if (!g_system->IsWindowExist())
211 {
212 log_err("必须创建窗口之后调用!");
213 return;
214 }
215 _sizeStandard = size;
216 _refresh_standard_size();
217 }
218
221 float GetScale()
222 {
223 return _uiScale;
224 }
225
226 float GetOrderPickup() { return _orderPickup; }
227
229 {
230 return _allPanel;
231 }
232
234 {
235 for (auto& node : _allPanel)
236 {
237 node->GetValue()->_refresh_lang();
238 }
239 }
240
245 /*Components* CloneCpt(Components* cpt)
246 {
247 if (auto p = cpt->Cast<UI::Text>())
248 return p->Clone()->GetCpt();
249 if (auto p = cpt->Cast<UI::ButtonSprite>())
250 return p->Clone()->GetCpt();
251 assert(0);
252 return nullptr;
253 }*/
254
256 void SetHide(bool hide)
257 {
258 _bHide = hide;
259 }
260
262 bool IsHide()
263 {
264 return _bHide;
265 }
266
267 EditBox* GetFocusEditBox() { return _edtFocus; }
268
270 void SetFocusEditBox(EditBox* edt, bool focus = true);
271
276
278 {
279 for (auto& node : _allPanel)
280 {
281 delete node->GetValue();
282 }
283 _release();
284 }
285
288 void _update();
289 void _render();
290
292 void _resize();
294
296 void _on_char(char32_t ch);
298 void _on_key(int action, bool ctrl);
299private:
301 NTree<Panel*> _allPanel;
303 Panel* _panelRoot;
304
306 Float _uiScale;
307 Size _sizeStandard;// 标准大小
308
309 float _orderPickup;
310
311 bool _bDebug;// 调试绘图
312 bool _bHide;// 隐藏ui界面
313
314 EditBox* _edtFocus;// 焦点输入框
315
316 // 默认ui大小
318
319 //-------------------------模板--------------------------
321 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
322 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
323 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
324 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
325 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
326 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
327
328
330 struct RegKey
331 {
332 ElementType _type;
333 std::string _name;
334 json _json;
335
336 bool operator<(const RegKey& b) const
337 {
338 return _type < b._type;
339 }
340 };
341 std::list<RegKey> _allReg;
342
343 void _reg_text(json& j, const std::string& name);
344 void _reg_msg_box(json& j, const std::string& name);
345 void _reg_button(json& j, const std::string& name);
346 void _reg_background(json& j, const std::string& name);
347 void _reg_locator(json& j, std::string_view name);
348 void _reg_list_box(json& j, std::string_view name);
349
350 void _release()
351 {
352 for (auto& iter : _allText)
353 delete iter.second;
354 for (auto& iter : _allMsgBox)
355 delete iter.second;
356 for (auto& iter : _allBtnSprite)
357 delete iter.second;
358 for (auto& iter : _allBackground)
359 delete iter.second;
360 for (auto& iter : _allListBox)
361 delete iter.second;
362 }
363
364 void _refresh_scale()
365 {
366 _panelRoot->_coor->SetScale(_uiScale);
368 }
369 void _refresh_standard_size();
370
371 void _debug_render();
372};
373
374
375}
376
377namespace dl
378{
379// panel可能是全局的,注意生命周期
380extern UI::Manager* g_ui;
381}
382
383
384#endif
void SetParent(Coor *coor)
void SetScale(const Position &scale)
精灵按钮(注意精灵没有Coor)
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()
void SetOrderPickup(float order)
设置拾取ui的当前order
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()
窗口大小变化时调用
bool IsHide()
是否隐藏ui
Background * CreateBackgroundDefault()
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
#define log_err(...)
通用类型定义
作为绘图背景
文本变色按钮
UI之输入框
ui之panel
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
float Float
GC< Text > gcText
System * g_system
UI::Manager * g_ui
PointU Size