diff --git a/src/sdl/MainMenu.cpp b/src/sdl/MainMenu.cpp index ad9d8e0..0d2a095 100644 --- a/src/sdl/MainMenu.cpp +++ b/src/sdl/MainMenu.cpp @@ -33,9 +33,7 @@ void MainMenu::init() { return; } - TextButton romButton(0, 25.0f * this->roms.size(), WIDTH, 25, text); - - this->roms.emplace(file, romButton); + this->roms.emplace(file, TextButton(0, 25.0f * this->roms.size(), WIDTH, 25, text)); } } @@ -45,30 +43,6 @@ void MainMenu::init() { return; } this->chooseFolder = std::make_unique(0, 400, WIDTH, 80, text); - - // font = TTF_OpenFont("font.ttf", 32); // Ensure the path is correct - // if (!font) { - // // printf("TTF_OpenFont Error: %s\n", SDL_GetError()); - // return; - // } - // - // // Create the text surface - // SDL_Color color = {0, 255, 0, 255}; // Green color - // text = TTF_RenderText_Solid(font, "Some test", 9, color); - // if (!text) { - // // printf("TTF_RenderText_Solid Error: %s\n", TTF_GetError()); - // return; - // } - // - // // Create the text texture - // textTexture = SDL_CreateTextureFromSurface(renderer, text); - // if (!textTexture) { - // printf("SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError()); - // SDL_DestroySurface(text); // Free the surface if texture creation fails - // return; - // } - // - // SDL_DestroySurface(text); } bool MainMenu::handleEvent(SDL_Event &event) { @@ -105,7 +79,7 @@ void MainMenu::update() { } for (auto &romButton: roms) { - romButton.second.update(&this->inputHandler, point); + romButton.second.update(this->inputHandler, point); if (!romButton.second.isJustClicked()) { continue; @@ -113,7 +87,7 @@ void MainMenu::update() { this->windows.emplace_back(std::make_unique(romButton.first))->init(); } - this->chooseFolder->update(&this->inputHandler, point); + this->chooseFolder->update(this->inputHandler, point); if (this->chooseFolder->isJustClicked()) { @@ -154,8 +128,7 @@ void MainMenu::update() { return; } - TextButton romButton(0, 25.0f * roms.size(), WIDTH, 25, text); - roms.emplace(file.path().string(), romButton); + roms.emplace(file.path().string(), TextButton(0, 25.0f * roms.size(), WIDTH, 25, text)); } config.writeFile(configFilePath); } @@ -166,7 +139,7 @@ void MainMenu::update() { } void MainMenu::render() { - SDL_SetRenderDrawColor(this->renderer, 0x00, 0x00, 0x00, 0x00); + SDL_SetRenderDrawColor(this->renderer, 0x00, 0x00, 0x00, 255); SDL_RenderClear(renderer); for (auto &romButton: roms) { romButton.second.draw(renderer); @@ -185,11 +158,11 @@ void MainMenu::resize(SDL_Event& event) { for (auto& romButton : roms) { romButton.second.updateSize(this->originalSize, size); - romButton.second.update(&this->inputHandler, point); + romButton.second.update(this->inputHandler, point); } chooseFolder->updateSize(this->originalSize,size); - chooseFolder->update(&this->inputHandler, point); + chooseFolder->update(this->inputHandler, point); } void MainMenu::close() { diff --git a/src/sdl/MainMenu.h b/src/sdl/MainMenu.h index 21f5a6c..2f5b1cd 100644 --- a/src/sdl/MainMenu.h +++ b/src/sdl/MainMenu.h @@ -1,6 +1,7 @@ #ifndef MAINMENU_H #define MAINMENU_H +#include #include #include diff --git a/src/sdl/ui/TextButton.cpp b/src/sdl/ui/TextButton.cpp index 3dcfbce..59c213b 100644 --- a/src/sdl/ui/TextButton.cpp +++ b/src/sdl/ui/TextButton.cpp @@ -2,9 +2,14 @@ #include -TextButton::TextButton(float x, float y, float width, float height, TTF_Text* text) : text(text) { +TextButton::TextButton(float x, float y, float width, float height, TTF_Text* text) : + text(text), idleColor(SDL_Color{192, 192, 192, 255}), + hoverColor(SDL_Color{128, 128, 128, 255}), activeColor(SDL_Color{64, 64, 64, 255}), + deleteColor(SDL_Color{255, 0, 0, 255}) { this->button = SDL_FRect{x, y, width, height}; + currentColor = idleColor; + TTF_SetTextColor(text, 0, 0, 0, 255); SDL_Point textSize{}; TTF_GetTextSize(text, &textSize.x, &textSize.y); @@ -14,15 +19,11 @@ TextButton::TextButton(float x, float y, float width, float height, TTF_Text* te this->originalPosition = {button.x, button.y}; this->originalSize = {button.w, button.h}; - this->idleColor = SDL_Color{192, 192, 192}; - this->hoverColor = SDL_Color{128, 128, 128}; - this->activeColor = SDL_Color{64, 64, 64}; - this->isPressed = false; this->isHovered = false; } -void TextButton::updateSize(const SDL_Point originalSize, const SDL_Point updatedSize) { +void TextButton::updateSize(const SDL_Point& originalSize, const SDL_Point& updatedSize) { this->button.x = this->originalPosition.x / originalSize.x * updatedSize.x; this->button.y = this->originalPosition.y / originalSize.y * updatedSize.y; this->button.w = this->originalSize.x / originalSize.x * updatedSize.x; @@ -34,15 +35,15 @@ void TextButton::updateSize(const SDL_Point originalSize, const SDL_Point update this->textPos.y = this->button.y + this->button.h / 2 - textSize.y / 2 + 4; } -void TextButton::update(InputHandler* inputHandler, SDL_FPoint pos) { +void TextButton::update(InputHandler& inputHandler, SDL_FPoint& pos) { this->lastPressed = this->isPressed; this->isHovered = SDL_PointInRectFloat(&pos, &this->button); - if (this->isHovered && inputHandler->isJustClicked(SDL_BUTTON_LEFT)) { + if (this->isHovered && inputHandler.isJustClicked(SDL_BUTTON_LEFT)) { this->isPressed = true; updateColour(this->activeColor); - } else if (this->isHovered && inputHandler->isPressed(SDL_KMOD_LSHIFT)) { - updateColour( SDL_Color{255, 0, 0}); + } else if (this->isHovered && inputHandler.isPressed(SDL_SCANCODE_LSHIFT)) { + updateColour(this->deleteColor); } else if (this->isHovered) { updateColour(this->hoverColor); } else { @@ -51,18 +52,17 @@ void TextButton::update(InputHandler* inputHandler, SDL_FPoint pos) { } } -void TextButton::updateColour(const SDL_Color color) { - if (this->color.r == color.r && this->color.g == color.g && this->color.b == color.b) { - return; - } +void TextButton::updateColour(SDL_Color& color) { + // if (this->currentColor == color) { + // return; + // } this->currentColor = color; } void TextButton::draw(SDL_Renderer* window) const { - SDL_SetRenderDrawColor(window, this->currentColor.r, this->currentColor.g, this->currentColor.b, this->currentColor.a); + SDL_SetRenderDrawColor(window, currentColor.r, currentColor.g, currentColor.b, currentColor.a); SDL_RenderFillRect(window, &this->button); - if (text == nullptr) return; if (!TTF_DrawRendererText(text, textPos.x, textPos.y)) { SDL_Log("Text rendering failed: %s\n", SDL_GetError()); } diff --git a/src/sdl/ui/TextButton.h b/src/sdl/ui/TextButton.h index a8b56db..454ca85 100644 --- a/src/sdl/ui/TextButton.h +++ b/src/sdl/ui/TextButton.h @@ -2,7 +2,6 @@ #define TEXTBUTTON_H #include "../InputHandler.h" -#include #include #include @@ -14,10 +13,10 @@ class TextButton { SDL_FPoint originalPosition; TTF_Text* text; SDL_FPoint textPos; - SDL_Color color; SDL_Color idleColor; SDL_Color hoverColor; SDL_Color activeColor; + SDL_Color deleteColor; SDL_Color currentColor; bool isPressed; bool lastPressed{}; @@ -26,9 +25,9 @@ class TextButton { public: TextButton(float x, float y, float width, float height, TTF_Text* text); - void updateSize(SDL_Point originalSize, SDL_Point updatedSize); + void updateSize(const SDL_Point & originalSize, const SDL_Point & updatedSize); - void update(InputHandler* inputHandler, SDL_FPoint pos); + void update(InputHandler& inputHandler, SDL_FPoint& pos); void draw(SDL_Renderer* window) const; @@ -36,7 +35,7 @@ class TextButton { bool isJustClicked() const; - void updateColour(SDL_Color color); + void updateColour(SDL_Color& color); };