aboutsummaryrefslogtreecommitdiff
path: root/mwe/events
diff options
context:
space:
mode:
Diffstat (limited to 'mwe/events')
-rw-r--r--mwe/events/CMakeLists.txt5
-rw-r--r--mwe/events/include/event.h13
-rw-r--r--mwe/events/include/iKeyListener.h2
-rw-r--r--mwe/events/include/iMouseListener.h26
-rw-r--r--mwe/events/include/keyListenerTest.h12
-rw-r--r--mwe/events/include/mouseListenerTest.h14
-rw-r--r--mwe/events/src/event.cpp24
-rw-r--r--mwe/events/src/keyListenerTest.cpp17
-rw-r--r--mwe/events/src/main.cpp11
-rw-r--r--mwe/events/src/mouseListenerTest.cpp25
10 files changed, 111 insertions, 38 deletions
diff --git a/mwe/events/CMakeLists.txt b/mwe/events/CMakeLists.txt
index e429516..096b8ad 100644
--- a/mwe/events/CMakeLists.txt
+++ b/mwe/events/CMakeLists.txt
@@ -13,13 +13,16 @@ find_package(SDL2 REQUIRED)
add_executable(gameloop
src/window.cpp
src/main.cpp
- #src/eventHandler.cpp
src/eventManager.cpp
src/event.cpp
src/loopManager.cpp
src/timer.cpp
src/keyCodes.cpp
src/eventHandler.cpp
+ src/iMouseListener.cpp
+ src/iKeyListener.cpp
+ src/mouseListenerTest.cpp
+ src/keyListenerTest.cpp
)
add_subdirectory(../../lib/SDL_ttf SDL_ttf)
# Link the SDL2 library to your project
diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h
index 8968c6c..65560a1 100644
--- a/mwe/events/include/event.h
+++ b/mwe/events/include/event.h
@@ -67,15 +67,14 @@ public:
class MouseClickEvent : public Event {
public:
MouseClickEvent(int x, int y, MouseButton button);
-
+
REGISTER_EVENT_TYPE("KeyClickedEvent");
- int getX() const { return x; }
- int getY() const { return y; }
+ std::pair<int, int> getMousePosition() const;
MouseButton getButton() const { return button; }
private:
- int x;
- int y;
+ int mouseX = 0;
+ int mouseY = 0;
MouseButton button;
};
// KeyReleasedEvent class
@@ -147,9 +146,7 @@ public:
REGISTER_EVENT_TYPE(TextSubmitEvent)
- std::string getText() const {
- return text;
- }
+ std::string getText() const;
private:
std::string text;
diff --git a/mwe/events/include/iKeyListener.h b/mwe/events/include/iKeyListener.h
index 87ee1d5..5f39ece 100644
--- a/mwe/events/include/iKeyListener.h
+++ b/mwe/events/include/iKeyListener.h
@@ -11,6 +11,8 @@ public:
protected:
void subscribeEvents(int listenerId = 0);
void unsubscribeEvents(int listenerId = 0);
+ void activate(int listenerId = 0) { subscribeEvents(listenerId); }
+ void deactivate(int listenerId = 0) { unsubscribeEvents(listenerId); }
private:
EventHandler<KeyPressedEvent> keyPressedHandler;
diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h
index 7ae371c..03fd4c3 100644
--- a/mwe/events/include/iMouseListener.h
+++ b/mwe/events/include/iMouseListener.h
@@ -1,36 +1,18 @@
+#pragma once
#include "event.h"
#include "eventManager.h"
#include "eventHandler.h"
class IMouseListener {
public:
- virtual ~IMouseListener() {
- unsubscribeEvents();
- }
+ virtual ~IMouseListener();
virtual void onMouseClicked(const MouseClickEvent& event) = 0;
virtual void onMousePressed(const MousePressedEvent& event) = 0;
virtual void onMouseReleased(const MouseReleasedEvent& event) = 0;
virtual void onMouseMoved(const MouseMovedEvent& event) = 0;
-
protected:
- void subscribeEvents(int listenerId = 0) {
- mouseClickHandler = [this](const MouseClickEvent& event) { this->onMouseClicked(event); };
- mousePressHandler = [this](const MousePressedEvent& event) { this->onMousePressed(event); };
- mouseReleaseHandler = [this](const MouseReleasedEvent& event) { this->onMouseReleased(event); };
- mouseMoveHandler = [this](const MouseMovedEvent& event) { this->onMouseMoved(event); };
-
- subscribe<MouseClickEvent>(mouseClickHandler, listenerId);
- subscribe<MousePressedEvent>(mousePressHandler, listenerId);
- subscribe<MouseReleasedEvent>(mouseReleaseHandler, listenerId);
- subscribe<MouseMovedEvent>(mouseMoveHandler, listenerId);
- }
-
- void unsubscribeEvents(int listenerId = 0) {
- unsubscribe<MouseClickEvent>(mouseClickHandler, listenerId);
- unsubscribe<MousePressedEvent>(mousePressHandler, listenerId);
- unsubscribe<MouseReleasedEvent>(mouseReleaseHandler, listenerId);
- unsubscribe<MouseMovedEvent>(mouseMoveHandler, listenerId);
- }
+ void subscribeEvents(int listenerId = 0);
+ void unsubscribeEvents(int listenerId = 0);
private:
EventHandler<MouseClickEvent> mouseClickHandler;
diff --git a/mwe/events/include/keyListenerTest.h b/mwe/events/include/keyListenerTest.h
new file mode 100644
index 0000000..5990cd1
--- /dev/null
+++ b/mwe/events/include/keyListenerTest.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "iKeyListener.h"
+#include <iostream>
+
+class KeyListenerTest : public IKeyListener {
+public:
+ KeyListenerTest(int listenerId);
+ ~KeyListenerTest();
+
+ void onKeyPressed(const KeyPressedEvent& event) override;
+ void onKeyReleased(const KeyReleasedEvent& event) override;
+};
diff --git a/mwe/events/include/mouseListenerTest.h b/mwe/events/include/mouseListenerTest.h
new file mode 100644
index 0000000..dae04f5
--- /dev/null
+++ b/mwe/events/include/mouseListenerTest.h
@@ -0,0 +1,14 @@
+#pragma once
+#include "iMouseListener.h"
+#include <iostream>
+
+class MouseListenerTest : public IMouseListener {
+public:
+ MouseListenerTest(int listenerId);
+ ~MouseListenerTest();
+
+ void onMouseClicked(const MouseClickEvent& event) override;
+ void onMousePressed(const MousePressedEvent& event) override;
+ void onMouseReleased(const MouseReleasedEvent& event) override;
+ void onMouseMoved(const MouseMovedEvent& event) override;
+};
diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp
index 2509088..fc8eec9 100644
--- a/mwe/events/src/event.cpp
+++ b/mwe/events/src/event.cpp
@@ -53,10 +53,28 @@ Collision CollisionEvent::getCollisionData() const
return this->collisionData;
}
-TextSubmitEvent::TextSubmitEvent(std::string text) : Event("TextSubmitEvent"){
+TextSubmitEvent::TextSubmitEvent(std::string text)
+ : text(text), Event("TextSubmitEvent") {}
+std::string TextSubmitEvent::getText() const {
+ return this->text;
}
-std::string TextSubmitEvent::getText() const{
- return this->text;
+
+MouseReleasedEvent::MouseReleasedEvent(int x, int y, MouseButton button) : mouseX(x), mouseY(y), button(button),Event("MouseReleased"){
+
+}
+std::pair<int, int> MouseReleasedEvent::getMousePosition() const{
+ return {mouseX,mouseY};
+}
+MouseClickEvent::MouseClickEvent(int x,int y,MouseButton button) : mouseX(x), mouseY(y), button(button),Event("MouseClickEvent"){
+
}
+MouseMovedEvent::MouseMovedEvent(int x, int y) : mouseX(x), mouseY(y),Event("MouseMovedEvent"){
+}
+std::pair<int, int> MouseClickEvent::getMousePosition() const {
+ return {mouseX, mouseY};
+}
+std::pair<int, int> MouseMovedEvent::getMousePosition() const {
+ return {mouseX, mouseY};
+}
diff --git a/mwe/events/src/keyListenerTest.cpp b/mwe/events/src/keyListenerTest.cpp
new file mode 100644
index 0000000..90b92a5
--- /dev/null
+++ b/mwe/events/src/keyListenerTest.cpp
@@ -0,0 +1,17 @@
+#include "keyListenerTest.h"
+
+KeyListenerTest::KeyListenerTest(int listenerId) {
+ subscribeEvents(listenerId);
+}
+
+KeyListenerTest::~KeyListenerTest() {
+ unsubscribeEvents();
+}
+
+void KeyListenerTest::onKeyPressed(const KeyPressedEvent& event) {
+ std::cout << "Key pressed: " << event.getKeyCode() << std::endl;
+}
+
+void KeyListenerTest::onKeyReleased(const KeyReleasedEvent& event) {
+ std::cout << "Key released: " << event.getKeyCode() << std::endl;
+}
diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp
index d056f85..03dff16 100644
--- a/mwe/events/src/main.cpp
+++ b/mwe/events/src/main.cpp
@@ -6,6 +6,10 @@
#include "loopManager.h"
#include "event.h"
#include "customTypes.h"
+#include "iKeyListener.h"
+#include "iMouseListener.h"
+#include "keyListenerTest.h"
+#include "mouseListenerTest.h"
class PlayerDamagedEvent : public Event {
public:
PlayerDamagedEvent(int damage, int playerID)
@@ -46,10 +50,9 @@ void testCollisionEvent() {
}
int main(int argc, char * args[]) {
LoopManager gameLoop;
- // Create an event handler for KeyPressedEvent
- // EventHandler<KeyPressedEvent> callback = [](const KeyPressedEvent& e) {
- // onKeyPressed(e);
- // };
+ int testListenerId = 0;
+ KeyListenerTest keyListener(testListenerId);
+ MouseListenerTest mouseListener(testListenerId);
// custom event class poc
subscribe<PlayerDamagedEvent>(onPlayerDamaged);
triggerEvent(PlayerDamagedEvent(50, 1));
diff --git a/mwe/events/src/mouseListenerTest.cpp b/mwe/events/src/mouseListenerTest.cpp
new file mode 100644
index 0000000..4b3aa3e
--- /dev/null
+++ b/mwe/events/src/mouseListenerTest.cpp
@@ -0,0 +1,25 @@
+#include "mouseListenerTest.h"
+
+MouseListenerTest::MouseListenerTest(int listenerId) {
+ subscribeEvents(listenerId);
+}
+
+MouseListenerTest::~MouseListenerTest() {
+ unsubscribeEvents();
+}
+
+void MouseListenerTest::onMouseClicked(const MouseClickEvent& event) {
+ std::cout << "Mouse clicked at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl;
+}
+
+void MouseListenerTest::onMousePressed(const MousePressedEvent& event) {
+ std::cout << "Mouse button pressed at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl;
+}
+
+void MouseListenerTest::onMouseReleased(const MouseReleasedEvent& event) {
+ std::cout << "Mouse button released at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl;
+}
+
+void MouseListenerTest::onMouseMoved(const MouseMovedEvent& event) {
+ std::cout << "Mouse moved to: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl;
+}