dl
dl_ui_panel.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
15#include "misc/dl_component.h"
16#include "base/dl_type_def.h"
17#include "misc/dl_locator.h"
18#include "base/dl_tree.h"
19#include "dl_ui_background.h"
20#include "render/dl_factory.h"
21#include "ui/dl_ui_sprite.h"
22#include "ui/dl_ui_text.h"
23#include "ui/dl_ui_msg_box.h"
25#include "io/dl_json.h"
26
27namespace dl::UI
28{
29enum class ElementType
30{
31 UNKNOWN = -1,
33 TEXT,
34 BUTTON,
35 MSG_BOX,
37};
38
39inline ElementType String2ElementType(std::string_view str)
40{
41 if (String::EqualToLower(str, "text"))
42 return ElementType::TEXT;
43 if (String::EqualToLower(str, "msg_box"))
45 if (String::EqualToLower(str, "button"))
47 if (String::EqualToLower(str, "background"))
49 if (String::EqualToLower(str, "locator"))
51
52 log_err("未知的type字段:{}", str);
54}
55
56class EditBox;
57
58class Panel
59{
60 friend class Manager;
61public:
65 template<typename T>
66 void Add(T* p)
67 {
68 AddCpt(p->GetCpt());
69 }
73 void AddCpt(Components* cpt);
77 void Add(std::string_view json_path);
78
83
87 void SetPosPixel(const Position& pos_pixel)
88 {
89 if (!_bAutoCoor)
90 {
91 log_err0("关闭了自动设置位置,设置像素位置无效!");
92 return;
93 }
94 _posPixel = pos_pixel;
95 _coor->SetPosition(_posPixel);
96 }
97 void DtPosPixel(const Position& dt)
98 {
99 if (!_bAutoCoor)
100 {
101 log_err0("关闭了自动设置位置,设置像素位置无效!");
102 return;
103 }
104 _posPixel += dt;
105 _coor->DtPosition(dt);
106 }
110 void SetPosScale(const Position& pos_scale)
111 {
112 if (!_bAutoCoor)
113 {
114 log_err0("关闭了自动设置位置,设置比例位置无效!");
115 return;
116 }
117 _posScale = pos_scale;
118 _bNeedRefreshPos = true;
119 }
123 void SetLocator(const Locator& loc)
124 {
125 _locator = loc;
126 _bNeedRefreshLocator = true;
127 }
128 void SetLocator(const Locator::SL& sl)
129 {
130 SetLocator(Locator{ sl });
131 }
132 void SetLocator(std::string_view name);
137 void SetAutoCoor(bool auto_coor) { _bAutoCoor = auto_coor; }
142 void SetAutoRun(bool auto_run) { _bAutoRun = auto_run; }
143
148 void SetOrder(float order)
149 {
150 _order = order;
151 _refresh_order();
152 }
153
157 void SetSelect(std::size_t index)
158 {
159 std::size_t i = 0;
160 for (auto& iter : _allEle)
161 {
162 if (auto comp = iter->Get<Cpt::SetSelect>())
163 {
164 comp->Call(i == index);
165 }
166 }
167 }
171 void SetSelectAll(bool select)
172 {
173 for (auto& iter : _allEle)
174 {
175 if (auto comp = iter->Get<Cpt::SetSelect>())
176 {
177 comp->Call(select);
178 }
179 }
180 }
185 void Update();
186 void Render();
187
191 bool IsUpdate() { return _bUpdate; }
195 bool IsRender() { return _bRender; }
201 {
202 assert(!_bAutoCoor);
203 return _coor;
204 }
206 {
207 return _coor;
208 }
213 {
214 if(_node->GetParent())
215 return _node->GetParent()->GetValue();
216 return nullptr;
217 }
218
220 template<typename T>
221 std::size_t GetIndex()
222 {
223 size_t i = 0;
224 for (auto& iter : _allEle)
225 {
226 if (auto comp = iter->Get<T>())
227 {
228 if (comp->Call())
229 return i;
230 }
231 ++i;
232 }
233 return -1;
234 }
236 template<typename T>
238 {
239 for (auto& iter : _allEle)
240 {
241 if (auto comp = iter->Get<T>())
242 {
243 if (comp->Call())
244 return iter;
245 }
246 }
247 return nullptr;
248 }
249
255 {
256 return GetIndex<Cpt::IsClick>();
257 }
258
264 {
265 return GetIndex<Cpt::IsSelect>();
266 }
267
273 {
274 return Get<Cpt::IsSelect>();
275 }
276
277 std::string GetSelectName()
278 {
279 Components* cpt = GetSelect();
280 return cpt ? cpt->GetName() : "";
281 }
282
287 bool IsIndexOpen(size_t index)
288 {
289 if (index >= _allEle.size())
290 {
291 log_err0("越界!");
292 return false;
293 }
294 if (auto comp = _allEle[index]->Get<Cpt::IsSelect>())
295 {
296 return comp->Call();
297 }
298
299 log_err0("对象没有Open组件!");
300 return false;
301 }
302
308 {
309 if (_bg)
310 {
311 return _bg->GetSize();
312 }
313 return _locator.GetSize();
314 }
315
317 {
318 return _coor->GetPosition();
319 }
320
325 template<typename T>
326 T* GetElement(size_t n = 0)
327 {
328 if(n == 0)
329 return Cpt::GetFirst<T>(_allEle);
330 else
331 return Cpt::GetN<T>(_allEle, n);
332 }
333
335 template<typename T>
336 std::vector<T*> GetElementVector()
337 {
338 std::vector<T*> ret;
339 for (auto& iter : _allEle)
340 {
341 T* cpt = iter->Cast<T>();
342 if (cpt)
343 ret.push_back(cpt);
344 }
345 return ret;
346 }
347
350 {
351 return GetElement<UI::Text>();
352 }
353
355 bool IsPickup()
356 {
357 if (_bg && _bg->IsPickup())
358 return true;
359 for (Components* cpt : _allEle)
360 {
361 Cpt::IsPickup* comp = cpt->Get<Cpt::IsPickup>();
362 if (!comp)
363 continue;
364 if (comp->Call())
365 return true;
366 }
367 return false;
368 }
369
370 void SetBackgroundSize(const Size& size)
371 {
372 if (!_bg)
373 _bg = Background::Create();
375 spr->SetQuad(createQuad(size, false));
376
377 _bg->Set(spr);
378 _bg->GetCoor()->SetParent(_coor);
379 }
380
382
383 //----------------------------------辅助函数-----------------------------
385 {
386 UI::Button* btn = GetElement<UI::Button>();
387 return dynamic_cast<UI::ButtonSprite*>(btn);
388 }
389 UI::EditBox* ExGetEditBox(size_t n = 0);
390private:
391 Panel() :
392 _order{ Z_DEFAULT_UI },
393 _bAutoRun{ true },
394 _bNeedRefreshPos{ false },
395 _bNeedRefreshLocator{ false },
396 _bDisableLocator{ false },
397 _bAutoCoor{ true },
398 _bUpdate{ false },
399 _bRender{ false },
400 _posPixel{},
401 _posScale{},
402 _bg{ nullptr }
403 {
404 _coor = Coor::Create();
405 }
406 ~Panel()
407 {
408 //删除元素
409 for (Components*& cpt : _allEle)
410 {
411 if (cpt->IsPure())
412 delete cpt;
413 else if (auto comp = cpt->Get<Cpt::DeleteThis>())
414 {
415 comp->Call();
416 }
417
418 }
419 delete _bg;
420 delete _coor;
421 }
422 std::vector<Panel*> _child;// 子面板(TODO)
423 std::vector<Components*> _allEle;// 子元素
424
425 Coor* _coor;
426 float _order;
427
428 bool _bAutoRun;// 自动运行
429 bool _bNeedRefreshPos; // 更新位置
430 bool _bNeedRefreshLocator; // 重新定位
431 bool _bDisableLocator;// 禁用定位器,暂未使用
432 bool _bAutoCoor;// 接手Coor后,需要设为真
433 bool _bUpdate;// 此帧是否已运行
434 bool _bRender;// 此帧是否已绘制
435
436 //树节点引用
437 NTree<Panel*>::Node* _node;
438
439 //location
440 Position _posPixel;
441 Position _posScale;
442
443 //locator
444 Locator _locator;
445
446 //背景(修改为组件)panel添加默认指针缓存组件
447 Background* _bg;
448
449
450 void _refresh_lang()
451 {
452
453 }
454
455 void _refresh_pos(const Size& size);
456
458 void _refresh_size(const Size& size);
460 void _refresh_order();
461
462 void _update_child();
463 void _render_child();
464};
465
466}
467
468#endif
std::string GetName()
Definition: dl_component.h:258
2d坐标系
Definition: dl_coor.h:27
void SetParent(Coor *coor)
Position GetPosition() const
static Coor * Create(Coor *parent=nullptr)
void SetPosition(const Position &pos)
void DtPosition(const Position &dt)
gcSprite CreateSpriteDefault(const Color &col=ColorDef::WHITE)
创建默认精灵
定位器
Definition: dl_locator.h:21
Size GetSize()
Definition: dl_locator.h:79
void SetQuad(const Quad &quad)
static Background * Create()
void Set(gcSprite spr)
按钮(Button、Switch、Radio)
精灵按钮(注意精灵没有Coor)
ui输入框
UI::ButtonSprite * ExGetButtonSprite()
Definition: dl_ui_panel.h:384
void DtPosPixel(const Position &dt)
Definition: dl_ui_panel.h:97
UI::Text * GetText()
返回第一个text
Definition: dl_ui_panel.h:349
void Add(T *p)
添加一个Element
Definition: dl_ui_panel.h:66
Coor * GetCoorRef()
Definition: dl_ui_panel.h:205
size_t GetClickIndex()
返回被点击的元素
Definition: dl_ui_panel.h:254
void Update()
执行逻辑 @node 默认情况会自动调用
void SetLocator(const Locator::SL &sl)
Definition: dl_ui_panel.h:128
bool IsIndexOpen(size_t index)
index元素是否open状态
Definition: dl_ui_panel.h:287
void SetSelect(std::size_t index)
设置被选中的元素
Definition: dl_ui_panel.h:157
void SetOrder(float order)
设置order
Definition: dl_ui_panel.h:148
Components * GetSelect()
返回被选中的元素
Definition: dl_ui_panel.h:272
std::size_t GetIndex()
返回第一个满足条件的组件下标
Definition: dl_ui_panel.h:221
Size GetSize()
返回大小
Definition: dl_ui_panel.h:307
void SetLocator(const Locator &loc)
设置Locator
Definition: dl_ui_panel.h:123
std::vector< T * > GetElementVector()
返回所有类型的元素
Definition: dl_ui_panel.h:336
void SetPosPixel(const Position &pos_pixel)
设置 像素位置
Definition: dl_ui_panel.h:87
std::string GetSelectName()
Definition: dl_ui_panel.h:277
void SetAutoCoor(bool auto_coor)
设置是否自动调整位置
Definition: dl_ui_panel.h:137
void OnChangeElementData(Components *cpt)
T * GetElement(size_t n=0)
返回第n个元素
Definition: dl_ui_panel.h:326
bool IsRender()
执行了Render
Definition: dl_ui_panel.h:195
bool IsUpdate()
执行了Update
Definition: dl_ui_panel.h:191
Panel * GetParent()
返回父panel
Definition: dl_ui_panel.h:212
size_t GetSelectIndex()
返回被选中的元素下标
Definition: dl_ui_panel.h:263
void SetBackgroundSize(const Size &size)
Definition: dl_ui_panel.h:370
void SetAutoRun(bool auto_run)
设置是否自动执行
Definition: dl_ui_panel.h:142
Panel * Clone()
克隆
void SetLocator(std::string_view name)
void SetSelectAll(bool select)
设置所有子元素的选中状态
Definition: dl_ui_panel.h:171
UI::EditBox * ExGetEditBox(size_t n=0)
bool IsPickup()
是否被鼠标选中
Definition: dl_ui_panel.h:355
Coor * GetCoor()
返回坐标系
Definition: dl_ui_panel.h:200
void Add(std::string_view json_path)
添加多个Element,通过json文件
Position GetPosition()
Definition: dl_ui_panel.h:316
void AddCpt(Components *cpt)
添加一个Element
void SetPosScale(const Position &pos_scale)
设置 比例位置
Definition: dl_ui_panel.h:110
Components * Get()
返回第一个满足条件的组件
Definition: dl_ui_panel.h:237
ui文本
Definition: dl_ui_text.h:26
组件系统
资源工厂
json格式相关
#define log_err(...)
Definition: dl_log.h:196
通用类型定义
作为绘图背景
constexpr Color NONE
Definition: dl_color.h:248
ElementType String2ElementType(std::string_view str)
Definition: dl_ui_panel.h:39
PointU Size
Definition: dl_type_def.h:34
Factory * g_factory
constexpr Quad createQuad(const Position &lt, const Position &rb)
创建一个Quad,通过左上角和右下角的坐标
constexpr float Z_DEFAULT_UI
Definition: dl_sprite.h:26
Position2 Position
Definition: dl_type_def.h:43
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())
创建参数
Definition: dl_locator.h:31