aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-12-25 14:43:19 +0800
committercrupest <crupest@outlook.com>2020-12-25 14:43:19 +0800
commita14704fbd9b9fb377b7009a9fbe641a9b8d0fdfb (patch)
tree7ee33e1d51f15841ee908ec16b937985f7d9be96
parentd23cdd9c6f2fbec1329c704bde7e183b5ef07e2e (diff)
downloadcru-a14704fbd9b9fb377b7009a9fbe641a9b8d0fdfb.tar.gz
cru-a14704fbd9b9fb377b7009a9fbe641a9b8d0fdfb.tar.bz2
cru-a14704fbd9b9fb377b7009a9fbe641a9b8d0fdfb.zip
...
-rw-r--r--include/cru/ui/style/Styler.hpp48
-rw-r--r--src/ui/UiManager.cpp25
-rw-r--r--src/ui/controls/Button.cpp27
-rw-r--r--src/ui/style/Styler.cpp14
4 files changed, 79 insertions, 35 deletions
diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp
index 10b169b1..865cbbaf 100644
--- a/include/cru/ui/style/Styler.hpp
+++ b/include/cru/ui/style/Styler.hpp
@@ -3,8 +3,11 @@
#include "ApplyBorderStyleInfo.hpp"
#include "cru/common/Base.hpp"
#include "cru/common/ClonablePtr.hpp"
+#include "cru/platform/gui/Cursor.hpp"
+#include "cru/ui/controls/Control.hpp"
#include <memory>
+#include <vector>
namespace cru::ui::style {
class Styler : public Object {
@@ -14,6 +17,31 @@ class Styler : public Object {
virtual Styler* Clone() const = 0;
};
+class CompoundStyler : public Styler {
+ public:
+ template <typename... S>
+ static ClonablePtr<CompoundStyler> Create(ClonablePtr<S>... s) {
+ return ClonablePtr<CompoundStyler>(
+ new CompoundStyler(std::vector<ClonablePtr<Styler>>{std::move(s)...}));
+ }
+
+ explicit CompoundStyler(std::vector<ClonablePtr<Styler>> stylers)
+ : stylers_(std::move(stylers)) {}
+
+ void Apply(controls::Control* control) const override {
+ for (const auto& styler : stylers_) {
+ styler->Apply(control);
+ }
+ }
+
+ virtual CompoundStyler* Clone() const override {
+ return new CompoundStyler(stylers_);
+ }
+
+ private:
+ std::vector<ClonablePtr<Styler>> stylers_;
+};
+
class BorderStyler : public Styler {
public:
static ClonablePtr<BorderStyler> Create(ApplyBorderStyleInfo style) {
@@ -29,4 +57,24 @@ class BorderStyler : public Styler {
private:
ApplyBorderStyleInfo style_;
};
+
+class CursorStyler : public Styler {
+ public:
+ static ClonablePtr<CursorStyler> Create(
+ std::shared_ptr<platform::gui::ICursor> cursor) {
+ return ClonablePtr<CursorStyler>(new CursorStyler(std::move(cursor)));
+ }
+
+ static ClonablePtr<CursorStyler> Create(platform::gui::SystemCursorType type);
+
+ explicit CursorStyler(std::shared_ptr<platform::gui::ICursor> cursor)
+ : cursor_(std::move(cursor)) {}
+
+ void Apply(controls::Control* control) const override;
+
+ CursorStyler* Clone() const override { return new CursorStyler(cursor_); }
+
+ private:
+ std::shared_ptr<platform::gui::ICursor> cursor_;
+};
} // namespace cru::ui::style
diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp
index 5b4d4931..7981aa86 100644
--- a/src/ui/UiManager.cpp
+++ b/src/ui/UiManager.cpp
@@ -6,6 +6,7 @@
#include "cru/platform/graphics/Brush.hpp"
#include "cru/platform/graphics/Factory.hpp"
#include "cru/platform/graphics/Font.hpp"
+#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/style/ApplyBorderStyleInfo.hpp"
@@ -59,23 +60,31 @@ UiManager::UiManager() {
u"DefaultButton"});
theme_resource_.button_style.AddStyleRule(
{ClickStateCondition::Create(ClickState::None),
- BorderStyler::Create(ApplyBorderStyleInfo{
- CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}),
+ CompoundStyler::Create(
+ BorderStyler::Create(ApplyBorderStyleInfo{
+ CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}),
+ CursorStyler::Create(platform::gui::SystemCursorType::Arrow)),
u"DefaultButtonNormal"});
theme_resource_.button_style.AddStyleRule(
{ClickStateCondition::Create(ClickState::Hover),
- BorderStyler::Create(ApplyBorderStyleInfo{
- CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}),
+ CompoundStyler::Create(
+ BorderStyler::Create(ApplyBorderStyleInfo{
+ CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}),
+ CursorStyler::Create(platform::gui::SystemCursorType::Hand)),
u"DefaultButtonHover"});
theme_resource_.button_style.AddStyleRule(
{ClickStateCondition::Create(ClickState::Press),
- BorderStyler::Create(ApplyBorderStyleInfo{
- CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
+ CompoundStyler::Create(
+ BorderStyler::Create(ApplyBorderStyleInfo{
+ CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
+ CursorStyler::Create(platform::gui::SystemCursorType::Hand)),
u"DefaultButtonPress"});
theme_resource_.button_style.AddStyleRule(
{ClickStateCondition::Create(ClickState::PressInactive),
- BorderStyler::Create(ApplyBorderStyleInfo{
- CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
+ CompoundStyler::Create(
+ BorderStyler::Create(ApplyBorderStyleInfo{
+ CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
+ CursorStyler::Create(platform::gui::SystemCursorType::Arrow)),
u"DefaultButtonPressInactive"});
theme_resource_.text_box_style.AddStyleRule(
diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp
index 8bd9f93f..c6480b77 100644
--- a/src/ui/controls/Button.cpp
+++ b/src/ui/controls/Button.cpp
@@ -1,5 +1,4 @@
#include "cru/ui/controls/Button.hpp"
-#include <memory>
#include "../Helper.hpp"
#include "cru/platform/graphics/Brush.hpp"
@@ -10,38 +9,12 @@
#include "cru/ui/render/BorderRenderObject.hpp"
namespace cru::ui::controls {
-using cru::platform::gui::SystemCursorType;
-
-namespace {
-std::shared_ptr<platform::gui::ICursor> GetSystemCursor(SystemCursorType type) {
- return GetUiApplication()->GetCursorManager()->GetSystemCursor(type);
-}
-} // namespace
-
Button::Button() : click_detector_(this) {
render_object_ = std::make_unique<render::BorderRenderObject>();
render_object_->SetAttachedControl(this);
SetContainerRenderObject(render_object_.get());
render_object_->SetBorderEnabled(true);
- click_detector_.StateChangeEvent()->AddHandler(
- [this](const helper::ClickState& state) {
- switch (state) {
- case helper::ClickState::None:
- SetCursor(GetSystemCursor(SystemCursorType::Arrow));
- break;
- case helper::ClickState::Hover:
- SetCursor(GetSystemCursor(SystemCursorType::Hand));
- break;
- case helper::ClickState::Press:
- SetCursor(GetSystemCursor(SystemCursorType::Hand));
- break;
- case helper::ClickState::PressInactive:
- SetCursor(GetSystemCursor(SystemCursorType::Arrow));
- break;
- }
- });
-
GetStyleRuleSet()->SetParent(
&UiManager::GetInstance()->GetThemeResources()->button_style);
}
diff --git a/src/ui/style/Styler.cpp b/src/ui/style/Styler.cpp
index 823ac718..da3a2247 100644
--- a/src/ui/style/Styler.cpp
+++ b/src/ui/style/Styler.cpp
@@ -1,4 +1,9 @@
#include "cru/ui/style/Styler.hpp"
+
+#include "../Helper.hpp"
+#include "cru/common/ClonablePtr.hpp"
+#include "cru/platform/gui/Cursor.hpp"
+#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/controls/IBorderControl.hpp"
#include "cru/ui/style/ApplyBorderStyleInfo.hpp"
@@ -12,4 +17,13 @@ void BorderStyler::Apply(controls::Control *control) const {
border_control->ApplyBorderStyle(style_);
}
}
+
+ClonablePtr<CursorStyler> CursorStyler::Create(
+ platform::gui::SystemCursorType type) {
+ return Create(GetUiApplication()->GetCursorManager()->GetSystemCursor(type));
+}
+
+void CursorStyler::Apply(controls::Control *control) const {
+ control->SetCursor(cursor_);
+}
} // namespace cru::ui::style