Skip to content

Commit

Permalink
Some simplifications in Menu class.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 13, 2024
1 parent ec66a3d commit 7ff3195
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
30 changes: 19 additions & 11 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ std::pair<bool, Level> Menu::playGame()
case UserChoice::LEVEL_4:
Screen::hideMouse();
return {true, Level::LEVEL_4};

default:
break;
}
}
}
Expand All @@ -93,7 +96,7 @@ Menu::UserChoice Menu::getUserChoice()
return UserChoice::EXIT;

if (action == InputAction::ACCEPT)
return items_[currentItem].second;
return items_[static_cast<std::size_t>(currentItem)].second;

if (action == InputAction::BACK)
return items_[items_.size() - 1].second;
Expand All @@ -106,21 +109,26 @@ Menu::UserChoice Menu::getUserChoice()
}
}

void Menu::drawMenuItems(int currentItem)
void Menu::drawMenuItem(int item, ResourceType resource)
{
const auto itemHeight{static_cast<float>(getItemHeight())};
const auto [itemX, itemY]{getItemPosition(item)};
screen_.drawScaledBitmap(resource, itemX, itemY, getItemWidth(),
getItemHeight());
const auto itemMiddleY{itemY + static_cast<int>(itemHeight / 2.F)};
screen_.drawText(screen_.getCenterX(), itemMiddleY,
items_[static_cast<std::size_t>(item)].first);
}

void Menu::drawMenuItems(int currentItem)
{
for (int item = 0; item < static_cast<int>(items_.size()); ++item)
{
ResourceType itemResource{ResourceType::MENU_ITEM};
ResourceType resource{ResourceType::MENU_ITEM};
if (item == currentItem)
itemResource = ResourceType::MENU_ITME_SELECTED;

const auto [itemX, itemY]{getItemPosition(item)};
screen_.drawScaledBitmap(itemResource, itemX, itemY, getItemWidth(),
getItemHeight());
const auto itemMiddleY{itemY + static_cast<int>(itemHeight / 2.F)};
screen_.drawText(screen_.getCenterX(), itemMiddleY,
items_[static_cast<std::size_t>(item)].first);
resource = ResourceType::MENU_ITEM_SELECTED;

drawMenuItem(item, resource);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <string>
#include <vector>

#include "ResourceType.h"

class Screen;
enum class InputAction : char;
enum class Level : char;
Expand Down Expand Up @@ -30,6 +32,8 @@ class Menu final
EXIT
};

void drawMenuItem(int item, ResourceType resource);

void drawMenuItems(int currentItem);

int getCurrentItem(std::pair<int, int> mousePosition, InputAction action,
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceType.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enum class ResourceType : char
{
BACKGROUND = 0,
MENU_ITEM,
MENU_ITME_SELECTED,
MENU_ITEM_SELECTED,
PLAIN,
BRICK,
WATER,
Expand Down
2 changes: 1 addition & 1 deletion src/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Resources
std::unordered_map<ResourceType, std::string> resourcePaths_{
{ResourceType::BACKGROUND, "image/menu/background.bmp"},
{ResourceType::MENU_ITEM, "image/menu/item.tga"},
{ResourceType::MENU_ITME_SELECTED, "image/menu/item_select.tga"},
{ResourceType::MENU_ITEM_SELECTED, "image/menu/item_select.tga"},
{ResourceType::PLAIN, "image/board/plain.tga"},
{ResourceType::BRICK, "image/board/brick.tga"},
{ResourceType::WATER, "image/board/water.tga"},
Expand Down

0 comments on commit 7ff3195

Please sign in to comment.