[deploy] add MouseSensor

This commit is contained in:
Nomango 2020-10-14 00:54:56 +08:00
parent cbe454c4ad
commit dc12c77880
11 changed files with 204 additions and 146 deletions

View File

@ -23,6 +23,7 @@
<ClInclude Include="..\..\src\kiwano\base\component\Button.h" />
<ClInclude Include="..\..\src\kiwano\base\component\Component.h" />
<ClInclude Include="..\..\src\kiwano\base\component\ComponentManager.h" />
<ClInclude Include="..\..\src\kiwano\base\component\MouseSensor.h" />
<ClInclude Include="..\..\src\kiwano\base\Director.h" />
<ClInclude Include="..\..\src\kiwano\base\Module.h" />
<ClInclude Include="..\..\src\kiwano\base\ObjectBase.h" />
@ -150,6 +151,7 @@
<ClCompile Include="..\..\src\kiwano\base\component\Button.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\Component.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\ComponentManager.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\MouseSensor.cpp" />
<ClCompile Include="..\..\src\kiwano\base\Director.cpp" />
<ClCompile Include="..\..\src\kiwano\base\Module.cpp" />
<ClCompile Include="..\..\src\kiwano\base\ObjectBase.cpp" />

View File

@ -393,6 +393,9 @@
<ClInclude Include="..\..\src\kiwano\2d\transition\BoxTransition.h">
<Filter>2d\transition</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\base\component\MouseSensor.h">
<Filter>base\component</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
@ -647,6 +650,9 @@
<ClCompile Include="..\..\src\kiwano\2d\transition\BoxTransition.cpp">
<Filter>2d\transition</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\base\component\MouseSensor.cpp">
<Filter>base\component</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="suppress_warning.ruleset" />

View File

