diff options
author | crupest <crupest@outlook.com> | 2022-02-16 20:22:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-16 20:22:25 +0800 |
commit | 3cd44092c44651650a760752a3d374f610ca4f77 (patch) | |
tree | d3481e8d93d8d47674d0df46eb765ccf9100bca8 | |
parent | 6459edc7c8af9e5c9bafe4f1635194334f42f415 (diff) | |
download | cru-3cd44092c44651650a760752a3d374f610ca4f77.tar.gz cru-3cd44092c44651650a760752a3d374f610ca4f77.tar.bz2 cru-3cd44092c44651650a760752a3d374f610ca4f77.zip |
...
11 files changed, 348 insertions, 27 deletions
diff --git a/include/cru/ui/style/Styler.h b/include/cru/ui/style/Styler.h index d6cbc760..29c19975 100644 --- a/include/cru/ui/style/Styler.h +++ b/include/cru/ui/style/Styler.h @@ -37,6 +37,8 @@ class CRU_UI_API CompoundStyler : public Styler { } } + std::vector<ClonablePtr<Styler>> GetChildren() const { return stylers_; } + virtual CompoundStyler* Clone() const override { return new CompoundStyler(stylers_); } @@ -84,6 +86,8 @@ class CRU_UI_API CursorStyler : public Styler { CursorStyler* Clone() const override { return new CursorStyler(cursor_); } + std::shared_ptr<platform::gui::ICursor> GetCursor() const { return cursor_; } + private: std::shared_ptr<platform::gui::ICursor> cursor_; }; diff --git a/src/theme_builder/CMakeLists.txt b/src/theme_builder/CMakeLists.txt index 5b968918..a0597d7b 100644 --- a/src/theme_builder/CMakeLists.txt +++ b/src/theme_builder/CMakeLists.txt @@ -16,6 +16,9 @@ add_executable(cru_theme_builder components/properties/TextPropertyEditor.cpp components/properties/ThicknessPropertyEditor.cpp components/stylers/BorderStylerEditor.cpp + components/stylers/CompoundStylerEditor.cpp + components/stylers/CursorStylerEditor.cpp + components/stylers/StylerEditor.cpp ) if(APPLE) diff --git a/src/theme_builder/components/conditions/CompoundConditionEditor.cpp b/src/theme_builder/components/conditions/CompoundConditionEditor.cpp index 511fffdd..76198dc0 100644 --- a/src/theme_builder/components/conditions/CompoundConditionEditor.cpp +++ b/src/theme_builder/components/conditions/CompoundConditionEditor.cpp @@ -38,29 +38,36 @@ CompoundConditionEditor::CompoundConditionEditor() { u"Checked Condtion", }); add_child_button_.MenuItemSelectedEvent()->AddHandler([this](Index index) { - std::unique_ptr<ConditionEditor> child; + std::unique_ptr<ConditionEditor> editor; switch (index) { case 0: - child = std::make_unique<AndConditionEditor>(); + editor = std::make_unique<AndConditionEditor>(); break; case 1: - child = std::make_unique<OrConditionEditor>(); + editor = std::make_unique<OrConditionEditor>(); break; case 2: - child = std::make_unique<ClickStateConditionEditor>(); + editor = std::make_unique<ClickStateConditionEditor>(); break; case 3: - child = std::make_unique<FocusConditionEditor>(); + editor = std::make_unique<FocusConditionEditor>(); break; case 4: - child = std::make_unique<CheckedConditionEditor>(); + editor = std::make_unique<CheckedConditionEditor>(); break; default: break; } - if (child) { - auto c = std::make_unique<CompoundConditionEditorChild>(std::move(child)); - children_.push_back(std::move(c)); + if (editor) { + auto child = + std::make_unique<CompoundConditionEditorChild>(std::move(editor)); + child->RemoveEvent()->AddSpyOnlyHandler([this, c = child.get()] { + auto index = this->children_container_.IndexOf(c->GetRootControl()); + this->children_.erase(this->children_.begin() + index); + this->children_container_.RemoveChildAt(index); + change_event_.Raise(nullptr); + }); + children_.push_back(std::move(child)); children_container_.AddChild(children_.back()->GetRootControl()); change_event_.Raise(nullptr); } diff --git a/src/theme_builder/components/stylers/BorderStylerEditor.cpp b/src/theme_builder/components/stylers/BorderStylerEditor.cpp index 4f36aec8..36fc61d0 100644 --- a/src/theme_builder/components/stylers/BorderStylerEditor.cpp +++ b/src/theme_builder/components/stylers/BorderStylerEditor.cpp @@ -8,11 +8,11 @@ namespace cru::theme_builder::components::stylers { BorderStylerEditor::BorderStylerEditor() { - container_.AddChild(corner_radius_editor_.GetRootControl()); - container_.AddChild(thickness_editor_.GetRootControl()); - container_.AddChild(brush_editor_.GetRootControl()); - container_.AddChild(foreground_brush_editor_.GetRootControl()); - container_.AddChild(background_brush_editor_.GetRootControl()); + GetContainer()->AddChild(corner_radius_editor_.GetRootControl()); + GetContainer()->AddChild(thickness_editor_.GetRootControl()); + GetContainer()->AddChild(brush_editor_.GetRootControl()); + GetContainer()->AddChild(foreground_brush_editor_.GetRootControl()); + GetContainer()->AddChild(background_brush_editor_.GetRootControl()); auto connect = [this](IEvent<std::nullptr_t>* event) { event->AddHandler( @@ -26,7 +26,7 @@ BorderStylerEditor::BorderStylerEditor() { connect(background_brush_editor_.ChangeEvent()); } -BorderStylerEditor::~BorderStylerEditor() { container_.RemoveFromParent(); } +BorderStylerEditor::~BorderStylerEditor() {} ClonablePtr<ui::style::BorderStyler> BorderStylerEditor::GetValue() { auto graphics_factory = @@ -54,8 +54,8 @@ ClonablePtr<ui::style::BorderStyler> BorderStylerEditor::GetValue() { return ui::style::BorderStyler::Create(border_style); } -void BorderStylerEditor::SetValue( - const ClonablePtr<ui::style::BorderStyler>& styler) { +void BorderStylerEditor::SetValue(ui::style::BorderStyler* styler, + bool trigger_change) { Expects(styler); auto border_style = styler->GetBorderStyle(); @@ -90,6 +90,10 @@ void BorderStylerEditor::SetValue( ->GetColor(), false); } + + if (trigger_change) { + change_event_.Raise(nullptr); + } } } // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/BorderStylerEditor.h b/src/theme_builder/components/stylers/BorderStylerEditor.h index 8e9a4e6f..9e2147b4 100644 --- a/src/theme_builder/components/stylers/BorderStylerEditor.h +++ b/src/theme_builder/components/stylers/BorderStylerEditor.h @@ -3,28 +3,27 @@ #include "../properties/CornerRadiusPropertyEditor.h" #include "../properties/OptionalPropertyEditor.h" #include "../properties/ThicknessPropertyEditor.h" +#include "StylerEditor.h" #include "cru/common/ClonablePtr.h" -#include "cru/common/Event.h" -#include "cru/ui/components/Component.h" -#include "cru/ui/controls/CheckBox.h" -#include "cru/ui/controls/FlexLayout.h" -#include "cru/ui/style/Styler.h" namespace cru::theme_builder::components::stylers { -class BorderStylerEditor : public ui::components::Component { +class BorderStylerEditor : public StylerEditor { public: BorderStylerEditor(); ~BorderStylerEditor() override; - ui::controls::Control* GetRootControl() override { return nullptr; } - ClonablePtr<ui::style::BorderStyler> GetValue(); - void SetValue(const ClonablePtr<ui::style::BorderStyler>& styler); + void SetValue(ui::style::BorderStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr<ui::style::BorderStyler>& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr<ui::style::Styler> GetStyler() override { return GetValue(); } IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: - ui::controls::FlexLayout container_; properties::OptionalPropertyEditor<properties::CornerRadiusPropertyEditor> corner_radius_editor_; properties::OptionalPropertyEditor<properties::ThicknessPropertyEditor> diff --git a/src/theme_builder/components/stylers/CompoundStylerEditor.cpp b/src/theme_builder/components/stylers/CompoundStylerEditor.cpp new file mode 100644 index 00000000..52f911bc --- /dev/null +++ b/src/theme_builder/components/stylers/CompoundStylerEditor.cpp @@ -0,0 +1,98 @@ +#include "CompoundStylerEditor.h" +#include "BorderStylerEditor.h" +#include "CursorStylerEditor.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +CompoundStylerEditorChild::CompoundStylerEditorChild( + std::unique_ptr<StylerEditor>&& editor) + : styler_editor_(std::move(editor)) { + container_.SetFlexDirection(ui::controls::FlexDirection::Horizontal); + container_.AddChild(&remove_button_); + + remove_button_.SetChild(&remove_button_text_); + remove_button_text_.SetText(u"-"); + + container_.AddChild(editor->GetRootControl()); + + remove_button_.ClickEvent()->AddSpyOnlyHandler( + [this] { this->remove_event_.Raise(nullptr); }); +} + +CompoundStylerEditorChild::~CompoundStylerEditorChild() { + container_.RemoveFromParent(); +} + +CompoundStylerEditor::CompoundStylerEditor() { + children_container_.SetFlexDirection(ui::controls::FlexDirection::Vertical); + GetContainer()->AddChild(&children_container_); + + add_child_button_.SetButtonText(u"+"); + add_child_button_.SetMenuItems({ + u"Compound Styler", + u"Border Styler", + u"Cursor Styler", + }); + add_child_button_.MenuItemSelectedEvent()->AddHandler([this](Index index) { + std::unique_ptr<StylerEditor> editor; + switch (index) { + case 0: + editor = std::make_unique<CompoundStylerEditor>(); + break; + case 1: + editor = std::make_unique<BorderStylerEditor>(); + break; + case 2: + editor = std::make_unique<CursorStylerEditor>(); + break; + default: + break; + } + if (editor) { + auto child = + std::make_unique<CompoundStylerEditorChild>(std::move(editor)); + child->RemoveEvent()->AddSpyOnlyHandler([this, c = child.get()] { + auto index = this->children_container_.IndexOf(c->GetRootControl()); + this->children_.erase(this->children_.begin() + index); + this->children_container_.RemoveChildAt(index); + change_event_.Raise(nullptr); + }); + children_container_.AddChild(child->GetRootControl()); + children_.push_back(std::move(child)); + change_event_.Raise(nullptr); + } + }); +} + +CompoundStylerEditor::~CompoundStylerEditor() {} + +ClonablePtr<ui::style::CompoundStyler> CompoundStylerEditor::GetValue() { + std::vector<ClonablePtr<ui::style::Styler>> children_styler; + for (auto& child : children_) { + children_styler.push_back(child->GetStylerEditor()->GetStyler()); + } + return ui::style::CompoundStyler::Create(std::move(children_styler)); +} + +void CompoundStylerEditor::SetValue(ui::style::CompoundStyler* value, + bool trigger_change) { + children_.clear(); + children_container_.ClearChildren(); + for (const auto& styler : value->GetChildren()) { + auto editor = CreateStylerEditor(styler.get()); + if (editor) { + auto child = + std::make_unique<CompoundStylerEditorChild>(std::move(editor)); + child->RemoveEvent()->AddSpyOnlyHandler([this, c = child.get()] { + auto index = this->children_container_.IndexOf(c->GetRootControl()); + this->children_.erase(this->children_.begin() + index); + this->children_container_.RemoveChildAt(index); + change_event_.Raise(nullptr); + }); + children_.push_back(std::move(child)); + children_container_.AddChild(children_.back()->GetRootControl()); + } + } +} +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/CompoundStylerEditor.h b/src/theme_builder/components/stylers/CompoundStylerEditor.h new file mode 100644 index 00000000..5b1f86e0 --- /dev/null +++ b/src/theme_builder/components/stylers/CompoundStylerEditor.h @@ -0,0 +1,54 @@ +#pragma once +#include "StylerEditor.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/components/PopupButton.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +class CompoundStylerEditorChild : public ui::components::Component { + public: + explicit CompoundStylerEditorChild(std::unique_ptr<StylerEditor>&& editor); + ~CompoundStylerEditorChild() override; + + public: + ui::controls::Control* GetRootControl() override { return &container_; } + + StylerEditor* GetStylerEditor() { return styler_editor_.get(); } + + IEvent<std::nullptr_t>* RemoveEvent() { return &remove_event_; } + + private: + ui::controls::FlexLayout container_; + ui::controls::Button remove_button_; + ui::controls::TextBlock remove_button_text_; + std::unique_ptr<StylerEditor> styler_editor_; + + Event<std::nullptr_t> remove_event_; +}; + +class CompoundStylerEditor : public StylerEditor { + public: + CompoundStylerEditor(); + ~CompoundStylerEditor() override; + + public: + ClonablePtr<ui::style::CompoundStyler> GetValue(); + void SetValue(ui::style::CompoundStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr<ui::style::CompoundStyler>& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr<ui::style::Styler> GetStyler() override { return GetValue(); } + + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } + + private: + ui::controls::FlexLayout children_container_; + std::vector<std::unique_ptr<CompoundStylerEditorChild>> children_; + ui::components::PopupMenuTextButton add_child_button_; + + Event<std::nullptr_t> change_event_; +}; +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/CursorStylerEditor.cpp b/src/theme_builder/components/stylers/CursorStylerEditor.cpp new file mode 100644 index 00000000..4cbd69cd --- /dev/null +++ b/src/theme_builder/components/stylers/CursorStylerEditor.cpp @@ -0,0 +1,61 @@ +#include "CursorStylerEditor.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/platform/gui/UiApplication.h" + +namespace cru::theme_builder::components::stylers { +CursorStylerEditor::CursorStylerEditor() { + GetContainer()->AddChild(cursor_select_.GetRootControl()); + + cursor_select_.SetItems({u"arrow", u"hand", u"ibeam"}); + cursor_select_.SetSelectedIndex(0); +} + +CursorStylerEditor::~CursorStylerEditor() {} + +ClonablePtr<ui::style::CursorStyler> CursorStylerEditor::GetValue() { + auto cursor_manager = + platform::gui::IUiApplication::GetInstance()->GetCursorManager(); + + std::shared_ptr<platform::gui::ICursor> cursor; + + switch (cursor_select_.GetSelectedIndex()) { + case 0: + cursor = cursor_manager->GetSystemCursor( + platform::gui::SystemCursorType::Arrow); + break; + case 1: + cursor = cursor_manager->GetSystemCursor( + platform::gui::SystemCursorType::Hand); + break; + case 2: + cursor = cursor_manager->GetSystemCursor( + platform::gui::SystemCursorType::IBeam); + break; + } + + return ui::style::CursorStyler::Create(cursor); +} + +void CursorStylerEditor::SetValue(ui::style::CursorStyler* styler, + bool trigger_change) { + auto cursor_manager = + platform::gui::IUiApplication::GetInstance()->GetCursorManager(); + + auto cursor = styler->GetCursor(); + + if (cursor == + cursor_manager->GetSystemCursor(platform::gui::SystemCursorType::Arrow)) { + cursor_select_.SetSelectedIndex(0); + } else if (cursor == cursor_manager->GetSystemCursor( + platform::gui::SystemCursorType::Hand)) { + cursor_select_.SetSelectedIndex(1); + } else if (cursor == cursor_manager->GetSystemCursor( + platform::gui::SystemCursorType::IBeam)) { + cursor_select_.SetSelectedIndex(2); + } + + if (trigger_change) { + change_event_.Raise(nullptr); + } +} +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/CursorStylerEditor.h b/src/theme_builder/components/stylers/CursorStylerEditor.h new file mode 100644 index 00000000..49b16a89 --- /dev/null +++ b/src/theme_builder/components/stylers/CursorStylerEditor.h @@ -0,0 +1,28 @@ +#pragma once +#include "../properties/SelectPropertyEditor.h" +#include "StylerEditor.h" + +namespace cru::theme_builder::components::stylers { +class CursorStylerEditor : public StylerEditor { + public: + CursorStylerEditor(); + ~CursorStylerEditor() override; + + public: + ClonablePtr<ui::style::CursorStyler> GetValue(); + void SetValue(ui::style::CursorStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr<ui::style::CursorStyler>& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr<ui::style::Styler> GetStyler() override { return GetValue(); } + + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } + + private: + properties::SelectPropertyEditor cursor_select_; + + Event<std::nullptr_t> change_event_; +}; +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/StylerEditor.cpp b/src/theme_builder/components/stylers/StylerEditor.cpp new file mode 100644 index 00000000..9c7852d4 --- /dev/null +++ b/src/theme_builder/components/stylers/StylerEditor.cpp @@ -0,0 +1,34 @@ +#include "StylerEditor.h" +#include "BorderStylerEditor.h" +#include "CompoundStylerEditor.h" +#include "CursorStylerEditor.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +StylerEditor::StylerEditor() { + container_.SetFlexDirection(ui::controls::FlexDirection::Vertical); + container_.AddChild(&label_); +} + +StylerEditor::~StylerEditor() { container_.RemoveFromParent(); } + +std::unique_ptr<StylerEditor> CreateStylerEditor(ui::style::Styler* styler) { + if (auto compound_styler = dynamic_cast<ui::style::CompoundStyler*>(styler)) { + auto result = std::make_unique<CompoundStylerEditor>(); + result->SetValue(compound_styler); + return result; + } else if (auto border_styler = + dynamic_cast<ui::style::BorderStyler*>(styler)) { + auto editor = std::make_unique<BorderStylerEditor>(); + editor->SetValue(border_styler); + return editor; + } else if (auto cursor_styler = + dynamic_cast<ui::style::CursorStyler*>(styler)) { + auto editor = std::make_unique<CursorStylerEditor>(); + editor->SetValue(cursor_styler); + return editor; + } else { + return nullptr; + } +} +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/StylerEditor.h b/src/theme_builder/components/stylers/StylerEditor.h new file mode 100644 index 00000000..6da0f6fa --- /dev/null +++ b/src/theme_builder/components/stylers/StylerEditor.h @@ -0,0 +1,29 @@ +#pragma once +#include "cru/ui/components/Component.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/TextBlock.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +class StylerEditor : public ui::components::Component { + public: + StylerEditor(); + ~StylerEditor() override; + + public: + ui::controls::Control* GetRootControl() override { return &container_; } + + ui::controls::FlexLayout* GetContainer() { return &container_; } + + String GetLabel() const { return label_.GetText(); } + void SetLabel(String label) { label_.SetText(std::move(label)); } + + virtual ClonablePtr<ui::style::Styler> GetStyler() = 0; + + private: + ui::controls::FlexLayout container_; + ui::controls::TextBlock label_; +}; + +std::unique_ptr<StylerEditor> CreateStylerEditor(ui::style::Styler* styler); +} // namespace cru::theme_builder::components::stylers |