dl
dl_ui_background.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_RENDER
16#include "render/dl_sprite.h"
17#include "render/dl_sprite9.h"
18#include "misc/dl_component.h"
19
20namespace dl::UI
21{
23class Background
24{
25public:
26 enum class Type
27 {
31 };
32
33 static Background* Create()
34 {
35 Background* ret = new Background;
36 ret->_coor = Coor::Create();
37 return ret;
38 }
39
40 Background* Clone()
41 {
42 Background* ret = new Background;
43 ret->_coor = _coor->Clone();
44 switch (_ui._type)
45 {
46 case Type::SPRITE:
47 ret->_ui._spr = g_factory->CloneSprite(_ui._spr);
48 ret->_ui._spr->SetCoor(ret->_coor);
49 break;
50 case Type::SPRITE9:
51 ret->_ui._spr9 = _ui._spr9->Clone();
52 ret->_ui._spr9->GetCoor()->SetParent(ret->_coor);
53 break;
54 default:
55 break;
56 }
57 ret->_ui._type = _ui._type;
58 ret->_extraSize = _extraSize;
59 ret->_offset = _offset;
60 return ret;
61 }
62
63 void Set(gcSprite spr)
64 {
65 Clear();
66 _ui._spr = spr;
67 _ui._type = Type::SPRITE;
68
69 _ui._spr->SetCoor(_coor);
70 }
71 void Set(Sprite9* spr9)
72 {
73 Clear();
74 _ui._spr9 = spr9;
75 _ui._type = Type::SPRITE9;
76
77 _ui._spr9->GetCoor()->SetParent(_coor);
78 }
79
80 void Clear()
81 {
82 switch (_ui._type)
83 {
84 case Type::SPRITE:
85 break;
86 case Type::SPRITE9:
87 delete _ui._spr9;
88 _ui._spr9 = nullptr;
89 break;
90 default:
91 break;
92 }
93 _ui._type = Type::NONE;
94 }
95
97
98 void SetSize(const Size& size);
102 void SetOffset(const Point& offset)
103 {
104 _offset = offset;
105 }
107 {
108 return _offset;
109 }
110
114 void SetExtra(const Point& extra)
115 {
116 _extraSize = extra;
117 }
119 {
120 return _extraSize;
121 }
122
123
125 {
126 return _coor;
127 }
128
129 bool IsPickup()
130 {
131 switch (_ui._type)
132 {
133 case Type::SPRITE:
134 return _ui._spr->IsPickup();
135 case Type::SPRITE9:
136 return _ui._spr9->IsPickup();
137 default:
138 return false;
139 }
140 }
141
142 void Render()
143 {
144 switch (_ui._type)
145 {
146 case Type::SPRITE:
147 _ui._spr->Render();
148 break;
149 case Type::SPRITE9:
150 _ui._spr9->Render();
151 break;
152 default:
153 break;
154 }
155 }
156
157 void SetOrder(float order)
158 {
159 switch (_ui._type)
160 {
161 case Type::SPRITE:
162 _ui._spr->SetOrder(order);
163 break;
164 case Type::SPRITE9:
165 _ui._spr9->SetOrder(order);
166 break;
167 default:
168 break;
169 }
170 }
171
172 void SetUI(bool ui)
173 {
174 switch (_ui._type)
175 {
176 case Type::SPRITE:
177 _ui._spr->SetUI(ui);
178 break;
179 case Type::SPRITE9:
180 _ui._spr9->SetUI(ui);
181 break;
182 default:
183 break;
184 }
185 }
186
187 Components* GetCpt() { return &_cpt; }
188
190 {
191 delete _ui._spr9;
192 delete _coor;
193 }
194private:
195 Background() :
196 _cpt{ this },
197 _extraSize{},
198 _offset{}
199 {
200 _init_cpt();
201 }
202 Coor* _coor;
203
204 Components _cpt;
205 Cpt::DeleteThis _cptDeleteThis;
206 Cpt::SetOrder _cptSetOrder;
207 Cpt::Render _cptRender;
208 Cpt::IsPickup _cptIsPickup;
209 Cpt::GetCoor _cptGetCoor;
210 Cpt::ElementData _cptElemData;
211
212 void _delete_this() { delete this; }
213
214 void _init_cpt()
215 {
216 _cptDeleteThis = std::bind(&Background::_delete_this, this);
217 _cptSetOrder = std::bind(&Background::SetOrder, this, std::placeholders::_1);
218 _cptIsPickup = std::bind(&Background::IsPickup, this);
219 _cptRender = std::bind(&Background::Render, this);
220 _cptGetCoor = std::bind(&Background::GetCoor, this);
221
222 _cpt.PushBack(&_cptDeleteThis);
223 _cpt.PushBack(&_cptSetOrder);
224 _cpt.PushBack(&_cptIsPickup);
225 _cpt.PushBack(&_cptRender);
226 _cpt.PushBack(&_cptGetCoor);
227
228 _cpt.PushBack(&_cptElemData);
229 }
230
231 struct UI
232 {
234 gcSprite _spr;
235 Sprite9* _spr9 = nullptr;
236 }_ui;
237
238 Point _extraSize;
239 Point _offset;
240};
241
242
243}
244
245
246#endif
void PushBack(AnyType any)
添加一个任意对象
2d坐标系
Coor * Clone()
void SetParent(Coor *coor)
static Coor * Create(Coor *parent=nullptr)
元素通用属性(默认左上对齐,便于理解)
九图精灵
Coor * GetCoor()
返回坐标系
Sprite9 * Clone()
void SetCoor(Coor *coor, bool own=false)
void Set(Sprite9 *spr9)
void SetOffset(const Point &offset)
设置偏移
static Background * Create()
void SetSize(const Size &size)
void SetExtra(const Point &extra)
设置额外大小
组件系统
九图精灵
2d绘图元素
Function< std::function< void(float)>, str_uid("SetOrder")> SetOrder
设置order
Function< std::function< dl::Coor *()>, str_uid("GetCoor")> GetCoor
返回坐标系
Function< std::function< bool()>, str_uid("IsPickup")> IsPickup
---------------------------—具体组件----------------------------------—
Function< std::function< void()>, str_uid("Render")> Render
绘制
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
Factory * g_factory
GC< Sprite > gcSprite
PointU Size
Point2 Point