Skip to content

Commit

Permalink
Improve menu item positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkwouter committed Oct 11, 2024
1 parent dde5e7c commit 4d3921e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/states/HighscoreState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ HighscoreState::HighscoreState(SDL_Renderer * renderer, FontManager * fonts, Sou
{
this->text_title = fonts->getTexture(renderer, _("High Scores"), FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a});
this->text_bottom = fonts->getTexture(renderer, _("press confirm to go back"), FontType::NORMAL, {255, 255, 255, 255});
this->text_start_y = this->options->getScreenHeight() / 4;

std::string standard_mode_completed = options->getStandardModeCompleted() ? _("yes") : _("no");
texts.push_back(fonts->getTexture(renderer, _("standard mode completed: ") + standard_mode_completed, FontType::NORMAL, {255, 255, 255, 255}));
texts.push_back(fonts->getTexture(renderer, _("highest level reached in challenge mode: ") + std::to_string(options->getChallengeModeHighscore()), FontType::NORMAL, {255, 255, 255, 255}));
texts.push_back(fonts->getTexture(renderer, _("matches in relaxed mode: ") + std::to_string(options->getRelaxedModeScore()), FontType::NORMAL, {255, 255, 255, 255}));

this->text_start_y = getTextY(0);
}

HighscoreState::~HighscoreState() {
Expand Down Expand Up @@ -56,9 +55,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->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
Expand All @@ -79,7 +79,7 @@ void HighscoreState::draw(SDL_Renderer * renderer) {
}

int HighscoreState::getTextY(int number) {
return this->options->getScreenHeight()/(((int) texts.size())+this->text_offset*2)*(number+this->text_offset);
return this->text_start_y + (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()) * number));
}

State HighscoreState::getNextState() {
Expand Down
1 change: 0 additions & 1 deletion src/states/HighscoreState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class HighscoreState : public BaseState {
SDL_Texture * text_bottom;
std::vector<SDL_Texture*> texts;

int text_offset = 2;
int text_start_y;

State next_state = State::MENU;
Expand Down
13 changes: 7 additions & 6 deletions src/states/MenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -89,7 +88,8 @@ void MenuState::handleEvents(std::vector<Event> 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:
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/states/MenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class MenuState : public BaseState {
std::vector<SDL_Texture*> texts;
std::vector<SDL_Texture*> sub_texts;

int text_offset = 2;
int text_start_y;

int selection = 0;
Expand Down
11 changes: 6 additions & 5 deletions src/states/OptionsState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void OptionsState::handleEvents(std::vector<Event> 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:
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/states/OptionsState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class OptionsState : public BaseState {

State next_state = State::MENU;

int text_offset = 2;
int text_start_y;

int selection = 0;
Expand Down

0 comments on commit 4d3921e

Please sign in to comment.