diff options
author | crupest <crupest@outlook.com> | 2022-02-12 18:17:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-12 18:17:52 +0800 |
commit | b7946e28a08c945e26d39f095d2e55c952a936c4 (patch) | |
tree | 1e3df327fd8d36c88aa9840ff711545f316e3129 | |
parent | 2fd37d41bb804a06acc8e2d341d5ce5d8370184b (diff) | |
download | cru-b7946e28a08c945e26d39f095d2e55c952a936c4.tar.gz cru-b7946e28a08c945e26d39f095d2e55c952a936c4.tar.bz2 cru-b7946e28a08c945e26d39f095d2e55c952a936c4.zip |
...
8 files changed, 237 insertions, 0 deletions
diff --git a/include/cru/ui/controls/Container.h b/include/cru/ui/controls/Container.h index 3df2b445..98958b3b 100644 --- a/include/cru/ui/controls/Container.h +++ b/include/cru/ui/controls/Container.h @@ -16,6 +16,23 @@ class CRU_UI_API Container ~Container() override; public: + std::shared_ptr<platform::graphics::IBrush> GetForegroundBrush() const { + return GetContainerRenderObject()->GetForegroundBrush(); + } + void SetForegroundBrush( + const std::shared_ptr<platform::graphics::IBrush>& brush) { + GetContainerRenderObject()->SetForegroundBrush(brush); + } + + std::shared_ptr<platform::graphics::IBrush> GetBackgroundBrush() const { + return GetContainerRenderObject()->GetBackgroundBrush(); + } + void SetBackgroundBrush( + const std::shared_ptr<platform::graphics::IBrush>& brush) { + GetContainerRenderObject()->SetBackgroundBrush(brush); + } + + public: String GetControlType() const final { return kControlType.ToString(); } }; } // namespace cru::ui::controls diff --git a/src/theme_builder/CMakeLists.txt b/src/theme_builder/CMakeLists.txt index 108281a1..95619369 100644 --- a/src/theme_builder/CMakeLists.txt +++ b/src/theme_builder/CMakeLists.txt @@ -4,7 +4,10 @@ add_executable(cru_theme_builder components/StyleRuleEditor.cpp components/StyleRuleSetEditor.cpp components/conditions/ConditionEditor.cpp + components/properties/ColorPropertyEditor.cpp + components/properties/PointPropertyEditor.cpp components/properties/TextPropertyEditor.cpp + components/properties/ThicknessPropertyEditor.cpp ) if(APPLE) diff --git a/src/theme_builder/components/properties/ColorPropertyEditor.cpp b/src/theme_builder/components/properties/ColorPropertyEditor.cpp new file mode 100644 index 00000000..cf9599b3 --- /dev/null +++ b/src/theme_builder/components/properties/ColorPropertyEditor.cpp @@ -0,0 +1,40 @@ +#include "ColorPropertyEditor.h" +#include "cru/platform/graphics/Factory.h" +#include "cru/ui/Base.h" + +namespace cru::theme_builder::components::properties { +ColorPropertyEditor::ColorPropertyEditor() { + container_.AddChild(&label_); + container_.AddChild(&color_cube_); + container_.AddChild(&color_text_); + + color_cube_brush_ = platform::gui::IUiApplication::GetInstance() + ->GetGraphicsFactory() + ->CreateSolidColorBrush(color_); + color_cube_.SetForegroundBrush(color_cube_brush_); + color_text_.SetText(color_.ToString()); + + color_text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) { + auto text = color_text_.GetTextView(); + auto color = ui::Color::Parse(text); + if (color) { + color_ = *color; + color_cube_brush_->SetColor(*color); + is_color_text_valid_ = true; + color_change_event_.Raise(*color); + } else { + is_color_text_valid_ = false; + // TODO: Show error! + } + }); +} + +ColorPropertyEditor::~ColorPropertyEditor() {} + +void ColorPropertyEditor::SetColor(const ui::Color &color) { + color_cube_brush_->SetColor(color); + color_text_.SetText(color.ToString()); + is_color_text_valid_ = true; + color_change_event_.Raise(color); +} +} // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ColorPropertyEditor.h b/src/theme_builder/components/properties/ColorPropertyEditor.h new file mode 100644 index 00000000..7c7155ab --- /dev/null +++ b/src/theme_builder/components/properties/ColorPropertyEditor.h @@ -0,0 +1,36 @@ +#pragma once +#include "cru/platform/graphics/Base.h" +#include "cru/ui/components/Component.h" +#include "cru/ui/controls/Container.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/TextBlock.h" +#include "cru/ui/controls/TextBox.h" + +namespace cru::theme_builder::components::properties { +class ColorPropertyEditor : public ui::components::Component { + public: + ColorPropertyEditor(); + ~ColorPropertyEditor() override; + + public: + String GetLabel() const { return label_.GetText(); } + void SetLabel(String label) { label_.SetText(std::move(label)); } + + ui::Color GetColor() const { return color_; } + void SetColor(const ui::Color& color); + + IEvent<ui::Color>* ColorChangeEvent() { return &color_change_event_; } + + private: + ui::Color color_ = ui::colors::transparent; + + ui::controls::FlexLayout container_; + ui::controls::TextBlock label_; + ui::controls::Container color_cube_; + std::shared_ptr<platform::graphics::ISolidColorBrush> color_cube_brush_; + ui::controls::TextBox color_text_; + bool is_color_text_valid_; + + Event<ui::Color> color_change_event_; +}; +} // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/PointPropertyEditor.cpp b/src/theme_builder/components/properties/PointPropertyEditor.cpp new file mode 100644 index 00000000..a8549c8d --- /dev/null +++ b/src/theme_builder/components/properties/PointPropertyEditor.cpp @@ -0,0 +1,39 @@ +#include "PointPropertyEditor.h" +#include "cru/common/Format.h" +#include "cru/ui/mapper/MapperRegistry.h" +#include "cru/ui/mapper/PointMapper.h" + +namespace cru::theme_builder::components::properties { +PointPropertyEditor::PointPropertyEditor() { + container_.AddChild(&label_); + container_.AddChild(&text_); + + text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) { + auto text = text_.GetTextView(); + auto point_mapper = + ui::mapper::MapperRegistry::GetInstance()->GetMapper<ui::Point>(); + try { + auto point = point_mapper->MapFromString(text.ToString()); + point_ = point; + is_text_valid_ = true; + point_change_event_.Raise(point); + } catch (const Exception&) { + is_text_valid_ = false; + // TODO: Show error! + } + }); +} + +PointPropertyEditor::~PointPropertyEditor() {} + +void PointPropertyEditor::SetPoint(const ui::Point& point) { + point_ = point; + text_.SetText(ConvertPointToString(point)); + is_text_valid_ = true; + point_change_event_.Raise(point); +} + +String PointPropertyEditor::ConvertPointToString(const ui::Point& point) { + return Format(u"{} {}", point.x, point.y); +} +} // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/PointPropertyEditor.h b/src/theme_builder/components/properties/PointPropertyEditor.h new file mode 100644 index 00000000..8588d77c --- /dev/null +++ b/src/theme_builder/components/properties/PointPropertyEditor.h @@ -0,0 +1,32 @@ +#pragma once +#include "cru/ui/components/Component.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/TextBlock.h" +#include "cru/ui/controls/TextBox.h" + +namespace cru::theme_builder::components::properties { +class PointPropertyEditor : public ui::components::Component { + public: + PointPropertyEditor(); + ~PointPropertyEditor() override; + + public: + ui::Point GetPoint() const { return point_; } + void SetPoint(const ui::Point& point); + + IEvent<ui::Point>* PointChangeEvent() { return &point_change_event_; } + + private: + static String ConvertPointToString(const ui::Point& point); + + private: + ui::Point point_; + + ui::controls::FlexLayout container_; + ui::controls::TextBlock label_; + ui::controls::TextBox text_; + bool is_text_valid_; + + Event<ui::Point> point_change_event_; +}; +} // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp b/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp new file mode 100644 index 00000000..3121d288 --- /dev/null +++ b/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp @@ -0,0 +1,35 @@ +#include "ThicknessPropertyEditor.h" +#include "cru/ui/mapper/MapperRegistry.h" +#include "cru/ui/mapper/ThicknessMapper.h" + +namespace cru::theme_builder::components::properties { +ThicknessPropertyEditor::ThicknessPropertyEditor() { + container_.AddChild(&label_); + container_.AddChild(&text_); + + text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) { + auto text = text_.GetTextView(); + auto thickness_mapper = + ui::mapper::MapperRegistry::GetInstance()->GetMapper<ui::Thickness>(); + try { + auto thickness = thickness_mapper->MapFromString(text.ToString()); + thickness_ = thickness; + is_text_valid_ = true; + thickness_change_event_.Raise(thickness); + } catch (const Exception &) { + is_text_valid_ = false; + // TODO: Show error! + } + }); +} + +ThicknessPropertyEditor::~ThicknessPropertyEditor() {} + +void ThicknessPropertyEditor::SetThickness(const ui::Thickness &thickness) { + thickness_ = thickness; + text_.SetText(Format(u"{} {} {} {}", thickness_.left, thickness_.top, + thickness_.right, thickness_.bottom)); + is_text_valid_ = true; + thickness_change_event_.Raise(thickness_); +} +} // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ThicknessPropertyEditor.h b/src/theme_builder/components/properties/ThicknessPropertyEditor.h new file mode 100644 index 00000000..62157b6f --- /dev/null +++ b/src/theme_builder/components/properties/ThicknessPropertyEditor.h @@ -0,0 +1,35 @@ +#pragma once +#include "cru/ui/components/Component.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/TextBlock.h" +#include "cru/ui/controls/TextBox.h" + +namespace cru::theme_builder::components::properties { +class ThicknessPropertyEditor : public ui::components::Component { + public: + ThicknessPropertyEditor(); + ~ThicknessPropertyEditor() override; + + ui::controls::Control* GetRootControl() override { return &container_; } + + String GetLabel() const { return label_.GetText(); } + void SetLabel(String label) { label_.SetText(std::move(label)); } + + ui::Thickness GetThickness() const { return thickness_; } + void SetThickness(const ui::Thickness& thickness); + + IEvent<ui::Thickness>* ThicknessChangeEvent() { + return &thickness_change_event_; + } + + private: + ui::Thickness thickness_; + + ui::controls::FlexLayout container_; + ui::controls::TextBlock label_; + ui::controls::TextBox text_; + bool is_text_valid_; + + Event<ui::Thickness> thickness_change_event_; +}; +} // namespace cru::theme_builder::components::properties |