From 2585dc3cab48ccad0cfa0c63354662d656c86c46 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 3 Nov 2024 13:34:27 +0100 Subject: `make format` --- mwe/events/src/event.cpp | 29 +++--- mwe/events/src/iKeyListener.cpp | 19 ++-- mwe/events/src/iMouseListener.cpp | 35 ++++---- mwe/events/src/inputSystem.cpp | 162 ++++++++++++++++----------------- mwe/events/src/keyListenerTest.cpp | 14 ++- mwe/events/src/loopManager.cpp | 22 +++-- mwe/events/src/main.cpp | 13 ++- mwe/events/src/mouseListenerTest.cpp | 26 +++--- mwe/events/src/uiObject.cpp | 30 +++---- mwe/events/src/uiRenderer.cpp | 170 ++++++++++++++++++----------------- mwe/events/src/window.cpp | 72 +++++++-------- 11 files changed, 287 insertions(+), 305 deletions(-) (limited to 'mwe/events/src') diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp index eae4d14..0c9f3ed 100644 --- a/mwe/events/src/event.cpp +++ b/mwe/events/src/event.cpp @@ -51,27 +51,20 @@ Collision CollisionEvent::getCollisionData() const { return this->collisionData; } -TextSubmitEvent::TextSubmitEvent(std::string text) - : text(text), Event("TextSubmitEvent") { - - } +TextSubmitEvent::TextSubmitEvent(std::string text) + : text(text), Event("TextSubmitEvent") {} -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 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::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 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 MouseClickEvent::getMousePosition() const { return {mouseX, mouseY}; } diff --git a/mwe/events/src/iKeyListener.cpp b/mwe/events/src/iKeyListener.cpp index 59cc89a..0235fab 100644 --- a/mwe/events/src/iKeyListener.cpp +++ b/mwe/events/src/iKeyListener.cpp @@ -1,18 +1,19 @@ #include "iKeyListener.h" -IKeyListener::~IKeyListener() { - unsubscribeEvents(); -} +IKeyListener::~IKeyListener() { unsubscribeEvents(); } void IKeyListener::subscribeEvents(int listenerId) { - keyPressedHandler = [this](const KeyPressedEvent& event) { this->onKeyPressed(event); }; - keyReleasedHandler = [this](const KeyReleasedEvent& event) { this->onKeyReleased(event); }; + keyPressedHandler + = [this](const KeyPressedEvent & event) { this->onKeyPressed(event); }; + keyReleasedHandler = [this](const KeyReleasedEvent & event) { + this->onKeyReleased(event); + }; - subscribe(keyPressedHandler, listenerId); - subscribe(keyReleasedHandler, listenerId); + subscribe(keyPressedHandler, listenerId); + subscribe(keyReleasedHandler, listenerId); } void IKeyListener::unsubscribeEvents(int listenerId) { - unsubscribe(keyPressedHandler, listenerId); - unsubscribe(keyReleasedHandler, listenerId); + unsubscribe(keyPressedHandler, listenerId); + unsubscribe(keyReleasedHandler, listenerId); } diff --git a/mwe/events/src/iMouseListener.cpp b/mwe/events/src/iMouseListener.cpp index 3239304..69d8c9f 100644 --- a/mwe/events/src/iMouseListener.cpp +++ b/mwe/events/src/iMouseListener.cpp @@ -1,23 +1,28 @@ #include "iMouseListener.h" -IMouseListener::~IMouseListener() { - unsubscribeEvents(); -} +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); }; + 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); + 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); + unsubscribe(mouseClickHandler, listenerId); + unsubscribe(mousePressHandler, listenerId); + unsubscribe(mouseReleaseHandler, listenerId); + unsubscribe(mouseMoveHandler, listenerId); } diff --git a/mwe/events/src/inputSystem.cpp b/mwe/events/src/inputSystem.cpp index bb26e8b..b87b12e 100644 --- a/mwe/events/src/inputSystem.cpp +++ b/mwe/events/src/inputSystem.cpp @@ -2,98 +2,94 @@ InputSystem::InputSystem() {} -void InputSystem::registerButton(Button* button) { - buttons.push_back(button); -} -void InputSystem::registerTextInput(TextInput* input) { - textInputs.push_back(input); -} -void InputSystem::registerText(Text* label) { - texts.push_back(label); +void InputSystem::registerButton(Button * button) { buttons.push_back(button); } +void InputSystem::registerTextInput(TextInput * input) { + textInputs.push_back(input); } +void InputSystem::registerText(Text * label) { texts.push_back(label); } void InputSystem::processInput() { - SDL_Event event; - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - triggerEvent(ShutDownEvent()); - break; - case SDL_KEYDOWN: - triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); - processKeyPress(event.key.keysym.sym); - break; - case SDL_TEXTINPUT: - // Process typed characters - processTextInput(event.text.text); - break; - case SDL_MOUSEBUTTONDOWN: { - int mouseX, mouseY; - SDL_GetMouseState(&mouseX, &mouseY); - processMouseClick(mouseX, mouseY); - triggerEvent(MousePressedEvent(mouseX, mouseY)); - break; - } - } - } + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + triggerEvent(ShutDownEvent()); + break; + case SDL_KEYDOWN: + triggerEvent( + KeyPressedEvent(getCustomKey(event.key.keysym.sym))); + processKeyPress(event.key.keysym.sym); + break; + case SDL_TEXTINPUT: + // Process typed characters + processTextInput(event.text.text); + break; + case SDL_MOUSEBUTTONDOWN: { + int mouseX, mouseY; + SDL_GetMouseState(&mouseX, &mouseY); + processMouseClick(mouseX, mouseY); + triggerEvent(MousePressedEvent(mouseX, mouseY)); + break; + } + } + } } - void InputSystem::processMouseClick(int mouseX, int mouseY) { - for (auto* button : buttons) { - if (mouseX >= button->x && mouseX <= (button->x + button->width) && - mouseY >= button->y && mouseY <= (button->y + button->height)) { - button->onClick(); - } - } - for(auto* textInput : textInputs){ - if (mouseX >= textInput->x && mouseX <= textInput->x + textInput->width && - mouseY >= textInput->y && mouseY <= textInput->y + textInput->height) { - textInput->isActive = true; - } else { - textInput->isActive = false; - } + for (auto * button : buttons) { + if (mouseX >= button->x && mouseX <= (button->x + button->width) + && mouseY >= button->y && mouseY <= (button->y + button->height)) { + button->onClick(); + } + } + for (auto * textInput : textInputs) { + if (mouseX >= textInput->x && mouseX <= textInput->x + textInput->width + && mouseY >= textInput->y + && mouseY <= textInput->y + textInput->height) { + textInput->isActive = true; + } else { + textInput->isActive = false; + } } } void InputSystem::processKeyPress(Keycode key) { - // for (auto* textInput : textInputs) { - // if (textInput->isActive) { - // if (key == SDLK_RETURN || key == SDLK_KP_ENTER) { - // // Submit the text - // if (textInput->onSubmit) { - // textInput->onSubmit(); - // } - // } - // else if (key == SDLK_BACKSPACE) { - // // Handle backspace - // if (!textInput->textBuffer.empty() && textInput->caretPosition > 0) { - // textInput->textBuffer.erase(textInput->caretPosition - 1, 1); - // textInput->caretPosition--; - // } - // } - // else if (key == SDLK_LEFT) { - // // Move caret left - // if (textInput->caretPosition > 0) { - // textInput->caretPosition--; - // } - // } - // else if (key == SDLK_RIGHT) { - // // Move caret right - // if (textInput->caretPosition < textInput->textBuffer.size()) { - // textInput->caretPosition++; - // } - // } - // } - // } + // for (auto* textInput : textInputs) { + // if (textInput->isActive) { + // if (key == SDLK_RETURN || key == SDLK_KP_ENTER) { + // // Submit the text + // if (textInput->onSubmit) { + // textInput->onSubmit(); + // } + // } + // else if (key == SDLK_BACKSPACE) { + // // Handle backspace + // if (!textInput->textBuffer.empty() && textInput->caretPosition > 0) { + // textInput->textBuffer.erase(textInput->caretPosition - 1, 1); + // textInput->caretPosition--; + // } + // } + // else if (key == SDLK_LEFT) { + // // Move caret left + // if (textInput->caretPosition > 0) { + // textInput->caretPosition--; + // } + // } + // else if (key == SDLK_RIGHT) { + // // Move caret right + // if (textInput->caretPosition < textInput->textBuffer.size()) { + // textInput->caretPosition++; + // } + // } + // } + // } } -void InputSystem::processTextInput(const std::string& text) { - // for (auto* textInput : textInputs) { - // if (textInput->isActive) { - // // Insert text at caret position - // textInput->textBuffer.insert(textInput->caretPosition, text); - // textInput->caretPosition += text.length(); - // } - // } +void InputSystem::processTextInput(const std::string & text) { + // for (auto* textInput : textInputs) { + // if (textInput->isActive) { + // // Insert text at caret position + // textInput->textBuffer.insert(textInput->caretPosition, text); + // textInput->caretPosition += text.length(); + // } + // } } - diff --git a/mwe/events/src/keyListenerTest.cpp b/mwe/events/src/keyListenerTest.cpp index 90b92a5..8446dfa 100644 --- a/mwe/events/src/keyListenerTest.cpp +++ b/mwe/events/src/keyListenerTest.cpp @@ -1,17 +1,15 @@ #include "keyListenerTest.h" KeyListenerTest::KeyListenerTest(int listenerId) { - subscribeEvents(listenerId); + subscribeEvents(listenerId); } -KeyListenerTest::~KeyListenerTest() { - unsubscribeEvents(); -} +KeyListenerTest::~KeyListenerTest() { unsubscribeEvents(); } -void KeyListenerTest::onKeyPressed(const KeyPressedEvent& event) { - std::cout << "Key pressed: " << event.getKeyCode() << std::endl; +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; +void KeyListenerTest::onKeyReleased(const KeyReleasedEvent & event) { + std::cout << "Key released: " << event.getKeyCode() << std::endl; } diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp index f673d44..0b7d888 100644 --- a/mwe/events/src/loopManager.cpp +++ b/mwe/events/src/loopManager.cpp @@ -1,8 +1,8 @@ #include "loopManager.h" -LoopManager::LoopManager() - : inputSystem(std::make_unique()){ - shutdownHandler = [this](const ShutDownEvent& event) { this->onShutdown(event); }; +LoopManager::LoopManager() : inputSystem(std::make_unique()) { + shutdownHandler + = [this](const ShutDownEvent & event) { this->onShutdown(event); }; subscribe(shutdownHandler); } void LoopManager::processInput() { @@ -70,22 +70,20 @@ void LoopManager::setup() { } }; subscribe(closeWindowCallback, false); - Button* testButton = new Button(200,200); - testButton->color = {100,0,100}; - testButton->onClick = []() { - std::cout << "Button was clicked" << std::endl; - }; + Button * testButton = new Button(200, 200); + testButton->color = {100, 0, 100}; + testButton->onClick + = []() { std::cout << "Button was clicked" << std::endl; }; testButton->x = 200; testButton->y = 200; inputSystem->registerButton(testButton); window.addUIObject(testButton); - - TextInput* testInput = new TextInput(200,200); + TextInput * testInput = new TextInput(200, 200); testInput->x = 100; testInput->y = 100; - testInput->backgroundColor = {20,50,80}; + testInput->backgroundColor = {20, 50, 80}; inputSystem->registerTextInput(testInput); window.addUIObject(testInput); } @@ -95,7 +93,7 @@ void LoopManager::render() { window.renderUIObjects(); } } -void LoopManager::onShutdown(const ShutDownEvent& e){ +void LoopManager::onShutdown(const ShutDownEvent & e) { this->gameRunning = false; } void LoopManager::update() { diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index 972fc70..d49cf74 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -1,16 +1,13 @@ #include "customTypes.h" #include "event.h" -#include "loopManager.h" -#include -#include -#include -#include "loopManager.h" -#include "event.h" -#include "customTypes.h" #include "iKeyListener.h" #include "iMouseListener.h" #include "keyListenerTest.h" +#include "loopManager.h" #include "mouseListenerTest.h" +#include +#include +#include class PlayerDamagedEvent : public Event { public: PlayerDamagedEvent(int damage, int playerID) @@ -53,7 +50,7 @@ int main(int argc, char * args[]) { LoopManager gameLoop; int testListenerId = 0; KeyListenerTest keyListener(testListenerId); - MouseListenerTest mouseListener(testListenerId); + MouseListenerTest mouseListener(testListenerId); // custom event class poc subscribe(onPlayerDamaged); triggerEvent(PlayerDamagedEvent(50, 1)); diff --git a/mwe/events/src/mouseListenerTest.cpp b/mwe/events/src/mouseListenerTest.cpp index 4b3aa3e..a2a7e6d 100644 --- a/mwe/events/src/mouseListenerTest.cpp +++ b/mwe/events/src/mouseListenerTest.cpp @@ -1,25 +1,27 @@ #include "mouseListenerTest.h" MouseListenerTest::MouseListenerTest(int listenerId) { - subscribeEvents(listenerId); + subscribeEvents(listenerId); } -MouseListenerTest::~MouseListenerTest() { - unsubscribeEvents(); -} +MouseListenerTest::~MouseListenerTest() { unsubscribeEvents(); } -void MouseListenerTest::onMouseClicked(const MouseClickEvent& event) { - std::cout << "Mouse clicked at: (" << event.getMousePosition().first << ", " << event.getMousePosition().second << ")" << std::endl; +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::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::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; +void MouseListenerTest::onMouseMoved(const MouseMovedEvent & event) { + std::cout << "Mouse moved to: (" << event.getMousePosition().first << ", " + << event.getMousePosition().second << ")" << std::endl; } diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp index 31ff486..8405469 100644 --- a/mwe/events/src/uiObject.cpp +++ b/mwe/events/src/uiObject.cpp @@ -1,28 +1,24 @@ #include "uiObject.h" // Constructor for UIObject -UIObject::UIObject(int width, int height) - : width(width), height(height){ - - } +UIObject::UIObject(int width, int height) : width(width), height(height) {} // Constructor for Button -Button::Button(int width, int height) - : UIObject(width, height) { -} +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; + : 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(""), - 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; + : UIObject(width, height), textBuffer(""), placeholder(""), 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; } - diff --git a/mwe/events/src/uiRenderer.cpp b/mwe/events/src/uiRenderer.cpp index dbe8dfe..9fec272 100644 --- a/mwe/events/src/uiRenderer.cpp +++ b/mwe/events/src/uiRenderer.cpp @@ -1,103 +1,107 @@ #include "uiRenderer.h" // Constructor -UIRenderer::UIRenderer(SDL_Renderer* renderer) : renderer(renderer) {} +UIRenderer::UIRenderer(SDL_Renderer * renderer) : renderer(renderer) {} // Render function -void UIRenderer::render(UIObject* uiObject) { - if (Button* button = dynamic_cast(uiObject)) { - renderButton(button); - } else if (Text* text = dynamic_cast(uiObject)) { - renderText(text); - } else if (TextInput* textInput = dynamic_cast(uiObject)) { - renderTextInput(textInput); - } +void UIRenderer::render(UIObject * uiObject) { + if (Button * button = dynamic_cast