[deploy] add MouseEventListener & KeyEventListener

This commit is contained in:
Nomango 2020-10-14 16:01:43 +08:00
parent dc12c77880
commit adcce6e613
20 changed files with 465 additions and 34 deletions

View File

@ -48,10 +48,12 @@
<ClInclude Include="..\..\src\kiwano\core\Time.h" />
<ClInclude Include="..\..\src\kiwano\event\Event.h" />
<ClInclude Include="..\..\src\kiwano\event\EventDispatcher.h" />
<ClInclude Include="..\..\src\kiwano\event\EventListener.h" />
<ClInclude Include="..\..\src\kiwano\event\Events.h" />
<ClInclude Include="..\..\src\kiwano\event\EventType.h" />
<ClInclude Include="..\..\src\kiwano\event\KeyEvent.h" />
<ClInclude Include="..\..\src\kiwano\event\listener\EventListener.h" />
<ClInclude Include="..\..\src\kiwano\event\listener\KeyEventListener.h" />
<ClInclude Include="..\..\src\kiwano\event\listener\MouseEventListener.h" />
<ClInclude Include="..\..\src\kiwano\event\MouseEvent.h" />
<ClInclude Include="..\..\src\kiwano\event\WindowEvent.h" />
<ClInclude Include="..\..\src\kiwano\kiwano.h" />
@ -165,8 +167,10 @@
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
<ClCompile Include="..\..\src\kiwano\event\Event.cpp" />
<ClCompile Include="..\..\src\kiwano\event\EventDispatcher.cpp" />
<ClCompile Include="..\..\src\kiwano\event\EventListener.cpp" />
<ClCompile Include="..\..\src\kiwano\event\KeyEvent.cpp" />
<ClCompile Include="..\..\src\kiwano\event\listener\EventListener.cpp" />
<ClCompile Include="..\..\src\kiwano\event\listener\KeyEventListener.cpp" />
<ClCompile Include="..\..\src\kiwano\event\listener\MouseEventListener.cpp" />
<ClCompile Include="..\..\src\kiwano\event\MouseEvent.cpp" />
<ClCompile Include="..\..\src\kiwano\event\WindowEvent.cpp" />
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />

View File

@ -40,6 +40,9 @@
<Filter Include="2d\transition">
<UniqueIdentifier>{f70cecd8-6d5b-405d-8466-d3ca2db9b806}</UniqueIdentifier>
</Filter>
<Filter Include="event\listener">
<UniqueIdentifier>{554a3b32-ec18-4123-a12e-b176ec10fbdc}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\kiwano\2d\Canvas.h">
@ -279,9 +282,6 @@
<ClInclude Include="..\..\src\kiwano\event\EventDispatcher.h">
<Filter>event</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\event\EventListener.h">
<Filter>event</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\event\Events.h">
<Filter>event</Filter>
</ClInclude>
@ -396,6 +396,15 @@
<ClInclude Include="..\..\src\kiwano\base\component\MouseSensor.h">
<Filter>base\component</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\event\listener\EventListener.h">
<Filter>event\listener</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\event\listener\MouseEventListener.h">
<Filter>event\listener</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\event\listener\KeyEventListener.h">
<Filter>event\listener</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
@ -560,9 +569,6 @@
<ClCompile Include="..\..\src\kiwano\event\EventDispatcher.cpp">
<Filter>event</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\event\EventListener.cpp">
<Filter>event</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\event\KeyEvent.cpp">
<Filter>event</Filter>
</ClCompile>
@ -653,6 +659,15 @@
<ClCompile Include="..\..\src\kiwano\base\component\MouseSensor.cpp">
<Filter>base\component</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\event\listener\EventListener.cpp">
<Filter>event\listener</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\event\listener\MouseEventListener.cpp">
<Filter>event\listener</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\event\listener\KeyEventListener.cpp">
<Filter>event\listener</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="suppress_warning.ruleset" />

View File

