From 314b7a84886aa33ac965e1c11cc1ef68747bb170 Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 29 Mar 2025 21:57:27 +0100 Subject: [PATCH] added distribution menu --- src/Core/Bag.h | 3 +- src/Core/DistributionMode.h | 27 ++++++ src/Core/PiecesList.cpp | 5 +- src/Core/PiecesList.h | 17 +--- src/GraphicalUI/AppMenus/GameBoardAppMenu.cpp | 2 - .../AppMenus/GameDistributionAppMenu.cpp | 85 +++++++++++++++++++ .../AppMenus/GameDistributionAppMenu.h | 21 +++++ .../AppMenus/GamePiecesAppMenu.cpp | 49 ++++++++++- src/GraphicalUI/AppMenus/GamePiecesAppMenu.h | 3 + src/GraphicalUI/Settings.cpp | 27 +++--- src/GraphicalUI/Settings.h | 4 +- src/GraphicalUI/main.cpp | 4 + 12 files changed, 207 insertions(+), 40 deletions(-) create mode 100644 src/Core/DistributionMode.h create mode 100644 src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp create mode 100644 src/GraphicalUI/AppMenus/GameDistributionAppMenu.h diff --git a/src/Core/Bag.h b/src/Core/Bag.h index 152c73c..3d2a284 100644 --- a/src/Core/Bag.h +++ b/src/Core/Bag.h @@ -2,6 +2,7 @@ #include "../Pieces/Piece.h" #include "PiecesList.h" +#include "DistributionMode.h" #include #include @@ -18,7 +19,7 @@ class Bag { std::shared_ptr piecesList; // the list of loaded pieces int highestSize; // the highest size of piece in the bag std::vector> selectedPieces; // the list of pieces that can be given to the player - PiecesDistributionMode distributionMode; // the distribution mode + DistributionMode distributionMode; // the distribution mode std::vector propotionsPerSize; // the proportion of pieces for each size std::pair next; // the next piece to give std::vector currentBags; // for each size, the list of pieces that are still to be taken out before starting a new bag diff --git a/src/Core/DistributionMode.h b/src/Core/DistributionMode.h new file mode 100644 index 0000000..8edfdb5 --- /dev/null +++ b/src/Core/DistributionMode.h @@ -0,0 +1,27 @@ +#pragma once + +#include + + +/** + * The modes of pieces distribution managed by the app + */ +enum DistributionMode { + DEFAULT, + UNIFORM, + CUSTOM +}; + + +/** + * @return A string containing the name of the given distribution mode + */ +inline std::string getPiecesDistributionName(DistributionMode distributionMode) { + static const std::string DISTRIBUTION_NAMES[] = { + "DEFAULT", + "UNIFORM", + "CUSTOM" + }; + + return DISTRIBUTION_NAMES[distributionMode]; +} diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp index 84db76a..f7a0e5a 100644 --- a/src/Core/PiecesList.cpp +++ b/src/Core/PiecesList.cpp @@ -2,6 +2,7 @@ #include "../Pieces/Piece.h" #include "../Pieces/PiecesFiles.h" +#include "DistributionMode.h" #include #include @@ -91,7 +92,7 @@ void PiecesList::unselectAll() { this->selectedPieces.clear(); } -bool PiecesList::setDistributionMode(PiecesDistributionMode distributionMode) { +bool PiecesList::setDistributionMode(DistributionMode distributionMode) { if (distributionMode == DEFAULT || distributionMode == UNIFORM || distributionMode == CUSTOM) { this->distributionMode = distributionMode; return true; @@ -113,7 +114,7 @@ std::vector> PiecesList::getSelectedPieces() const { return this->selectedPieces; } -PiecesDistributionMode PiecesList::getDistributionMode() const { +DistributionMode PiecesList::getDistributionMode() const { return this->distributionMode; } diff --git a/src/Core/PiecesList.h b/src/Core/PiecesList.h index 9b5259d..c35227c 100644 --- a/src/Core/PiecesList.h +++ b/src/Core/PiecesList.h @@ -1,21 +1,12 @@ #pragma once #include "../Pieces/Piece.h" +#include "DistributionMode.h" #include #include -/** - * The type of pieces distribution managed by the game - */ -enum PiecesDistributionMode { - DEFAULT, - UNIFORM, - CUSTOM -}; - - /** * A container for all loaded pieces to prevent loading and copying them multiple times, * also allows for the player to select a list of pieces to be used in a game @@ -28,7 +19,7 @@ class PiecesList { std::vector> holelessPieces; // the list of holeless loaded pieces by size std::vector> otherPieces; // the list of other loaded pieces by size std::vector> selectedPieces; // the list of all currently selected pieces - PiecesDistributionMode distributionMode; // the current pieces distribution mode + DistributionMode distributionMode; // the current pieces distribution mode std::vector proportionsPerSize; // the proportion of piece for each sizes std::vector customProportionsPerSize; // the proportion of piece for each sizes when the distribution mode is set to custom @@ -83,7 +74,7 @@ class PiecesList { * Changes the current pieces distribution mode * @return If the mode is supported */ - bool setDistributionMode(PiecesDistributionMode distributionMode); + bool setDistributionMode(DistributionMode distributionMode); /** * Changes the distribution of the specified size for when the distribution mode is set to custom, @@ -110,7 +101,7 @@ class PiecesList { /** * @return The current distribution mode */ - PiecesDistributionMode getDistributionMode() const; + DistributionMode getDistributionMode() const; /** * @return The proportion of pieces for each loaded size diff --git a/src/GraphicalUI/AppMenus/GameBoardAppMenu.cpp b/src/GraphicalUI/AppMenus/GameBoardAppMenu.cpp index 97e208e..bcecc15 100644 --- a/src/GraphicalUI/AppMenus/GameBoardAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GameBoardAppMenu.cpp @@ -57,8 +57,6 @@ void GameBoardAppMenu::drawFrame() const { this->placeTitle(text, {}, "BOARD SETTINGS", 5.f, {}); - sf::Vector2u windowSize = this->renderWindow->getSize(); - this->placeText(text, this->playerCursor, "< BOARD WIDTH: " + std::to_string(menu.getBoardWidth()) + " >", 5.f, 15.f, sf::Vector2u{0, 0}); this->placeText(text, this->playerCursor, "< BOARD HEIGHT: " + std::to_string(menu.getBoardHeight()) + " >", 5.f, 25.f, sf::Vector2u{0, 1}); diff --git a/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp new file mode 100644 index 0000000..7d5e2a8 --- /dev/null +++ b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp @@ -0,0 +1,85 @@ +#include "GameDistributionAppMenu.h" + +#include "AppMenu.h" +#include "../PlayerCursor.h" + +#include +#include +#include + + +GameDistributionAppMenu::GameDistributionAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + AppMenu(menuStack, settings, renderWindow), + playerCursor({1}) { + + for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { + this->playerCursor.addRow(i, 1); + } +} + +void GameDistributionAppMenu::computeFrame() { + this->updateMetaBinds(); + this->playerCursor.updatePosition(); + + PiecesList& piecesList = this->settings->getMenu().getPiecesList(); + + if (this->playerCursor.getPosition().y == 0) { + if (this->playerCursor.movedLeft()) { + piecesList.setDistributionMode(DistributionMode((int) piecesList.getDistributionMode() - 1)); + } + if (this->playerCursor.movedRight()) { + piecesList.setDistributionMode(DistributionMode((int) piecesList.getDistributionMode() + 1)); + } + } + else { + if (piecesList.getDistributionMode() != CUSTOM) { + this->playerCursor.goToPosition({0, 0}); + } + else { + if (this->playerCursor.movedLeft()) { + this->settings->decreaseDistribution(this->playerCursor.getPosition().y); + } + if (this->playerCursor.movedRight()) { + this->settings->increaseDistribution(this->playerCursor.getPosition().y); + } + } + } + + if (this->escReleased) { + this->settings->confirmDistribution(); + this->menuStack->pop(); + } +} + +void GameDistributionAppMenu::drawFrame() const { + this->renderWindow->clear(sf::Color(200, 200, 200)); + + const Menu& menu = this->settings->getMenu(); + + sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2); + text.setFillColor(sf::Color(0, 0, 0)); + text.setOutlineColor(sf::Color(255, 255, 255)); + + this->placeTitle(text, {}, "DISTRIBUTION SETTINGS", 5.f, {}); + + const DistributionMode distributionMode = this->settings->getMenu().readPiecesList().getDistributionMode(); + const std::vector& distributions = this->settings->getDistributions(); + + int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 1, 0, MAXIMUM_PIECES_SIZE - 3); + if (firstElem == 0) { + this->placeText(text, this->playerCursor, "< DISTRIBUTION MODE: " + getPiecesDistributionName(distributionMode) + " >", 5.f, 15.f, sf::Vector2u{0, 0}); + } + else { + this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem) + " PROBABILITY: " + std::to_string(distributions.at(firstElem)) + " >", 5.f, 15.f, sf::Vector2u{(unsigned int) firstElem, 0}); + } + + if (distributionMode != CUSTOM) { + text.setFillColor(sf::Color(100, 100, 100)); + } + + this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 1) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 1)) + " >", 5.f, 25.f, sf::Vector2u{0, (unsigned int) firstElem + 1}); + this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 2) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 2)) + " >", 5.f, 35.f, sf::Vector2u{0, (unsigned int) firstElem + 2}); + this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 3) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 3)) + " >", 5.f, 45.f, sf::Vector2u{0, (unsigned int) firstElem + 3}); + + this->renderWindow->display(); +} diff --git a/src/GraphicalUI/AppMenus/GameDistributionAppMenu.h b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.h new file mode 100644 index 0000000..3c16794 --- /dev/null +++ b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.h @@ -0,0 +1,21 @@ +#pragma once + +#include "AppMenu.h" +#include "../PlayerCursor.h" + +#include +#include +#include + + +class GameDistributionAppMenu : public AppMenu { + private: + PlayerCursor playerCursor; + + public: + GameDistributionAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + + void computeFrame() override; + + void drawFrame() const override; +}; diff --git a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp index 3ebe2c5..03daa4a 100644 --- a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp @@ -1,6 +1,7 @@ #include "GamePiecesAppMenu.h" #include "AppMenu.h" +#include "GameDistributionAppMenu.h" #include "../PlayerCursor.h" #include @@ -10,19 +11,61 @@ GamePiecesAppMenu::GamePiecesAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : AppMenu(menuStack, settings, renderWindow), - playerCursor({1}) { - + playerCursor({1, 1}) { + + for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { + this->playerCursor.addRow(i + 1, this->settings->getMenu().readPiecesList().getNumberOfPieces(i) + 4); + } } void GamePiecesAppMenu::computeFrame() { this->updateMetaBinds(); this->playerCursor.updatePosition(); + if (this->enterReleased) { + if (this->playerCursor.getPosition().y == 0) { + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); + } + if (this->playerCursor.getPosition().y > 1) { + //TODO + } + } if (this->escReleased) { - this->menuStack->pop(); + if (this->playerCursor.getPosition().y == 1) { + //TODO + } + else { + this->menuStack->pop(); + } } } void GamePiecesAppMenu::drawFrame() const { + this->renderWindow->clear(sf::Color(200, 200, 200)); + sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2); + text.setFillColor(sf::Color(0, 0, 0)); + text.setOutlineColor(sf::Color(255, 255, 255)); + + this->placeTitle(text, {}, "PIECES SELECT", 5.f, {}); + + if (this->playerCursor.getPosition().y == 0) { + this->placeText(text, this->playerCursor, "PIECES DISTRIBUTION", 5.f, 15.f, sf::Vector2u{0, 0}); + // draw pieces line + this->drawRow(1, 35.f); + this->drawRow(2, 45.f); + } + else { + // draw pieces line + int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 2, 1, MAXIMUM_PIECES_SIZE - 1); + this->drawRow(firstElem, 25.f); + this->drawRow(firstElem, 35.f); + this->drawRow(firstElem, 45.f); + } + + this->renderWindow->display(); +} + +void GamePiecesAppMenu::drawRow(int piecesSize, float yPos) const { + //TODO } diff --git a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.h b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.h index 7f60118..a3619a8 100644 --- a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.h +++ b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.h @@ -18,4 +18,7 @@ class GamePiecesAppMenu : public AppMenu { void computeFrame() override; void drawFrame() const override; + + private: + void drawRow(int piecesSize, float yPos) const; }; diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 4777df5..51f15e4 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -69,18 +69,13 @@ void Settings::loadSettingsFromFile() { // piece distribution settingsFile.get(byte); - this->menu.getPiecesList().setDistributionMode(PiecesDistributionMode(byte)); + this->menu.getPiecesList().setDistributionMode(DistributionMode(byte)); this->distributions.clear(); this->distributions.push_back(0); for (int i = 1; i <= 15; i++) { - if (byte == CUSTOM) { - settingsFile.get(byte); - this->distributions.push_back(i); - } - else { - this->distributions.push_back(1); - } + settingsFile.get(byte); + this->distributions.push_back(byte); } this->confirmDistribution(); @@ -143,11 +138,9 @@ void Settings::saveSettingsToFile() const { byte = this->menu.readPiecesList().getDistributionMode(); settingsFile.write(&byte, 1); - if (this->menu.readPiecesList().getDistributionMode() == CUSTOM) { - for (int i = 1; i <= 15; i++) { - byte = this->distributions.at(i); - settingsFile.write(&byte, 1); - } + for (int i = 1; i <= 15; i++) { + byte = this->distributions.at(i); + settingsFile.write(&byte, 1); } // selected pieces @@ -256,10 +249,6 @@ void Settings::confirmSelectedPieces() { } } -bool Settings::setDistributionMode (PiecesDistributionMode distributionMode) { - return this->menu.getPiecesList().setDistributionMode(distributionMode); -} - bool Settings::increaseDistribution(int size) { if (size < 1 || size > MAXIMUM_PIECES_SIZE) return false; @@ -313,3 +302,7 @@ int Settings::getStartTimerLength() const { const std::vector>& Settings::getSelectedPieces() const { return this->selectedPieces; } + +const std::vector& Settings::getDistributions() const { + return this->distributions; +} diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index 9c14082..606b87d 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -60,8 +60,6 @@ class Settings { void confirmSelectedPieces(); - bool setDistributionMode (PiecesDistributionMode distributionMode); - bool increaseDistribution(int size); bool decreaseDistribution(int size); @@ -81,4 +79,6 @@ class Settings { int getStartTimerLength() const; const std::vector>& getSelectedPieces() const; + + const std::vector& getDistributions() const; }; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 8b40ce1..46f3212 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -82,6 +82,10 @@ void resetSettingsFile() { // piece distribution byte = DEFAULT; settingsFile.write(&byte, 1); + for (int i = 1; i <= 15; i++) { + byte = 1; + settingsFile.write(&byte, 1); + } // selected pieces byte = ALL_PIECES;