aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-20 20:30:33 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-20 20:30:33 +0100
commit5ec30d8915debef409f6db6f8ee9b822fb24d46b (patch)
tree6ed1f32c90b6464047c9e43134d1511cd3030af4 /src
parent397da65e03ec681922aeea3881918026d36068a7 (diff)
make format
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/EventHandler.hpp3
-rw-r--r--src/crepe/api/EventManager.cpp24
-rw-r--r--src/crepe/api/EventManager.h11
-rw-r--r--src/crepe/api/EventManager.hpp11
-rw-r--r--src/crepe/api/IKeyListener.cpp17
-rw-r--r--src/crepe/api/IKeyListener.h6
-rw-r--r--src/crepe/api/IMouseListener.cpp26
-rw-r--r--src/test/EventTest.cpp14
8 files changed, 52 insertions, 60 deletions
diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp
index 8d8136b..391dcca 100644
--- a/src/crepe/api/EventHandler.hpp
+++ b/src/crepe/api/EventHandler.hpp
@@ -7,7 +7,7 @@ namespace crepe {
// Implementation of EventHandlerWrapper constructor
template <typename EventType>
EventHandlerWrapper<EventType>::EventHandlerWrapper(const EventHandler<EventType> & handler)
- : handler(handler){}
+ : handler(handler) {}
// Implementation of EventHandlerWrapper::call
template <typename EventType>
@@ -15,5 +15,4 @@ bool EventHandlerWrapper<EventType>::call(const Event & e) {
return this->handler(static_cast<const EventType &>(e));
}
-
} //namespace crepe
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp
index 64d7c26..993db86 100644
--- a/src/crepe/api/EventManager.cpp
+++ b/src/crepe/api/EventManager.cpp
@@ -20,8 +20,7 @@ void EventManager::dispatch_events() {
}
std::vector<CallbackEntry> & handlers = handlers_it->second;
- for (auto handler_it = handlers.begin(); handler_it != handlers.end();
- ++handler_it) {
+ for (auto handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) {
// If callback is executed and returns true, remove the event from the queue
if ((*handler_it).callback->exec(*event)) {
event_it = this->events_queue.erase(event_it);
@@ -42,15 +41,14 @@ void EventManager::clear() {
}
void EventManager::unsubscribe(subscription_t event_id) {
- for (auto& [event_type, handlers] : this->subscribers) {
- for (auto it = handlers.begin(); it != handlers.end();) {
- if (it->id == event_id) {
- it = handlers.erase(it);
- return;
- } else {
- ++it;
- }
- }
- }
+ for (auto & [event_type, handlers] : this->subscribers) {
+ for (auto it = handlers.begin(); it != handlers.end();) {
+ if (it->id == event_id) {
+ it = handlers.erase(it);
+ return;
+ } else {
+ ++it;
+ }
+ }
+ }
}
-
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h
index 93e9ca2..bd9772a 100644
--- a/src/crepe/api/EventManager.h
+++ b/src/crepe/api/EventManager.h
@@ -48,7 +48,8 @@ public:
* \return A unique subscription ID associated with the registered callback.
*/
template <typename EventType>
- subscription_t subscribe(const EventHandler<EventType> & callback, int channel = CHANNEL_ALL);
+ subscription_t subscribe(const EventHandler<EventType> & callback,
+ int channel = CHANNEL_ALL);
/**
* \brief Unsubscribe a previously registered callback.
@@ -112,8 +113,8 @@ private:
*/
struct QueueEntry {
std::unique_ptr<Event> event; ///< The event instance.
- int channel = CHANNEL_ALL; ///< The channel associated with the event.
- std::type_index type; ///< The type of the event.
+ int channel = CHANNEL_ALL; ///< The channel associated with the event.
+ std::type_index type; ///< The type of the event.
};
/**
@@ -122,8 +123,8 @@ private:
*/
struct CallbackEntry {
std::unique_ptr<IEventHandlerWrapper> callback; ///< The callback function wrapper.
- int channel = CHANNEL_ALL; ///< The channel this callback listens to.
- subscription_t id = -1; ///< Unique subscription ID.
+ int channel = CHANNEL_ALL; ///< The channel this callback listens to.
+ subscription_t id = -1; ///< Unique subscription ID.
};
//! The queue of events to be processed during dispatch.
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index d7afa9f..b2a94bd 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -10,10 +10,7 @@ subscription_t EventManager::subscribe(const EventHandler<EventType> & callback,
= std::make_unique<EventHandlerWrapper<EventType>>(callback);
std::vector<CallbackEntry> & handlers = this->subscribers[event_type];
handlers.emplace_back(CallbackEntry{
- .callback = std::move(handler),
- .channel = channel,
- .id = subscription_counter
- });
+ .callback = std::move(handler), .channel = channel, .id = subscription_counter});
return subscription_counter;
}
@@ -25,9 +22,8 @@ void EventManager::queue_event(const EventType & event, int channel) {
auto event_ptr = std::make_unique<EventType>(event);
- this->events_queue.push_back(QueueEntry{.event = std::move(event_ptr),
- .channel = channel,
- .type = event_type});
+ this->events_queue.push_back(
+ QueueEntry{.event = std::move(event_ptr), .channel = channel, .type = event_type});
}
template <typename EventType>
@@ -49,5 +45,4 @@ void EventManager::trigger_event(const EventType & event, int channel) {
}
}
-
} // namespace crepe
diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp
index ebbf486..7aefaf7 100644
--- a/src/crepe/api/IKeyListener.cpp
+++ b/src/crepe/api/IKeyListener.cpp
@@ -3,15 +3,16 @@
using namespace crepe;
// Constructor with specified channel
-IKeyListener::IKeyListener(int channel) :
- event_manager(EventManager::get_instance()) {
- press_id = event_manager.subscribe<KeyPressEvent>([this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel);
- release_id = event_manager.subscribe<KeyReleaseEvent>([this](const KeyReleaseEvent & event) { return this->on_key_released(event); }, channel);
+IKeyListener::IKeyListener(int channel) : event_manager(EventManager::get_instance()) {
+ this->press_id = event_manager.subscribe<KeyPressEvent>(
+ [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel);
+ this->release_id = event_manager.subscribe<KeyReleaseEvent>(
+ [this](const KeyReleaseEvent & event) { return this->on_key_released(event); },
+ channel);
}
// Destructor, unsubscribe events
-IKeyListener::~IKeyListener() {
- event_manager.unsubscribe(press_id);
- event_manager.unsubscribe(release_id);
+IKeyListener::~IKeyListener() {
+ event_manager.unsubscribe(this->press_id);
+ event_manager.unsubscribe(this->release_id);
}
-
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
index 4726aa7..2a89cbc 100644
--- a/src/crepe/api/IKeyListener.h
+++ b/src/crepe/api/IKeyListener.h
@@ -36,12 +36,14 @@ public:
* \return True if the event was handled, false otherwise.
*/
virtual bool on_key_released(const KeyReleaseEvent & event) = 0;
+
private:
//! Key press event id
subscription_t press_id = -1;
- //!< Key release event id
+ //! Key release event id
subscription_t release_id = -1;
- EventManager & event_manager;
+ //! EventManager reference
+ EventManager & event_manager;;
};
} // namespace crepe
diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp
index a6cb163..7d38280 100644
--- a/src/crepe/api/IMouseListener.cpp
+++ b/src/crepe/api/IMouseListener.cpp
@@ -2,29 +2,27 @@
using namespace crepe;
-IMouseListener::IMouseListener(int channel)
- : event_manager(EventManager::get_instance()) {
- click_id = event_manager.subscribe<MouseClickEvent>(
+IMouseListener::IMouseListener(int channel) : event_manager(EventManager::get_instance()) {
+ this->click_id = event_manager.subscribe<MouseClickEvent>(
[this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); },
channel);
- press_id = event_manager.subscribe<MousePressEvent>(
+ this->press_id = event_manager.subscribe<MousePressEvent>(
[this](const MousePressEvent & event) { return this->on_mouse_pressed(event); },
channel);
- release_id = event_manager.subscribe<MouseReleaseEvent>(
+ this->release_id = event_manager.subscribe<MouseReleaseEvent>(
[this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); },
channel);
- move_id = event_manager.subscribe<MouseMoveEvent>(
- [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); },
- channel);
+ this->move_id = event_manager.subscribe<MouseMoveEvent>(
+ [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, channel);
}
IMouseListener::~IMouseListener() {
- // Unsubscribe event handlers
- event_manager.unsubscribe(click_id);
- event_manager.unsubscribe(press_id);
- event_manager.unsubscribe(release_id);
- event_manager.unsubscribe(move_id);
- }
+ // Unsubscribe event handlers
+ event_manager.unsubscribe(this->click_id);
+ event_manager.unsubscribe(this->press_id);
+ event_manager.unsubscribe(this->release_id);
+ event_manager.unsubscribe(this->move_id);
+}
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index d42e742..b0e6c9c 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -70,11 +70,13 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) {
EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
return false;
};
- EventManager::get_instance().subscribe<MouseClickEvent>(mouse_handler, EventManager::CHANNEL_ALL);
+ EventManager::get_instance().subscribe<MouseClickEvent>(mouse_handler,
+ EventManager::CHANNEL_ALL);
MouseClickEvent click_event{
.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE};
- EventManager::get_instance().trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL);
+ EventManager::get_instance().trigger_event<MouseClickEvent>(click_event,
+ EventManager::CHANNEL_ALL);
EXPECT_TRUE(triggered);
}
@@ -92,13 +94,13 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) {
MouseClickEvent click_event{
.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE};
- EventManager::get_instance().trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL);
+ EventManager::get_instance().trigger_event<MouseClickEvent>(click_event,
+ EventManager::CHANNEL_ALL);
EXPECT_FALSE(triggered);
EventManager::get_instance().trigger_event<MouseClickEvent>(click_event, test_channel);
}
-
TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
EventManager & event_manager = EventManager::get_instance();
@@ -128,8 +130,6 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE};
event_manager.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL);
event_manager.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL);
-
-
// Trigger event
event_manager.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL);
@@ -144,7 +144,6 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
event_manager.clear();
event_manager.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL);
event_manager.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL);
-
// Trigger event again
event_manager.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL);
@@ -186,7 +185,6 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
EXPECT_TRUE(triggered2);
}
-
TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
EventManager & event_manager = EventManager::get_instance();