@ -30,11 +30,11 @@ KGE_DECLARE_SMART_PTR(Event);
/**
* \~chinese
* \defgroup Events
* \defgroup Event
*/
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/
@ -60,16 +60,16 @@ public:
bool IsType() const;
/// \~chinese
/// @brief 安全转换为其他类型事件
/// @throw std::bad_cast 类型无法转换时抛出
/// @brief 转换为其他类型事件
/// @return 其他类型事件指针,如果事件类型无法转换,返回空指针
template <typename _Ty>
const _Ty* SafeCast() const;
const _Ty* Cast() const;
/// \~chinese
/// @brief 安全转换为其他类型事件
/// @throw std::bad_cast 类型无法转换时抛出
/// @brief 转换为其他类型事件
/// @return 其他类型事件指针,如果事件类型无法转换,返回空指针
template <typename _Ty>
_Ty* SafeCast();
_Ty* Cast();
private:
const EventType type_;
@ -102,16 +102,16 @@ inline bool Event::IsType() const
}
template <typename _Ty>
inline const _Ty* Event::SafeCast() const
inline const _Ty* Event::Cast() const
{
return const_cast<Event*>(this)->SafeCast<_Ty>();
return const_cast<Event*>(this)->Cast<_Ty>();
}
template <typename _Ty>
inline _Ty* Event::SafeCast()
inline _Ty* Event::Cast()
{
if (!this->IsType<_Ty>())
throw std::bad_cast();
return nullptr;
return dynamic_cast<_Ty*>(this);
}

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include <kiwano/event/EventListener.h>
#include <kiwano/event/listener/EventListener.h>
namespace kiwano
{

View File

@ -26,7 +26,7 @@
namespace kiwano
{
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/

View File

@ -30,7 +30,7 @@ KGE_DECLARE_SMART_PTR(KeyUpEvent);
KGE_DECLARE_SMART_PTR(KeyCharEvent);
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/

View File

@ -35,7 +35,7 @@ KGE_DECLARE_SMART_PTR(MouseOutEvent);
KGE_DECLARE_SMART_PTR(MouseWheelEvent);
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/

View File

@ -33,7 +33,7 @@ KGE_DECLARE_SMART_PTR(WindowClosedEvent);
class Window;
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include <kiwano/event/EventListener.h>
#include <kiwano/event/listener/EventListener.h>
namespace kiwano
{

View File

@ -30,6 +30,16 @@ class EventDispatcher;
KGE_DECLARE_SMART_PTR(EventListener);
/**
* \~chinese
* \defgroup EventListener
*/
/**
* \addtogroup EventListener
* @{
*/
/**
* \~chinese
* @brief
@ -114,6 +124,8 @@ private:
bool swallow_;
};
/** @} */
inline void EventListener::Start()
{
running_ = true;

View File

@ -0,0 +1,39 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano/event/listener/KeyEventListener.h>
namespace kiwano
{
void KeyEventListener::Handle(Event* evt)
{
if (auto key_evt = evt->Cast<KeyDownEvent>())
{
OnKeyDown(key_evt->code);
}
else if (auto key_evt = evt->Cast<KeyUpEvent>())
{
OnKeyUp(key_evt->code);
}
}
} // namespace kiwano

View File

@ -0,0 +1,60 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano/event/KeyEvent.h>
#include <kiwano/event/listener/EventListener.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(KeyEventListener);
/**
* \addtogroup EventListener
* @{
*/
/**
* \~chinese
* @brief
*/
class KGE_API KeyEventListener
: public EventListener
{
public:
/// \~chinese
/// @brief 按键按下时
/// @param key 按键键值
virtual void OnKeyDown(KeyCode key) {}
/// \~chinese
/// @brief 按键抬起时
/// @param key 按键键值
virtual void OnKeyUp(KeyCode key) {}
/// \~chinese
/// @brief 处理消息
void Handle(Event* evt) override;
};
/** @} */
} // namespace kiwano

View File

@ -0,0 +1,137 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano/event/listener/MouseEventListener.h>
namespace kiwano
{
void MouseEventListener::Handle(Event* evt)
{
if (auto mouse_evt = evt->Cast<MouseMoveEvent>())
{
OnMouseMoving(mouse_evt->pos);
}
else if (auto mouse_evt = evt->Cast<MouseDownEvent>())
{
OnMouseDown(mouse_evt->pos, mouse_evt->button);
}
else if (auto mouse_evt = evt->Cast<MouseUpEvent>())
{
OnMouseUp(mouse_evt->pos, mouse_evt->button);
}
else if (auto mouse_evt = evt->Cast<MouseWheelEvent>())
{
OnMouseWheelScrolling(mouse_evt->pos, mouse_evt->wheel);
}
}
MouseDragEventListener::MouseDragEventListener()
: is_dragging_{ false, false, false }
, drag_point_{}
{
}
void MouseDragEventListener::Handle(Event* evt)
{
if (auto mouse_evt = evt->Cast<MouseDownEvent>())
{
const auto button = mouse_evt->button;
if (OnDragStart(mouse_evt->pos, button))
{
SetDragging(button, true);
SetDragPoint(button, mouse_evt->pos);
SetDragPrevPoint(button, mouse_evt->pos);
}
}
if (auto mouse_evt = evt->Cast<MouseMoveEvent>())
{
for (int i = 0; i < MouseButtonNum; i++)
{
const auto button = MouseButton(i);
if (IsDragging(button))
{
auto new_pos = mouse_evt->pos;
auto offset = new_pos - GetDragPrevPoint(button);
OnDragging(offset, button);
SetDragPrevPoint(button, new_pos);
}
}
}
if (auto mouse_evt = evt->Cast<MouseUpEvent>())
{
const auto button = mouse_evt->button;
if (IsDragging(button))
{
OnDragEnd(mouse_evt->pos, button);
SetDragging(button, false);
SetDragPoint(button, Point());
SetDragPrevPoint(button, Point());
}
}
}
bool MouseDragEventListener::IsDragging(MouseButton button)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
return is_dragging_[index];
}
void MouseDragEventListener::SetDragging(MouseButton button, bool dragging)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
is_dragging_[index] = dragging;
}
Point MouseDragEventListener::GetDragPoint(MouseButton button)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
return drag_point_[index];
}
void MouseDragEventListener::SetDragPoint(MouseButton button, Point pos)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
drag_point_[index] = pos;
}
Point MouseDragEventListener::GetDragPrevPoint(MouseButton button)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
return drag_prev_point_[index];
}
void MouseDragEventListener::SetDragPrevPoint(MouseButton button, Point pos)
{
int index = int(button);
KGE_ASSERT(index >= 0 && index < MouseButtonNum);
drag_prev_point_[index] = pos;
}
} // namespace kiwano

