Added event system
This commit is contained in:
@ -47,7 +47,7 @@ namespace Dawn {
|
|||||||
* @param scene Scene to render.
|
* @param scene Scene to render.
|
||||||
* @param camera Camera within the scene to render.
|
* @param camera Camera within the scene to render.
|
||||||
*/
|
*/
|
||||||
virtual void renderSceneCamera(Scene &scene, Camera &camera) = 0;
|
virtual void renderSceneCamera(Scene &scene, Camera &camera);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleanup a render pipeline that has been initialized.
|
* Cleanup a render pipeline that has been initialized.
|
||||||
|
119
src/dawn/event/Event.hpp
Normal file
119
src/dawn/event/Event.hpp
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
// Copyright (c) 2022 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dawnlibs.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
|
||||||
|
template<typename... A>
|
||||||
|
struct IEventListener {
|
||||||
|
/**
|
||||||
|
* Abstracted method for C++ template reasons. Invokes the listener.
|
||||||
|
*
|
||||||
|
* @param args Arguments to pass to the listener.
|
||||||
|
*/
|
||||||
|
virtual void invoke(A... args) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T, typename... A>
|
||||||
|
struct EventListener : public IEventListener<A...> {
|
||||||
|
T *instance;
|
||||||
|
void (T::*callback)(A... args);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new event listener structure.
|
||||||
|
*
|
||||||
|
* @param instance Instance that the callback belongs to.
|
||||||
|
* @param callback Callback method that invokes back.
|
||||||
|
*/
|
||||||
|
EventListener(T *instance, void (T::*callback)(A... args)) :
|
||||||
|
instance(instance),
|
||||||
|
callback(callback)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void invoke(A... args) {
|
||||||
|
((*this->instance).*(this->callback))(args...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename...A>
|
||||||
|
class Event {
|
||||||
|
private:
|
||||||
|
std::vector<IEventListener<A...>*> listeners;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Add a listener to this event.
|
||||||
|
*
|
||||||
|
* @tparam T The class that will receive the event.
|
||||||
|
* @param instance Instance of type T that will receive the callback.
|
||||||
|
* @param callback Callback method attached to T to receive the event.
|
||||||
|
* @return The initialized event listener. You don't really need this.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
EventListener<T, A...> * addListener(
|
||||||
|
T *instance,
|
||||||
|
void (T::*callback)(A... args)
|
||||||
|
) {
|
||||||
|
auto listener = new EventListener<T,A...>(instance, callback);
|
||||||
|
this->listeners.push_back(listener);
|
||||||
|
return listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an event listener from this event.
|
||||||
|
*
|
||||||
|
* @tparam T The class that was once receiving the event.
|
||||||
|
* @param instance Instance of type T that did receive the callback.
|
||||||
|
* @param callback Callback method attached to T for the event.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
void removeListener(
|
||||||
|
T *instance,
|
||||||
|
void (T::*callback)(A... args)
|
||||||
|
) {
|
||||||
|
auto it = this->listeners.begin();
|
||||||
|
while(it != this->listeners.end()) {
|
||||||
|
auto listener = static_cast<EventListener<T,A...>*>(*it);
|
||||||
|
|
||||||
|
if(listener->instance != instance || listener->callback != callback) {
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->listeners.erase(it);
|
||||||
|
delete listener;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes the event and emits to all of the listeners.
|
||||||
|
*
|
||||||
|
* @param args Arguments for this event to pass to the listeners.
|
||||||
|
*/
|
||||||
|
void invoke(A... args) {
|
||||||
|
auto it = this->listeners.begin();
|
||||||
|
while(it != this->listeners.end()) {
|
||||||
|
(*it)->invoke(args...);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes the event instance. Will also destroy all of the event
|
||||||
|
* listeners.
|
||||||
|
*/
|
||||||
|
~Event() {
|
||||||
|
auto it = this->listeners.begin();
|
||||||
|
while(it != this->listeners.end()) {
|
||||||
|
delete *it;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#include "DawnPokerGame.hpp"
|
#include "DawnPokerGame.hpp"
|
||||||
|
#include "event/Event.hpp"
|
||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user