diff options
-rw-r--r-- | include/cru/ui/controls/button.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/ui_manager.hpp | 12 | ||||
-rw-r--r-- | src/ui/controls/button.cpp | 42 | ||||
-rw-r--r-- | src/ui/render/render_object.cpp | 4 | ||||
-rw-r--r-- | src/ui/ui_manager.cpp | 24 |
5 files changed, 54 insertions, 30 deletions
diff --git a/include/cru/ui/controls/button.hpp b/include/cru/ui/controls/button.hpp index 0fad0035..d4a03e1d 100644 --- a/include/cru/ui/controls/button.hpp +++ b/include/cru/ui/controls/button.hpp @@ -18,7 +18,7 @@ using render::CornerRadius; struct ButtonStateStyle { std::shared_ptr<platform::graph::Brush> border_brush; Thickness border_thickness; - CornerRadius corner_radius; + CornerRadius border_radius; std::shared_ptr<platform::graph::Brush> foreground_brush; std::shared_ptr<platform::graph::Brush> background_brush; }; diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index d2b8ad53..5501c62c 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -2,6 +2,8 @@ #include "base.hpp" #include "cru/common/base.hpp" +#include "controls/button.hpp" + #include <memory> namespace cru::platform::graph { @@ -29,6 +31,10 @@ class PredefineResources : public Object { std::shared_ptr<platform::graph::Font> text_block_font; }; +struct ThemeResources { + controls::ButtonStyle button_style; +}; + class UiManager : public Object { public: static UiManager* GetInstance(); @@ -47,7 +53,13 @@ class UiManager : public Object { return predefine_resources_.get(); } + ThemeResources* GetThemeResources() { + return &theme_resource_; + } + private: std::unique_ptr<PredefineResources> predefine_resources_; + + ThemeResources theme_resource_; }; } // namespace cru::ui diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp index 1a1f4286..2822f6db 100644 --- a/src/ui/controls/button.cpp +++ b/src/ui/controls/button.cpp @@ -14,33 +14,23 @@ namespace cru::ui::controls { using platform::native::GetSystemCursor; using platform::native::SystemCursor; -Button::Button() : click_detector_(this) { - // const auto predefined_resource = - // UiManager::GetInstance()->GetPredefineResources(); - - const auto factory = platform::graph::GraphFactory::GetInstance(); - border_style_.normal.brush = std::shared_ptr<platform::graph::Brush>( - factory->CreateSolidColorBrush(Color::FromHex(0x00bfff))); - border_style_.hover.brush = std::shared_ptr<platform::graph::Brush>( - factory->CreateSolidColorBrush(Color::FromHex(0x47d1ff))); - border_style_.press.brush = std::shared_ptr<platform::graph::Brush>( - factory->CreateSolidColorBrush(Color::FromHex(0x91e4ff))); - border_style_.press_cancel.brush = std::shared_ptr<platform::graph::Brush>( - factory->CreateSolidColorBrush(Color::FromHex(0x91e4ff))); - - border_style_.normal.thickness = border_style_.hover.thickness = - border_style_.press.thickness = border_style_.press_cancel.thickness = - Thickness{3}; +namespace { +void Set(render::BorderRenderObject* o, const ButtonStateStyle& s) { + o->SetBorderBrush(s.border_brush); + o->SetBorderThickness(s.border_thickness); + o->SetBorderRadius(s.border_radius); + o->SetForegroundBrush(s.foreground_brush); + o->SetBackgroundBrush(s.background_brush); +} +} // namespace - border_style_.normal.corner_radius = border_style_.hover.corner_radius = - border_style_.press.corner_radius = - border_style_.press_cancel.corner_radius = - render::CornerRadius{Point{10, 5}}; +Button::Button() : click_detector_(this) { + style_ = UiManager::GetInstance()->GetThemeResources()->button_style; render_object_.reset(new render::BorderRenderObject); render_object_->SetAttachedControl(this); + Set(render_object_.get(), style_.normal); render_object_->SetBorderEnabled(true); - (*render_object_->GetBorderStyle()) = border_style_.normal; MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs& args) { if (click_detector_.GetPressingButton() & trigger_button_) { @@ -84,19 +74,19 @@ void Button::OnChildChanged(Control* old_child, Control* new_child) { void Button::OnStateChange(ButtonState oldState, ButtonState newState) { switch (newState) { case ButtonState::Normal: - (*render_object_->GetBorderStyle()) = border_style_.normal; + Set(render_object_.get(), style_.normal); SetCursor(GetSystemCursor(SystemCursor::Arrow)); break; case ButtonState::Hover: - (*render_object_->GetBorderStyle()) = border_style_.hover; + Set(render_object_.get(), style_.hover); SetCursor(GetSystemCursor(SystemCursor::Hand)); break; case ButtonState::Press: - (*render_object_->GetBorderStyle()) = border_style_.press; + Set(render_object_.get(), style_.press); SetCursor(GetSystemCursor(SystemCursor::Hand)); break; case ButtonState::PressCancel: - (*render_object_->GetBorderStyle()) = border_style_.press_cancel; + Set(render_object_.get(), style_.press_cancel); SetCursor(GetSystemCursor(SystemCursor::Arrow)); break; } diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 118c68c4..a28b4f03 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -7,8 +7,8 @@ namespace cru::ui::render { void RenderObject::AddChild(RenderObject* render_object, const int position) { - assert(child_mode_ == ChildMode::None); - assert(child_mode_ == ChildMode::Single && children_.size() > 0); + assert(child_mode_ != ChildMode::None); + assert(!(child_mode_ == ChildMode::Single && children_.size() > 0)); assert(render_object->GetParent() == nullptr); // Render object already has a parent. diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index fa3304fb..f60f6355 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -28,5 +28,27 @@ UiManager* UiManager::GetInstance() { return instance; } -UiManager::UiManager() : predefine_resources_(new PredefineResources()) {} +UiManager::UiManager() : predefine_resources_(new PredefineResources()) { + const auto factory = GraphFactory::GetInstance(); + theme_resource_.button_style.normal.border_brush = std::shared_ptr<platform::graph::Brush>( + factory->CreateSolidColorBrush(Color::FromHex(0x00bfff))); + theme_resource_.button_style.hover.border_brush = std::shared_ptr<platform::graph::Brush>( + factory->CreateSolidColorBrush(Color::FromHex(0x47d1ff))); + theme_resource_.button_style.press.border_brush = std::shared_ptr<platform::graph::Brush>( + factory->CreateSolidColorBrush(Color::FromHex(0x91e4ff))); + theme_resource_.button_style.press_cancel.border_brush = std::shared_ptr<platform::graph::Brush>( + factory->CreateSolidColorBrush(Color::FromHex(0x91e4ff))); + + theme_resource_.button_style.normal.border_thickness = + theme_resource_.button_style.hover.border_thickness = + theme_resource_.button_style.press.border_thickness = + theme_resource_.button_style.press_cancel.border_thickness = + Thickness(3); + + theme_resource_.button_style.normal.border_radius = + theme_resource_.button_style.hover.border_radius = + theme_resource_.button_style.press.border_radius = + theme_resource_.button_style.press_cancel.border_radius = + controls::CornerRadius({5, 5}); +} } // namespace cru::ui |