diff options
| author | JAROWMR <jarorutjes07@gmail.com> | 2024-12-05 21:10:07 +0100 | 
|---|---|---|
| committer | JAROWMR <jarorutjes07@gmail.com> | 2024-12-05 21:10:07 +0100 | 
| commit | 9604616001ac273f2c966c3c829638e905def7bf (patch) | |
| tree | 7e958c2b658a4dbc9e79068d7309513efa928937 /src/crepe/manager/EventManager.cpp | |
| parent | 018a451051669506be447aa925ecb6a9f5aaf418 (diff) | |
| parent | 1f4e961d7f9d6887c807cac1a362f2d178b0860b (diff) | |
merge with master and keypressed added to game
Diffstat (limited to 'src/crepe/manager/EventManager.cpp')
| -rw-r--r-- | src/crepe/manager/EventManager.cpp | 46 | 
1 files changed, 46 insertions, 0 deletions
diff --git a/src/crepe/manager/EventManager.cpp b/src/crepe/manager/EventManager.cpp new file mode 100644 index 0000000..20f0dd3 --- /dev/null +++ b/src/crepe/manager/EventManager.cpp @@ -0,0 +1,46 @@ +#include "EventManager.h" + +using namespace crepe; +using namespace std; + +EventManager & EventManager::get_instance() { +	static EventManager instance; +	return instance; +} + +void EventManager::dispatch_events() { +	for (auto & event : this->events_queue) { +		this->handle_event(event.type, event.channel, *event.event.get()); +	} +	this->events_queue.clear(); +} + +void EventManager::handle_event(type_index type, event_channel_t channel, const Event & data) { +	auto handlers_it = this->subscribers.find(type); +	if (handlers_it == this->subscribers.end()) return; + +	vector<CallbackEntry> & handlers = handlers_it->second; +	for (auto & handler : handlers) { +		bool check_channel = handler.channel != CHANNEL_ALL || channel != CHANNEL_ALL; +		if (check_channel && handler.channel != channel) continue; + +		bool handled = handler.callback->exec(data); +		if (handled) return; +	} +} + +void EventManager::clear() { +	this->subscribers.clear(); +	this->events_queue.clear(); +} + +void EventManager::unsubscribe(subscription_t id) { +	for (auto & [event_type, handlers] : this->subscribers) { +		for (auto it = handlers.begin(); it != handlers.end(); it++) { +			// find listener with subscription id +			if ((*it).id != id) continue; +			it = handlers.erase(it); +			return; +		} +	} +}  |