From f60cbfbaa7bff3577de7431ce135f087dc0c7b91 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Fri, 11 Oct 2024 22:16:45 +0200 Subject: [PATCH] Improve menu item positioning --- src/states/HighscoreState.cpp | 3 ++- src/states/MenuState.cpp | 13 +++++++------ src/states/MenuState.hpp | 1 - src/states/OptionsState.cpp | 11 ++++++----- src/states/OptionsState.hpp | 1 - 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/states/HighscoreState.cpp b/src/states/HighscoreState.cpp index c454eca..2dc6979 100644 --- a/src/states/HighscoreState.cpp +++ b/src/states/HighscoreState.cpp @@ -56,9 +56,10 @@ void HighscoreState::draw(SDL_Renderer * renderer) { this->theme.draw(renderer); // Draw title - SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->options->getShellSize() / 2, 0, 0}; + SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->options->getScreenHeight() / 8, 0, 0}; SDL_QueryTexture(text_title, NULL, NULL, &rect_title.w, &rect_title.h); rect_title.x -= rect_title.w/2; + rect_title.y -= rect_title.h/2; SDL_RenderCopy(renderer, text_title, NULL, &rect_title); // Draw options diff --git a/src/states/MenuState.cpp b/src/states/MenuState.cpp index 668c3e1..4bb8c17 100644 --- a/src/states/MenuState.cpp +++ b/src/states/MenuState.cpp @@ -10,6 +10,7 @@ MenuState::MenuState(SDL_Renderer * renderer, FontManager * fonts, SoundManager theme(renderer, options, Theme::MENU) { this->text_title = fonts->getTexture(renderer, "OceanPop", FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); + this->text_start_y = this->options->getScreenHeight() / 4; for (int i = 1; i < ((int) State::EXIT + 1); i++) { std::string option_text; @@ -47,8 +48,6 @@ MenuState::MenuState(SDL_Renderer * renderer, FontManager * fonts, SoundManager sub_texts.push_back(NULL); } } - - this->text_start_y = getOptionY(0); } MenuState::~MenuState() { @@ -89,7 +88,8 @@ void MenuState::handleEvents(std::vector events) { case Event::MOUSEMOVE: SDL_GetMouseState(&mouse.x, &mouse.y); if (mouse.y >= this->text_start_y) { - this->selection = mouse.y/(this->options->getScreenHeight()/((int) this->texts.size() + text_offset)) - text_offset; + int item_size = (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()))); + this->selection = (int)((mouse.y - this->text_start_y + item_size/4) / item_size); } break; default: @@ -107,9 +107,10 @@ void MenuState::draw(SDL_Renderer * renderer) { this->theme.draw(renderer); // Draw title - SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->options->getShellSize() / 2, 0, 0}; + SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->text_start_y / 2, 0, 0}; SDL_QueryTexture(text_title, NULL, NULL, &rect_title.w, &rect_title.h); rect_title.x -= rect_title.w/2; + rect_title.y -= rect_title.h/2; SDL_RenderCopy(renderer, text_title, NULL, &rect_title); // Draw options @@ -141,14 +142,14 @@ void MenuState::draw(SDL_Renderer * renderer) { // Render the option text SDL_RenderCopy(renderer, texts[i], NULL, &rect); - if (sub_texts[i] != NULL && sub_rect.y + sub_rect.h < getOptionY(i+1) && selection == i) { + if (sub_texts[i] != NULL && selection == i) { SDL_RenderCopy(renderer, sub_texts[i], NULL, &sub_rect); } } } int MenuState::getOptionY(int number) { - return this->options->getScreenHeight()/(((int) texts.size())+this->text_offset)*(number+this->text_offset); + return this->text_start_y + (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()) * number)); } State MenuState::getNextState() { diff --git a/src/states/MenuState.hpp b/src/states/MenuState.hpp index cf332c5..5f80f00 100644 --- a/src/states/MenuState.hpp +++ b/src/states/MenuState.hpp @@ -24,7 +24,6 @@ class MenuState : public BaseState { std::vector texts; std::vector sub_texts; - int text_offset = 2; int text_start_y; int selection = 0; diff --git a/src/states/OptionsState.cpp b/src/states/OptionsState.cpp index 553efcd..fd7d2e7 100644 --- a/src/states/OptionsState.cpp +++ b/src/states/OptionsState.cpp @@ -100,7 +100,8 @@ void OptionsState::handleEvents(std::vector events) { case Event::MOUSEMOVE: SDL_GetMouseState(&mouse.x, &mouse.y); if (mouse.y >= this->text_start_y) { - this->selection = mouse.y/(this->options->getScreenHeight()/((int) this->texts.size() + text_offset)) - text_offset; + int item_size = (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()))); + this->selection = (int)((mouse.y - this->text_start_y + item_size/4) / item_size); } break; default: @@ -118,9 +119,10 @@ void OptionsState::draw(SDL_Renderer * renderer) { this->theme.draw(renderer); // Draw title - SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->options->getShellSize() / 2, 0, 0}; + SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->text_start_y / 2, 0, 0}; SDL_QueryTexture(text_title, NULL, NULL, &rect_title.w, &rect_title.h); rect_title.x -= rect_title.w/2; + rect_title.y -= rect_title.h/2; SDL_RenderCopy(renderer, text_title, NULL, &rect_title); // Draw options @@ -270,6 +272,7 @@ void OptionsState::changeResolution(int amount) { void OptionsState::loadTexts() { this->text_title = fonts->getTexture(this->renderer, _("Options"), FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); + this->text_start_y = this->options->getScreenHeight() / 4; for (int i = 0; i < (((int) Option::GO_BACK) + 1); i++) { std::string current_text; @@ -300,8 +303,6 @@ void OptionsState::loadTexts() { } texts.push_back(fonts->getTexture(renderer, current_text, FontType::NORMAL, {255, 255, 255, 255})); } - - this->text_start_y = getTextY(0); } void OptionsState::updateTexts() { @@ -314,7 +315,7 @@ void OptionsState::updateTexts() { } int OptionsState::getTextY(int number) { - return this->options->getScreenHeight()/(((int) texts.size())+this->text_offset)*(number+this->text_offset); + return this->text_start_y + (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()) * number)); } State OptionsState::getNextState() { diff --git a/src/states/OptionsState.hpp b/src/states/OptionsState.hpp index 4dc2c44..c72e260 100644 --- a/src/states/OptionsState.hpp +++ b/src/states/OptionsState.hpp @@ -38,7 +38,6 @@ class OptionsState : public BaseState { State next_state = State::MENU; - int text_offset = 2; int text_start_y; int selection = 0;