dl
dl_component.h
浏览该文件的文档.
1
15#pragma once
16
17#include <typeinfo>
18#include <typeindex>
19#include <string>
20#include <vector>
21#include <array>
22#include <functional>
23
24#include "dl_type.h"
25#include "dl_align.h"
26#include "math/dl_coor.h"
27
28namespace dl
29{
31namespace Cpt
32{
34 template<typename T, uint64_t UID>
36 {
37 public:
38 void Set(T v) { _v = v; }
39 T Get() { return _v; }
40
41 operator bool() { return _v != 0; }
42 Variable(T v) : _v(v) {}
44 private:
45 T _v;
46 };
47
50 template<typename T, uint64_t UID>
52 {
53 public:
54 Function() = default;
55 void operator=(const T& func) { Call = func; }
57 };
58}
59
62{
63 friend class VectorAny;
64public:
65 bool operator==(const AnyType& b) { return _p == b._p; }
66 bool operator!=(const AnyType& b) { return _id != b._id || _p != b._p; }
67
68 //默认构造id为自己
70 _p{nullptr},
71 _id{ typeid(AnyType) }
72 {}
73
74 template<typename T>
76 _id{ typeid(T) }
77 {
78 _p = p;
79#ifndef NDEBUG
80 _name = typeid(T).name();
81#endif
82 }
83
88 template<typename T>
90 {
91 if (std::type_index(typeid(T)) == _id)
92 {
93 return (T*)_p;
94 }
95 else
96 return nullptr;
97 }
98
99 std::string GetName()
100 {
101#ifndef NDEBUG
102 return _name;
103#else
104 return "Debug版本才有此字段!";
105#endif
106 }
107
108private:
109 void* _p;
110 std::type_index _id;
111#ifndef NDEBUG
112 std::string _name;
113#endif
114};
115
116
118{
119public:
125 {
126 _vec.push_back(any);
127 }
128
129 void Clear()
130 {
131 _vec.clear();
132 }
133
137 template<typename T>
138 std::vector<T*> GetMulti()
139 {
140 std::vector<T*> ret;
141 for (auto& iter : _vec)
142 {
143 if (iter._id == std::type_index(typeid(T)))
144 {
145 ret.push_back((T*)iter._p);
146 }
147 }
148
149 return ret;
150 }
151
155 template<typename T>
156 T* Get(size_t index = 0)
157 {
158 for (auto& iter : _vec)
159 {
160 if (iter._id == std::type_index(typeid(T)))
161 {
162 if (index == 0)
163 return (T*)(iter._p);
164 else
165 --index;
166 }
167 }
168 return nullptr;
169 }
170
174 auto& GetVector()
175 {
176 return _vec;
177 }
178
179 std::vector<AnyType>::iterator begin() { return _vec.begin(); }
180
181 std::vector<AnyType>::iterator end() { return _vec.end(); }
182
183 size_t GetSize() { return _vec.size(); }
184
185private:
186 std::vector<AnyType> _vec;
187};
188
191{
192public:
193 template <typename T>
194 Components(T* p) : _any(p){}
195
196 bool operator==(const Components& b) { return _any == b._any; }
197 bool operator!=(const Components& b) { return _any != b._any; }
198
203 template <typename T>
204 T* Cast() { return _any.Cast<T>(); }
205
209 template <typename T>
210 T* Get(size_t index = 0) { return _vec.Get<T>(index); }
211
217 {
218 _vec.PushBack(any);
219 }
220
221 void Clear()
222 {
223 _vec.Clear();
224 }
225
226 VectorAny& GetVectorAny() { return _vec; }
227
228private:
229 AnyType _any;
230 VectorAny _vec;
231};
232
233namespace Cpt
234{
238 template<typename T>
239 std::vector<T*> Get(std::vector<Components*>& vec)
240 {
241 std::vector<T*> ret;
242 for (Components* cpts : vec)
243 {
244 VectorAny& vec_any = cpts->GetVectorAny();
245 std::vector<T*> mul = vec_any.GetMulti<T>();
246 ret.insert(ret.end(), mul.begin(), mul.end());
247 }
248 return ret;
249 }
250
251
253 using IsPickup = Function<std::function<bool()>, str_uid("IsPickup")>;
254 using IsOpen = Function<std::function<bool()>, str_uid("IsOpen")>;
255 using IsClick = Function<std::function<bool()>, str_uid("IsClick")>;
256 using GetCoor = Function<std::function<dl::Coor* ()>, str_uid("GetCoor")>;
257 using GetOrder = Function<std::function<float()>, str_uid("GetOrder")>;
258 using Update = Function<std::function<void()>, str_uid("Update")>;
259 using Render = Function<std::function<void()>, str_uid("Render")>;
260 using SetOrder = Function<std::function<void(float)>, str_uid("SetOrder")>;
261 using DeleteThis = Function<std::function<void()>, str_uid("DeleteThis")>;
262
264 class Lang
265 {
266 public:
267 std::array<std::string, 2> _str;
268 std::function<void(std::string_view, std::string_view)> _func;
269 };
270
273 {
274 public:
276 _size{},
277 _anchor{},
279 {}
280
282 {
283 return -Align::Size2Anchor(_size, _hv) + _anchor;
284 }
285 public:
289 };
290}
291
292}
任意类型包装
Definition: dl_component.h:62
AnyType(T *p)
Definition: dl_component.h:75
T * Cast()
试图转到指定类型
Definition: dl_component.h:89
std::string GetName()
Definition: dl_component.h:99
bool operator==(const AnyType &b)
Definition: dl_component.h:65
bool operator!=(const AnyType &b)
Definition: dl_component.h:66
组件容器还需表示对象自己(比如UI::Text)
Definition: dl_component.h:191
T * Cast()
试图转到指定类型
Definition: dl_component.h:204
T * Get(size_t index=0)
返回第n个类型对象,失败返回nullptr
Definition: dl_component.h:210
bool operator==(const Components &b)
Definition: dl_component.h:196
bool operator!=(const Components &b)
Definition: dl_component.h:197
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:216
VectorAny & GetVectorAny()
Definition: dl_component.h:226
2d坐标系
Definition: dl_coor.h:27
元素通用属性(默认左中对齐,具体看类型)
Definition: dl_component.h:273
Align::Direction2 _hv
Definition: dl_component.h:288
void operator=(const T &func)
Definition: dl_component.h:55
Function()=default
语言本地化
Definition: dl_component.h:265
std::array< std::string, 2 > _str
Definition: dl_component.h:267
std::function< void(std::string_view, std::string_view)> _func
Definition: dl_component.h:268
变量,注意模板参数 需要实例化后不重复
Definition: dl_component.h:36
auto & GetVector()
返回所有对象
Definition: dl_component.h:174
T * Get(size_t index=0)
返回第n个类型对象,失败返回nullptr
Definition: dl_component.h:156
std::vector< AnyType >::iterator begin()
Definition: dl_component.h:179
size_t GetSize()
Definition: dl_component.h:183
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:124
std::vector< T * > GetMulti()
返回所有指定类型的对象
Definition: dl_component.h:138
std::vector< AnyType >::iterator end()
Definition: dl_component.h:181
方向和对齐定义
2d坐标系
std::array< Direction, 2 > Direction2
Definition: dl_align.h:31
constexpr std::array< T, N > Size2Anchor(const std::array< T, N > &size, const std::array< Direction, N > &dir)
垂直位置
Definition: dl_align.h:157
std::vector< T * > Get(std::vector< Components * > &vec)
从vector<Components*>返回指定类型的所有元素
Definition: dl_component.h:239
Definition: dl_array2.h:17
consteval uint64_t str_uid(const char(&str)[N])
固定条件的字符串,生成唯一id
Definition: dl_algorithm.h:599
Position2 Position
Definition: dl_type_def.h:43
通用类型