From a465a23bdccff1680f056f61b321be717627601e Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 10 Nov 2018 20:36:02 +0800 Subject: Add foreground/background support. --- src/ui/control.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++------------ src/ui/control.hpp | 8 ++++++-- 2 files changed, 48 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 63d322b3..5d15e287 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -202,16 +202,7 @@ namespace cru::ui const auto position = GetPositionRelative(); device_context->SetTransform(old_transform * D2D1::Matrix3x2F::Translation(position.x, position.y)); - OnDraw(device_context); - - const auto rect = GetRect(RectRange::Content); - graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(rect.left, rect.top), - [this](ID2D1DeviceContext* device_context) - { - OnDrawContent(device_context); - DrawEventArgs args(this, this, device_context); - draw_event.Raise(args); - }); + OnDrawCore(device_context); for (auto child : GetChildren()) child->Draw(device_context); @@ -385,14 +376,15 @@ namespace cru::ui window_ = nullptr; } + inline D2D1_RECT_F Convert(const Rect& rect) { return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, rect.top + rect.height); } - void Control::OnDraw(ID2D1DeviceContext* device_context) + void Control::OnDrawCore(ID2D1DeviceContext* device_context) { -#ifdef CRU_DEBUG_LAYOUT + #ifdef CRU_DEBUG_LAYOUT if (GetWindow()->IsDebugLayout()) { if (padding_geometry_ != nullptr) @@ -417,6 +409,34 @@ namespace cru::ui GetBorderProperty().GetStrokeStyle().Get() ); } + + //draw background. + const auto padding_rect = GetRect(RectRange::Padding); + graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), + [this](ID2D1DeviceContext* device_context) + { + OnDrawBackground(device_context); + DrawEventArgs args(this, this, device_context); + draw_background_event.Raise(args); + }); + + + const auto rect = GetRect(RectRange::Content); + graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(rect.left, rect.top), + [this](ID2D1DeviceContext* device_context) + { + OnDrawContent(device_context); + DrawEventArgs args(this, this, device_context); + draw_content_event.Raise(args); + }); + + graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), + [this](ID2D1DeviceContext* device_context) + { + OnDrawForeground(device_context); + DrawEventArgs args(this, this, device_context); + draw_foreground_event.Raise(args); + }); } void Control::OnDrawContent(ID2D1DeviceContext * device_context) @@ -424,6 +444,16 @@ namespace cru::ui } + void Control::OnDrawForeground(ID2D1DeviceContext* device_context) + { + + } + + void Control::OnDrawBackground(ID2D1DeviceContext* device_context) + { + + } + void Control::OnPositionChanged(PositionChangedEventArgs & args) { diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 8ac9ae72..7afc0c4e 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -221,7 +221,9 @@ namespace cru::ui events::FocusChangeEvent get_focus_event; events::FocusChangeEvent lose_focus_event; - events::DrawEvent draw_event; + events::DrawEvent draw_content_event; + events::DrawEvent draw_background_event; + events::DrawEvent draw_foreground_event; events::PositionChangedEvent position_changed_event; events::SizeChangedEvent size_changed_event; @@ -238,10 +240,12 @@ namespace cru::ui virtual void OnDetachToWindow(Window* window); private: - void OnDraw(ID2D1DeviceContext* device_context); + void OnDrawCore(ID2D1DeviceContext* device_context); protected: virtual void OnDrawContent(ID2D1DeviceContext* device_context); + virtual void OnDrawForeground(ID2D1DeviceContext* device_context); + virtual void OnDrawBackground(ID2D1DeviceContext* device_context); // For a event, the window event system will first dispatch event to core functions. // Therefore for particular controls, you should do essential actions in core functions, -- cgit v1.2.3