diff options
author | crupest <crupest@outlook.com> | 2018-11-10 21:53:33 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-11-10 21:53:33 +0800 |
commit | e5513daa53cb958b0c83d575c440f40aaf40f562 (patch) | |
tree | c6782f0cbfe4eca466f13ef571338e6b55342860 /src/ui/controls | |
parent | cfcd03f564e82419345a7a6900fdc17c5b8c2631 (diff) | |
download | cru-e5513daa53cb958b0c83d575c440f40aaf40f562.tar.gz cru-e5513daa53cb958b0c83d575c440f40aaf40f562.tar.bz2 cru-e5513daa53cb958b0c83d575c440f40aaf40f562.zip |
...
Diffstat (limited to 'src/ui/controls')
-rw-r--r-- | src/ui/controls/list_item.cpp | 58 | ||||
-rw-r--r-- | src/ui/controls/list_item.hpp | 46 |
2 files changed, 102 insertions, 2 deletions
diff --git a/src/ui/controls/list_item.cpp b/src/ui/controls/list_item.cpp index 25dd49a8..bdd44273 100644 --- a/src/ui/controls/list_item.cpp +++ b/src/ui/controls/list_item.cpp @@ -1,6 +1,62 @@ #include "list_item.hpp" +#include "ui/ui_manager.hpp" +#include "ui/convert_util.hpp" + namespace cru::ui::controls { - + ListItem::ListItem() : Control(true) + { + const auto predefine_resources = UiManager::GetInstance()->GetPredefineResources(); + + brushes_[State::Normal].border_brush = predefine_resources->list_item_normal_border_brush; + brushes_[State::Normal].fill_brush = predefine_resources->list_item_normal_fill_brush; + brushes_[State::Hover] .border_brush = predefine_resources->list_item_hover_border_brush; + brushes_[State::Hover] .fill_brush = predefine_resources->list_item_hover_fill_brush; + brushes_[State::Select].border_brush = predefine_resources->list_item_select_border_brush; + brushes_[State::Select].fill_brush = predefine_resources->list_item_select_fill_brush; + } + + StringView ListItem::GetControlType() const + { + return control_type; + } + + void ListItem::SetState(const State state) + { + state_ = state; + Repaint(); + } + + void ListItem::OnDrawForeground(ID2D1DeviceContext* device_context) + { + const auto rect = Rect(Point::Zero(), GetRect(RectRange::Padding).GetSize()); + device_context->FillRectangle(Convert(rect), brushes_[state_].fill_brush.Get()); + device_context->DrawRectangle(Convert(rect.Shrink(Thickness(0.5))), brushes_[state_].border_brush.Get(), 1); + } + + void ListItem::OnMouseEnterCore(events::MouseEventArgs& args) + { + if (GetState() == State::Select) + return; + + if (IsAnyMouseButtonDown()) + return; + + SetState(State::Hover); + } + + void ListItem::OnMouseLeaveCore(events::MouseEventArgs& args) + { + if (GetState() == State::Select) + return; + + SetState(State::Normal); + } + + void ListItem::OnMouseClickCore(events::MouseButtonEventArgs& args) + { + if (args.GetMouseButton() == MouseButton::Left) + SetState(State::Select); + } } diff --git a/src/ui/controls/list_item.hpp b/src/ui/controls/list_item.hpp index 8525e0e8..1de89b5f 100644 --- a/src/ui/controls/list_item.hpp +++ b/src/ui/controls/list_item.hpp @@ -1,5 +1,8 @@ #pragma once +#include <map> +#include <initializer_list> + #include "ui/control.hpp" namespace cru::ui::controls @@ -9,8 +12,29 @@ namespace cru::ui::controls public: static constexpr auto control_type = L"ListItem"; + enum class State + { + Normal, + Hover, + Select + }; + + private: + struct StateBrush + { + Microsoft::WRL::ComPtr<ID2D1Brush> border_brush; + Microsoft::WRL::ComPtr<ID2D1Brush> fill_brush; + }; + public: - static ListItem* Create(); + static ListItem* Create(const std::initializer_list<Control*>& children) + { + const auto list_item = new ListItem(); + for (auto control : children) + list_item->AddChild(control); + return list_item; + } + private: ListItem(); public: @@ -19,5 +43,25 @@ namespace cru::ui::controls ListItem& operator=(const ListItem& other) = delete; ListItem& operator=(ListItem&& other) = delete; ~ListItem() override = default; + + StringView GetControlType() const override; + + State GetState() const + { + return state_; + } + + void SetState(State state); + + protected: + void OnDrawForeground(ID2D1DeviceContext* device_context) override; + + void OnMouseEnterCore(events::MouseEventArgs& args) override final; + void OnMouseLeaveCore(events::MouseEventArgs& args) override final; + void OnMouseClickCore(events::MouseButtonEventArgs& args) override final; + + private: + State state_ = State::Normal; + std::map<State, StateBrush> brushes_{}; }; } |