Remove event declare macros

This commit is contained in:
Nomango 2019-12-22 11:24:04 +08:00
parent 939fadcb97
commit 13cbca8f49
6 changed files with 61 additions and 86 deletions

View File

@ -53,17 +53,5 @@ namespace kiwano
ContactEndEvent(); ContactEndEvent();
ContactEndEvent(Contact const& contact); 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;
/** @} */
} }
} }

View File

@ -66,8 +66,8 @@ namespace kiwano
style.line_spacing = 20.f; style.line_spacing = 20.f;
debug_text_->SetStyle(style); debug_text_->SetStyle(style);
AddListener(MouseEvents::Hover(), [=](Event&) { SetOpacity(0.4f); }); AddListener<MouseHoverEvent>([=](Event&) { SetOpacity(0.4f); });
AddListener(MouseEvents::Out(), [=](Event&) { SetOpacity(1.f); }); AddListener<MouseOutEvent>([=](Event&) { SetOpacity(1.f); });
} }
DebugActor::~DebugActor() DebugActor::~DebugActor()

View File

@ -29,14 +29,14 @@ namespace kiwano
{ {
auto handler = Closure(this, &Layer::HandleMessages); auto handler = Closure(this, &Layer::HandleMessages);
AddListener(MouseEvents::Down(), handler); AddListener<MouseDownEvent>(handler);
AddListener(MouseEvents::Up(), handler); AddListener<MouseUpEvent>(handler);
AddListener(MouseEvents::Move(), handler); AddListener<MouseMoveEvent>(handler);
AddListener(MouseEvents::Wheel(), handler); AddListener<MouseWheelEvent>(handler);
AddListener(KeyEvents::Down(), handler); AddListener<KeyDownEvent>(handler);
AddListener(KeyEvents::Up(), handler); AddListener<KeyUpEvent>(handler);
AddListener(KeyEvents::Char(), handler); AddListener<KeyCharEvent>(handler);
} }
Layer::~Layer() Layer::~Layer()

View File

@ -95,22 +95,47 @@ namespace kiwano
return type_ == KGE_EVENT(_Ty); return type_ == KGE_EVENT(_Ty);
} }
/// \~chinese
/// @brief 转换为其他类型事件
/// @throw std::bad_cast 类型无法转换时抛出
template <
typename _Ty,
typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type
>
inline const _Ty& Cast() const
{
return *dynamic_cast<const _Ty*>(this);
}
/// \~chinese
/// @brief 转换为其他类型事件
/// @throw std::bad_cast 类型无法转换时抛出
template <
typename _Ty,
typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type
>
inline _Ty& Cast()
{
return *dynamic_cast<_Ty*>(this);
}
/// \~chinese /// \~chinese
/// @brief 安全转换为其他类型事件 /// @brief 安全转换为其他类型事件
/// @throw std::bad_cast 无法转换的类型 /// @throw std::bad_cast 类型无法转换时抛出
template < template <
typename _Ty, typename _Ty,
typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type
> >
inline const _Ty& SafeCast() const inline const _Ty& SafeCast() const
{ {
if (!IsType<_Ty>()) throw std::bad_cast(); if (!IsType<_Ty>())
return *dynamic_cast<const _Ty*>(this); throw std::bad_cast();
return Cast<_Ty>();
} }
/// \~chinese /// \~chinese
/// @brief 安全转换为其他类型事件 /// @brief 安全转换为其他类型事件
/// @throw std::bad_cast 无法转换的类型 /// @throw std::bad_cast 类型无法转换时抛出
template < template <
typename _Ty, typename _Ty,
typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type typename = typename std::enable_if<std::is_base_of<Event, _Ty>::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;
/** @} */
} }

View File

@ -54,6 +54,24 @@ namespace kiwano
Callback const& callback Callback const& callback
); );
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
EventListener(Callback const& callback)
: EventListener(KGE_EVENT(_EventTy), callback)
{
}
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
EventListener(String const& name, Callback const& callback)
: EventListener(name, KGE_EVENT(_EventTy), callback)
{
}
virtual ~EventListener(); virtual ~EventListener();
void Start(); void Start();

View File

@ -32,10 +32,10 @@ namespace kiwano
{ {
SetResponsible(true); SetResponsible(true);
AddListener(MouseEvents::Hover(), Closure(this, &Button::UpdateStatus)); AddListener<MouseHoverEvent>(Closure(this, &Button::UpdateStatus));
AddListener(MouseEvents::Out(), Closure(this, &Button::UpdateStatus)); AddListener<MouseOutEvent>(Closure(this, &Button::UpdateStatus));
AddListener(MouseEvents::Down(), Closure(this, &Button::UpdateStatus)); AddListener<MouseDownEvent>(Closure(this, &Button::UpdateStatus));
AddListener(MouseEvents::Up(), Closure(this, &Button::UpdateStatus)); AddListener<MouseUpEvent>(Closure(this, &Button::UpdateStatus));
} }
Button::Button(const Callback& click) Button::Button(const Callback& click)
@ -105,10 +105,7 @@ namespace kiwano
void Button::UpdateStatus(Event& evt) void Button::UpdateStatus(Event& evt)
{ {
auto mouse_evt = dynamic_cast<MouseEvent*>(&evt); if (enabled_ && (evt.Cast<MouseEvent>().target == this))
KGE_ASSERT(mouse_evt != nullptr);
if (enabled_ && (mouse_evt->target == this))
{ {
if (evt.IsType<MouseHoverEvent>()) if (evt.IsType<MouseHoverEvent>())
{ {