dl
dl_ui_tree.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_RENDER
17#include "ui/dl_ui_background.h"
18#include "ui/dl_ui_panel.h"
19
20namespace dl::UI
21{
22
24class Tree
25{
26public:
27 template<typename T>
28 using CbSetContent = std::function<void(UI::Panel* panel, const T& v)>;
29 using CbSetPosition = std::function<void(UI::Panel* panel, size_t y, size_t extra)>;
30
36 static Tree* Create(Panel* item)
37 {
38 Tree* ret = new Tree;
39 ret->_item = item;
40 ret->_init();
41 return ret;
42 }
43
49 {
50 _fnSetPosition = func;
51 }
52
53 void Update()
54 {
55 _panel->Update();
56 }
57
58 void Render()
59 {
60 }
61
66 template<typename T>
67 void Refresh(const NTree<T>& tree, CbSetContent<T> cb)
68 {
69 _delete_panel_child(_panel);
70
71 using TreeNode = typename NTree<T>::Node;
72 const TreeNode* p = tree.GetRoot();
73 if (!p)
74 return;
75 std::function<void(const TreeNode*, UI::Panel* parent)> fn_refresh =
76 [&](const TreeNode* node, UI::Panel* parent) {
77
78 UI::Panel* panel = _item->Clone();
79 panel->SetAutoRun(true);
80 _set_panel_parent(panel, parent);
81 cb(panel, node->GetValue());
82
83 const std::vector<TreeNode*>& vec = node->GetChild();
84 size_t j = 0;
85 for (const TreeNode* child : node->GetChild())
86 {
87 fn_refresh(child, panel);
88 }
89 };
90 fn_refresh(p, _panel);
91
92 _refresh_pos();
93 }
94
95 void Clear();
96
97 bool IsPickup()
98 {
99 return _panel->IsPickup();
100 }
101
105 std::size_t IsSelectChange()
106 {
107 /*if (_bSelectChange)
108 return _curSelect;*/
109 return -1;
110 }
111
115 std::size_t GetSelectIndex()
116 {
117 return 0;
118 }
119
123 {
124 return nullptr;
125 }
126
128 void SetOrder(float order)
129 {
130 _panel->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
131 /*for (auto iter : _treePanel)
132 {
133 iter->GetValue()->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
134 }*/
135 }
136
137
139 void SetSize(const Position& size)
140 {
141 _panel->SetSize(size);
142 _cptElemData._size = size;
143 }
144
145 Coor* GetCoor() { return _panel->GetCoor(); }
146
147 Components* GetCpt() { return &_cpt; }
148
149
151 {
152 }
153private:
154 Tree() :
155 _cpt{ this }
156 {
157 _init_cpt();
158 }
159
160 Panel* _panel;
161 Panel* _item;
162
163 CbSetPosition _fnSetPosition;
164
165 Components _cpt;
166 Cpt::DeleteThis _cptDeleteThis;
167 Cpt::SetOrder _cptSetOrder;
168 Cpt::Update _cptUpdate;
169 Cpt::Render _cptRender;
170 Cpt::GetCoor _cptGetCoor;
171 Cpt::ElementData _cptElemData;
172
173 void _delete_this() { delete this; }
174
175 void _init_cpt()
176 {
177 _cptDeleteThis = std::bind(&Tree::_delete_this, this);
178 _cptSetOrder = std::bind(&Tree::SetOrder, this, std::placeholders::_1);
179 _cptUpdate = std::bind(&Tree::Update, this);
180 _cptRender = std::bind(&Tree::Render, this);
181 _cptGetCoor = std::bind(&Tree::GetCoor, this);
182
183 _cpt.PushBack(&_cptDeleteThis);
184 _cpt.PushBack(&_cptSetOrder);
185 _cpt.PushBack(&_cptUpdate);
186 _cpt.PushBack(&_cptRender);
187 _cpt.PushBack(&_cptGetCoor);
188
189 _cpt.PushBack(&_cptElemData);
190 }
191
192 void _init();
193 void _set_panel_parent(UI::Panel* panel, UI::Panel* parent);
194 void _delete_panel_child(UI::Panel* panel);
195
196 void _refresh_pos();
197};
198}
199
200#endif
void PushBack(AnyType any)
添加一个任意对象
2d坐标系
元素通用属性(默认左上对齐,便于理解)
N叉树
const Node * GetRoot() const
返回根节点,空树返回空
void SetAutoRun(bool auto_run)
设置是否自动执行
Panel * Clone()
克隆
std::size_t IsSelectChange()
选中状态变更
static Tree * Create(Panel *item)
创建列表框
std::size_t GetSelectIndex()
返回选中下标
std::function< void(UI::Panel *panel, size_t y, size_t extra)> CbSetPosition
Panel * GetSelect()
返回选中元素
void SetCallbackSetPosition(CbSetPosition func)
设置位置回调
void SetSize(const Position &size)
设置大小
void Refresh(const NTree< T > &tree, CbSetContent< T > cb)
按树构建ui
std::function< void(UI::Panel *panel, const T &v)> CbSetContent
Components * GetCpt()
void SetOrder(float order)
设置绘制顺序
组件实例
作为绘图背景
ui之panel
Function< std::function< void(float)>, str_uid("SetOrder")> SetOrder
设置order
Function< std::function< dl::Coor *()>, str_uid("GetCoor")> GetCoor
返回坐标系
Function< std::function< void()>, str_uid("Render")> Render
绘制
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
Function< std::function< void()>, str_uid("Update")> Update
逻辑
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Position2 Position