From 06d1d0442276a05b6caad6e3468f4afb1e8ee5df Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 28 Jun 2020 00:03:11 +0800 Subject: ... --- src/ui/controls/Button.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++ src/ui/controls/Container.cpp | 18 +++++++++++ src/ui/controls/button.cpp | 73 ------------------------------------------- src/ui/controls/container.cpp | 18 ----------- 4 files changed, 91 insertions(+), 91 deletions(-) create mode 100644 src/ui/controls/Button.cpp create mode 100644 src/ui/controls/Container.cpp delete mode 100644 src/ui/controls/button.cpp delete mode 100644 src/ui/controls/container.cpp (limited to 'src/ui/controls') diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp new file mode 100644 index 00000000..6f6af878 --- /dev/null +++ b/src/ui/controls/Button.cpp @@ -0,0 +1,73 @@ +#include "cru/ui/controls/Button.hpp" +#include + +#include "../Helper.hpp" +#include "cru/platform/graph/Brush.hpp" +#include "cru/platform/native/Cursor.hpp" +#include "cru/platform/native/UiApplication.hpp" +#include "cru/ui/render/BorderRenderObject.hpp" +#include "cru/ui/UiManager.hpp" +#include "cru/ui/Window.hpp" + +namespace cru::ui::controls { +using cru::platform::native::SystemCursorType; + +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); +} + +std::shared_ptr GetSystemCursor( + SystemCursorType type) { + return GetUiApplication()->GetCursorManager()->GetSystemCursor(type); +} +} // namespace + +Button::Button() : click_detector_(this) { + style_ = UiManager::GetInstance()->GetThemeResources()->button_style; + + render_object_ = std::make_unique(); + render_object_->SetAttachedControl(this); + Set(render_object_.get(), style_.normal); + render_object_->SetBorderEnabled(true); + + click_detector_.StateChangeEvent()->AddHandler( + [this](const ClickState& state) { + switch (state) { + case ClickState::None: + Set(render_object_.get(), style_.normal); + SetCursor(GetSystemCursor(SystemCursorType::Arrow)); + break; + case ClickState::Hover: + Set(render_object_.get(), style_.hover); + SetCursor(GetSystemCursor(SystemCursorType::Hand)); + break; + case ClickState::Press: + Set(render_object_.get(), style_.press); + SetCursor(GetSystemCursor(SystemCursorType::Hand)); + break; + case ClickState::PressInactive: + Set(render_object_.get(), style_.press_cancel); + SetCursor(GetSystemCursor(SystemCursorType::Arrow)); + break; + } + }); +} + +Button::~Button() = default; + +render::RenderObject* Button::GetRenderObject() const { + return render_object_.get(); +} + +void Button::OnChildChanged(Control* old_child, Control* new_child) { + if (old_child != nullptr) render_object_->RemoveChild(0); + if (new_child != nullptr) + render_object_->AddChild(new_child->GetRenderObject(), 0); +} + +} // namespace cru::ui::controls diff --git a/src/ui/controls/Container.cpp b/src/ui/controls/Container.cpp new file mode 100644 index 00000000..de58ee64 --- /dev/null +++ b/src/ui/controls/Container.cpp @@ -0,0 +1,18 @@ +#include "cru/ui/controls/Container.hpp" + +#include "cru/platform/graph/Factory.hpp" +#include "cru/ui/render/BorderRenderObject.hpp" + +namespace cru::ui::controls { +Container::Container() { + render_object_ = std::make_unique(); + render_object_->SetBorderEnabled(false); +} + +Container::~Container() = default; + +void Container::OnChildChanged(Control*, Control* new_child) { + render_object_->RemoveChild(0); + render_object_->AddChild(new_child->GetRenderObject(), 0); +} +} // namespace cru::ui::controls diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp deleted file mode 100644 index 6f6af878..00000000 --- a/src/ui/controls/button.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "cru/ui/controls/Button.hpp" -#include - -#include "../Helper.hpp" -#include "cru/platform/graph/Brush.hpp" -#include "cru/platform/native/Cursor.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/ui/render/BorderRenderObject.hpp" -#include "cru/ui/UiManager.hpp" -#include "cru/ui/Window.hpp" - -namespace cru::ui::controls { -using cru::platform::native::SystemCursorType; - -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); -} - -std::shared_ptr GetSystemCursor( - SystemCursorType type) { - return GetUiApplication()->GetCursorManager()->GetSystemCursor(type); -} -} // namespace - -Button::Button() : click_detector_(this) { - style_ = UiManager::GetInstance()->GetThemeResources()->button_style; - - render_object_ = std::make_unique(); - render_object_->SetAttachedControl(this); - Set(render_object_.get(), style_.normal); - render_object_->SetBorderEnabled(true); - - click_detector_.StateChangeEvent()->AddHandler( - [this](const ClickState& state) { - switch (state) { - case ClickState::None: - Set(render_object_.get(), style_.normal); - SetCursor(GetSystemCursor(SystemCursorType::Arrow)); - break; - case ClickState::Hover: - Set(render_object_.get(), style_.hover); - SetCursor(GetSystemCursor(SystemCursorType::Hand)); - break; - case ClickState::Press: - Set(render_object_.get(), style_.press); - SetCursor(GetSystemCursor(SystemCursorType::Hand)); - break; - case ClickState::PressInactive: - Set(render_object_.get(), style_.press_cancel); - SetCursor(GetSystemCursor(SystemCursorType::Arrow)); - break; - } - }); -} - -Button::~Button() = default; - -render::RenderObject* Button::GetRenderObject() const { - return render_object_.get(); -} - -void Button::OnChildChanged(Control* old_child, Control* new_child) { - if (old_child != nullptr) render_object_->RemoveChild(0); - if (new_child != nullptr) - render_object_->AddChild(new_child->GetRenderObject(), 0); -} - -} // namespace cru::ui::controls diff --git a/src/ui/controls/container.cpp b/src/ui/controls/container.cpp deleted file mode 100644 index de58ee64..00000000 --- a/src/ui/controls/container.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "cru/ui/controls/Container.hpp" - -#include "cru/platform/graph/Factory.hpp" -#include "cru/ui/render/BorderRenderObject.hpp" - -namespace cru::ui::controls { -Container::Container() { - render_object_ = std::make_unique(); - render_object_->SetBorderEnabled(false); -} - -Container::~Container() = default; - -void Container::OnChildChanged(Control*, Control* new_child) { - render_object_->RemoveChild(0); - render_object_->AddChild(new_child->GetRenderObject(), 0); -} -} // namespace cru::ui::controls -- cgit v1.2.3