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"
27#include "io/dl_json.h"
28
29namespace dl::UI
30{
41
42inline ElementType String2ElementType(std::string_view str)
43{
44 if (String::EqualToLower(str, "text"))
45 return ElementType::TEXT;
46 if (String::EqualToLower(str, "msg_box"))
48 if (String::EqualToLower(str, "button"))
50 if (String::EqualToLower(str, "background"))
52 if (String::EqualToLower(str, "locator"))
54 if (String::EqualToLower(str, "list_box"))
56
57 log_err("未知的type字段:{}", str);
59}
60
61class EditBox;
62
64class Panel
65{
66 friend class Manager;
67public:
69 {
70 E_SIZE = 1 << 0,
71 E_ELEMENT = 1 << 1,
72 E_CHILD = 1 << 2,
73 };
75 {
79 };
80
84 template<typename T>
85 void Add(T* p)
86 {
87 AddCpt(p->GetCpt());
88 }
89
90 void Add(Text* p) { AddCpt(p->GetCpt()); }
91
95 void AddCpt(Components* cpt);
99 void Add(std::string_view json_path);
103 void AddPanel(Panel* child);
107 Panel* Clone();
108
112 void SetPosPixel(const Position& pos_pixel)
113 {
114 if (!_bAutoCoor)
115 {
116 log_err0("关闭了自动设置位置,设置像素位置无效!");
117 return;
118 }
119 _posPixel = pos_pixel;
120 _coor->SetPosition(_posPixel - Align::Size2Anchor(GetSize(), _align));
121 }
122 void DtPosPixel(const Position& dt)
123 {
124 if (!_bAutoCoor)
125 {
126 log_err0("关闭了自动设置位置,设置像素位置无效!");
127 return;
128 }
129 _posPixel += dt;
130 _coor->DtPosition(dt);
131 }
132
135 void SetPosScale(const Position& pos_scale)
136 {
137 if (!_bAutoCoor)
138 {
139 log_err0("关闭了自动设置位置,设置比例位置无效!");
140 return;
141 }
142 _posScale = pos_scale;
143 _bNeedRefreshPos = true;
144 }
145
149 {
150 _align = hv;
151 _bNeedRefreshPos = true;
152 }
153
157 void SetLocator(const Locator& loc)
158 {
159 _locator = loc;
160 _bNeedRefreshLocator = true;
161 }
162 void SetLocator(const Locator::SL& sl)
163 {
164 SetLocator(Locator{ sl });
165 }
166 void SetLocator(std::string_view name);
171 void SetAutoCoor(bool auto_coor) { _bAutoCoor = auto_coor; }
176 void SetAutoRun(bool auto_run) { _bAutoRun = auto_run; }
181 void SetAutoRender(bool auto_render) { _bAutoRender = auto_render; }
187 {
188 _orderMode = mode;
189 _refresh_order();
190 }
191
195 void SetOrder(float order)
196 {
197 assert(_orderMode == OrderMode::FORCE);
198 if (_orderMode == OrderMode::FORCE)
199 {
200 _order = order;
201 _refresh_order();
202 }
203 }
204
208 void SetOrderInParent(int order_parent)
209 {
210 assert(_orderMode == OrderMode::CUSTOM);
211 if (_orderMode == OrderMode::CUSTOM)
212 {
213 _orderInParent = order_parent;
214 _refresh_order();
215 }
216 }
218 {
219 _refresh_order();
220 }
221
224 void SetName(std::string_view name)
225 {
226 _name = name;
227 }
228
232 void SetSelect(size_t index)
233 {
234 size_t i = 0;
235 for (auto& iter : _allEle)
236 {
237 if (auto comp = iter->Get<Cpt::SetSelect>())
238 {
239 comp->Call(i == index);
240 }
241 ++i;
242 }
243 }
244
247 void ElementSetSelect(size_t index, bool select)
248 {
249 if (index >= _allEle.size())
250 log_err0("越界!");
251 if (auto comp = _allEle[index]->Get<Cpt::SetSelect>())
252 {
253 comp->Call(select);
254 return;
255 }
256 log_err0("对象没有SetSelect组件!");
257 }
258
261 void SetSelectAll(bool select)
262 {
263 for (auto& iter : _allEle)
264 {
265 if (auto comp = iter->Get<Cpt::SetSelect>())
266 {
267 comp->Call(select);
268 }
269 }
270 }
271
275 void Update();
282 void Render();
291 bool IsUpdate() { return _bUpdate; }
295 bool IsRender() { return _bRender; }
299 std::string GetName() const { return _name; }
305 {
306 assert(!_bAutoCoor);
307 return _coor;
308 }
310 {
311 return _coor;
312 }
313
316 Panel* GetParent()
317 {
318 if(_node->GetParent())
319 return _node->GetParent()->GetValue();
320 return nullptr;
321 }
322
325 std::vector<Panel*> GetChildAll()
326 {
327 std::vector<Panel*> ret;
328 for (auto& iter : _node->GetChild())
329 ret.push_back(iter->GetValue());
330 return ret;
331 }
332
335 size_t GetChildNum(bool recursion = false)
336 {
337 size_t ret = _node->GetChild().size();
338 if (recursion)
339 {
340 for (auto& iter : _node->GetChild())
341 ret += iter->GetValue()->GetChildNum(true);
342 }
343 return ret;
344 }
345
348 size_t GetChildIndex(Panel* child)
349 {
350 size_t i = 0;
351 for (auto& iter : _node->GetChild())
352 {
353 if (iter->GetValue() == child)
354 return i;
355 ++i;
356 }
357 return -1;
358 }
359
363 Panel* GetChild(size_t i)
364 {
365 auto& vec = _node->GetChild();
366 if (i >= vec.size())
367 {
368 log_err("下标越界!");
369 return nullptr;
370 }
371 return vec[i]->GetValue();
372 }
373
381
383 template<typename T>
384 std::size_t GetIndex()
385 {
386 size_t i = 0;
387 for (auto& iter : _allEle)
388 {
389 if (auto comp = iter->Get<T>())
390 {
391 if (comp->Call())
392 return i;
393 }
394 ++i;
395 }
396 return -1;
397 }
398
399 template<typename T>
401 {
402 for (auto& iter : _allEle)
403 {
404 if (auto comp = iter->Get<T>())
405 {
406 if (comp->Call())
407 return iter;
408 }
409 }
410 return nullptr;
411 }
412
418 {
419 return GetIndex<Cpt::IsClick>();
420 }
421
426 {
427 return GetIndex<Cpt::IsDown>();
428 }
429
435 {
437 }
438
444 {
445 return Get<Cpt::IsSelect>();
446 }
447
448 std::string GetSelectName()
449 {
450 Components* cpt = GetSelect();
451 return cpt ? cpt->GetName() : "";
452 }
453
458 bool IsSelectIndex(size_t index)
459 {
460 if (index >= _allEle.size())
461 {
462 log_err0("越界!");
463 return false;
464 }
465 if (auto comp = _allEle[index]->Get<Cpt::IsSelect>())
466 {
467 return comp->Call();
468 }
469
470 log_err0("对象没有Open组件!");
471 return false;
472 }
473
478 {
479 if (_bSizeAuto)
480 return _locator.GetSize();
481 return _size;
482 }
483
487
489 {
490 return _coor->GetPosition();
491 }
492
494 {
495 return _locator;
496 }
497
501 template<typename T>
502 T* GetElement(size_t n = 0)
503 {
504 if(n == 0)
505 return Cpt::GetFirst<T>(_allEle);
506 else
507 return Cpt::GetN<T>(_allEle, n);
508 }
509
511 template<typename T>
512 std::vector<T*> GetElementVector()
513 {
514 std::vector<T*> ret;
515 for (auto& iter : _allEle)
516 {
517 T* cpt = iter->Cast<T>();
518 if (cpt)
519 ret.push_back(cpt);
520 }
521 return ret;
522 }
523
525 bool IsPickup()
526 {
527 if (_pickupMode & E_SIZE)
528 {
529 if (g_factory->GetOrderPickup() < _order)
530 return false;
531 Position pos = _coor->WorldToThis(toPosition(g_input->GetCursorPosition()));
532 Position size = toPosition(GetSize());
533 if (pos[0] >= 0
534 && pos[1] >= 0
535 && pos[0] <= size[0]
536 && pos[1] <= size[1])
537 return true;
538 }
539 if (_pickupMode & E_ELEMENT)
540 {
541 for (Components* cpt : _allEle)
542 {
543 Cpt::IsPickup* comp = cpt->Get<Cpt::IsPickup>();
544 if (!comp)
545 continue;
546 if (comp->Call())
547 return true;
548 }
549 }
550 if (_pickupMode & E_CHILD)
551 {
552 for (auto& iter : _node->GetChild())
553 {
554 Panel* child = iter->GetValue();
555 if (child->IsPickup())
556 return true;
557 }
558 }
559 return false;
560 }
561
562 // 指定大小
563 void SetSize(const Position& size)
564 {
565 _size = size;
566 _bSizeAuto = false;
567 _bSizeFull = false;
568 _refresh_size();
569 }
570 // 设置拾取模式
572 {
573 _pickupMode = mode;
574 }
575 // 与父节点一样大
577 {
578 _bSizeFull = true;
579 _refresh_size();
580 }
581
583
584 bool IsAutoRun() { return _bAutoRun; }
585 //----------------------------------辅助函数-----------------------------
587 UI::ButtonSprite* ExGetButtonSprite(size_t n = 0, bool show_log = true);
589 UI::EditBox* ExGetEditBox(size_t n = 0, bool show_log = true);
591 UI::Text* ExGetText(size_t n = 0, bool show_log = true);
593 void ExSetString(std::string_view str, size_t n = 0);
594
595 void SetDebugName(std::string_view name)
596 {
597#ifndef NDEBUG
598 _debugName = name;
599#endif
600 }
601private:
602 Panel() :
603 _order{ Z_DEFAULT_PANEL },
604 _orderMode{ OrderMode::AUTO },
605 _orderInParent{ -1 },
606 _bAutoRun{ true },
607 _bAutoRender{ true },
608 _bNeedRefreshPos{ false },
609 _bNeedRefreshLocator{ false },
610 _bDisableLocator{ false },
611 _bAutoCoor{ true },
612 _bUpdate{ false },
613 _bRender{ false },
614 _pickupMode{ E_SIZE },
615 _align{ Align::LT },
616 _posPixel{},
617 _posScale{},
618 _size{},
619 _bSizeFull{ true },
620 _bSizeAuto{ false }
621 {
622 _coor = Coor::Create();
623 }
624 ~Panel()
625 {
626 //删除元素
627 for (Components*& cpt : _allEle)
628 {
629 if (cpt->IsPure())
630 delete cpt;
631 else if (auto comp = cpt->Get<Cpt::DeleteThis>())
632 {
633 comp->Call();
634 }
635
636 }
637 delete _coor;
638 }
639#ifndef NDEBUG
640 std::string _debugName;
641#endif
642
643 // 添加到 Scene时自动设置
644 std::string _name;
645 std::vector<Components*> _allEle;// 子元素
646
647 Coor* _coor;
648 // 全局偏移(受 order模式控制)
649 float _order;
650 OrderMode _orderMode;
651 int _orderInParent;
652
653 bool _bAutoRun;// 自动运行(由父节点调用Update和Render)
654 bool _bAutoRender;// 如果调用了Update,则自动调用Render(由父节点)
655 bool _bNeedRefreshPos; // 更新位置
656 bool _bNeedRefreshLocator; // 重新定位
657 bool _bDisableLocator;// 禁用定位器,暂未使用
658 bool _bAutoCoor;// 接手Coor后,需要设为真
659 bool _bUpdate;// 此帧是否已运行
660 bool _bRender;// 此帧是否已绘制
661 PickupMode _pickupMode;
662
663 //树节点引用
664 NTree<Panel*>::Node* _node;
665
666 //location
667 Align::Direction2 _align; // 在父节点的对齐
668 Position _posPixel;
669 Position _posScale;
670
671 //locator
672 Locator _locator;
673
674 Position _size; // 指定大小
675 bool _bSizeFull;// 与父节点同大小
676 bool _bSizeAuto; // 自动大小
677
678 void _refresh_lang()
679 {
680
681 }
682
684 void _refresh_pos(const Position& size);
685
687 void _refresh_size();
689 void _refresh_order();
690
691 void _update_child();
692 void _render_child();
693
694 void _update_cpt_drag(Components* cpt);
695 void _update_cpt_radio(Components* cpt);
696};
697
698}
699
700#endif
std::string GetName()
2d坐标系
static Coor * Create(Coor *parent=nullptr)
2d定位器
精灵按钮(注意精灵没有Coor)
void DtPosPixel(const Position &dt)
UI::ButtonSprite * ExGetButtonSprite(size_t n=0, bool show_log=true)
返回第n个按钮
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内容
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 UpdateRadio()
执行逻辑Radio @node 手动调用,把子panel作为radio处理
Panel * Clone()
克隆
std::string GetName() const
返回名字
size_t GetChildIndex(Panel *child)
返回子panel下标
void SetAlign(const Align::Direction2 &hv)
设置 在父节点的对齐
void SetLocator(std::string_view name)
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(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之文本
方向、对齐定义
std::array< Direction, 2 > Direction2
constexpr std::array< T, N > Size2Anchor(const std::array< T, N > &size, const std::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
Input * g_input
Factory * g_factory
Position4 RectF
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
转换到Position,已知长度
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr float Z_DEFAULT_PANEL