diff options
author | crupest <crupest@outlook.com> | 2018-11-14 21:07:59 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-11-14 21:07:59 +0800 |
commit | 714a757ee435b83156ddee6b16edc99408357170 (patch) | |
tree | 637aaae9536229ef903c403e35418652732889a1 | |
parent | 77f9d6d47660b642c991809387e4e5c114760f84 (diff) | |
download | cru-714a757ee435b83156ddee6b16edc99408357170.tar.gz cru-714a757ee435b83156ddee6b16edc99408357170.tar.bz2 cru-714a757ee435b83156ddee6b16edc99408357170.zip |
Add fore/bakcground brush.
-rw-r--r-- | CruUI-Generate/cru_ui.cpp | 41 | ||||
-rw-r--r-- | CruUI-Generate/cru_ui.hpp | 31 | ||||
-rw-r--r-- | src/graph/graph.cpp | 7 | ||||
-rw-r--r-- | src/graph/graph.hpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/ui/control.cpp | 33 | ||||
-rw-r--r-- | src/ui/control.hpp | 29 |
7 files changed, 123 insertions, 22 deletions
diff --git a/CruUI-Generate/cru_ui.cpp b/CruUI-Generate/cru_ui.cpp index 252dfd9a..19c09c6e 100644 --- a/CruUI-Generate/cru_ui.cpp +++ b/CruUI-Generate/cru_ui.cpp @@ -394,6 +394,7 @@ int APIENTRY wWinMain( const auto button = Button::Create(); button->GetLayoutParams()->padding = Thickness(20, 5); button->AddChild(TextBlock::Create(L"Show popup window parenting null.")); + button->SetBackgroundBrush(cru::graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Gold))); button->mouse_click_event.AddHandler([](auto) { auto popup = Window::CreatePopup(nullptr); @@ -761,6 +762,13 @@ namespace cru::graph d2d1_factory_->ReloadSystemMetrics() ); } + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(const D2D1_COLOR_F& color) + { + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush; + ThrowIfFailed(GraphManager::GetInstance()->GetD2D1DeviceContext()->CreateSolidColorBrush(color, &brush)); + return brush; + } } //-------------------------------------------------------- //-------end of file: src\graph\graph.cpp @@ -1196,22 +1204,17 @@ namespace cru::ui } #endif - if (is_bordered_) - { - const auto border_rect = GetRect(RectRange::HalfBorder); - device_context->DrawRoundedRectangle( - D2D1::RoundedRect( - Convert(border_rect), - GetBorderProperty().GetRadiusX(), - GetBorderProperty().GetRadiusY() - ), + if (is_bordered_ && border_geometry_ != nullptr) + device_context->DrawGeometry( + border_geometry_.Get(), GetBorderProperty().GetBrush().Get(), GetBorderProperty().GetStrokeWidth(), GetBorderProperty().GetStrokeStyle().Get() ); - } //draw background. + if (in_border_geometry_ != nullptr && background_brush_ != nullptr) + device_context->FillGeometry(in_border_geometry_.Get(), background_brush_.Get()); const auto padding_rect = GetRect(RectRange::Padding); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) @@ -1231,6 +1234,10 @@ namespace cru::ui draw_content_event.Raise(args); }); + + //draw foreground. + if (in_border_geometry_ != nullptr && foreground_brush_ != nullptr) + device_context->FillGeometry(in_border_geometry_.Get(), foreground_brush_.Get()); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) { @@ -1327,6 +1334,17 @@ namespace cru::ui graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(bound_rounded_rect, &geometry) ); border_geometry_ = std::move(geometry); + + const auto in_border_rect = GetRect(RectRange::Padding); + const auto in_border_rounded_rect = D2D1::RoundedRect(Convert(in_border_rect), + GetBorderProperty().GetRadiusX() - GetBorderProperty().GetStrokeWidth() / 2.0f, + GetBorderProperty().GetRadiusY() - GetBorderProperty().GetStrokeWidth() / 2.0f); + + Microsoft::WRL::ComPtr<ID2D1RoundedRectangleGeometry> geometry2; + ThrowIfFailed( + graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(in_border_rounded_rect, &geometry2) + ); + in_border_geometry_ = std::move(geometry2); } else { @@ -1335,7 +1353,8 @@ namespace cru::ui ThrowIfFailed( graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRectangleGeometry(Convert(bound_rect), &geometry) ); - border_geometry_ = std::move(geometry); + border_geometry_ = geometry; + in_border_geometry_ = std::move(geometry); } } diff --git a/CruUI-Generate/cru_ui.hpp b/CruUI-Generate/cru_ui.hpp index 302ce611..35c7abeb 100644 --- a/CruUI-Generate/cru_ui.hpp +++ b/CruUI-Generate/cru_ui.hpp @@ -1608,6 +1608,29 @@ namespace cru::ui virtual void Repaint(); + Microsoft::WRL::ComPtr<ID2D1Brush> GetForegroundBrush() const + { + return foreground_brush_; + } + + void SetForegroundBrush(Microsoft::WRL::ComPtr<ID2D1Brush> foreground_brush) + { + foreground_brush_ = std::move(foreground_brush); + Repaint(); + } + + Microsoft::WRL::ComPtr<ID2D1Brush> GetBackgroundBrush() const + { + return background_brush_; + } + + void SetBackgroundBrush(Microsoft::WRL::ComPtr<ID2D1Brush> background_brush) + { + background_brush_ = std::move(background_brush); + Repaint(); + } + + //*************** region: focus *************** bool RequestFocus(); @@ -1724,6 +1747,8 @@ namespace cru::ui void OnDrawCore(ID2D1DeviceContext* device_context); protected: + + //*************** region: graphic events *************** virtual void OnDrawContent(ID2D1DeviceContext* device_context); virtual void OnDrawForeground(ID2D1DeviceContext* device_context); virtual void OnDrawBackground(ID2D1DeviceContext* device_context); @@ -1852,6 +1877,10 @@ namespace cru::ui BorderProperty border_property_; Microsoft::WRL::ComPtr<ID2D1Geometry> border_geometry_ = nullptr; + Microsoft::WRL::ComPtr<ID2D1Geometry> in_border_geometry_ = nullptr; //used for foreground and background brush. + + Microsoft::WRL::ComPtr<ID2D1Brush> foreground_brush_ = nullptr; + Microsoft::WRL::ComPtr<ID2D1Brush> background_brush_ = nullptr; AnyMap additional_property_map_{}; @@ -2919,6 +2948,8 @@ namespace cru::graph action(device_context); device_context->SetTransform(old_transform); } + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(const D2D1_COLOR_F& color); } //-------------------------------------------------------- //-------end of file: src\graph\graph.hpp diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index 70bec35b..eef95c8c 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -216,4 +216,11 @@ namespace cru::graph d2d1_factory_->ReloadSystemMetrics() ); } + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(const D2D1_COLOR_F& color) + { + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush; + ThrowIfFailed(GraphManager::GetInstance()->GetD2D1DeviceContext()->CreateSolidColorBrush(color, &brush)); + return brush; + } } diff --git a/src/graph/graph.hpp b/src/graph/graph.hpp index b859e82f..7771b48f 100644 --- a/src/graph/graph.hpp +++ b/src/graph/graph.hpp @@ -172,4 +172,6 @@ namespace cru::graph action(device_context); device_context->SetTransform(old_transform); } + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(const D2D1_COLOR_F& color); } diff --git a/src/main.cpp b/src/main.cpp index 76579c0d..d4194f45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "ui/controls/text_box.hpp" #include "ui/controls/list_item.hpp" #include "ui/controls/popup_menu.hpp" +#include "graph/graph.hpp" using cru::String; using cru::StringView; @@ -135,6 +136,7 @@ int APIENTRY wWinMain( const auto button = Button::Create(); button->GetLayoutParams()->padding = Thickness(20, 5); button->AddChild(TextBlock::Create(L"Show popup window parenting null.")); + button->SetBackgroundBrush(cru::graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Gold))); button->mouse_click_event.AddHandler([](auto) { auto popup = Window::CreatePopup(nullptr); diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 42e5f71f..0595bf5d 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -407,22 +407,17 @@ namespace cru::ui } #endif - if (is_bordered_) - { - const auto border_rect = GetRect(RectRange::HalfBorder); - device_context->DrawRoundedRectangle( - D2D1::RoundedRect( - Convert(border_rect), - GetBorderProperty().GetRadiusX(), - GetBorderProperty().GetRadiusY() - ), + if (is_bordered_ && border_geometry_ != nullptr) + device_context->DrawGeometry( + border_geometry_.Get(), GetBorderProperty().GetBrush().Get(), GetBorderProperty().GetStrokeWidth(), GetBorderProperty().GetStrokeStyle().Get() ); - } //draw background. + if (in_border_geometry_ != nullptr && background_brush_ != nullptr) + device_context->FillGeometry(in_border_geometry_.Get(), background_brush_.Get()); const auto padding_rect = GetRect(RectRange::Padding); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) @@ -442,6 +437,10 @@ namespace cru::ui draw_content_event.Raise(args); }); + + //draw foreground. + if (in_border_geometry_ != nullptr && foreground_brush_ != nullptr) + device_context->FillGeometry(in_border_geometry_.Get(), foreground_brush_.Get()); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) { @@ -538,6 +537,17 @@ namespace cru::ui graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(bound_rounded_rect, &geometry) ); border_geometry_ = std::move(geometry); + + const auto in_border_rect = GetRect(RectRange::Padding); + const auto in_border_rounded_rect = D2D1::RoundedRect(Convert(in_border_rect), + GetBorderProperty().GetRadiusX() - GetBorderProperty().GetStrokeWidth() / 2.0f, + GetBorderProperty().GetRadiusY() - GetBorderProperty().GetStrokeWidth() / 2.0f); + + Microsoft::WRL::ComPtr<ID2D1RoundedRectangleGeometry> geometry2; + ThrowIfFailed( + graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(in_border_rounded_rect, &geometry2) + ); + in_border_geometry_ = std::move(geometry2); } else { @@ -546,7 +556,8 @@ namespace cru::ui ThrowIfFailed( graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRectangleGeometry(Convert(bound_rect), &geometry) ); - border_geometry_ = std::move(geometry); + border_geometry_ = geometry; + in_border_geometry_ = std::move(geometry); } } diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 776eacb0..a1ad7ea7 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -129,6 +129,29 @@ namespace cru::ui virtual void Repaint(); + Microsoft::WRL::ComPtr<ID2D1Brush> GetForegroundBrush() const + { + return foreground_brush_; + } + + void SetForegroundBrush(Microsoft::WRL::ComPtr<ID2D1Brush> foreground_brush) + { + foreground_brush_ = std::move(foreground_brush); + Repaint(); + } + + Microsoft::WRL::ComPtr<ID2D1Brush> GetBackgroundBrush() const + { + return background_brush_; + } + + void SetBackgroundBrush(Microsoft::WRL::ComPtr<ID2D1Brush> background_brush) + { + background_brush_ = std::move(background_brush); + Repaint(); + } + + //*************** region: focus *************** bool RequestFocus(); @@ -245,6 +268,8 @@ namespace cru::ui void OnDrawCore(ID2D1DeviceContext* device_context); protected: + + //*************** region: graphic events *************** virtual void OnDrawContent(ID2D1DeviceContext* device_context); virtual void OnDrawForeground(ID2D1DeviceContext* device_context); virtual void OnDrawBackground(ID2D1DeviceContext* device_context); @@ -373,6 +398,10 @@ namespace cru::ui BorderProperty border_property_; Microsoft::WRL::ComPtr<ID2D1Geometry> border_geometry_ = nullptr; + Microsoft::WRL::ComPtr<ID2D1Geometry> in_border_geometry_ = nullptr; //used for foreground and background brush. + + Microsoft::WRL::ComPtr<ID2D1Brush> foreground_brush_ = nullptr; + Microsoft::WRL::ComPtr<ID2D1Brush> background_brush_ = nullptr; AnyMap additional_property_map_{}; |