View File

@ -0,0 +1,137 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano/event/MouseEvent.h>
#include <kiwano/event/listener/EventListener.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(MouseEventListener);
KGE_DECLARE_SMART_PTR(MouseDragEventListener);
/**
* \addtogroup EventListener
* @{
*/
/**
* \~chinese
* @brief
*/
class KGE_API MouseEventListener
: public EventListener
{
public:
/// \~chinese
/// @brief 鼠标移动时
/// @param pos 鼠标位置
virtual void OnMouseMoving(const Point& pos) {}
/// \~chinese
/// @brief 鼠标按下时
/// @param pos 鼠标位置
/// @param button 鼠标键值
virtual void OnMouseDown(const Point& pos, MouseButton button) {}
/// \~chinese
/// @brief 鼠标抬起时
/// @param pos 鼠标位置
/// @param button 鼠标键值
virtual void OnMouseUp(const Point& pos, MouseButton button) {}
/// \~chinese
/// @brief 鼠标滚轮滚动时
/// @param pos 鼠标位置
/// @param wheel 鼠标滚轮滚动值
virtual void OnMouseWheelScrolling(const Point& pos, float wheel) {}
/// \~chinese
/// @brief 处理消息
void Handle(Event* evt) override;
};
/**
* \~chinese
* @brief
*/
class KGE_API MouseDragEventListener : public EventListener
{
public:
MouseDragEventListener();
/// \~chinese
/// @brief 鼠标拖动开始
/// @param pos 鼠标位置
/// @param button 鼠标键值
/// @return 是否响应这次拖动
virtual bool OnDragStart(const Point& pos, MouseButton button)
{
return true;
}
/// \~chinese
/// @brief 鼠标拖动时
/// @param offset 鼠标位置偏移量
/// @param button 鼠标键值
virtual void OnDragging(const Point& offset, MouseButton button) {}
/// \~chinese
/// @brief 鼠标拖动结束
/// @param pos 鼠标位置
/// @param button 鼠标键值
virtual void OnDragEnd(const Point& pos, MouseButton button) {}
/// \~chinese
/// @brief 获取鼠标按键的拖动状态
bool IsDragging(MouseButton button);
/// \~chinese
/// @brief 获取鼠标按键的起始拖动位置
Point GetDragPoint(MouseButton button);
/// \~chinese
/// @brief 处理消息
void Handle(Event* evt) override;
private:
void SetDragging(MouseButton button, bool dragging);
void SetDragPoint(MouseButton button, Point pos);
Point GetDragPrevPoint(MouseButton button);
void SetDragPrevPoint(MouseButton button, Point pos);
private:
enum : int
{
MouseButtonNum = 3
};
bool is_dragging_[MouseButtonNum];
Point drag_point_[MouseButtonNum];
Point drag_prev_point_[MouseButtonNum];
};
/** @} */
} // namespace kiwano

