diff options
author | crupest <crupest@outlook.com> | 2022-01-20 23:17:17 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-20 23:17:17 +0800 |
commit | 193b3fe67eaadf291ae77f689362d87861c26118 (patch) | |
tree | bebf8343a730b3b43502d403be83350b871d672b | |
parent | c0f802594e17efd5b93d4019431bf1a6d24249c3 (diff) | |
download | cru-193b3fe67eaadf291ae77f689362d87861c26118.tar.gz cru-193b3fe67eaadf291ae77f689362d87861c26118.tar.bz2 cru-193b3fe67eaadf291ae77f689362d87861c26118.zip |
...
-rw-r--r-- | include/cru/ui/Base.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/mapper/CornerRadiusMapper.hpp | 18 | ||||
-rw-r--r-- | include/cru/ui/mapper/MapperRegistry.hpp | 12 | ||||
-rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ui/mapper/CornerRadiusMapper.cpp | 46 | ||||
-rw-r--r-- | src/ui/mapper/MapperRegistry.cpp | 15 |
6 files changed, 97 insertions, 1 deletions
diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index a8176b0d..db360c07 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -87,6 +87,11 @@ struct CornerRadius { left_bottom(left_bottom), right_bottom(right_bottom) {} + CornerRadius& SetAll(const Point& point) { + left_top = right_top = left_bottom = right_bottom = point; + return *this; + } + Point left_top; Point right_top; Point left_bottom; diff --git a/include/cru/ui/mapper/CornerRadiusMapper.hpp b/include/cru/ui/mapper/CornerRadiusMapper.hpp new file mode 100644 index 00000000..b9fe6a52 --- /dev/null +++ b/include/cru/ui/mapper/CornerRadiusMapper.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "Mapper.hpp" + +namespace cru::ui::mapper { +class CRU_UI_API CornerRadiusMapper : public BasicMapper<CornerRadius> { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CornerRadiusMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::unique_ptr<CornerRadius> DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/MapperRegistry.hpp b/include/cru/ui/mapper/MapperRegistry.hpp index a170440d..94643ce0 100644 --- a/include/cru/ui/mapper/MapperRegistry.hpp +++ b/include/cru/ui/mapper/MapperRegistry.hpp @@ -8,6 +8,8 @@ namespace cru::ui::mapper { class CRU_UI_API MapperRegistry { public: + static MapperRegistry* GetInstance(); + MapperRegistry(); CRU_DELETE_COPY(MapperRegistry) @@ -17,6 +19,16 @@ class CRU_UI_API MapperRegistry { const std::vector<MapperBase*>& GetAllMappers() const { return mapper_list_; } + template <typename T> + BasicMapper<T>* GetMapper() const { + for (auto mapper : mapper_list_) { + if (mapper->GetTypeIndex() == typeid(T)) { + return static_cast<BasicMapper<T>*>(mapper); + } + } + return nullptr; + } + void RegisterMapper(MapperBase* mapper); void UnregisterMapper(MapperBase* mapper); diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index ce71e89b..475231d3 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -31,7 +31,7 @@ add_library(cru_ui SHARED mapper/PointMapper.cpp mapper/SizeMapper.cpp mapper/ThicknessMapper.cpp - render/BorderRenderObject.cpp + render/CornerRenderObject.cpp render/CanvasRenderObject.cpp render/FlexLayoutRenderObject.cpp render/LayoutHelper.cpp diff --git a/src/ui/mapper/CornerRadiusMapper.cpp b/src/ui/mapper/CornerRadiusMapper.cpp new file mode 100644 index 00000000..ef63e26f --- /dev/null +++ b/src/ui/mapper/CornerRadiusMapper.cpp @@ -0,0 +1,46 @@ +#include "cru/ui/mapper/CornerRadiusMapper.hpp" +#include "cru/ui/mapper/MapperRegistry.hpp" +#include "cru/ui/mapper/PointMapper.hpp" + +namespace cru::ui::mapper { +bool CornerRadiusMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { + if (node->GetTag() == u"CornerRadius") { + return true; + } + return false; +} + +std::unique_ptr<CornerRadius> CornerRadiusMapper::DoMapFromXml( + xml::XmlElementNode* node) { + auto point_mapper = MapperRegistry::GetInstance()->GetMapper<Point>(); + + auto result = std::make_unique<CornerRadius>(); + + auto all = node->GetOptionalAttribute(u"all"); + if (all) { + result->SetAll(*point_mapper->MapFromString(*all)); + } + + auto lefttop = node->GetOptionalAttribute(u"lefttop"); + if (lefttop) { + result->left_top = *point_mapper->MapFromString(*lefttop); + } + + auto righttop = node->GetOptionalAttribute(u"righttop"); + if (righttop) { + result->right_top = *point_mapper->MapFromString(*righttop); + } + + auto rightbottom = node->GetOptionalAttribute(u"rightbottom"); + if (rightbottom) { + result->right_bottom = *point_mapper->MapFromString(*rightbottom); + } + + auto leftbottom = node->GetOptionalAttribute(u"leftbottom"); + if (leftbottom) { + result->left_bottom = *point_mapper->MapFromString(*leftbottom); + } + + return result; +} +} // namespace cru::ui::mapper diff --git a/src/ui/mapper/MapperRegistry.cpp b/src/ui/mapper/MapperRegistry.cpp index e445a7d0..206d1d32 100644 --- a/src/ui/mapper/MapperRegistry.cpp +++ b/src/ui/mapper/MapperRegistry.cpp @@ -1,6 +1,21 @@ #include "cru/ui/mapper/MapperRegistry.hpp" +#include "cru/ui/mapper/CornerRadiusMapper.hpp" +#include "cru/ui/mapper/PointMapper.hpp" +#include "cru/ui/mapper/SizeMapper.hpp" +#include "cru/ui/mapper/ThicknessMapper.hpp" namespace cru::ui::mapper { +MapperRegistry *MapperRegistry::GetInstance() { + static MapperRegistry instance; + + instance.RegisterMapper(new CornerRadiusMapper()); + instance.RegisterMapper(new PointMapper()); + instance.RegisterMapper(new SizeMapper()); + instance.RegisterMapper(new ThicknessMapper()); + + return &instance; +} + MapperRegistry::MapperRegistry() {} MapperRegistry::~MapperRegistry() { |