From ae6f797561cdfa438ebef1fbbf94d784d315e655 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 1 Jan 2020 17:38:45 +0800 Subject: ... --- include/cru/common/base.hpp | 67 +++++++++++++++++++++++++++++ include/cru/common/event.hpp | 17 +++++--- include/cru/platform/native/basic_types.hpp | 6 ++- include/cru/ui/click_detector.hpp | 38 ++++++++-------- 4 files changed, 100 insertions(+), 28 deletions(-) (limited to 'include') diff --git a/include/cru/common/base.hpp b/include/cru/common/base.hpp index 4264142d..d4f164bf 100644 --- a/include/cru/common/base.hpp +++ b/include/cru/common/base.hpp @@ -2,6 +2,7 @@ #include "pre_config.hpp" #include +#include #define CRU_UNUSED(entity) static_cast(entity); @@ -40,4 +41,70 @@ struct Interface { [[noreturn]] inline void UnreachableCode() { throw std::runtime_error("Unreachable code."); } + } // namespace cru + +template +struct enable_bitmask_operators : std::false_type {}; + +#define CRU_ENABLE_BITMASK_OPERATORS(enumname) \ + template <> \ + struct enable_bitmask_operators : std::true_type {}; + +template +auto operator|(Enum lhs, Enum rhs) + -> std::enable_if_t::value, Enum> { + using underlying = typename std::underlying_type::type; + return static_cast(static_cast(lhs) | + static_cast(rhs)); +} + +template +auto operator&(Enum lhs, Enum rhs) + -> std::enable_if_t::value, Enum> { + using underlying = typename std::underlying_type::type; + return static_cast(static_cast(lhs) & + static_cast(rhs)); +} + +template +auto operator^(Enum lhs, Enum rhs) + -> std::enable_if_t::value, Enum> { + using underlying = typename std::underlying_type::type; + return static_cast(static_cast(lhs) ^ + static_cast(rhs)); +} + +template +auto operator~(Enum rhs) + -> std::enable_if_t::value, Enum> { + using underlying = typename std::underlying_type::type; + return static_cast(~static_cast(rhs)); +} + +template +auto operator|=(Enum& lhs, Enum rhs) + -> std::enable_if_t::value, Enum&> { + using underlying = typename std::underlying_type::type; + lhs = static_cast(static_cast(lhs) | + static_cast(rhs)); + return lhs; +} + +template +auto operator&=(Enum& lhs, Enum rhs) + -> std::enable_if_t::value, Enum&> { + using underlying = typename std::underlying_type::type; + lhs = static_cast(static_cast(lhs) & + static_cast(rhs)); + return lhs; +} + +template +auto operator^=(Enum& lhs, Enum rhs) + -> std::enable_if_t::value, Enum&> { + using underlying = typename std::underlying_type::type; + lhs = static_cast(static_cast(lhs) ^ + static_cast(rhs)); + return lhs; +} diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index 33b2e3f6..55a00d7e 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -3,6 +3,7 @@ #include "self_resolvable.hpp" +#include #include #include #include @@ -99,7 +100,8 @@ struct IEvent { IEvent(IEvent&& other) = delete; IEvent& operator=(const IEvent& other) = delete; IEvent& operator=(IEvent&& other) = delete; - ~IEvent() = default; + ~IEvent() = default; // Note that user can't destroy a Event via IEvent. So + // destructor should be protected. public: virtual EventRevoker AddHandler(const EventHandler& handler) = 0; @@ -130,14 +132,16 @@ class Event : public details::EventBase, public IEvent { EventRevoker AddHandler(const EventHandler& handler) override { const auto token = current_token_++; - this->handler_data_list_.emplace_after(this->last_handler_iterator_ ,token, handler); + this->handler_data_list_.emplace_after(this->last_handler_iterator_, token, + handler); ++(this->last_handler_iterator_); return CreateRevoker(token); } EventRevoker AddHandler(EventHandler&& handler) override { const auto token = current_token_++; - this->handler_data_list_.emplace_after(this->last_handler_iterator_ ,token, std::move(handler)); + this->handler_data_list_.emplace_after(this->last_handler_iterator_, token, + std::move(handler)); ++(this->last_handler_iterator_); return CreateRevoker(token); } @@ -163,12 +167,15 @@ class Event : public details::EventBase, public IEvent { protected: void RemoveHandler(EventHandlerToken token) override { this->handler_data_list_.remove_if( - [token](const HandlerData& data) { return data.token == token; }); + [token](const HandlerData& data) { return data.token == token; }); } private: std::forward_list handler_data_list_{}; - typename std::forward_list::const_iterator last_handler_iterator_ = this->handler_data_list_.cbefore_begin(); // remember the last handler to make push back O(1) + typename std::forward_list< + HandlerData>::const_iterator last_handler_iterator_ = + this->handler_data_list_ + .cbefore_begin(); // remember the last handler to make push back O(1) EventHandlerToken current_token_ = 0; }; diff --git a/include/cru/platform/native/basic_types.hpp b/include/cru/platform/native/basic_types.hpp index a53fa671..247df06d 100644 --- a/include/cru/platform/native/basic_types.hpp +++ b/include/cru/platform/native/basic_types.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/pre_config.hpp" +#include "cru/common/base.hpp" namespace cru::platform::native { struct Dpi { @@ -7,5 +7,7 @@ struct Dpi { float y; }; -enum MouseButton : unsigned { Left = 0b1, Right = 0b10, Middle = 0b100 }; +enum class MouseButton : unsigned { Left = 0b1, Right = 0b10, Middle = 0b100 }; } // namespace cru::platform::native + +CRU_ENABLE_BITMASK_OPERATORS(::cru::platform::native::MouseButton) diff --git a/include/cru/ui/click_detector.hpp b/include/cru/ui/click_detector.hpp index 1a88b8a6..d5e4ac78 100644 --- a/include/cru/ui/click_detector.hpp +++ b/include/cru/ui/click_detector.hpp @@ -1,9 +1,8 @@ #pragma once #include "control.hpp" -#include -#include #include +#include namespace cru::ui { class ClickEventArgs : Object { @@ -32,33 +31,30 @@ class ClickEventArgs : Object { MouseButton button_; }; +enum class ClickState { + None, // Mouse is outside the control. + Hover, // Mouse hovers on the control but not pressed + Press, // Mouse is pressed and if released click is done. + PressInactive // Mouse is pressed but if released click is canceled. +}; + class ClickDetector : public Object { public: explicit ClickDetector(Control* control); - ClickDetector(const ClickDetector& other) = delete; - ClickDetector& operator=(const ClickDetector& other) = delete; - ClickDetector(ClickDetector&& other) = delete; - ClickDetector& operator=(ClickDetector&& other) = delete; + CRU_DELETE_COPY(ClickDetector) + CRU_DELETE_MOVE(ClickDetector) ~ClickDetector() override = default; Control* GetControl() const { return control_; } - // Return a union of buttons being pressed. Return 0 if no button is being - // pressed. - MouseButton GetPressingButton() const { - unsigned result = 0; - if (click_map_.left.has_value()) result |= MouseButton::Left; - if (click_map_.middle.has_value()) result |= MouseButton::Middle; - if (click_map_.right.has_value()) result |= MouseButton::Right; - return static_cast(result); - } + MouseButton GetTriggerButton() const { return trigger_button_; } + void SetTriggerButton(MouseButton trigger_button); IEvent* ClickEvent() { return &event_; } - IEvent* ClickBeginEvent() { return &begin_event_; } - IEvent* ClickEndEvent() { return &end_event_; } + IEvent* StateChangeEvent() { return &state_change_event_; } private: std::optional& FromButton(MouseButton button) { @@ -77,12 +73,12 @@ class ClickDetector : public Object { private: Control* control_; - Event event_; + MouseButton trigger_button_ = MouseButton::Left | MouseButton::Right; - Event begin_event_; - Event end_event_; + Event event_; + Event state_change_event_; - std::forward_list event_rovoker_guards_; + std::vector event_rovoker_guards_; struct { std::optional left = std::nullopt; -- cgit v1.2.3