diff --git a/src/kiwano-physics/ContactEvent.h b/src/kiwano-physics/ContactEvent.h index 0010bb35..a01068f3 100644 --- a/src/kiwano-physics/ContactEvent.h +++ b/src/kiwano-physics/ContactEvent.h @@ -53,17 +53,5 @@ namespace kiwano ContactEndEvent(); ContactEndEvent(Contact const& contact); }; - - /** - * \addtogroup EventTypes - * @{ - */ - - KGE_EVENT_BEGIN(PhysicEvents); - KGE_DEFINE_EVENT(ContactBegin, ContactBeginEvent); ///< 接触开始 @see kiwano::physics::ContactBeginEvent - KGE_DEFINE_EVENT(ContactEnd, ContactEndEvent); ///< 接触结束 @see kiwano::physics::ContactEndEvent - KGE_EVENT_END; - - /** @} */ } } diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index cbb1e3d7..67613533 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -66,8 +66,8 @@ namespace kiwano style.line_spacing = 20.f; debug_text_->SetStyle(style); - AddListener(MouseEvents::Hover(), [=](Event&) { SetOpacity(0.4f); }); - AddListener(MouseEvents::Out(), [=](Event&) { SetOpacity(1.f); }); + AddListener([=](Event&) { SetOpacity(0.4f); }); + AddListener([=](Event&) { SetOpacity(1.f); }); } DebugActor::~DebugActor() diff --git a/src/kiwano/2d/Layer.cpp b/src/kiwano/2d/Layer.cpp index abf5e776..f4b3a454 100644 --- a/src/kiwano/2d/Layer.cpp +++ b/src/kiwano/2d/Layer.cpp @@ -29,14 +29,14 @@ namespace kiwano { auto handler = Closure(this, &Layer::HandleMessages); - AddListener(MouseEvents::Down(), handler); - AddListener(MouseEvents::Up(), handler); - AddListener(MouseEvents::Move(), handler); - AddListener(MouseEvents::Wheel(), handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); - AddListener(KeyEvents::Down(), handler); - AddListener(KeyEvents::Up(), handler); - AddListener(KeyEvents::Char(), handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); } Layer::~Layer() diff --git a/src/kiwano/core/Event.h b/src/kiwano/core/Event.h index c8388297..02a1cbfe 100644 --- a/src/kiwano/core/Event.h +++ b/src/kiwano/core/Event.h @@ -95,22 +95,47 @@ namespace kiwano return type_ == KGE_EVENT(_Ty); } + /// \~chinese + /// @brief 转换为其他类型事件 + /// @throw std::bad_cast 类型无法转换时抛出 + template < + typename _Ty, + typename = typename std::enable_if::value, int>::type + > + inline const _Ty& Cast() const + { + return *dynamic_cast(this); + } + + /// \~chinese + /// @brief 转换为其他类型事件 + /// @throw std::bad_cast 类型无法转换时抛出 + template < + typename _Ty, + typename = typename std::enable_if::value, int>::type + > + inline _Ty& Cast() + { + return *dynamic_cast<_Ty*>(this); + } + /// \~chinese /// @brief 安全转换为其他类型事件 - /// @throw std::bad_cast 无法转换的类型 + /// @throw std::bad_cast 类型无法转换时抛出 template < typename _Ty, typename = typename std::enable_if::value, int>::type > inline const _Ty& SafeCast() const { - if (!IsType<_Ty>()) throw std::bad_cast(); - return *dynamic_cast(this); + if (!IsType<_Ty>()) + throw std::bad_cast(); + return Cast<_Ty>(); } /// \~chinese /// @brief 安全转换为其他类型事件 - /// @throw std::bad_cast 无法转换的类型 + /// @throw std::bad_cast 类型无法转换时抛出 template < typename _Ty, typename = typename std::enable_if::value, int>::type @@ -305,57 +330,4 @@ namespace kiwano /** @} */ - - /** - * \~chinese - * \defgroup EventTypes 事件类型 - * - */ - - /** - * \addtogroup EventTypes - * @{ - */ - - -#define KGE_EVENT_BEGIN(NAME) struct NAME { -#define KGE_EVENT_END }; -#define KGE_DEFINE_EVENT(EVENT_NAME, EVENT_TYPE) \ - static inline const EventType& EVENT_NAME() \ - { \ - static EventType event_type = KGE_EVENT(EVENT_TYPE); \ - return event_type; \ - } - - /// \~chinese - /// @brief 鼠标事件 - KGE_EVENT_BEGIN(MouseEvents); - KGE_DEFINE_EVENT(Move, MouseMoveEvent); ///< 鼠标移动 - KGE_DEFINE_EVENT(Down, MouseDownEvent); ///< 鼠标按下 - KGE_DEFINE_EVENT(Up, MouseUpEvent); ///< 鼠标抬起 - KGE_DEFINE_EVENT(Wheel, MouseWheelEvent); ///< 滚轮滚动 - KGE_DEFINE_EVENT(Hover, MouseHoverEvent); ///< 鼠标移入 - KGE_DEFINE_EVENT(Out, MouseOutEvent); ///< 鼠标移出 - KGE_DEFINE_EVENT(Click, MouseClickEvent); ///< 鼠标点击 - KGE_EVENT_END; - - /// \~chinese - /// @brief 键盘按键事件 - KGE_EVENT_BEGIN(KeyEvents); - KGE_DEFINE_EVENT(Down, KeyDownEvent); ///< 按键按下 - KGE_DEFINE_EVENT(Up, KeyUpEvent); ///< 按键抬起 - KGE_DEFINE_EVENT(Char, KeyCharEvent); ///< 输出字符 - KGE_EVENT_END; - - /// \~chinese - /// @brief 窗口事件 - KGE_EVENT_BEGIN(WindowEvents); - KGE_DEFINE_EVENT(Moved, WindowMovedEvent); ///< 窗口移动 - KGE_DEFINE_EVENT(Resized, WindowResizedEvent); ///< 窗口大小变化 - KGE_DEFINE_EVENT(FocusChanged, WindowFocusChangedEvent); ///< 获得或失去焦点 - KGE_DEFINE_EVENT(TitleChanged, WindowTitleChangedEvent); ///< 标题变化 - KGE_DEFINE_EVENT(Closed, WindowClosedEvent); ///< 窗口被关闭 - KGE_EVENT_END; - - /** @} */ } diff --git a/src/kiwano/core/EventListener.h b/src/kiwano/core/EventListener.h index a7a082a0..1b7b195b 100644 --- a/src/kiwano/core/EventListener.h +++ b/src/kiwano/core/EventListener.h @@ -54,6 +54,24 @@ namespace kiwano Callback const& callback ); + template < + typename _EventTy, + typename = typename std::enable_if::value, int>::type + > + EventListener(Callback const& callback) + : EventListener(KGE_EVENT(_EventTy), callback) + { + } + + template < + typename _EventTy, + typename = typename std::enable_if::value, int>::type + > + EventListener(String const& name, Callback const& callback) + : EventListener(name, KGE_EVENT(_EventTy), callback) + { + } + virtual ~EventListener(); void Start(); diff --git a/src/kiwano/ui/Button.cpp b/src/kiwano/ui/Button.cpp index 10a74a77..83acd556 100644 --- a/src/kiwano/ui/Button.cpp +++ b/src/kiwano/ui/Button.cpp @@ -32,10 +32,10 @@ namespace kiwano { SetResponsible(true); - AddListener(MouseEvents::Hover(), Closure(this, &Button::UpdateStatus)); - AddListener(MouseEvents::Out(), Closure(this, &Button::UpdateStatus)); - AddListener(MouseEvents::Down(), Closure(this, &Button::UpdateStatus)); - AddListener(MouseEvents::Up(), Closure(this, &Button::UpdateStatus)); + AddListener(Closure(this, &Button::UpdateStatus)); + AddListener(Closure(this, &Button::UpdateStatus)); + AddListener(Closure(this, &Button::UpdateStatus)); + AddListener(Closure(this, &Button::UpdateStatus)); } Button::Button(const Callback& click) @@ -105,10 +105,7 @@ namespace kiwano void Button::UpdateStatus(Event& evt) { - auto mouse_evt = dynamic_cast(&evt); - KGE_ASSERT(mouse_evt != nullptr); - - if (enabled_ && (mouse_evt->target == this)) + if (enabled_ && (evt.Cast().target == this)) { if (evt.IsType()) {