aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/toggle_button.cpp
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2018-11-28 19:32:58 +0800
committerGitHub <noreply@github.com>2018-11-28 19:32:58 +0800
commitdd75868a05cfc9d71b26f1197022c03ba481fe33 (patch)
tree6f50579da8be5b6be8792b0d832b76ba822c2760 /src/ui/controls/toggle_button.cpp
parentee22597122612cd75fe62f5d808cb51478373fad (diff)
parentd98e9ef82be277190c883e4ee7547516dbd620b6 (diff)
downloadcru-dd75868a05cfc9d71b26f1197022c03ba481fe33.tar.gz
cru-dd75868a05cfc9d71b26f1197022c03ba481fe33.tar.bz2
cru-dd75868a05cfc9d71b26f1197022c03ba481fe33.zip
Merge pull request #30 from crupest/routed-event
Develop routed events.
Diffstat (limited to 'src/ui/controls/toggle_button.cpp')
-rw-r--r--src/ui/controls/toggle_button.cpp74
1 files changed, 30 insertions, 44 deletions
diff --git a/src/ui/controls/toggle_button.cpp b/src/ui/controls/toggle_button.cpp
index e3d8662a..874a245f 100644
--- a/src/ui/controls/toggle_button.cpp
+++ b/src/ui/controls/toggle_button.cpp
@@ -3,6 +3,7 @@
#include "graph/graph.hpp"
#include "ui/animations/animation.hpp"
#include "ui/ui_manager.hpp"
+#include "ui/convert_util.hpp"
namespace cru::ui::controls
{
@@ -21,13 +22,34 @@ namespace cru::ui::controls
on_brush_ = UiManager::GetInstance()->GetPredefineResources()->toggle_button_on_brush;
off_brush_ = UiManager::GetInstance()->GetPredefineResources()->toggle_button_off_brush;
- }
- inline D2D1_POINT_2F ConvertPoint(const Point& point)
- {
- return D2D1::Point2F(point.x, point.y);
+ draw_content_event.AddHandler([this](events::DrawEventArgs& args)
+ {
+ const auto device_context = args.GetDeviceContext();
+ const auto size = GetSize();
+ graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(size.width / 2, size.height / 2), [this](ID2D1DeviceContext* device_context)
+ {
+ if (state_)
+ {
+ device_context->DrawGeometry(frame_path_.Get(), on_brush_.Get(), stroke_width);
+ device_context->FillEllipse(D2D1::Ellipse(D2D1::Point2F(current_circle_position_, 0), inner_circle_radius, inner_circle_radius), on_brush_.Get());
+ }
+ else
+ {
+ device_context->DrawGeometry(frame_path_.Get(), off_brush_.Get(), stroke_width);
+ device_context->FillEllipse(D2D1::Ellipse(D2D1::Point2F(current_circle_position_, 0), inner_circle_radius, inner_circle_radius), off_brush_.Get());
+ }
+ });
+ });
+
+ mouse_click_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args)
+ {
+ if (args.GetMouseButton() == MouseButton::Left)
+ Toggle();
+ });
}
+
StringView ToggleButton::GetControlType() const
{
return control_type;
@@ -38,9 +60,9 @@ namespace cru::ui::controls
const auto size = GetSize();
const auto transform = D2D1::Matrix3x2F::Translation(size.width / 2, size.height / 2);
BOOL contains;
- frame_path_->FillContainsPoint(ConvertPoint(point), transform, &contains);
+ frame_path_->FillContainsPoint(Convert(point), transform, &contains);
if (!contains)
- frame_path_->StrokeContainsPoint(ConvertPoint(point), stroke_width, nullptr, transform, &contains);
+ frame_path_->StrokeContainsPoint(Convert(point), stroke_width, nullptr, transform, &contains);
return contains != 0;
}
@@ -72,7 +94,8 @@ namespace cru::ui::controls
})
.Start();
- RaiseToggleEvent(state);
+ events::ToggleEventArgs args(this, this, state);
+ toggle_event.Raise(args);
InvalidateDraw();
}
}
@@ -82,36 +105,6 @@ namespace cru::ui::controls
SetState(!GetState());
}
- void ToggleButton::OnToggle(events::ToggleEventArgs& args)
- {
-
- }
-
- void ToggleButton::OnDrawContent(ID2D1DeviceContext* device_context)
- {
- Control::OnDrawContent(device_context);
- const auto size = GetSize();
- graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(size.width / 2, size.height / 2), [this](ID2D1DeviceContext* device_context)
- {
- if (state_)
- {
- device_context->DrawGeometry(frame_path_.Get(), on_brush_.Get(), stroke_width);
- device_context->FillEllipse(D2D1::Ellipse(D2D1::Point2F(current_circle_position_, 0), inner_circle_radius, inner_circle_radius), on_brush_.Get());
- }
- else
- {
- device_context->DrawGeometry(frame_path_.Get(), off_brush_.Get(), stroke_width);
- device_context->FillEllipse(D2D1::Ellipse(D2D1::Point2F(current_circle_position_, 0), inner_circle_radius, inner_circle_radius), off_brush_.Get());
- }
- });
- }
-
- void ToggleButton::OnMouseClickCore(events::MouseButtonEventArgs& args)
- {
- Control::OnMouseClickCore(args);
- Toggle();
- }
-
Size ToggleButton::OnMeasureContent(const Size& available_size)
{
const Size result_size(
@@ -121,11 +114,4 @@ namespace cru::ui::controls
return result_size;
}
-
- void ToggleButton::RaiseToggleEvent(bool new_state)
- {
- events::ToggleEventArgs args(this, this, new_state);
- OnToggle(args);
- toggle_event.Raise(args);
- }
}