Magic_Game/src/kiwano/base/EventDispatcher.cpp

138 lines
3.6 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-10-11 21:55:29 +08:00
#include <kiwano/base/EventDispatcher.h>
#include <kiwano/base/Logger.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
void EventDispatcher::Dispatch(Event& evt)
{
2019-08-20 19:32:36 +08:00
if (listeners_.empty())
return;
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-10-31 20:34:38 +08:00
if (listener->IsRunning() && listener->type_ == evt.type)
{
listener->callback_(evt);
}
}
}
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;
}
2019-10-12 11:26:41 +08:00
EventListener* EventDispatcher::AddListener(EventType type, EventListener::Callback callback, String const& name)
{
EventListenerPtr listener = new EventListener(type, callback, name);
2019-08-20 21:15:15 +08:00
return AddListener(listener);
}
void EventDispatcher::StartListeners(String const & listener_name)
{
2019-08-13 21:16:38 +08:00
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
{
if (listener->IsName(listener_name))
{
listener->Start();
}
}
}
void EventDispatcher::StopListeners(String const & listener_name)
{
2019-08-13 21:16:38 +08:00
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
{
if (listener->IsName(listener_name))
{
listener->Stop();
}
}
}
void EventDispatcher::RemoveListeners(String const & listener_name)
{
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();
if (listener->IsName(listener_name))
{
2019-08-20 19:32:36 +08:00
listeners_.remove(listener);
}
}
}
2019-10-12 11:26:41 +08:00
void EventDispatcher::StartListeners(uint32_t type)
{
2019-08-13 21:16:38 +08:00
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
{
if (listener->type_ == type)
{
listener->Start();
}
}
}
2019-10-12 11:26:41 +08:00
void EventDispatcher::StopListeners(uint32_t type)
{
2019-08-13 21:16:38 +08:00
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
{
if (listener->type_ == type)
{
listener->Stop();
}
}
}
2019-10-12 11:26:41 +08:00
void EventDispatcher::RemoveListeners(uint32_t type)
{
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();
if (listener->type_ == type)
{
2019-08-20 19:32:36 +08:00
listeners_.remove(listener);
}
}
}
}