From 3cd44092c44651650a760752a3d374f610ca4f77 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 16 Feb 2022 20:22:25 +0800 Subject: ... --- src/theme_builder/CMakeLists.txt | 3 + .../conditions/CompoundConditionEditor.cpp | 25 ++++-- .../components/stylers/BorderStylerEditor.cpp | 20 +++-- .../components/stylers/BorderStylerEditor.h | 19 ++--- .../components/stylers/CompoundStylerEditor.cpp | 98 ++++++++++++++++++++++ .../components/stylers/CompoundStylerEditor.h | 54 ++++++++++++ .../components/stylers/CursorStylerEditor.cpp | 61 ++++++++++++++ .../components/stylers/CursorStylerEditor.h | 28 +++++++ .../components/stylers/StylerEditor.cpp | 34 ++++++++ .../components/stylers/StylerEditor.h | 29 +++++++ 10 files changed, 344 insertions(+), 27 deletions(-) create mode 100644 src/theme_builder/components/stylers/CompoundStylerEditor.cpp create mode 100644 src/theme_builder/components/stylers/CompoundStylerEditor.h create mode 100644 src/theme_builder/components/stylers/CursorStylerEditor.cpp create mode 100644 src/theme_builder/components/stylers/CursorStylerEditor.h create mode 100644 src/theme_builder/components/stylers/StylerEditor.cpp create mode 100644 src/theme_builder/components/stylers/StylerEditor.h (limited to 'src') 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 child; + std::unique_ptr editor; switch (index) { case 0: - child = std::make_unique(); + editor = std::make_unique(); break; case 1: - child = std::make_unique(); + editor = std::make_unique(); break; case 2: - child = std::make_unique(); + editor = std::make_unique(); break; case 3: - child = std::make_unique(); + editor = std::make_unique(); break; case 4: - child = std::make_unique(); + editor = std::make_unique(); break; default: break; } - if (child) { - auto c = std::make_unique(std::move(child)); - children_.push_back(std::move(c)); + if (editor) { + auto child = + std::make_unique(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* event) { event->AddHandler( @@ -26,7 +26,7 @@ BorderStylerEditor::BorderStylerEditor() { connect(background_brush_editor_.ChangeEvent()); } -BorderStylerEditor::~BorderStylerEditor() { container_.RemoveFromParent(); } +BorderStylerEditor::~BorderStylerEditor() {} ClonablePtr BorderStylerEditor::GetValue() { auto graphics_factory = @@ -54,8 +54,8 @@ ClonablePtr BorderStylerEditor::GetValue() { return ui::style::BorderStyler::Create(border_style); } -void BorderStylerEditor::SetValue( - const ClonablePtr& 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 GetValue(); - void SetValue(const ClonablePtr& styler); + void SetValue(ui::style::BorderStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr GetStyler() override { return GetValue(); } IEvent* ChangeEvent() { return &change_event_; } private: - ui::controls::FlexLayout container_; properties::OptionalPropertyEditor corner_radius_editor_; properties::OptionalPropertyEditor 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&& 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 editor; + switch (index) { + case 0: + editor = std::make_unique(); + break; + case 1: + editor = std::make_unique(); + break; + case 2: + editor = std::make_unique(); + break; + default: + break; + } + if (editor) { + auto child = + std::make_unique(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 CompoundStylerEditor::GetValue() { + std::vector> 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(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&& editor); + ~CompoundStylerEditorChild() override; + + public: + ui::controls::Control* GetRootControl() override { return &container_; } + + StylerEditor* GetStylerEditor() { return styler_editor_.get(); } + + IEvent* RemoveEvent() { return &remove_event_; } + + private: + ui::controls::FlexLayout container_; + ui::controls::Button remove_button_; + ui::controls::TextBlock remove_button_text_; + std::unique_ptr styler_editor_; + + Event remove_event_; +}; + +class CompoundStylerEditor : public StylerEditor { + public: + CompoundStylerEditor(); + ~CompoundStylerEditor() override; + + public: + ClonablePtr GetValue(); + void SetValue(ui::style::CompoundStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr GetStyler() override { return GetValue(); } + + IEvent* ChangeEvent() { return &change_event_; } + + private: + ui::controls::FlexLayout children_container_; + std::vector> children_; + ui::components::PopupMenuTextButton add_child_button_; + + Event 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 CursorStylerEditor::GetValue() { + auto cursor_manager = + platform::gui::IUiApplication::GetInstance()->GetCursorManager(); + + std::shared_ptr 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 GetValue(); + void SetValue(ui::style::CursorStyler* styler, bool trigger_change = true); + void SetValue(const ClonablePtr& styler, + bool trigger_change = true) { + SetValue(styler.get(), trigger_change); + } + + ClonablePtr GetStyler() override { return GetValue(); } + + IEvent* ChangeEvent() { return &change_event_; } + + private: + properties::SelectPropertyEditor cursor_select_; + + Event 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 CreateStylerEditor(ui::style::Styler* styler) { + if (auto compound_styler = dynamic_cast(styler)) { + auto result = std::make_unique(); + result->SetValue(compound_styler); + return result; + } else if (auto border_styler = + dynamic_cast(styler)) { + auto editor = std::make_unique(); + editor->SetValue(border_styler); + return editor; + } else if (auto cursor_styler = + dynamic_cast(styler)) { + auto editor = std::make_unique(); + 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 GetStyler() = 0; + + private: + ui::controls::FlexLayout container_; + ui::controls::TextBlock label_; +}; + +std::unique_ptr CreateStylerEditor(ui::style::Styler* styler); +} // namespace cru::theme_builder::components::stylers -- cgit v1.2.3