@ -44,9 +44,6 @@ Actor::Actor()
, visible_(true)
, visible_in_rt_(true)
, update_pausing_(false)
, hover_(false)
, pressed_(false)
, responsible_(false)
, cascade_opacity_(true)
, show_border_(false)
, evt_dispatch_enabled_(true)
@ -225,8 +222,8 @@ void Actor::SetEventDispatchEnabled(bool enabled)
void Actor::DoSerialize(Serializer* serializer) const
{
ObjectBase::DoSerialize(serializer);
(*serializer) << visible_ << update_pausing_ << cascade_opacity_ << responsible_ << z_order_ << opacity_ << anchor_
<< size_ << transform_;
(*serializer) << visible_ << update_pausing_ << cascade_opacity_ << z_order_ << opacity_ << anchor_ << size_
<< transform_;
}
void Actor::DoDeserialize(Deserializer* deserializer)
@ -235,8 +232,8 @@ void Actor::DoDeserialize(Deserializer* deserializer)
float opacity = 1.0f;
Transform transform;
(*deserializer) >> visible_ >> update_pausing_ >> cascade_opacity_ >> responsible_ >> z_order_ >> opacity >> anchor_
>> size_ >> transform;
(*deserializer) >> visible_ >> update_pausing_ >> cascade_opacity_ >> z_order_ >> opacity >> anchor_ >> size_
>> transform;
SetOpacity(opacity);
SetTransform(transform);
@ -248,49 +245,6 @@ bool Actor::HandleEvent(Event* evt)
if (!EventDispatcher::DispatchEvent(evt))
return false;
if (responsible_)
{
if (evt->IsType<MouseMoveEvent>())
{
auto mouse_evt = dynamic_cast<MouseMoveEvent*>(evt);
bool contains = ContainsPoint(mouse_evt->pos);
if (!hover_ && contains)
{
hover_ = true;
MouseHoverEventPtr hover = new MouseHoverEvent;
hover->pos = mouse_evt->pos;
HandleEvent(hover.Get());
}
else if (hover_ && !contains)
{
hover_ = false;
pressed_ = false;
MouseOutEventPtr out = new MouseOutEvent;
out->pos = mouse_evt->pos;
HandleEvent(out.Get());
}
}
if (evt->IsType<MouseDownEvent>() && hover_)
{
pressed_ = true;
}
if (evt->IsType<MouseUpEvent>() && pressed_)
{
pressed_ = false;
auto mouse_up_evt = dynamic_cast<MouseUpEvent*>(evt);
MouseClickEventPtr click = new MouseClickEvent;
click->pos = mouse_up_evt->pos;
click->button = mouse_up_evt->button;
HandleEvent(click.Get());
}
}
return true;
}
@ -660,11 +614,6 @@ void Actor::RemoveAllChildren()
children_.Clear();
}
void Actor::SetResponsible(bool enable)
{
responsible_ = enable;
}
bool Actor::ContainsPoint(const Point& point) const
{
if (size_.x == 0.f || size_.y == 0.f)

View File

@ -98,10 +98,6 @@ public:
/// @brief 获取显示状态
bool IsVisible() const;
/// \~chinese
/// @brief 获取响应状态
bool IsResponsible() const;
/// \~chinese
/// @brief 是否启用级联透明度
bool IsCascadeOpacityEnabled() const;
@ -334,11 +330,6 @@ public:
/// @brief 设置 Z 轴顺序,默认为 0
void SetZOrder(int zorder);
/// \~chinese
/// @brief 设置角色是否可响应,默认为 false
/// @details 可响应的角色会收到鼠标的 Hover | Out | Click 消息
void SetResponsible(bool enable);
/// \~chinese
/// @brief 添加子角色
void AddChild(ActorPtr child);
@ -425,6 +416,11 @@ public:
/// @return 是否继续分发该事件
virtual bool DispatchEvent(Event* evt);
/// \~chinese
/// @brief 处理事件且不分发
/// @param evt 事件
bool HandleEvent(Event* evt);
/// \~chinese
/// @brief 开启或关闭事件分发功能
/// @param enabled 是否开启
@ -483,10 +479,6 @@ protected:
/// @brief 设置节点所在舞台
void SetStage(Stage* stage);
/// \~chinese
/// @brief 处理事件
bool HandleEvent(Event* evt);
/// \~chinese
/// @brief 设置物理身体
void SetPhysicBody(physics::PhysicBody* body);
@ -498,9 +490,6 @@ private:
bool update_pausing_;
bool cascade_opacity_;
bool show_border_;
bool hover_;
bool pressed_;
bool responsible_;
bool evt_dispatch_enabled_;
mutable bool visible_in_rt_;
@ -549,11 +538,6 @@ inline bool Actor::IsVisible() const
return visible_;
}
inline bool Actor::IsResponsible() const
{
return responsible_;
}
inline bool Actor::IsCascadeOpacityEnabled() const
{
return cascade_opacity_;

View File

@ -21,6 +21,7 @@
#include <kiwano/2d/DebugActor.h>
#include <kiwano/utils/Logger.h>
#include <kiwano/render/Renderer.h>
#include <kiwano/base/component/MouseSensor.h>
#include <psapi.h>
#pragma comment(lib, "psapi.lib")
@ -49,9 +50,11 @@ DebugActor::DebugActor()
{
SetName("kiwano-debug-actor");
SetPosition(Point{ 10, 10 });
SetResponsible(true);
SetCascadeOpacityEnabled(true);
MouseSensorPtr sensor = new MouseSensor;
this->AddComponent(sensor);
comma_locale_ = std::locale(std::locale(), new comma_numpunct);
background_brush_ = MakePtr<Brush>();

View File

@ -24,7 +24,6 @@ namespace kiwano
{
ButtonBase::ButtonBase()
: status_(Status::Normal)
{
SetName("__KGE_BUTTON__");
}
@ -33,41 +32,25 @@ ButtonBase::~ButtonBase()
{
}
void ButtonBase::InitComponent(Actor* actor)
{
Component::InitComponent(actor);
if (actor)
{
actor->SetResponsible(true);
}
}
void ButtonBase::DestroyComponent()
{
Component::DestroyComponent();
}
void ButtonBase::HandleEvent(kiwano::Event* evt)
{
MouseSensor::HandleEvent(evt);
if (evt->IsType<MouseHoverEvent>())
{
OnEvent(Event::MouseEntered);
status_ = Status::Hover;
}
else if (evt->IsType<MouseOutEvent>())
{
OnEvent(Event::MouseExited);
status_ = Status::Normal;
}
else if (evt->IsType<MouseDownEvent>() && status_ == Status::Hover)
else if (evt->IsType<MouseDownEvent>() && this->IsHovering())
{
OnEvent(Event::Pressed);
status_ = Status::Pressed;
}
else if (evt->IsType<MouseUpEvent>() && status_ == Status::Pressed)
else if (evt->IsType<MouseUpEvent>() && this->IsPressing())
{
OnEvent(Event::Released);
status_ = Status::Hover;
}
else if (evt->IsType<MouseClickEvent>())
{

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include <kiwano/2d/Actor.h>
#include <kiwano/base/component/MouseSensor.h>
namespace kiwano
{
@ -35,7 +35,7 @@ KGE_DECLARE_SMART_PTR(Button);
* \~chinese
* @brief
*/
class KGE_API ButtonBase : public Component
class KGE_API ButtonBase : public MouseSensor
{
public:
/// \~chinese
@ -58,25 +58,9 @@ public:
virtual void OnEvent(Event evt) = 0;
protected:
/// \~chinese
/// @brief 初始化组件
void InitComponent(Actor* actor) override;
/// \~chinese
/// @brief 销毁组件
void DestroyComponent() override;
/// \~chinese
/// @brief 处理角色事件
void HandleEvent(kiwano::Event* evt) override;
private:
enum class Status
{
Normal,
Hover,
Pressed
} status_;
};
/**

View File

@ -121,8 +121,6 @@ void ComponentManager::RemoveAllComponents()
}
void ComponentManager::Update(Duration dt)
{
if (!components_.empty())
{
if (!components_.empty())
{
@ -135,11 +133,8 @@ void ComponentManager::Update(Duration dt)
}
}
}
}
void ComponentManager::Render(RenderContext& ctx)
{
if (!components_.empty())
{
if (!components_.empty())
{
@ -152,11 +147,8 @@ void ComponentManager::Render(RenderContext& ctx)
}
}
}
}
void ComponentManager::DispatchToComponents(Event* evt)
{
if (!components_.empty())
{
if (!components_.empty())
{
@ -169,6 +161,5 @@ void ComponentManager::DispatchToComponents(Event* evt)
}
}
}
}
} // namespace kiwano

View File

@ -0,0 +1,78 @@
// 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.
#include <kiwano/base/component/MouseSensor.h>
#include <kiwano/2d/Actor.h>
namespace kiwano
{
MouseSensor::MouseSensor()
: hover_(false)
, pressed_(false)
{
}
MouseSensor::~MouseSensor() {}
void MouseSensor::HandleEvent(Event* evt)
{
Actor* target = GetBoundActor();
if (evt->IsType<MouseMoveEvent>())
{
auto mouse_evt = dynamic_cast<MouseMoveEvent*>(evt);
bool contains = target->ContainsPoint(mouse_evt->pos);
if (!hover_ && contains)
{
hover_ = true;
MouseHoverEventPtr hover = new MouseHoverEvent;
hover->pos = mouse_evt->pos;
target->HandleEvent(hover.Get());
}
else if (hover_ && !contains)
{
hover_ = false;
pressed_ = false;
MouseOutEventPtr out = new MouseOutEvent;
out->pos = mouse_evt->pos;
target->HandleEvent(out.Get());
}
}
if (evt->IsType<MouseDownEvent>() && hover_)
{
pressed_ = true;
}
if (evt->IsType<MouseUpEvent>() && pressed_)
{
pressed_ = false;
auto mouse_up_evt = dynamic_cast<MouseUpEvent*>(evt);
MouseClickEventPtr click = new MouseClickEvent;
click->pos = mouse_up_evt->pos;
click->button = mouse_up_evt->button;
target->HandleEvent(click.Get());
}
}
} // namespace kiwano

View File

@ -0,0 +1,77 @@
// 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/base/component/Component.h>
#include <kiwano/event/MouseEvent.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(MouseSensor);
/**
* \addtogroup Component
* @{
*/
/**
* \~chinese
* @brief
* @details 使 Hover | Out | Click
*/
class KGE_API MouseSensor : public Component
{
public:
MouseSensor();
virtual ~MouseSensor();
/// \~chinese
/// @brief 鼠标是否正悬于角色上
bool IsHovering() const;
/// \~chinese
/// @brief 鼠标是否正按于角色上
bool IsPressing() const;
protected:
/// \~chinese
/// @brief 处理角色事件
void HandleEvent(Event* evt) override;
private:
bool hover_;
bool pressed_;
};
/** @} */
inline bool MouseSensor::IsHovering() const
{
return hover_;
}
inline bool MouseSensor::IsPressing() const
{
return pressed_;
}
} // namespace kiwano

View File

@ -49,18 +49,6 @@
#include <kiwano/core/RefBasePtr.hpp>
#include <kiwano/core/Time.h>
//
// base
//
#include <kiwano/base/RefObject.h>
#include <kiwano/base/ObjectBase.h>
#include <kiwano/base/Director.h>
#include <kiwano/base/Module.h>
#include <kiwano/base/component/Component.h>
#include <kiwano/base/component/ComponentManager.h>
#include <kiwano/base/component/Button.h>
//
// event
@ -73,6 +61,19 @@
#include <kiwano/event/EventListener.h>
#include <kiwano/event/EventDispatcher.h>
//
// base
//
#include <kiwano/base/RefObject.h>
#include <kiwano/base/ObjectBase.h>
#include <kiwano/base/Director.h>
#include <kiwano/base/Module.h>
#include <kiwano/base/component/Component.h>
#include <kiwano/base/component/ComponentManager.h>
#include <kiwano/base/component/MouseSensor.h>
#include <kiwano/base/component/Button.h>
//
// renderer
//