View File

@ -49,7 +49,6 @@
#include <kiwano/core/RefBasePtr.hpp>
#include <kiwano/core/Time.h>
//
// event
//
@ -58,7 +57,9 @@
#include <kiwano/event/KeyEvent.h>
#include <kiwano/event/MouseEvent.h>
#include <kiwano/event/WindowEvent.h>
#include <kiwano/event/EventListener.h>
#include <kiwano/event/listener/EventListener.h>
#include <kiwano/event/listener/MouseEventListener.h>
#include <kiwano/event/listener/KeyEventListener.h>
#include <kiwano/event/EventDispatcher.h>
//

View File

@ -118,4 +118,30 @@ enum class KeyCode
Last
};
inline KeyCode operator+(KeyCode lhs, int rhs) noexcept
{
return KeyCode(int(lhs) + rhs);
}
inline KeyCode operator-(KeyCode lhs, int rhs) noexcept
{
return KeyCode(int(lhs) - rhs);
}
inline KeyCode operator+(int lhs, KeyCode rhs) noexcept
{
return KeyCode(lhs + int(rhs));
}
inline KeyCode operator-(int lhs, KeyCode rhs) noexcept
{
return KeyCode(lhs - int(rhs));
}
inline int operator-(KeyCode lhs, KeyCode rhs) noexcept
{
return int(lhs) - int(rhs);
}
} // namespace kiwano

View File

@ -161,9 +161,9 @@ void RendererImpl::HandleEvent(EventModuleContext& ctx)
{
Renderer::HandleEvent(ctx);
if (ctx.evt->IsType<WindowMovedEvent>())
auto evt = ctx.evt->Cast<WindowMovedEvent>();
if (evt)
{
auto evt = ctx.evt->SafeCast<WindowMovedEvent>();
HMONITOR monitor = ::MonitorFromWindow(evt->window->GetHandle(), MONITOR_DEFAULTTONULL);
if (monitor_ != monitor)
{

View File

@ -57,9 +57,9 @@ void Renderer::HandleEvent(EventModuleContext& ctx)
{
if (auto_reset_resolution_)
{
if (ctx.evt->IsType<WindowResizedEvent>())
auto evt = ctx.evt->Cast<WindowResizedEvent>();
if (evt)
{
auto evt = ctx.evt->SafeCast<WindowResizedEvent>();
Resize(evt->width, evt->height);
}
}

View File

@ -281,7 +281,7 @@ bool ConfigIni::HasKey(const String& section, const String& key) const
{
if (HasSection(section))
{
return !!sections_.at(section).count(section);
return !!sections_.at(section).count(key);
}
return false;
}

View File

@ -30,7 +30,7 @@ KGE_DECLARE_SMART_PTR(TickEvent);
KGE_DECLARE_SMART_PTR(EventTicker);
/**
* \addtogroup Events
* \addtogroup Event
* @{
*/