dl
dl_component.h
浏览该文件的文档.
1
17#pragma once
18
19#include <typeinfo>
20#include <typeindex>
21#include <string>
22#include <vector>
23#include <array>
24#include <functional>
25
26#include "dl_type.h"
27#include "math/dl_align.h"
28#include "math/dl_coor.h"
29
30namespace dl
31{
33namespace Cpt
34{
36 template<typename T, uint64_t UID>
38 {
39 public:
40 void Set(T v) { _v = v; }
41 T Get() { return _v; }
42
43 operator bool() { return _v != 0; }
44 Variable(T v) : _v(v) {}
46 private:
47 T _v;
48 };
49
52 template<typename T, uint64_t UID>
54 {
55 public:
56 Function() = default;
57 void operator=(const T& func) { Call = func; }
59 };
60}
61
64{
65 friend class VectorAny;
66public:
67 bool operator==(const AnyType& b) { return _p == b._p; }
68 bool operator!=(const AnyType& b) { return _id != b._id || _p != b._p; }
69
70 //默认构造id为自己
72 _p{nullptr},
73 _id{ typeid(AnyType) }
74 {}
75
76 template<typename T>
78 _id{ typeid(T) }
79 {
80 _p = p;
81#ifndef NDEBUG
82 _nameDebug = typeid(T).name();
83#endif
84 }
85
90 template<typename T>
92 {
93 if (std::type_index(typeid(T)) == _id)
94 {
95 return (T*)_p;
96 }
97 else
98 return nullptr;
99 }
100
104 std::type_index GetId()
105 {
106 return _id;
107 }
108
112 bool IsNull()
113 {
114 return !_p;
115 }
116
117 std::string GetNameDebug()
118 {
119#ifndef NDEBUG
120 return _nameDebug;
121#else
122 return "Debug版本才有此字段!";
123#endif
124 }
125
126private:
127 void* _p;
128 std::type_index _id;
129#ifndef NDEBUG
130 std::string _nameDebug;
131#endif
132};
133
134
136{
137public:
143 {
144 _vec.push_back(any);
145 }
146
147 void Clear()
148 {
149 _vec.clear();
150 }
151
155 template<typename T>
156 std::vector<T*> GetMulti()
157 {
158 std::vector<T*> ret;
159 for (auto& iter : _vec)
160 {
161 if (iter._id == std::type_index(typeid(T)))
162 {
163 ret.push_back((T*)iter._p);
164 }
165 }
166
167 return ret;
168 }
169
173 template<typename T>
174 T* Get(size_t index = 0)
175 {
176 for (auto& iter : _vec)
177 {
178 if (iter._id == std::type_index(typeid(T)))
179 {
180 if (index == 0)
181 return (T*)(iter._p);
182 else
183 --index;
184 }
185 }
186 return nullptr;
187 }
188
192 auto& GetVector()
193 {
194 return _vec;
195 }
196
197 std::vector<AnyType>::iterator begin() { return _vec.begin(); }
198
199 std::vector<AnyType>::iterator end() { return _vec.end(); }
200
201 size_t GetSize() { return _vec.size(); }
202
203private:
204 std::vector<AnyType> _vec;
205};
206
210{
211public:
212 template <typename T>
213 Components(T* p) : _any(p)
214 {
215#ifndef NDEBUG
216 _nameDebug = _any.GetNameDebug();
217#endif
218 }
219
220 bool operator==(const Components& b) { return _any == b._any; }
221 bool operator!=(const Components& b) { return _any != b._any; }
222
227 template <typename T>
228 T* Cast() { return _any.Cast<T>(); }
229
233 template <typename T>
234 T* Get(size_t index = 0) { return _vec.Get<T>(index); }
235
238 {
239 return _any;
240 }
241
247 {
248 _vec.PushBack(any);
249 }
250
251 void Clear()
252 {
253 _vec.Clear();
254 }
255
256 VectorAny& GetVectorAny() { return _vec; }
257
258 std::string GetName()
259 {
260 return _name;
261 }
262
264 void SetName(std::string_view name)
265 {
266 _name = name;
267 }
268
270 bool IsPure()
271 {
272 return _any.IsNull();
273 }
274private:
275 VectorAny _vec;
276 AnyType _any;
277 std::string _name;
278#ifndef NDEBUG
279 std::string _nameDebug;
280#endif
281};
282
283inline Components* createComponents(std::string_view name = {})
284{
285 Components* ret = new Components((int*)nullptr);
286 ret->SetName(name);
287 return ret;
288}
289
290namespace Cpt
291{
295 template<typename T>
296 std::vector<T*> GetComp(std::vector<Components*>& vec)
297 {
298 std::vector<T*> ret;
299 for (Components* cpt : vec)
300 {
301 VectorAny& vec_any = cpt->GetVectorAny();
302 std::vector<T*> mul = vec_any.GetMulti<T>();
303 ret.insert(ret.end(), mul.begin(), mul.end());
304 }
305 return ret;
306 }
307
311 template<typename T>
312 T* GetFirst(std::vector<Components*>& vec)
313 {
314 for (Components* cpt : vec)
315 {
316 T* p = cpt->GetSelf().Cast<T>();
317 if (p)
318 return p;
319 }
320 return nullptr;
321 }
322
325 template<typename T>
326 T* GetN(std::vector<Components*>& vec, size_t n)
327 {
328 for (Components* cpt : vec)
329 {
330 T* p = cpt->GetSelf().Cast<T>();
331 if(!p || n--)
332 continue;
333 return p;
334 }
335 return nullptr;
336 }
337
338
340 using IsPickup = Function<std::function<bool()>, str_uid("IsPickup")>;
341 using IsSelect = Function<std::function<bool()>, str_uid("IsSelect")>;
342 using IsClick = Function<std::function<bool()>, str_uid("IsClick")>;
343 using IsDown = Function<std::function<bool()>, str_uid("IsDown")>;
344 using GetCoor = Function<std::function<dl::Coor* ()>, str_uid("GetCoor")>;
345 using GetOrder = Function<std::function<float()>, str_uid("GetOrder")>;
346 using Update = Function<std::function<void()>, str_uid("Update")>;
347 using Render = Function<std::function<void()>, str_uid("Render")>;
348 using SetOrder = Function<std::function<void(float)>, str_uid("SetOrder")>;
349 using SetSelect = Function<std::function<void(bool)>, str_uid("SetSelect")>;
350 using DeleteThis = Function<std::function<void()>, str_uid("DeleteThis")>;
351 using Clone = Function<std::function<Components* ()>, str_uid("Clone")>;
352
354 class Lang
355 {
356 public:
357 std::array<std::string, 2> _str;
358 std::function<void(std::string_view, std::string_view)> _func;
359 };
360
363 {
364 public:
366 _size{},
367 _anchor{},
368 _hv{ Align::LT },
369 _orderOffset{ 0 },
370 _bRunAble{true},
371 _bUpdateAble{true},
372 _bRenderAble{true}
373 {}
374
376 {
377 return -(Align::Size2Anchor(_size, _hv) + _anchor);
378 }
379
382 {
383 return _size;
384 }
385
387 {
388 return _anchor;
389 }
390
392 {
393 return _orderOffset;
394 }
395 public:
400
401 bool _bRunAble; // 可运行
402 bool _bUpdateAble; // 可更新
403 bool _bRenderAble; // 可绘制
404 };
405}
406
407}
任意类型包装
std::type_index GetId()
返回类型id
T * Cast()
试图转到指定类型
bool operator==(const AnyType &b)
bool operator!=(const AnyType &b)
std::string GetNameDebug()
bool IsNull()
是否为空
friend class VectorAny
T * Cast()
试图转到指定类型
T * Get(size_t index=0)
返回第n个类型对象,失败返回nullptr
void SetName(std::string_view name)
设置自定义名字
AnyType GetSelf()
返回自身
bool operator==(const Components &b)
bool operator!=(const Components &b)
std::string GetName()
void PushBack(AnyType any)
添加一个任意对象
VectorAny & GetVectorAny()
bool IsPure()
单纯的Componets,any为空
2d坐标系
Position GetSize()
自身大小
Position GetAnchor()
锚点位置
int GetOrderOffset()
order偏移
Position GetOffset()
位置偏移(实际coor位置)
void operator=(const T &func)
Function()=default
语言本地化
std::array< std::string, 2 > _str
std::function< void(std::string_view, std::string_view)> _func
auto & GetVector()
返回所有对象
T * Get(size_t index=0)
返回第n个类型对象,失败返回nullptr
std::vector< AnyType >::iterator begin()
void PushBack(AnyType any)
添加一个任意对象
std::vector< T * > GetMulti()
返回所有指定类型的对象
std::vector< AnyType >::iterator end()
方向和对齐定义
2d坐标系
通用类型
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("IsSelect")> IsSelect
选择
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< bool()>, str_uid("IsDown")> IsDown
按下状态
Function< std::function< float()>, str_uid("GetOrder")> GetOrder
返回order
Function< std::function< void(bool)>, str_uid("SetSelect")> SetSelect
设置是否选择
Function< std::function< void()>, str_uid("Render")> Render
绘制
T * GetN(std::vector< Components * > &vec, size_t n)
从vector<Components*>返回指定类型的第n个元素
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
Function< std::function< void()>, str_uid("Update")> Update
逻辑
Function< std::function< bool()>, str_uid("IsClick")> IsClick
点击
Function< std::function< Components *()>, str_uid("Clone")> Clone
克隆
std::vector< T * > GetComp(std::vector< Components * > &vec)
从vector<Components*>返回指定类型的所有组件
T * GetFirst(std::vector< Components * > &vec)
从vector<Components*>返回指定类型的第一个元素
Position2 Position
consteval uint64_t str_uid(const char(&str)[N])
固定条件的字符串,生成唯一id
Components * createComponents(std::string_view name={})