aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-10-02 00:15:52 +0800
committercrupest <crupest@outlook.com>2018-10-02 00:15:52 +0800
commit3b7e3d0ca26b526c8c4e6fc335085ac28d63bbab (patch)
tree095da7f36e7f4e150080feb753a2995fe2bac8c3 /src
parentc9a423ef94f684ff21e79526f77f8ddc31a2100d (diff)
downloadcru-3b7e3d0ca26b526c8c4e6fc335085ac28d63bbab.tar.gz
cru-3b7e3d0ca26b526c8c4e6fc335085ac28d63bbab.tar.bz2
cru-3b7e3d0ca26b526c8c4e6fc335085ac28d63bbab.zip
Make button use border delegate.
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp8
-rw-r--r--src/ui/controls/border_delegate.cpp9
-rw-r--r--src/ui/controls/border_delegate.h6
-rw-r--r--src/ui/controls/button.cpp22
-rw-r--r--src/ui/controls/button.h8
5 files changed, 43 insertions, 10 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 4f711208..e11fbbe0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -143,12 +143,20 @@ int APIENTRY wWinMain(
window.AddChild(layout);
*/
+ /*
window.AddChild(
CreateWithLayout<Border>(LayoutSideParams::Exactly(200), LayoutSideParams::Content(),
std::initializer_list<cru::ui::Control*>{
CreateWithLayout<TextBox>(LayoutSideParams::Stretch(), LayoutSideParams::Content())
}
));
+ */
+
+ window.AddChild(
+ Button::Create(
+ {TextBlock::Create(L"Button")}
+ )
+ );
window.Show();
diff --git a/src/ui/controls/border_delegate.cpp b/src/ui/controls/border_delegate.cpp
index 4031d787..ec7ba437 100644
--- a/src/ui/controls/border_delegate.cpp
+++ b/src/ui/controls/border_delegate.cpp
@@ -61,12 +61,19 @@ namespace cru::ui::controls
border_property_->RemovePropertyChangedListener(border_property_changed_listener_);
border_property_ = std::move(border_property);
border_property_->AddPropertyChangedListener(border_property_changed_listener_);
+ control_->Repaint();
}
void BorderDelegate::Draw(ID2D1DeviceContext* device_context, const Size& size) const
{
device_context->DrawRoundedRectangle(
- D2D1::RoundedRect(D2D1::RectF(0.0f, 0.0f, size.width, size.height),
+ D2D1::RoundedRect(
+ D2D1::RectF(
+ border_property_->GetWidth() / 2.0f,
+ border_property_->GetWidth() / 2.0f,
+ size.width - border_property_->GetWidth(),
+ size.height - border_property_->GetWidth()
+ ),
border_property_->GetRadiusX(),
border_property_->GetRadiusY()
),
diff --git a/src/ui/controls/border_delegate.h b/src/ui/controls/border_delegate.h
index 12c5444a..5a7dfbed 100644
--- a/src/ui/controls/border_delegate.h
+++ b/src/ui/controls/border_delegate.h
@@ -7,6 +7,12 @@ namespace cru::ui::controls
class BorderProperty : public PropertyChangedNotifyObject
{
public:
+ using Ptr = std::shared_ptr<BorderProperty>;
+ static Ptr Create()
+ {
+ return std::make_shared<BorderProperty>();
+ }
+
constexpr static auto brush_property_name = L"Brush";
constexpr static auto width_property_name = L"Width";
constexpr static auto stroke_style_property_name = L"StrokeStyle";
diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp
index b7614f93..630c0c39 100644
--- a/src/ui/controls/button.cpp
+++ b/src/ui/controls/button.cpp
@@ -8,26 +8,36 @@ namespace cru::ui::controls
Button::Button() : Control(true)
{
- normal_border_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::RoyalBlue));
- pressed_border_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::MediumBlue));
- current_border_brush_ = normal_border_brush_.Get();
+ normal_border_border_ = BorderProperty::Create();
+ normal_border_border_->SetBrush(CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::RoyalBlue)));
+ normal_border_border_->SetWidth(2);
+ normal_border_border_->SetRadiusX(6);
+ normal_border_border_->SetRadiusY(6);
+
+ pressed_border_border_ = BorderProperty::Create();
+ pressed_border_border_->SetBrush(CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Blue)));
+ pressed_border_border_->SetWidth(2);
+ pressed_border_border_->SetRadiusX(6);
+ pressed_border_border_->SetRadiusY(6);
+
+ border_delegate_ = std::make_unique<BorderDelegate>(this, normal_border_border_);
}
void Button::OnDraw(ID2D1DeviceContext* device_context)
{
Control::OnDraw(device_context);
- device_context->DrawRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(0, 0, GetSize().width, GetSize().height), 6, 6), current_border_brush_, 2);
+ border_delegate_->Draw(device_context, GetSize());
}
void Button::OnMouseClickBegin(MouseButton button)
{
- current_border_brush_ = pressed_border_brush_.Get();
+ border_delegate_->SetBorderProperty(pressed_border_border_);
Repaint();
}
void Button::OnMouseClickEnd(MouseButton button)
{
- current_border_brush_ = normal_border_brush_.Get();
+ border_delegate_->SetBorderProperty(normal_border_border_);
Repaint();
}
}
diff --git a/src/ui/controls/button.h b/src/ui/controls/button.h
index bd3f6eb3..7a303984 100644
--- a/src/ui/controls/button.h
+++ b/src/ui/controls/button.h
@@ -3,6 +3,7 @@
#include <initializer_list>
#include "ui/control.h"
+#include "border_delegate.h"
namespace cru::ui::controls
{
@@ -34,8 +35,9 @@ namespace cru::ui::controls
void OnMouseClickEnd(MouseButton button) override final;
private:
- Microsoft::WRL::ComPtr<ID2D1Brush> normal_border_brush_;
- Microsoft::WRL::ComPtr<ID2D1Brush> pressed_border_brush_;
- ID2D1Brush* current_border_brush_;
+ std::unique_ptr<BorderDelegate> border_delegate_;
+
+ BorderProperty::Ptr normal_border_border_;
+ BorderProperty::Ptr pressed_border_border_;
};
}