dl
dl_ui_panel.h
浏览该文件的文档.
1
14#pragma once
15
16#ifndef DL_DISABLE_RENDER
18#include "base/dl_type_def.h"
19#include "misc/dl_locator.h"
20#include "base/dl_tree.h"
21#include "dl_ui_background.h"
22#include "render/dl_factory.h"
23#include "ui/dl_ui_sprite.h"
24#include "ui/dl_ui_text.h"
25#include "ui/dl_ui_msg_box.h"
28#include "io/dl_json.h"
29
30namespace dl::UI
31{
42
43inline ElementType String2ElementType(std::string_view str)
44{
45 if (String::EqualToLower(str, "text"))
46 return ElementType::TEXT;
47 if (String::EqualToLower(str, "msg_box"))
49 if (String::EqualToLower(str, "button"))
51 if (String::EqualToLower(str, "background"))
53 if (String::EqualToLower(str, "locator"))
55 if (String::EqualToLower(str, "list_box"))
57
58 log_err("未知的type字段:{}", str);
60}
61
62class EditBox;
63
65class Panel
66{
67 friend class Manager;
68public:
70 {
71 E_SIZE = 1 << 1,
72 E_ELEMENT = 1 << 2,
73 E_CHILD = 1 << 4,
74 };
76 {
80 };
81
84 void AddCpt(Components* cpt);
88 template<typename T>
89 void Add(T* p)
90 {
91 AddCpt(p->GetCpt());
92 }
93
94 void Add(Text* p) { AddCpt(p->GetCpt()); }
98 void Add(std::string_view json_path);
102 void AddPanel(Panel* child);
103
108 void DelCpt(Components* cpt);
112 template<typename T>
113 void Del(T* p)
114 {
115 DelCpt(p->GetCpt());
116 }
117
120 Panel* Clone();
121
125 void SetPosPixel(const Position& pos_pixel)
126 {
127 if (!_bAutoCoor)
128 {
129 log_err0("关闭了自动设置位置,设置像素位置无效!");
130 return;
131 }
132 _posPixel = pos_pixel;
133 _coor->SetPosition(_posPixel - Align::Size2Anchor(GetSize(), _align));
134 }
135 void DtPosPixel(const Position& dt)
136 {
137 if (!_bAutoCoor)
138 {
139 log_err0("关闭了自动设置位置,设置像素位置无效!");
140 return;
141 }
142 _posPixel += dt;
143 _coor->DtPosition(dt);
144 }
145
148 void SetPosScale(const Position& pos_scale)
149 {
150 if (!_bAutoCoor)
151 {
152 log_err0("关闭了自动设置位置,设置比例位置无效!");
153 return;
154 }
155 _posScale = pos_scale;
156 _bNeedRefreshPos = true;
157 }
158
162 {
163 _align = hv;
164 _bNeedRefreshPos = true;
165 }
166
170 void SetLocator(const Locator& loc)
171 {
172 _locator = loc;
173 _bNeedRefreshLocator = true;
174 }
175 void SetLocator(const Locator::SL& sl)
176 {
177 SetLocator(Locator{ sl });
178 }
179 void SetLocator(std::string_view name);
184 void SetAutoCoor(bool auto_coor) { _bAutoCoor = auto_coor; }
189 void SetAutoRun(bool auto_run) { _bAutoRun = auto_run; }
194 void SetAutoRender(bool auto_render) { _bAutoRender = auto_render; }
200 {
201 _orderMode = mode;
202 _refresh_order();
203 }
204
208 void SetOrder(float order)
209 {
210 assert(_orderMode == OrderMode::FORCE);
211 if (_orderMode == OrderMode::FORCE)
212 {
213 _order = order;
214 _refresh_order();
215 }
216 }
217
221 void SetOrderInParent(int order_parent)
222 {
223 assert(_orderMode == OrderMode::CUSTOM);
224 if (_orderMode == OrderMode::CUSTOM)
225 {
226 _orderInParent = order_parent;
227 _refresh_order();
228 }
229 }
231 {
232 _refresh_order();
233 }
234
237 void SetName(std::string_view name)
238 {
239 _name = name;
240 }
241
245 void SetSelect(size_t index)
246 {
247 size_t i = 0;
248 for (auto& iter : _allEle)
249 {
250 if (auto comp = iter->Get<Cpt::SetSelect>())
251 {
252 comp->Call(i == index);
253 }
254 ++i;
255 }
256 }
257
260 void ElementSetSelect(size_t index, bool select)
261 {
262 if (index >= _allEle.size())
263 log_err0("越界!");
264 if (auto comp = _allEle[index]->Get<Cpt::SetSelect>())
265 {
266 comp->Call(select);
267 return;
268 }
269 log_err0("对象没有SetSelect组件!");
270 }
271
274 void SetSelectAll(bool select)
275 {
276 for (auto& iter : _allEle)
277 {
278 if (auto comp = iter->Get<Cpt::SetSelect>())
279 {
280 comp->Call(select);
281 }
282 }
283 }
284
288 void Update();
295 void Render();
304 bool IsUpdate() { return _bUpdate; }
308 bool IsRender() { return _bRender; }
312 std::string GetName() const { return _name; }
318 {
319 assert(!_bAutoCoor);
320 return _coor;
321 }
323 {
324 return _coor;
325 }
326
329 Panel* GetParent()
330 {
331 if(_node->GetParent())
332 return _node->GetParent()->GetValue();
333 return nullptr;
334 }
335
338 std::vector<Panel*> GetChildAll()
339 {
340 std::vector<Panel*> ret;
341 for (auto& iter : _node->GetChild())
342 ret.push_back(iter->GetValue());
343 return ret;
344 }
345
348 size_t GetChildNum(bool recursion = false)
349 {
350 size_t ret = _node->GetChild().size();
351 if (recursion)
352 {
353 for (auto& iter : _node->GetChild())
354 ret += iter->GetValue()->GetChildNum(true);
355 }
356 return ret;
357 }
358
361 size_t GetChildIndex(Panel* child)
362 {
363 size_t i = 0;
364 for (auto& iter : _node->GetChild())
365 {
366 if (iter->GetValue() == child)
367 return i;
368 ++i;
369 }
370 return -1;
371 }
372
376 Panel* GetChild(size_t i)
377 {
378 auto& vec = _node->GetChild();
379 if (i >= vec.size())
380 {
381 log_err("下标越界!");
382 return nullptr;
383 }
384 return vec[i]->GetValue();
385 }
386
393 void SetChildSelect(Panel* panel);
398
400 template<typename T>
401 std::size_t GetIndex()
402 {
403 size_t i = 0;
404 for (auto& iter : _allEle)
405 {
406 if (auto comp = iter->Get<T>())
407 {
408 if (comp->Call())
409 return i;
410 }
411 ++i;
412 }
413 return -1;
414 }
415
416 template<typename T>
418 {
419 for (auto& iter : _allEle)
420 {
421 if (auto comp = iter->Get<T>())
422 {
423 if (comp->Call())
424 return iter;
425 }
426 }
427 return nullptr;
428 }
429
435 {
436 return GetIndex<Cpt::IsClick>();
437 }
438
443 {
444 return GetIndex<Cpt::IsDown>();
445 }
446
452 {
454 }
455
461 {
462 return Get<Cpt::IsSelect>();
463 }
464
465 std::string GetSelectName()
466 {
467 Components* cpt = GetSelect();
468 return cpt ? cpt->GetName() : "";
469 }
470
475 bool IsSelectIndex(size_t index)
476 {
477 if (index >= _allEle.size())
478 {
479 log_err0("越界!");
480 return false;
481 }
482 if (auto comp = _allEle[index]->Get<Cpt::IsSelect>())
483 {
484 return comp->Call();
485 }
486
487 log_err0("对象没有Open组件!");
488 return false;
489 }
490
495 {
496 if (_bSizeAuto)
497 return _locator.GetSize();
498 return _size;
499 }
500
504
506 {
507 return _coor->GetPosition();
508 }
509 float GetOrder()
510 {
511 return _order;
512 }
513
515 {
516 return _locator;
517 }
518
522 template<typename T>
523 T* GetElement(size_t n = 0)
524 {
525 if(n == 0)
526 return Cpt::GetFirst<T>(_allEle);
527 else
528 return Cpt::GetN<T>(_allEle, n);
529 }
530
532 template<typename T>
533 std::vector<T*> GetElementVector()
534 {
535 std::vector<T*> ret;
536 for (auto& iter : _allEle)
537 {
538 T* cpt = iter->Cast<T>();
539 if (cpt)
540 ret.push_back(cpt);
541 }
542 return ret;
543 }
544
546 bool IsPickup()
547 {
548 if (_pickupMode & E_SIZE)
549 {
550 if (g_factory->GetOrderPickup() < _order)
551 return false;
552 Position pos = _coor->WorldToThis(toPosition(g_input->GetCursorPosition()));
553 Position size = toPosition(GetSize());
554 if (pos[0] >= 0
555 && pos[1] >= 0
556 && pos[0] <= size[0]
557 && pos[1] <= size[1])
558 return true;
559 }
560 if (_pickupMode & E_ELEMENT)
561 {
562 for (Components* cpt : _allEle)
563 {
564 Cpt::IsPickup* comp = cpt->Get<Cpt::IsPickup>();
565 if (!comp)
566 continue;
567 if (comp->Call())
568 return true;
569 }
570 }
571 if (_pickupMode & E_CHILD)
572 {
573 for (auto& iter : _node->GetChild())
574 {
575 Panel* child = iter->GetValue();
576 if (child->IsPickup())
577 return true;
578 }
579 }
580 return false;
581 }
582
583 // 指定大小
584 void SetSize(const Position& size)
585 {
586 _size = size;
587 _bSizeAuto = false;
588 _bSizeFull = false;
589 _refresh_size();
590 }
591 // 设置拾取模式
593 {
594 _pickupMode = mode;
595 }
596 void SetPickupMode(int mode)
597 {
599 }
600 // 与父节点一样大
602 {
603 _bSizeFull = true;
604 _refresh_size();
605 }
606
608
609 bool IsAutoRun() { return _bAutoRun; }
610 //----------------------------------辅助函数-----------------------------
612 UI::Button* ExGetButton(size_t n = 0, bool show_log = true);
613 UI::ButtonSprite* ExGetButtonSprite(size_t n = 0, bool show_log = true);
614 UI::ButtonSprite9* ExGetButtonSprite9(size_t n = 0, bool show_log = true);
616 UI::EditBox* ExGetEditBox(size_t n = 0, bool show_log = true);
618 UI::Text* ExGetText(size_t n = 0, bool show_log = true);
620 void ExSetString(std::string_view str, size_t n = 0);
621
622 void SetDebugName(std::string_view name)
623 {
624#ifndef NDEBUG
625 _debugName = name;
626#endif
627 }
628private:
629 Panel() :
630 _order{ Z_DEFAULT_PANEL },
631 _orderMode{ OrderMode::AUTO },
632 _orderInParent{ -1 },
633 _bAutoRun{ true },
634 _bAutoRender{ true },
635 _bNeedRefreshPos{ false },
636 _bNeedRefreshLocator{ false },
637 _bDisableLocator{ false },
638 _bAutoCoor{ true },
639 _bUpdate{ false },
640 _bRender{ false },
641 _pickupMode{ E_SIZE },
642 _align{ Align::LT },
643 _posPixel{},
644 _posScale{},
645 _size{},
646 _bSizeFull{ true },
647 _bSizeAuto{ false }
648 {
649 _coor = Coor::Create();
650 }
651 ~Panel()
652 {
653 //删除元素
654 for (Components*& cpt : _allEle)
655 {
656 if (cpt->IsPure())
657 delete cpt;
658 else if (auto comp = cpt->Get<Cpt::DeleteThis>())
659 {
660 comp->Call();
661 }
662
663 }
664 delete _coor;
665 }
666#ifndef NDEBUG
667 std::string _debugName;
668#endif
669
670 // 添加到 Scene时自动设置
671 std::string _name;
672 std::vector<Components*> _allEle;// 子元素
673
674 Coor* _coor;
675 // 全局偏移(受 order模式控制)
676 float _order;
677 OrderMode _orderMode;
678 int _orderInParent;
679
680 bool _bAutoRun;// 自动运行(由父节点调用Update和Render)
681 bool _bAutoRender;// 如果调用了Update,则自动调用Render(由父节点)
682 bool _bNeedRefreshPos; // 更新位置
683 bool _bNeedRefreshLocator; // 重新定位
684 bool _bDisableLocator;// 禁用定位器,暂未使用
685 bool _bAutoCoor;// 接手Coor后,需要设为真
686 bool _bUpdate;// 此帧是否已运行
687 bool _bRender;// 此帧是否已绘制
688 PickupMode _pickupMode;
689
690 //树节点引用
691 NTree<Panel*>::Node* _node;
692
693 //location
694 Align::Direction2 _align; // 在父节点的对齐
695 Position _posPixel;
696 Position _posScale;
697
698 //locator
699 Locator _locator;
700
701 Position _size; // 指定大小
702 bool _bSizeFull;// 与父节点同大小
703 bool _bSizeAuto; // 自动大小
704
705 void _refresh_lang()
706 {
707
708 }
709
711 void _refresh_pos(const Position& size);
712
714 void _refresh_size();
716 void _refresh_order();
717
718 void _update_child();
719 void _render_child();
720
721 void _update_cpt_drag(Components* cpt);
722 void _update_cpt_radio(Components* cpt);
723};
724
725}
726
727#endif
std::string GetName()
2d坐标系
static Coor * Create(Coor *parent=nullptr)
2d定位器
按钮(Button、Switch、Radio)
精灵按钮(注意精灵没有Coor)
void DtPosPixel(const Position &dt)
UI::ButtonSprite * ExGetButtonSprite(size_t n=0, bool show_log=true)
void Add(T *p)
添加一个Element
bool IsSelectIndex(size_t index)
index元素是否选中状态
const Locator & GetLocator()
size_t GetClickIndex()
返回被点击的元素
void Update()
执行逻辑 @node 默认情况会自动调用
void Add(Text *p)
脚本导出使用
UI::EditBox * ExGetEditBox(size_t n=0, bool show_log=true)
返回第n个列表框
@ E_ELEMENT
子元素
@ E_SIZE
自己大小
size_t GetChildNum(bool recursion=false)
返回子panel数量
Panel * GetChildPickup()
返回光标悬停的子panel
void SetLocator(const Locator::SL &sl)
@ AUTO
自动设置
@ CUSTOM
手动设置
@ FORCE
强行设置全局
void SetName(std::string_view name)
设置名字
void SetOrder(float order)
设置order
Components * GetSelect()
返回被选中的元素
std::size_t GetIndex()
返回第一个满足条件的组件下标
UI::Text * ExGetText(size_t n=0, bool show_log=true)
返回第n个text
void SetAutoRender(bool auto_render)
设置是否自动绘制
void AddPanel(Panel *child)
添加子panel
void SetLocator(const Locator &loc)
设置Locator
std::vector< T * > GetElementVector()
返回所有类型的元素
size_t GetDownIndex()
返回被按下的元素
void SetSize(const Position &size)
Panel * GetChildSelect()
返回被选中的子panel
RectF GetRect()
返回所有元素和子节点组成的包围盒
void Render()
执行绘制 @node 默认情况会自动调用
void SetOrderMode(OrderMode mode)
设置order模式
std::vector< Panel * > GetChildAll()
返回子panel
void SetPosPixel(const Position &pos_pixel)
设置 像素位置
void ElementSetSelect(size_t index, bool select)
设置元素是否选中
std::string GetSelectName()
void SetAutoCoor(bool auto_coor)
设置是否自动调整位置
void ExSetString(std::string_view str, size_t n=0)
设置第n个text内容
UI::Button * ExGetButton(size_t n=0, bool show_log=true)
返回第n个按钮
void OnChangeElementData(Components *cpt)
T * GetElement(size_t n=0)
返回第n个元素
void SetDebugName(std::string_view name)
bool IsRender()
执行了Render
bool IsUpdate()
执行了Update
Panel * GetParent()
返回父panel
size_t GetSelectIndex()
返回被选中的元素下标
void SetAutoRun(bool auto_run)
设置是否自动执行
void DelCpt(Components *cpt)
移除子Element,不删除
void UpdateRadio()
执行逻辑Radio @node 手动调用,把子panel作为radio处理
Panel * Clone()
克隆
std::string GetName() const
返回名字
size_t GetChildIndex(Panel *child)
返回子panel下标
void SetChildSelect(Panel *panel)
设置被选中的子panel,可以传空
UI::ButtonSprite9 * ExGetButtonSprite9(size_t n=0, bool show_log=true)
void SetAlign(const Align::Direction2 &hv)
设置 在父节点的对齐
void SetLocator(std::string_view name)
void Del(T *p)
添加一个Element
Position GetSize()
返回大小
void SetSelectAll(bool select)
设置所有子元素的选中状态
void SetOrderInParent(int order_parent)
设置order
friend class Manager
void SetSelect(size_t index)
设置被选中的元素
bool IsPickup()
是否被鼠标选中(任意子元素、子面板被选中)
Coor * GetCoor()
返回坐标系
void Add(std::string_view json_path)
添加多个Element,通过json文件
void SetPickupMode(int mode)
void SetPickupMode(PickupMode mode)
Panel * GetChild(size_t i)
返回子panel
Position GetPosition()
void AddCpt(Components *cpt)
添加一个Element
void SetPosScale(const Position &pos_scale)
设置 比例位置
Components * Get()
返回第一个满足条件的组件
Components * GetCpt()
组件实例
资源工厂
json格式相关
2d定位器
#define log_err(...)
N叉树
通用类型定义
作为绘图背景
九图精灵按钮
UI之精灵
UI之文本
方向、对齐定义
Array< Direction, 2 > Direction2
constexpr Array< T, N > Size2Anchor(const Array< T, N > &size, const Array< Direction, N > &dir)
垂直位置
Function< std::function< bool()>, str_uid("IsPickup")> IsPickup
---------------------------—具体组件----------------------------------—
Function< std::function< void(bool)>, str_uid("SetSelect")> SetSelect
设置是否选择
T * GetN(std::vector< Components * > &vec, size_t n)
从vector<Components*>返回指定类型的第n个元素
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
T * GetFirst(std::vector< Components * > &vec)
从vector<Components*>返回指定类型的第一个元素
ElementType String2ElementType(std::string_view str)
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
Input * g_input
Factory * g_factory
Position4 RectF
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr float Z_DEFAULT_PANEL