diff options
author | 杨宇千 <crupest@outlook.com> | 2018-11-10 22:39:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-10 22:39:26 +0800 |
commit | 8b04c0dd788be75c2dd7d8f58aebc7d6bf6752df (patch) | |
tree | 2d00b4f6a7af93a13d271d78e6ef682c335c91c7 /src/ui/controls | |
parent | 7c2fb4578b6997b5ab0d98121cda253f734139c1 (diff) | |
parent | b2eced8d9719eb00796c2674fc2c23ab0c9bbdbf (diff) | |
download | cru-8b04c0dd788be75c2dd7d8f58aebc7d6bf6752df.tar.gz cru-8b04c0dd788be75c2dd7d8f58aebc7d6bf6752df.tar.bz2 cru-8b04c0dd788be75c2dd7d8f58aebc7d6bf6752df.zip |
Merge pull request #11 from crupest/listitem
Add ListItem.
Diffstat (limited to 'src/ui/controls')
-rw-r--r-- | src/ui/controls/list_item.cpp | 62 | ||||
-rw-r--r-- | src/ui/controls/list_item.hpp | 67 |
2 files changed, 129 insertions, 0 deletions
diff --git a/src/ui/controls/list_item.cpp b/src/ui/controls/list_item.cpp new file mode 100644 index 00000000..bdd44273 --- /dev/null +++ b/src/ui/controls/list_item.cpp @@ -0,0 +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 new file mode 100644 index 00000000..1de89b5f --- /dev/null +++ b/src/ui/controls/list_item.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include <map> +#include <initializer_list> + +#include "ui/control.hpp" + +namespace cru::ui::controls +{ + class ListItem : public Control + { + 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(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: + ListItem(const ListItem& other) = delete; + ListItem(ListItem&& other) = delete; + 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_{}; + }; +} |