diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-28 11:06:21 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-28 11:06:21 +0100 |
commit | 29f9a26046e35c7eb0157df92757ce8e39f1ec74 (patch) | |
tree | 18be4912561c2fcc9a57b99466c74977e11f2abf /mwe/events/include | |
parent | f1b2c5a38f49bb319babf11bf64c420a63e104f2 (diff) |
iMouseListener and iKeyListener implementation
Diffstat (limited to 'mwe/events/include')
-rw-r--r-- | mwe/events/include/event.h | 51 | ||||
-rw-r--r-- | mwe/events/include/gameObject.h | 20 | ||||
-rw-r--r-- | mwe/events/include/iKeyListener.h | 19 | ||||
-rw-r--r-- | mwe/events/include/iMouseListener.h | 40 | ||||
-rw-r--r-- | mwe/events/include/keyCodes.h | 11 | ||||
-rw-r--r-- | mwe/events/include/uiObject.h | 66 |
6 files changed, 207 insertions, 0 deletions
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; +}; |