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{
40 enum class SizeType
41 {
42 SMALL,
43 NORMAL,
44 LARGE,
45 LONG,
47 };
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
72 Panel* CreatePanel(Panel* parent = nullptr)
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 }
87 void DeletePanel(Panel* panel, bool del_child = false)
88 {
89 if(del_child)
90 {
91 auto vec = panel->_node->GetChild();
92 for(auto& iter : vec)
93 {
94 DeletePanel(iter->GetValue(), true);
95 }
96 }
97 // 会导致父节点的子节点列表迭代器失效
98 _allPanel.Del(panel->_node);
99 delete panel;
100 }
105 void DeletePanelChild(Panel* panel, bool recursion = true)
106 {
107 auto vec = panel->_node->GetChild();
108 for (auto& iter : vec)
109 {
110 DeletePanel(iter->GetValue(), recursion);
111 }
112 }
116 void SetPanelParent(Panel* panel, Panel* parent)
117 {
118 _allPanel.SetParent(panel->_node, parent->_node);
119 panel->_coor->SetParent(parent->_coor);
120 }
122 {
123 return _panelRoot;
124 }
125
126 //-----------------------------已注册元素创建(不会返回失败)-------------------------
130 UI::Text* CreateText(std::string_view name);
134 gcText CreateTextOnly(std::string_view name);
138 MsgBox* CreateMsgBox(std::string_view name);
142 ButtonSprite* CreateButtonSprite(std::string_view name);
146 Background* CreateBackground(std::string_view name);
150 Locator::SL CreateLocator(std::string_view name);
154 ListBox* CreateListBox(std::string_view name);
155
156 //-------------------------------默认(创建失败时会使用)---------------------------
157 enum class ButtonStyle
158 {
159 SMALL,
160 NORMAL,
161 LARGE,
162 LONG,
163 CLOSE,
164 };
165
170 ListBox* CreateListBoxDefault(bool add_text = false);
171 EditBox* CreateEditBoxDefault(bool single_line = true);
173
177 void Register(std::string_view json_path);
182
186 void RegisterAll(std::string_view path);
187
191 void SetOrderPickup(float order) { _orderPickup = order; }
195 void SetScale (float s)
196 {
197 _uiScale = s;
198 _refresh_scale();
199 }
204 void SetStandardSize(const Size& size)
205 {
206 if (!g_system->IsWindowExist())
207 {
208 log_err("必须创建窗口之后调用!");
209 return;
210 }
211 _sizeStandard = size;
212 _refresh_standard_size();
213 }
217 float GetScale()
218 {
219 return _uiScale;
220 }
221
222 float GetOrderPickup() { return _orderPickup; }
223
225 {
226 return _allPanel;
227 }
228
230 {
231 for (auto& node : _allPanel)
232 {
233 node->GetValue()->_refresh_lang();
234 }
235 }
236
241 /*Components* CloneCpt(Components* cpt)
242 {
243 if (auto p = cpt->Cast<UI::Text>())
244 return p->Clone()->GetCpt();
245 if (auto p = cpt->Cast<UI::ButtonSprite>())
246 return p->Clone()->GetCpt();
247 assert(0);
248 return nullptr;
249 }*/
250
252 void SetHide(bool hide)
253 {
254 _bHide = hide;
255 }
256
258 bool IsHide()
259 {
260 return _bHide;
261 }
262
263 EditBox* GetFocusEditBox() { return _edtFocus; }
264
266 void SetFocusEditBox(EditBox* edt, bool focus = true);
267
272
274 {
275 for (auto& node : _allPanel)
276 {
277 delete node->GetValue();
278 }
279 _release();
280 }
281
284 void _update();
285 void _render();
286
288 void _resize();
290
292 void _on_char(char32_t ch);
294 void _on_key(int action, bool ctrl);
295private:
297 NTree<Panel*> _allPanel;
299 Panel* _panelRoot;
300
302 Float _uiScale;
303 Size _sizeStandard;// 标准大小
304
305 float _orderPickup;
306
307 bool _bDebug;// 调试绘图
308 bool _bHide;// 隐藏ui界面
309
310 EditBox* _edtFocus;// 焦点输入框
311
312 // 默认ui大小
314
315 //-------------------------模板--------------------------
317 std::unordered_map<std::string, Text*, string_hash, std::equal_to<>> _allText;
318 std::unordered_map<std::string, MsgBox*, string_hash, std::equal_to<>> _allMsgBox;
319 std::unordered_map<std::string, ButtonSprite*, string_hash, std::equal_to<>> _allBtnSprite;
320 std::unordered_map<std::string, Background*, string_hash, std::equal_to<>> _allBackground;
321 std::unordered_map<std::string, Locator::SL, string_hash, std::equal_to<>> _allLocator;
322 std::unordered_map<std::string, ListBox*, string_hash, std::equal_to<>> _allListBox;
323
324
326 struct RegKey
327 {
328 ElementType _type;
329 std::string _name;
330 json _json;
331
332 bool operator<(const RegKey& b) const
333 {
334 return _type < b._type;
335 }
336 };
337 std::list<RegKey> _allReg;
338
339 void _reg_text(json& j, const std::string& name);
340 void _reg_msg_box(json& j, const std::string& name);
341 void _reg_button(json& j, const std::string& name);
342 void _reg_background(json& j, const std::string& name);
343 void _reg_locator(json& j, std::string_view name);
344 void _reg_list_box(json& j, std::string_view name);
345
346 void _release()
347 {
348 for (auto& iter : _allText)
349 delete iter.second;
350 for (auto& iter : _allMsgBox)
351 delete iter.second;
352 for (auto& iter : _allBtnSprite)
353 delete iter.second;
354 for (auto& iter : _allBackground)
355 delete iter.second;
356 for (auto& iter : _allListBox)
357 delete iter.second;
358 }
359
360 void _refresh_scale()
361 {
362 _panelRoot->_coor->SetScale(_uiScale);
364 }
365 void _refresh_standard_size();
366
367 void _debug_render();
368};
369
370
371}
372
373namespace dl
374{
375// panel可能是全局的,注意生命周期
376extern UI::Manager* g_ui;
377}
378
379
380#endif
void SetParent(Coor *coor)
void SetScale(const Position &scale)
bool IsWindowExist()
窗口是否存在
精灵按钮(注意精灵没有Coor)
ui输入框
ui列表框
MsgBox * CreateMsgBox(std::string_view name)
创建一个MsgBox
Panel * GetPanelRoot()
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
Definition: dl_ui_manager.h:87
void _on_key(int action, bool ctrl)
按键操作
ListBox * CreateListBoxDefault(bool add_text=false)
Panel * CreatePanel(Panel *parent=nullptr)
创建一个Panel
Definition: dl_ui_manager.h:72
void SetScale(float s)
设置ui缩放
void _resize()
窗口大小变化时调用
float GetOrderPickup()
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()
ui文本
Definition: dl_ui_text.h:26
组件系统
资源工厂
json格式相关
nlohmann::json json
Definition: dl_json.h:19
#define log_err(...)
Definition: dl_log.h:243
通用类型定义
作为绘图背景
文本变色按钮
UI之输入框
ui之panel
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Definition: dl_math.h:502
Definition: dl_array2.h:18
PointU Size
Definition: dl_type_def.h:34
float Float
Definition: dl_type_def.h:37
System * g_system
UI::Manager * g_ui
创建参数
Definition: dl_locator.h:32