aboutsummaryrefslogtreecommitdiff
path: root/mwe
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-28 11:06:21 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-28 11:06:21 +0100
commit29f9a26046e35c7eb0157df92757ce8e39f1ec74 (patch)
tree18be4912561c2fcc9a57b99466c74977e11f2abf /mwe
parentf1b2c5a38f49bb319babf11bf64c420a63e104f2 (diff)
iMouseListener and iKeyListener implementation
Diffstat (limited to 'mwe')
-rw-r--r--mwe/events/CMakeLists.txt3
-rw-r--r--mwe/events/font/Arial.ttfbin0 -> 275572 bytes
-rw-r--r--mwe/events/include/event.h51
-rw-r--r--mwe/events/include/gameObject.h20
-rw-r--r--mwe/events/include/iKeyListener.h19
-rw-r--r--mwe/events/include/iMouseListener.h40
-rw-r--r--mwe/events/include/keyCodes.h11
-rw-r--r--mwe/events/include/uiObject.h66
-rw-r--r--mwe/events/src/event.cpp8
-rw-r--r--mwe/events/src/iKeyListener.cpp18
-rw-r--r--mwe/events/src/iMouseListener.cpp23
-rw-r--r--mwe/events/src/uiObject.cpp28
12 files changed, 286 insertions, 1 deletions
diff --git a/mwe/events/CMakeLists.txt b/mwe/events/CMakeLists.txt
index 585d869..e429516 100644
--- a/mwe/events/CMakeLists.txt
+++ b/mwe/events/CMakeLists.txt
@@ -21,7 +21,7 @@ add_executable(gameloop
src/keyCodes.cpp
src/eventHandler.cpp
)
-
+add_subdirectory(../../lib/SDL_ttf SDL_ttf)
# Link the SDL2 library to your project
target_link_libraries(gameloop ${SDL2_LIBRARIES})
@@ -29,3 +29,4 @@ target_link_libraries(gameloop ${SDL2_LIBRARIES})
target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS})
target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include)
+file(COPY ${PROJECT_SOURCE_DIR}/font DESTINATION ${CMAKE_BINARY_DIR})
diff --git a/mwe/events/font/Arial.ttf b/mwe/events/font/Arial.ttf
new file mode 100644
index 0000000..7ff88f2
--- /dev/null
+++ b/mwe/events/font/Arial.ttf
Binary files differ
diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h
index 0208085..8968c6c 100644
--- a/mwe/events/include/event.h
+++ b/mwe/events/include/event.h
@@ -64,7 +64,20 @@ public:
Keycode key = 0;
int repeatCount = 0;
};
+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; }
+ MouseButton getButton() const { return button; }
+private:
+ int x;
+ int y;
+ MouseButton button;
+};
// KeyReleasedEvent class
class KeyReleasedEvent : public Event {
public:
@@ -90,6 +103,31 @@ public:
private:
int mouseX = 0;
int mouseY = 0;
+ MouseButton button;
+};
+class MouseReleasedEvent : public Event {
+public:
+ MouseReleasedEvent(int mouseX, int mouseY, MouseButton button);
+
+ REGISTER_EVENT_TYPE(MouseReleasedEvent)
+
+ std::pair<int, int> getMousePosition() const;
+ MouseButton getMouseButton() const;
+private:
+ int mouseX = 0;
+ int mouseY = 0;
+ MouseButton button;
+};
+class MouseMovedEvent : public Event {
+public:
+ MouseMovedEvent(int mouseX, int mouseY);
+
+ REGISTER_EVENT_TYPE(MouseMovedEvent)
+
+ std::pair<int, int> getMousePosition() const;
+private:
+ int mouseX = 0;
+ int mouseY = 0;
};
class CollisionEvent : public Event {
public:
@@ -103,3 +141,16 @@ private:
Collision collisionData;
};
+class TextSubmitEvent : public Event {
+public:
+ TextSubmitEvent(std::string submittedText);
+
+ REGISTER_EVENT_TYPE(TextSubmitEvent)
+
+ std::string getText() const {
+ return text;
+ }
+
+private:
+ std::string text;
+};
diff --git a/mwe/events/include/gameObject.h b/mwe/events/include/gameObject.h
new file mode 100644
index 0000000..70d4517
--- /dev/null
+++ b/mwe/events/include/gameObject.h
@@ -0,0 +1,20 @@
+#pragma once
+#include <cstdint>
+#include <string>
+class GameObject {
+public:
+ GameObject(std::uint32_t id, std::string name, std::string tag, int layer);
+
+ template <typename... Args>
+ void addSpriteComponent(Args &&... args);
+ template <typename... Args>
+ void addRigidbodyComponent(Args &&... args);
+ template <typename... Args>
+ void addColiderComponent(Args &&... args);
+
+ std::uint32_t mId;
+ std::string mName;
+ std::string mTag;
+ bool mActive;
+ int mLayer;
+};
diff --git a/mwe/events/include/iKeyListener.h b/mwe/events/include/iKeyListener.h
new file mode 100644
index 0000000..87ee1d5
--- /dev/null
+++ b/mwe/events/include/iKeyListener.h
@@ -0,0 +1,19 @@
+#pragma once
+#include "event.h"
+#include "eventManager.h"
+#include "eventHandler.h"
+class IKeyListener {
+public:
+ virtual ~IKeyListener();
+ virtual void onKeyPressed(const KeyPressedEvent& event) = 0;
+ virtual void onKeyReleased(const KeyReleasedEvent& event) = 0;
+
+protected:
+ void subscribeEvents(int listenerId = 0);
+ void unsubscribeEvents(int listenerId = 0);
+
+private:
+ EventHandler<KeyPressedEvent> keyPressedHandler;
+ EventHandler<KeyReleasedEvent> keyReleasedHandler;
+};
+
diff --git a/mwe/events/include/iMouseListener.h b/mwe/events/include/iMouseListener.h
new file mode 100644
index 0000000..7ae371c
--- /dev/null
+++ b/mwe/events/include/iMouseListener.h
@@ -0,0 +1,40 @@
+#include "event.h"
+#include "eventManager.h"
+#include "eventHandler.h"
+class IMouseListener {
+public:
+ virtual ~IMouseListener() {
+ unsubscribeEvents();
+ }
+
+ 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);
+ }
+
+private:
+ EventHandler<MouseClickEvent> mouseClickHandler;
+ EventHandler<MousePressedEvent> mousePressHandler;
+ EventHandler<MouseReleasedEvent> mouseReleaseHandler;
+ EventHandler<MouseMovedEvent> mouseMoveHandler;
+};
diff --git a/mwe/events/include/keyCodes.h b/mwe/events/include/keyCodes.h
index 0879efc..61deba2 100644
--- a/mwe/events/include/keyCodes.h
+++ b/mwe/events/include/keyCodes.h
@@ -3,6 +3,16 @@
#include <cstdint>
#include <unordered_map>
using Keycode = uint16_t;
+enum class MouseButton {
+ None = 0,
+ Left_Mouse = 1,
+ Right_Mouse = 2,
+ Middle_Mouse = 3,
+ X1_Mouse = 4,
+ X2_Mouse = 5,
+ Scroll_Up = 6,
+ Scroll_Down = 7,
+};
enum : Keycode {
// From glfw3.h
Space = 32,
@@ -136,6 +146,7 @@ enum : Keycode {
RightSuper = 347,
Menu = 348
};
+
// Define the mapping
extern const std::unordered_map<SDL_Keycode, Keycode> sdlToCustom;
diff --git a/mwe/events/include/uiObject.h b/mwe/events/include/uiObject.h
new file mode 100644
index 0000000..46d32be
--- /dev/null
+++ b/mwe/events/include/uiObject.h
@@ -0,0 +1,66 @@
+#include "gameObject.h"
+#include <SDL2/SDL.h>
+#include <SDL_ttf.h>
+#include "event.h"
+#include "eventHandler.h"
+struct Alignment {
+ enum class Horizontal { LEFT, CENTER, RIGHT };
+ enum class Vertical { TOP, MIDDLE, BOTTOM };
+ enum class PositioningMode { RELATIVE, STATIC,ABSOLUTE };
+
+ Horizontal horizontal = Horizontal::CENTER;
+ Vertical vertical = Vertical::MIDDLE;
+ PositioningMode mode = PositioningMode::RELATIVE;
+
+ int staticX = 0;
+ int staticY = 0;
+
+ int marginTop = 0;
+ int marginBottom = 0;
+ int marginLeft = 0;
+ int marginRight = 0;
+};
+struct RGBColor{
+ int red,
+ int green,
+ int blue
+};
+class UIObject : public GameObject{
+ public:
+ UIObject(int width,int height);
+ private:
+ int width;
+ int height;
+};
+class Button : public UIObject{
+ public:
+ Button(int width,int height);
+ EventHandler<KeyPressedEvent> onKeyPressed;
+ EventHandler<KeyReleasedEvent> onKeyReleased;
+
+};
+class Text : public UIObject{
+ public:
+ Text(int width,int height);
+ private:
+ std::string text;
+ int size;
+ Alignment alignment;
+ //font resource
+ TTF_Font *font;
+ RGBColor color;
+};
+class TextInput : public UIObject{
+ public:
+ TextInput (int width,int height);
+ std::string textBuffer;
+ std::string placeholder;
+ size_t caretPosition;
+ bool isActive;
+ RGBColor textColor;
+ RGBColor backgroundColor;
+ size_t maxLength;
+ Alignment alignment;
+ TTF_Font* font;
+ EventHandler<TextSubmitEvent> onSubmit;
+};
diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp
index 38ff62a..2509088 100644
--- a/mwe/events/src/event.cpp
+++ b/mwe/events/src/event.cpp
@@ -52,3 +52,11 @@ Collision CollisionEvent::getCollisionData() const
{
return this->collisionData;
}
+
+TextSubmitEvent::TextSubmitEvent(std::string text) : Event("TextSubmitEvent"){
+
+}
+std::string TextSubmitEvent::getText() const{
+ return this->text;
+}
+
diff --git a/mwe/events/src/iKeyListener.cpp b/mwe/events/src/iKeyListener.cpp
new file mode 100644
index 0000000..59cc89a
--- /dev/null
+++ b/mwe/events/src/iKeyListener.cpp
@@ -0,0 +1,18 @@
+#include "iKeyListener.h"
+
+IKeyListener::~IKeyListener() {
+ unsubscribeEvents();
+}
+
+void IKeyListener::subscribeEvents(int listenerId) {
+ keyPressedHandler = [this](const KeyPressedEvent& event) { this->onKeyPressed(event); };
+ keyReleasedHandler = [this](const KeyReleasedEvent& event) { this->onKeyReleased(event); };
+
+ subscribe<KeyPressedEvent>(keyPressedHandler, listenerId);
+ subscribe<KeyReleasedEvent>(keyReleasedHandler, listenerId);
+}
+
+void IKeyListener::unsubscribeEvents(int listenerId) {
+ unsubscribe<KeyPressedEvent>(keyPressedHandler, listenerId);
+ unsubscribe<KeyReleasedEvent>(keyReleasedHandler, listenerId);
+}
diff --git a/mwe/events/src/iMouseListener.cpp b/mwe/events/src/iMouseListener.cpp
new file mode 100644
index 0000000..3239304
--- /dev/null
+++ b/mwe/events/src/iMouseListener.cpp
@@ -0,0 +1,23 @@
+#include "iMouseListener.h"
+IMouseListener::~IMouseListener() {
+ unsubscribeEvents();
+}
+
+void IMouseListener::subscribeEvents(int listenerId) {
+ 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 IMouseListener::unsubscribeEvents(int listenerId) {
+ unsubscribe<MouseClickEvent>(mouseClickHandler, listenerId);
+ unsubscribe<MousePressedEvent>(mousePressHandler, listenerId);
+ unsubscribe<MouseReleasedEvent>(mouseReleaseHandler, listenerId);
+ unsubscribe<MouseMovedEvent>(mouseMoveHandler, listenerId);
+}
diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp
new file mode 100644
index 0000000..b941858
--- /dev/null
+++ b/mwe/events/src/uiObject.cpp
@@ -0,0 +1,28 @@
+#include "uiObject.h"
+
+// Constructor for UIObject
+UIObject::UIObject(int width, int height)
+ : width(width), height(height) {
+
+ }
+
+// Constructor for Button
+Button::Button(int width, int height)
+ : UIObject(width, height) {
+}
+
+Text::Text(int width, int height)
+ : UIObject(width, height), size(12), font(nullptr), color{255, 255, 255} { // Default size and color
+ alignment.horizontal = Alignment::Horizontal::CENTER;
+ alignment.vertical = Alignment::Vertical::MIDDLE;
+ alignment.mode = Alignment::PositioningMode::RELATIVE;
+}
+
+TextInput::TextInput(int width, int height)
+ : UIObject(width, height), textBuffer(""), placeholder(""), caretPosition(0),
+ isActive(false), textColor{255, 255, 255}, backgroundColor{0, 0, 0}, maxLength(100), font(nullptr) {
+ alignment.horizontal = Alignment::Horizontal::LEFT;
+ alignment.vertical = Alignment::Vertical::TOP;
+ alignment.mode = Alignment::PositioningMode::RELATIVE;
+}
+