From 29f9a26046e35c7eb0157df92757ce8e39f1ec74 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 28 Oct 2024 11:06:21 +0100 Subject: iMouseListener and iKeyListener implementation --- mwe/events/src/event.cpp | 8 ++++++++ mwe/events/src/iKeyListener.cpp | 18 ++++++++++++++++++ mwe/events/src/iMouseListener.cpp | 23 +++++++++++++++++++++++ mwe/events/src/uiObject.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 mwe/events/src/iKeyListener.cpp create mode 100644 mwe/events/src/iMouseListener.cpp create mode 100644 mwe/events/src/uiObject.cpp (limited to 'mwe/events/src') 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(keyPressedHandler, listenerId); + subscribe(keyReleasedHandler, listenerId); +} + +void IKeyListener::unsubscribeEvents(int listenerId) { + unsubscribe(keyPressedHandler, listenerId); + unsubscribe(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(mouseClickHandler, listenerId); + subscribe(mousePressHandler, listenerId); + subscribe(mouseReleaseHandler, listenerId); + subscribe(mouseMoveHandler, listenerId); +} + +void IMouseListener::unsubscribeEvents(int listenerId) { + unsubscribe(mouseClickHandler, listenerId); + unsubscribe(mousePressHandler, listenerId); + unsubscribe(mouseReleaseHandler, listenerId); + unsubscribe(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; +} + -- cgit v1.2.3