/** * @file dl_component.h * @brief 组件系统 * * * @version 1.0 * @author lveyou * @date 22-01-01 * * @note 用于取代绑定于代码的静态继承关系,动态化功能组件 * @note 避免使用std::any,以完全掌控性能 * @note 如果没有组件需求,直接使用vector即可 * @note 组件仅仅是{AnyType,VectorAny}的组合 * * @note 命名缩写规范:ele表示多个Components(单个元素)、cpt表示Components、comp表示单个组件 */ #pragma once #include #include #include #include #include #include #include "dl_type.h" #include "math/dl_align.h" #include "math/dl_coor.h" namespace dl { //! 子组件命名空间 namespace Cpt { //! 变量,注意模板参数 需要实例化后不重复 template 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 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 AnyType(T* p): _id{ typeid(T) } { _p = p; #ifndef NDEBUG _nameDebug = typeid(T).name(); #endif } /** * @brief 试图转到指定类型 * @note 失败返回nullptr */ template 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 std::vector GetMulti() { std::vector 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 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::iterator begin() { return _vec.begin(); } std::vector::iterator end() { return _vec.end(); } size_t GetSize() { return _vec.size(); } private: std::vector _vec; }; //! any表示实际的对象(比如UI::Text) //! any为空则为Components class Components { public: template 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 T* Cast() { return _any.Cast(); } /** * @brief 返回第n个类型对象,失败返回nullptr */ template T* Get(size_t index = 0) { return _vec.Get(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返回指定类型的所有组件 */ template std::vector GetComp(std::vector& vec) { std::vector ret; for (Components* cpt : vec) { VectorAny& vec_any = cpt->GetVectorAny(); std::vector mul = vec_any.GetMulti(); ret.insert(ret.end(), mul.begin(), mul.end()); } return ret; } /** * @brief 从vector返回指定类型的第一个元素 */ template T* GetFirst(std::vector& vec) { for (Components* cpt : vec) { T* p = cpt->GetSelf().Cast(); if (p) return p; } return nullptr; } //! ------------------------------具体组件------------------------------------- using IsPickup = Function, str_uid("IsPickup")>;//!< 拾取 using IsSelect = Function, str_uid("IsSelect")>;//!< 选择 using IsClick = Function, str_uid("IsClick")>;//!< 点击 using GetCoor = Function, str_uid("GetCoor")>;//!< 返回坐标系 using GetOrder = Function, str_uid("GetOrder")>;//!< 返回order using Update = Function, str_uid("Update")>;//!< 逻辑 using Render = Function, str_uid("Render")>;//!< 绘制 using SetOrder = Function, str_uid("SetOrder")>;//!< 设置order using SetSelect = Function, str_uid("SetSelect")>;//!< 设置是否选择 using DeleteThis = Function, str_uid("DeleteThis")>;//!< 删除自身 using Clone = Function, str_uid("Clone")>;//!< 克隆 //! 语言本地化 class Lang { public: std::array _str; std::function _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; }; } }