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"
33#include "ui/dl_ui_list_box.h"
34#include "ui/dl_ui_edit_box.h"
35#include "ui/dl_ui_tree.h"
36
37namespace dl::UI
38{
39
41{
42public:
44 _bHide{ false },
45 _sizeStandard{},
46 _edtFocus{ nullptr }
47 {
48 //根面板
49 _panelRoot = new Panel;
50 _panelRoot->_node = _allPanel.Add(_panelRoot, nullptr);
51 }
52
58 Panel* CreatePanel(Panel* parent = nullptr)
59 {
60 if (!parent)
61 parent = _panelRoot;
62
63 Panel* p = new Panel;
64 p->_order = Math::OffsetOrder(parent->_order, 0, -1);
65 p->_node = _allPanel.Add(p, parent);
66 p->_coor->SetParent(parent->_coor);
67 return p;
68 }
72 void DeletePanel(Panel* panel)
73 {
74 _allPanel.Del(panel->_node);
75 delete panel;
76 }
80 void SetPanelParent(Panel* panel, Panel* parent)
81 {
82 _allPanel.SetParent(panel->_node, parent->_node);
83 panel->_coor->SetParent(parent->_coor);
84 }
86 {
87 return _panelRoot;
88 }
89
90 //-----------------------------已注册元素创建(不会返回失败)-------------------------
94 UI::Text* CreateText(std::string_view name);
98 gcText CreateTextOnly(std::string_view name);
102 MsgBox* CreateMsgBox(std::string_view name);
106 ButtonSprite* CreateButtonSprite(std::string_view name);
110 Background* CreateBackground(std::string_view name);
114 Locator::SL CreateLocator(std::string_view name);
115
116 //-------------------------------默认(创建失败时会使用)---------------------------
117 enum class ButtonStyle
118 {
119 SMALL,
120 NORMAL,
121 LARGE,
122 LONG,
123 CLOSE,
124 };
125
130 ListBox* CreateListBoxDefault(bool add_text = false);
131 EditBox* CreateEditBoxDefault(bool single_line = true);
133
137 void Register(std::string_view json_path);
142
146 void RegisterAll(std::string_view path);
147
151 void SetOrderPickup(float order) { _orderPickup = order; }
155 void SetScale (float s)
156 {
157 _uiScale = s;
158 _refresh_scale();
159 }
164 void SetStandardSize(const Size& size)
165 {
166 if (!g_system->IsWindowExist())
167 {
168 log_err("必须创建窗口之后调用!");
169 return;
170 }
171 _sizeStandard = size;
172 _refresh_standard_size();
173 }
177 float GetScale()
178 {
179 return _uiScale;
180 }
181
182 float GetOrderPickup() { return _orderPickup; }
183
185 {
186 return _allPanel;
187 }
188
190 {
191 for (auto& node : _allPanel)
192 {
193 node->GetValue()->_refresh_lang();
194 }
195 }
196
201 /*Components* CloneCpt(Components* cpt)
202 {
203 if (auto p = cpt->Cast<UI::Text>())
204 return p->Clone()->GetCpt();
205 if (auto p = cpt->Cast<UI::ButtonSprite>())
206 return p->Clone()->GetCpt();
207 assert(0);
208 return nullptr;
209 }*/
210
212 void SetHide(bool hide)
213 {
214 _bHide = hide;
215 }
216
218 bool IsHide()
219 {
220 return _bHide;
221 }
222
223 EditBox* GetFocusEditBox() { return _edtFocus; }
224
226 void SetFocusEditBox(EditBox* edt, bool focus = true);
227
232
234 {
235 for (auto& node : _allPanel)
236 {
237 delete node->GetValue();
238 }
239 _release();
240 }
241
244 void _update();
245 void _render();
246
248 void _resize();
250
252 void _on_char(char32_t ch);
254 void _on_key(int action, bool ctrl);
255private:
257 NTree<Panel*> _allPanel;
259 Panel* _panelRoot;
260
262 Float _uiScale;
263 Size _sizeStandard;// 标准大小
264
265 float _orderPickup;
266
267 bool _bDebug;// 调试绘图
268 bool _bHide;// 隐藏ui界面
269
270 EditBox* _edtFocus;// 焦点输入框
271
272 //-------------------------模板--------------------------
274 std::unordered_map<std::string, Text*> _allText;
275 std::unordered_map<std::string, MsgBox*> _allMsgBox;
276 std::unordered_map<std::string, ButtonSprite*> _allBtnSprite;
277 std::unordered_map<std::string, Background*> _allBackground;
278 std::unordered_map<std::string, Locator::SL> _allLocator;
279
280
282 struct RegKey
283 {
284 ElementType _type;
285 std::string _name;
286 json _json;
287
288 bool operator<(const RegKey& b) const
289 {
290 return _type < b._type;
291 }
292 };
293 std::list<RegKey> _allReg;
294
295 void _reg_text(json& j, const std::string& name);
296 void _reg_msg_box(json& j, const std::string& name);
297 void _reg_button(json& j, const std::string& name);
298 void _reg_background(json& j, const std::string& name);
299 void _reg_locator(json& j, std::string_view name);
300
301 void _release()
302 {
303 for (auto& iter : _allText)
304 delete iter.second;
305 for (auto& iter : _allMsgBox)
306 delete iter.second;
307 for (auto& iter : _allBtnSprite)
308 delete iter.second;
309 for (auto& iter : _allBackground)
310 delete iter.second;
311 }
312
313 void _refresh_scale()
314 {
315 _panelRoot->_coor->SetScale(_uiScale);
317 }
318 void _refresh_standard_size();
319
320 void _debug_render();
321};
322
323
324}
325
326namespace dl
327{
328// panel可能是全局的,注意生命周期
329extern UI::Manager* g_ui;
330}
331
332
333#endif
void SetParent(Coor *coor)
void SetScale(const Position &scale)
bool IsWindowExist()
窗口是否存在
精灵按钮(注意精灵没有Coor)
ui输入框
ui列表框
MsgBox * CreateMsgBox(std::string_view name)
创建一个MsgBox
Panel * GetPanelRoot()
Definition: dl_ui_manager.h:85
UI::Text * CreateText(std::string_view name)
创建一个UI::Text
void SetOrderPickup(float order)
设置拾取ui的当前order
EditBox * GetFocusEditBox()
Background * CreateBackground(std::string_view name)
创建一个Background
void SetStandardSize(const Size &size)
设置ui标准大小,窗口缩放后,ui会自动缩放
void _pre_update()
system调用
void SetPanelParent(Panel *panel, Panel *parent)
设置panel的父panel
Definition: dl_ui_manager.h:80
EditBox * CreateEditBoxDefault(bool single_line=true)
void _on_char(char32_t ch)
输入时调用
void DeletePanel(Panel *panel)
删除一个Panel
Definition: dl_ui_manager.h:72
void SetHide(bool hide)
Components克隆
void RegisterAll(std::string_view path)
注册多个,传入目录。会调用RegisterEnd
void _on_key(int action, bool ctrl)
按键操作
ListBox * CreateListBoxDefault(bool add_text=false)
auto & GetPanelAll()
Panel * CreatePanel(Panel *parent=nullptr)
创建一个Panel
Definition: dl_ui_manager.h:58
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:196
通用类型定义
作为绘图背景
文本变色按钮
UI之输入框
ui之panel
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Definition: dl_math.h:490
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:31