-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
220 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef __DK_EVENT_H__ | ||
#define __DK_EVENT_H__ | ||
|
||
namespace dk | ||
{ | ||
|
||
class event | ||
{ | ||
public: | ||
}; | ||
|
||
} | ||
|
||
#endif // !__DK_EVENT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef __DK_EVENT_CATEGORY_H__ | ||
#define __DK_EVENT_CATEGORY_H__ | ||
|
||
#include <type_traits> | ||
#include "event.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
template<typename... EventTypes> | ||
class event_category | ||
{ | ||
private: | ||
//static_assert((std::is_base_of_v<event, EventTypes> && ...), "Maybe event would have some fields in the future"); | ||
}; | ||
|
||
} | ||
|
||
#endif // !__DK_EVENT_CATEGORY_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef __DK_EVENT_LISTENER_H__ | ||
#define __DK_EVENT_LISTENER_H__ | ||
|
||
#include <type_traits> | ||
#include "event_category.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
template<typename EventType> | ||
class event_listener | ||
{ | ||
private: | ||
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future"); | ||
|
||
public: | ||
virtual void handle(const EventType& e) = 0; | ||
void handle() { this->handle({}); } | ||
}; | ||
|
||
template<typename... EventTypes> | ||
class event_listener<event_category<EventTypes...>> : public event_listener<EventTypes>... {}; | ||
|
||
} | ||
|
||
#endif // !__DK_EVENT_LISTENER_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __DK_EVENT_SYSTEM_H__ | ||
#define __DK_EVENT_SYSTEM_H__ | ||
|
||
#include "event_listener.h" | ||
#include "containers/vector.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
template<typename EventType> | ||
class event_system | ||
{ | ||
private: | ||
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future"); | ||
|
||
vector<event_listener<EventType>*> m_listeners; | ||
|
||
public: | ||
static event_system& get() { static event_system sys; return sys; } | ||
|
||
template<typename... Args> void send(Args&&... args) | ||
{ | ||
EventType e(std::forward<Args>(args)...); | ||
for (auto& l: m_listeners) | ||
l->handle(e); | ||
} | ||
|
||
void subscribe(event_listener<EventType>* listener) | ||
{ | ||
m_listeners.emplace_back(listener); | ||
} | ||
}; | ||
|
||
template<typename... EventTypes> | ||
class event_system<event_category<EventTypes...>> | ||
{ | ||
private: | ||
// TODO: another send for event_category??? | ||
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future"); | ||
|
||
public: | ||
static void subscribe(event_listener<event_category<EventTypes...>>* listener) | ||
{ | ||
(event_system<EventTypes>::get().subscribe(listener), ...); | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif // !__DK_EVENT_SYSTEM_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __DK_CREATE_EVENT_H__ | ||
#define __DK_CREATE_EVENT_H__ | ||
|
||
#include "event.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
class create_event : event {}; | ||
|
||
} | ||
|
||
#endif // !__DK_CREATE_EVENT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __DK_DESTROY_EVENT_H__ | ||
#define __DK_DESTROY_EVENT_H__ | ||
|
||
#include "event.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
class destroy_event : event {}; | ||
|
||
} | ||
|
||
#endif // !__DK_DESTROY_EVENT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __DK_RENDER_EVENT_H__ | ||
#define __DK_RENDER_EVENT_H__ | ||
|
||
#include "event.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
class render_event : event {}; | ||
|
||
} | ||
|
||
#endif // !__DK_RENDER_EVENT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef __DK_UPDATE_EVENT_H__ | ||
#define __DK_UPDATE_EVENT_H__ | ||
|
||
#include "event.h" | ||
|
||
namespace dk | ||
{ | ||
|
||
class update_event : event {}; | ||
|
||
} | ||
|
||
#endif // !__DK_UPDATE_EVENT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters