Files
dl_old/dl/misc/dl_component.h
T
2023-12-26 18:37:41 +08:00

378 lines
7.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file dl_component.h
* @brief 组件系统
*
*
* @version 1.0
* @author lveyou
* @date 22-01-01
*
* @note 用于取代绑定于代码的静态继承关系,动态化功能组件
* @note 避免使用std::any,以完全掌控性能
* @note 如果没有组件需求,直接使用vector<any>即可
* @note 组件仅仅是{AnyType,VectorAny}的组合
*
* @note 命名缩写规范:ele表示多个Components(单个元素)、cpt表示Components、comp表示单个组件
*/
#pragma once
#include <typeinfo>
#include <typeindex>
#include <string>
#include <vector>
#include <array>
#include <functional>
#include "dl_type.h"
#include "math/dl_align.h"
#include "math/dl_coor.h"
namespace dl
{
//! 子组件命名空间
namespace Cpt
{
//! 变量,注意模板参数 需要实例化后不重复
template<typename T, uint64_t UID>
class Variable
{
public:
void Set(T v) { _v = v; }
T Get() { return _v; }
operator bool() { return _v != 0; }
Variable(T v) : _v(v) {}
Variable() {}
private:
T _v;
};
//! 函数,注意模板参数 需要实例化后不重复
//! 由于this指针在构造完成之前无效,不能在初始化列表构造
template<typename T, uint64_t UID>
class Function
{
public:
Function() = default;
void operator=(const T& func) { Call = func; }
T Call;
};
}
//! 任意类型包装
class AnyType
{
friend class VectorAny;
public:
bool operator==(const AnyType& b) { return _p == b._p; }
bool operator!=(const AnyType& b) { return _id != b._id || _p != b._p; }
//默认构造id为自己
AnyType():
_p{nullptr},
_id{ typeid(AnyType) }
{}
template<typename T>
AnyType(T* p):
_id{ typeid(T) }
{
_p = p;
#ifndef NDEBUG
_nameDebug = typeid(T).name();
#endif
}
/**
* @brief 试图转到指定类型
* @note 失败返回nullptr
*/
template<typename T>
T* Cast()
{
if (std::type_index(typeid(T)) == _id)
{
return (T*)_p;
}
else
return nullptr;
}
/**
* @brief 返回类型id
*/
std::type_index GetId()
{
return _id;
}
/**
* @brief 是否为空
*/
bool IsNull()
{
return !_p;
}
std::string GetNameDebug()
{
#ifndef NDEBUG
return _nameDebug;
#else
return "Debug版本才有此字段!";
#endif
}
private:
void* _p;
std::type_index _id;
#ifndef NDEBUG
std::string _nameDebug;
#endif
};
class VectorAny
{
public:
/**
* @brief 添加一个任意对象
* @param[in] any
*/
void PushBack(AnyType any)
{
_vec.push_back(any);
}
void Clear()
{
_vec.clear();
}
/**
* @brief 返回所有指定类型的对象
*/
template<typename T>
std::vector<T*> GetMulti()
{
std::vector<T*> ret;
for (auto& iter : _vec)
{
if (iter._id == std::type_index(typeid(T)))
{
ret.push_back((T*)iter._p);
}
}
return ret;
}
/**
* @brief 返回第n个类型对象,失败返回nullptr
*/
template<typename T>
T* Get(size_t index = 0)
{
for (auto& iter : _vec)
{
if (iter._id == std::type_index(typeid(T)))
{
if (index == 0)
return (T*)(iter._p);
else
--index;
}
}
return nullptr;
}
/**
* @brief 返回所有对象
*/
auto& GetVector()
{
return _vec;
}
std::vector<AnyType>::iterator begin() { return _vec.begin(); }
std::vector<AnyType>::iterator end() { return _vec.end(); }
size_t GetSize() { return _vec.size(); }
private:
std::vector<AnyType> _vec;
};
//! any表示实际的对象(比如UI::Text)
//! any为空则为Components
class Components
{
public:
template <typename T>
Components(T* p) : _any(p)
{
#ifndef NDEBUG
_nameDebug = _any.GetNameDebug();
#endif
}
bool operator==(const Components& b) { return _any == b._any; }
bool operator!=(const Components& b) { return _any != b._any; }
/**
* @brief 试图转到指定类型
* @note 失败返回nullptr
*/
template <typename T>
T* Cast() { return _any.Cast<T>(); }
/**
* @brief 返回第n个类型对象,失败返回nullptr
*/
template <typename T>
T* Get(size_t index = 0) { return _vec.Get<T>(index); }
//! 返回自身
AnyType GetSelf()
{
return _any;
}
/**
* @brief 添加一个任意对象
* @param[in] any
*/
void PushBack(AnyType any)
{
_vec.PushBack(any);
}
void Clear()
{
_vec.Clear();
}
VectorAny& GetVectorAny() { return _vec; }
std::string GetName()
{
return _name;
}
//! 设置自定义名字
void SetName(std::string_view name)
{
_name = name;
}
//! 单纯的Componets,any为空
bool IsPure()
{
return _any.IsNull();
}
private:
VectorAny _vec;
AnyType _any;
std::string _name;
#ifndef NDEBUG
std::string _nameDebug;
#endif
};
inline Components* createComponents(std::string_view name = {})
{
Components* ret = new Components((int*)nullptr);
ret->SetName(name);
return ret;
}
namespace Cpt
{
/**
* @brief 从vector<Components*>返回指定类型的所有组件
*/
template<typename T>
std::vector<T*> GetComp(std::vector<Components*>& vec)
{
std::vector<T*> ret;
for (Components* cpt : vec)
{
VectorAny& vec_any = cpt->GetVectorAny();
std::vector<T*> mul = vec_any.GetMulti<T>();
ret.insert(ret.end(), mul.begin(), mul.end());
}
return ret;
}
/**
* @brief 从vector<Components*>返回指定类型的第一个元素
*/
template<typename T>
T* GetFirst(std::vector<Components*>& vec)
{
for (Components* cpt : vec)
{
T* p = cpt->GetSelf().Cast<T>();
if (p)
return p;
}
return nullptr;
}
//! ------------------------------具体组件-------------------------------------
using IsPickup = Function<std::function<bool()>, str_uid("IsPickup")>;//!< 拾取
using IsSelect = Function<std::function<bool()>, str_uid("IsSelect")>;//!< 选择
using IsClick = Function<std::function<bool()>, str_uid("IsClick")>;//!< 点击
using GetCoor = Function<std::function<dl::Coor* ()>, str_uid("GetCoor")>;//!< 返回坐标系
using GetOrder = Function<std::function<float()>, str_uid("GetOrder")>;//!< 返回order
using Update = Function<std::function<void()>, str_uid("Update")>;//!< 逻辑
using Render = Function<std::function<void()>, str_uid("Render")>;//!< 绘制
using SetOrder = Function<std::function<void(float)>, str_uid("SetOrder")>;//!< 设置order
using SetSelect = Function<std::function<void(bool)>, str_uid("SetSelect")>;//!< 设置是否选择
using DeleteThis = Function<std::function<void()>, str_uid("DeleteThis")>;//!< 删除自身
using Clone = Function<std::function<Components* ()>, str_uid("Clone")>;//!< 克隆
//! 语言本地化
class Lang
{
public:
std::array<std::string, 2> _str;
std::function<void(std::string_view, std::string_view)> _func;
};
//! 元素通用属性(默认左上对齐,便于理解)
class ElementData
{
public:
ElementData() :
_size{},
_anchor{},
_hv{ Align::LT }
{}
//! 位置偏移(实际coor位置)
Position GetOffset()
{
return -Align::Size2Anchor(_size, _hv) + _anchor;
}
//! 自身大小
Position GetSize()
{
return _size;
}
//! 锚点位置
Position GetAnchor()
{
return _anchor;
}
public:
Position _size;
Position _anchor;
Align::Direction2 _hv;
};
}
}