2023-05-23 15:28:53 -04:00
|
|
|
/* +------------------------------------------------------+
|
|
|
|
____/ \____ /| - Open source game framework licensed to freely use, |
|
|
|
|
\ / / | copy, modify and sell without restriction |
|
|
|
|
+--\ ^__^ /--+ | |
|
|
|
|
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
|
|
| SPACE ~~~~~ | /
|
|
|
|
| ~~~~~~~ BOX |/
|
|
|
|
+-------------*/
|
2019-05-03 02:09:48 -04:00
|
|
|
|
2021-11-09 23:30:27 -05:00
|
|
|
#include "Delegate.hpp"
|
2019-05-04 20:30:39 -04:00
|
|
|
|
2023-05-23 15:28:53 -04:00
|
|
|
using namespace sb;
|
|
|
|
|
2019-05-07 03:33:54 -04:00
|
|
|
Delegate::Delegate(Node* parent) : Node(parent) {}
|
2019-05-03 02:09:48 -04:00
|
|
|
|
2020-09-02 23:32:01 -04:00
|
|
|
void Delegate::add_subscriber(Subscriber s, std::uint32_t type)
|
2019-05-03 02:09:48 -04:00
|
|
|
{
|
|
|
|
if (subscribers.count(type) == 0)
|
|
|
|
{
|
|
|
|
subscribers[type] = {};
|
|
|
|
}
|
|
|
|
subscribers[type].push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delegate::dispatch()
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
2021-11-09 23:30:27 -05:00
|
|
|
/* Don't even notify subscribers of the event if input is suppressed and the input is a key or mouse click. */
|
|
|
|
if (get_input().is_suppressed() && (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP ||
|
|
|
|
event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP))
|
2020-09-02 23:32:01 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-03 02:09:48 -04:00
|
|
|
for (auto iter = subscribers.begin(); iter != subscribers.end(); iter++)
|
|
|
|
{
|
2020-09-02 23:32:01 -04:00
|
|
|
if (event.type == iter->first)
|
2019-05-03 02:09:48 -04:00
|
|
|
{
|
2020-07-27 14:19:11 -04:00
|
|
|
cancelling_propagation = false;
|
2019-05-18 21:25:27 -04:00
|
|
|
for (Subscriber s : iter->second)
|
2019-05-03 02:09:48 -04:00
|
|
|
{
|
2019-07-21 03:13:19 -04:00
|
|
|
if (s.o->is_active())
|
|
|
|
{
|
|
|
|
s.f(event);
|
2020-07-27 14:19:11 -04:00
|
|
|
if (cancelling_propagation)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-07-21 03:13:19 -04:00
|
|
|
}
|
2019-05-03 02:09:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-31 21:53:39 -04:00
|
|
|
if (event.type == command_event_type)
|
|
|
|
{
|
|
|
|
delete static_cast<bool*>(event.user.data2);
|
|
|
|
}
|
2019-05-03 02:09:48 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 03:33:54 -04:00
|
|
|
|
2020-07-22 00:53:40 -04:00
|
|
|
bool Delegate::compare(SDL_Event& event, const std::vector<std::string>& commands, bool neutral, bool cancel)
|
2019-05-07 03:33:54 -04:00
|
|
|
{
|
2020-07-21 15:04:12 -04:00
|
|
|
for (const std::string& command : commands)
|
|
|
|
{
|
|
|
|
if (compare(event, command, neutral, cancel))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-22 00:53:40 -04:00
|
|
|
bool Delegate::compare(SDL_Event& event, const std::string& command, bool neutral, bool cancel)
|
2020-07-21 15:04:12 -04:00
|
|
|
{
|
|
|
|
return event.type == command_event_type &&
|
2020-07-31 12:10:21 -04:00
|
|
|
(command == "" || command == get_event_command(event)) &&
|
|
|
|
(neutral || (cancel == get_event_cancel_state(event)));
|
2019-05-31 21:53:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 15:04:12 -04:00
|
|
|
bool Delegate::compare_cancel(SDL_Event& event, const std::string& command)
|
2019-05-31 21:53:39 -04:00
|
|
|
{
|
|
|
|
return compare(event, command, false, true);
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:04:12 -04:00
|
|
|
bool Delegate::compare_neutral(SDL_Event& event, const std::string& command)
|
2019-05-31 21:53:39 -04:00
|
|
|
{
|
|
|
|
return compare(event, command, true);
|
2019-05-07 03:33:54 -04:00
|
|
|
}
|
2020-07-27 14:19:11 -04:00
|
|
|
|
|
|
|
void Delegate::cancel_propagation()
|
|
|
|
{
|
|
|
|
cancelling_propagation = true;
|
|
|
|
}
|
2020-07-31 12:10:21 -04:00
|
|
|
|
2022-07-23 18:55:27 -04:00
|
|
|
const std::string& Delegate::get_event_command(SDL_Event& event)
|
2020-07-31 12:10:21 -04:00
|
|
|
{
|
|
|
|
return *static_cast<std::string*>(event.user.data1);
|
|
|
|
}
|
|
|
|
|
2022-07-23 18:55:27 -04:00
|
|
|
bool Delegate::get_event_cancel_state(SDL_Event& event)
|
2020-07-31 12:10:21 -04:00
|
|
|
{
|
|
|
|
return *static_cast<bool*>(event.user.data2);
|
|
|
|
}
|