Magic_Game/src/kiwano/core/EventDispatcher.cpp

172 lines
3.9 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// 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.
2019-11-13 14:33:15 +08:00
#include <kiwano/core/EventDispatcher.h>
#include <kiwano/core/Logger.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-10 18:08:54 +08:00
bool EventDispatcher::DispatchEvent(Event& evt)
{
2019-08-20 19:32:36 +08:00
if (listeners_.empty())
2020-01-10 18:08:54 +08:00
return true;
EventListenerPtr next;
2019-08-13 21:16:38 +08:00
for (auto listener = listeners_.first_item(); listener; listener = next)
{
2019-08-13 21:16:38 +08:00
next = listener->next_item();
2019-12-26 19:25:43 +08:00
if (listener->IsRunning())
listener->Receive(evt);
2019-12-25 23:37:36 +08:00
if (listener->IsRemoveable())
listeners_.remove(listener);
2020-01-10 18:08:54 +08:00
if (listener->IsSwallowEnabled())
return false;
}
2020-01-10 18:08:54 +08:00
return true;
}
2019-08-20 21:15:15 +08:00
EventListener* EventDispatcher::AddListener(EventListenerPtr listener)
2019-10-28 17:25:06 +08:00
{
return AddListener(listener.get());
}
EventListener* EventDispatcher::AddListener(EventListener* listener)
{
2019-04-11 14:40:54 +08:00
KGE_ASSERT(listener && "AddListener failed, NULL pointer exception");
if (listener)
{
2019-08-20 19:32:36 +08:00
listeners_.push_back(listener);
}
2019-10-28 17:25:06 +08:00
return listener;
}
EventListener* EventDispatcher::AddListener(String const& name, EventType type, EventListener::Callback callback)
{
EventListenerPtr listener = new EventListener(name, type, callback);
return AddListener(listener);
}
EventListener* EventDispatcher::AddListener(EventType type, EventListener::Callback callback)
{
EventListenerPtr listener = new EventListener(type, callback);
2019-08-20 21:15:15 +08:00
return AddListener(listener);
}
void EventDispatcher::StartListeners(String const & listener_name)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.IsName(listener_name))
{
2020-01-10 15:22:12 +08:00
listener.Start();
}
}
}
void EventDispatcher::StopListeners(String const & listener_name)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.IsName(listener_name))
{
2020-01-10 15:22:12 +08:00
listener.Stop();
}
}
}
void EventDispatcher::RemoveListeners(String const & listener_name)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.IsName(listener_name))
{
2020-01-10 15:22:12 +08:00
listener.Remove();
}
}
}
void EventDispatcher::StartListeners(const EventType& type)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.GetEventType() == type)
{
2020-01-10 15:22:12 +08:00
listener.Start();
}
}
}
void EventDispatcher::StopListeners(const EventType& type)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.GetEventType() == type)
{
2020-01-10 15:22:12 +08:00
listener.Stop();
}
}
}
void EventDispatcher::RemoveListeners(const EventType& type)
{
2019-12-25 23:37:36 +08:00
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
if (listener.GetEventType() == type)
{
2020-01-10 15:22:12 +08:00
listener.Remove();
}
}
}
2019-12-25 23:37:36 +08:00
void EventDispatcher::StartAllListeners()
{
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
listener.Start();
2019-12-25 23:37:36 +08:00
}
}
void EventDispatcher::StopAllListeners()
{
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
listener.Stop();
2019-12-25 23:37:36 +08:00
}
}
void EventDispatcher::RemoveAllListeners()
{
for (auto& listener : listeners_)
{
2020-01-10 15:22:12 +08:00
listener.Remove();
2019-12-25 23:37:36 +08:00
}
}
const EventDispatcher::Listeners& EventDispatcher::GetAllListeners() const
{
return listeners_;
}
}