From fd9fd4586a09d5144fcde384a6356611716d926a Mon Sep 17 00:00:00 2001 From: zulianc Date: Mon, 24 Mar 2025 16:20:37 +0100 Subject: [PATCH] on peut changer les settings wtf --- data/config/settings.bin | Bin 8 -> 9 bytes doc/files_format.md | 1 + src/GraphicalUI/AppMenus/AppMenu.h | 2 +- .../AppMenus/GamePlayingAppMenu.cpp | 36 ------- src/GraphicalUI/AppMenus/MainAppMenu.cpp | 3 +- src/GraphicalUI/AppMenus/PlayerCursor.cpp | 16 +++ src/GraphicalUI/AppMenus/PlayerCursor.h | 8 ++ .../AppMenus/SettingsMainAppMenu.cpp | 92 ++++++++++++++++++ .../AppMenus/SettingsMainAppMenu.h | 23 +++++ src/GraphicalUI/GraphApp.cpp | 1 + src/GraphicalUI/Keybinds.cpp | 11 +++ src/GraphicalUI/Keybinds.h | 3 + src/GraphicalUI/Settings.cpp | 33 ++++++- src/GraphicalUI/Settings.h | 7 ++ src/GraphicalUI/main.cpp | 4 + 15 files changed, 200 insertions(+), 40 deletions(-) create mode 100644 src/GraphicalUI/AppMenus/SettingsMainAppMenu.cpp create mode 100644 src/GraphicalUI/AppMenus/SettingsMainAppMenu.h diff --git a/data/config/settings.bin b/data/config/settings.bin index 4a6fcc70204e48a37fc93bb0a1925d682116c920..4e2cfc16e849473a31a5f8ab9d18391f9f1348c1 100644 GIT binary patch literal 9 QcmZQzHe%os5n*Nl00KtCU}gaT0HOdW diff --git a/doc/files_format.md b/doc/files_format.md index 0833829..437b98d 100644 --- a/doc/files_format.md +++ b/doc/files_format.md @@ -39,6 +39,7 @@ The settings file has the following format: - The number of the chosen keybinds (from 0 to 4), stored with 1 byte - The window size mode, stored with 1 byte +- The master volume, stored with 1 byte - The number of the last selected gamemode (converted from an Enum), stored with 1 byte - The last selected width of the board, stored with 1 byte - The last selected height of the board, stored with 1 byte diff --git a/src/GraphicalUI/AppMenus/AppMenu.h b/src/GraphicalUI/AppMenus/AppMenu.h index 079e261..fda18b2 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.h +++ b/src/GraphicalUI/AppMenus/AppMenu.h @@ -51,7 +51,7 @@ class AppMenu { virtual void computeFrame() = 0; - virtual void drawFrame() const = 0; + virtual void drawFrame() const = 0; }; diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index c170265..62eddea 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -136,42 +136,6 @@ void GamePlayingAppMenu::drawFrame() const { } } - /* - if (this->game.getNextPieces().size() > 0) { - for (int y = 10; y >= 0; y--) { - for (int x = 0; x <= 10; x++) { - Block block = this->game.getNextPieces().at(0).getBlockType(); - sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); - cell.setPosition(sf::Vector2f((x + 2 + this->game.getBoard().getWidth())*20, (this->game.getBoard().getBaseHeight() - y)*20)); - if (this->game.getNextPieces().at(0).getPositions().contains(Position({x, y}))) { - cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue)); - } - else { - cell.setFillColor(sf::Color(0, 0, 0)); - } - this->renderWindow->draw(cell); - } - } - } - - if (this->game.getHeldPiece() != nullptr) { - for (int y = 10; y >= 0; y--) { - for (int x = 0; x <= 10; x++) { - Block block = this->game.getHeldPiece()->getBlockType(); - sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); - cell.setPosition(sf::Vector2f((x + 12 + this->game.getBoard().getWidth())*20, (this->game.getBoard().getBaseHeight() - y)*20)); - if (this->game.getHeldPiece()->getPositions().contains(Position({x, y}))) { - cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue)); - } - else { - cell.setFillColor(sf::Color(0, 0, 0)); - } - this->renderWindow->draw(cell); - } - } - } - */ - this->renderWindow->display(); } diff --git a/src/GraphicalUI/AppMenus/MainAppMenu.cpp b/src/GraphicalUI/AppMenus/MainAppMenu.cpp index 9e6e115..f017c75 100644 --- a/src/GraphicalUI/AppMenus/MainAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/MainAppMenu.cpp @@ -2,6 +2,7 @@ #include "AppMenu.h" #include "GameSettingsAppMenu.h" +#include "SettingsMainAppMenu.h" #include "PlayerCursor.h" #include @@ -25,7 +26,7 @@ void MainAppMenu::computeFrame() { this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); } if (this->playerCursor.getPosition().y == 1) { - //TODO + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); } if (this->playerCursor.getPosition().y == 2) { //TODO diff --git a/src/GraphicalUI/AppMenus/PlayerCursor.cpp b/src/GraphicalUI/AppMenus/PlayerCursor.cpp index 40beb92..f145ab7 100644 --- a/src/GraphicalUI/AppMenus/PlayerCursor.cpp +++ b/src/GraphicalUI/AppMenus/PlayerCursor.cpp @@ -42,6 +42,22 @@ void PlayerCursor::updatePosition() { } } +bool PlayerCursor::movedLeft() const { + return this->shouldMove(this->leftDAS); +} + +bool PlayerCursor::movedRight() const { + return this->shouldMove(this->rightDAS); +} + +bool PlayerCursor::movedUp() const { + return this->shouldMove(this->upDAS); +} + +bool PlayerCursor::movedDown() const { + return this->shouldMove(this->downDAS); +} + void PlayerCursor::goToPosition(const sf::Vector2u& newPosition) { if (this->rows.size() > newPosition.y) { if (this->rows.at(newPosition.y) > newPosition.x) { diff --git a/src/GraphicalUI/AppMenus/PlayerCursor.h b/src/GraphicalUI/AppMenus/PlayerCursor.h index 0376779..4930c87 100644 --- a/src/GraphicalUI/AppMenus/PlayerCursor.h +++ b/src/GraphicalUI/AppMenus/PlayerCursor.h @@ -18,6 +18,14 @@ class PlayerCursor { void updatePosition(); + bool movedLeft() const; + + bool movedRight() const; + + bool movedUp() const; + + bool movedDown() const; + void goToPosition(const sf::Vector2u& newPosition); const sf::Vector2u& getPosition() const; diff --git a/src/GraphicalUI/AppMenus/SettingsMainAppMenu.cpp b/src/GraphicalUI/AppMenus/SettingsMainAppMenu.cpp new file mode 100644 index 0000000..0822f7c --- /dev/null +++ b/src/GraphicalUI/AppMenus/SettingsMainAppMenu.cpp @@ -0,0 +1,92 @@ +#include "SettingsMainAppMenu.h" + +#include "AppMenu.h" +#include "PlayerCursor.h" + +#include +#include +#include +#include + + +SettingsMainAppMenu::SettingsMainAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + AppMenu(menuStack, settings, renderWindow), + playerCursor(std::vector({1, 1, 1, 1})) { + +} + +void SettingsMainAppMenu::computeFrame() { + this->updateMetaBinds(); + this->playerCursor.updatePosition(); + + switch (this->playerCursor.getPosition().y) { + case 2 : { + if (this->playerCursor.movedLeft()) { + if (this->settings->shortenWindow()) { + changeVideoMode(*this->renderWindow, this->settings->getVideoMode()); + } + } + if (this->playerCursor.movedRight()) { + if (this->settings->widenWindow()) { + changeVideoMode(*this->renderWindow, this->settings->getVideoMode()); + } + } + break; + } + case 3 : { + if (this->playerCursor.movedLeft()) { + this->settings->lowerMasterVolume(); + } + if (this->playerCursor.movedRight()) { + this->settings->raiseMasterVolume(); + } + break; + } + } + + if (this->enterReleased) { + if (this->playerCursor.getPosition().y == 0) { + //TODO + } + if (this->playerCursor.getPosition().y == 1) { + //TODO + } + } + if (this->escReleased) { + this->menuStack->pop(); + } +} + +void SettingsMainAppMenu::drawFrame() const { + this->renderWindow->clear(sf::Color(200, 200, 200)); + + sf::Font font("data/fonts/pressstart/prstartk.ttf"); + sf::Text text(font, "", this->settings->getWindowSizeMultiplier() * 2); + text.setFillColor(sf::Color(0, 0, 0)); + text.setOutlineColor(sf::Color(255, 255, 255)); + + text.setString("SETTINGS"); + text.setOrigin(text.getLocalBounds().getCenter()); + text.setPosition(sf::Vector2f({(float) this->settings->getWindowSizeMultiplier() * 40, (float) this->settings->getWindowSizeMultiplier() * 5})); + this->renderWindow->draw(text); + + sf::Vector2u windowSize = this->renderWindow->getSize(); + + this->placeText(text, "CHANGE KEYBINDS", 5.f, 15.f, 0, 0); + this->placeText(text, "CHANGE CONTROLS", 5.f, 25.f, 0, 1); + this->placeText(text, "WINDOW SIZE: " + std::to_string(windowSize.x) + "x" + std::to_string(windowSize.y), 5.f, 35.f, 0, 2); + this->placeText(text, "VOLUME: " + std::to_string(this->settings->getMasterVolume()) + "%", 5.f, 45.f, 0, 3); + + this->renderWindow->display(); +} + +void SettingsMainAppMenu::placeText(sf::Text& text, const sf::String& string, float xPos, float yPos, unsigned int xCursorOutline, unsigned int yCursorOutline) const { + float sizeMultiplier = this->settings->getWindowSizeMultiplier(); + + text.setString(string); + text.setOutlineThickness((this->playerCursor.getPosition().x == xCursorOutline + && this->playerCursor.getPosition().y == yCursorOutline) ? (sizeMultiplier / 2) : 0); + text.setOrigin(sf::Vector2f({0, text.getLocalBounds().size.y / 2})); + text.setPosition(sf::Vector2f({sizeMultiplier * xPos, sizeMultiplier * yPos})); + this->renderWindow->draw(text); +} diff --git a/src/GraphicalUI/AppMenus/SettingsMainAppMenu.h b/src/GraphicalUI/AppMenus/SettingsMainAppMenu.h new file mode 100644 index 0000000..3ca9d22 --- /dev/null +++ b/src/GraphicalUI/AppMenus/SettingsMainAppMenu.h @@ -0,0 +1,23 @@ +#pragma once + +#include "AppMenu.h" +#include "PlayerCursor.h" + +#include +#include +#include + + +class SettingsMainAppMenu : public AppMenu { + private: + PlayerCursor playerCursor; + + public: + SettingsMainAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + + void computeFrame() override; + + void drawFrame() const override; + + void placeText(sf::Text& text, const sf::String& string, float xPos, float yPos, unsigned int xCursorOutline, unsigned int yCursorOutline) const; +}; diff --git a/src/GraphicalUI/GraphApp.cpp b/src/GraphicalUI/GraphApp.cpp index df35466..006d873 100644 --- a/src/GraphicalUI/GraphApp.cpp +++ b/src/GraphicalUI/GraphApp.cpp @@ -48,5 +48,6 @@ void GraphApp::run() { } } } + this->settings->saveSettingsToFile(); renderWindow->close(); } diff --git a/src/GraphicalUI/Keybinds.cpp b/src/GraphicalUI/Keybinds.cpp index a556a6e..93b470c 100644 --- a/src/GraphicalUI/Keybinds.cpp +++ b/src/GraphicalUI/Keybinds.cpp @@ -14,6 +14,7 @@ Keybinds::Keybinds(int layoutNumber) : for (Action action : ACTION_LIST_IN_ORDER) { this->keybinds.insert({action, std::set()}); } + this->modifiable = (layoutNumber == CUSTOMIZABLE_KEYBINDS); this->loadKeybindsFromFile(); } @@ -44,6 +45,8 @@ void Keybinds::loadKeybindsFromFile() { } void Keybinds::saveKeybindsToFile() const { + if (!this->modifiable) return; + std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin", std::ios::trunc | std::ios::binary); char byte; @@ -62,13 +65,21 @@ void Keybinds::saveKeybindsToFile() const { } void Keybinds::addKey(Action action, sfKey key) { + if (!this->modifiable) return; + this->keybinds.at(action).insert(key); } void Keybinds::clearKeys(Action action) { + if (!this->modifiable) return; + this->keybinds.at(action).clear(); } +bool Keybinds::isModifiable() const { + return this->modifiable; +} + const std::set Keybinds::getActions(sfKey key) const { std::set actions; diff --git a/src/GraphicalUI/Keybinds.h b/src/GraphicalUI/Keybinds.h index e219520..ba38c90 100644 --- a/src/GraphicalUI/Keybinds.h +++ b/src/GraphicalUI/Keybinds.h @@ -16,6 +16,7 @@ class Keybinds { private: std::map> keybinds; int layoutNumber; + bool modifiable; public: Keybinds(int layoutNumber); @@ -28,6 +29,8 @@ class Keybinds { void clearKeys(Action action); + bool isModifiable() const; + const std::set getActions(sfKey key) const; const std::set& getKeybinds(Action action) const; diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 58b8a76..86b0752 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -3,11 +3,12 @@ #include "../Core/Menu.h" #include "Keybinds.h" -#include #include +#include +#include static const sf::Vector2u BASE_WINDOW_SIZE = {80, 50}; -static const int WINDOW_SIZE_MULTIPLIERS[] = {4, 6, 10, 14, 20}; +static const int WINDOW_SIZE_MULTIPLIERS[] = {4, 6, 10, 14, 20, 30, 40}; static const int WINDOW_SIZE_LAST_MODE = (sizeof(WINDOW_SIZE_MULTIPLIERS) / sizeof(int)) - 1; @@ -36,6 +37,10 @@ void Settings::loadSettingsFromFile() { settingsFile.get(byte); this->windowSizeMode = byte; + // master volume + settingsFile.get(byte); + this->masterVolume = byte; + // gamemode settingsFile.get(byte); this->gamemode = Gamemode(byte); @@ -77,6 +82,10 @@ void Settings::saveSettingsToFile() const { byte = this->windowSizeMode; settingsFile.write(&byte, 1); + // master volume + byte = this->masterVolume; + settingsFile.write(&byte, 1); + // gamemode byte = this->gamemode; settingsFile.write(&byte, 1); @@ -142,6 +151,22 @@ bool Settings::shortenWindow() { return false; } +bool Settings::raiseMasterVolume() { + if (this->masterVolume < 100) { + this->masterVolume = std::min(this->masterVolume + 5, 100); + return true; + } + return false; +} + +bool Settings::lowerMasterVolume() { + if (this->masterVolume > 0) { + this->masterVolume = std::max(this->masterVolume - 5, 0); + return true; + } + return false; +} + void Settings::selectPieces(PiecesType type, int value) { this->selectedPieces.emplace_back(type, value); } @@ -190,6 +215,10 @@ int Settings::getWindowSizeMultiplier() const { return WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode]; } +int Settings::getMasterVolume() const { + return this->masterVolume; +} + const sf::VideoMode Settings::getVideoMode() const { return sf::VideoMode(BASE_WINDOW_SIZE * (unsigned int) WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode]); } diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index 2027d39..46e22b2 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -25,6 +25,7 @@ class Settings { int chosenKeybinds; Gamemode gamemode; int windowSizeMode; + int masterVolume; std::vector> selectedPieces; public: @@ -46,6 +47,10 @@ class Settings { bool shortenWindow(); + bool raiseMasterVolume(); + + bool lowerMasterVolume(); + void selectPieces(PiecesType type, int value); void unselectPieces(int index); @@ -60,6 +65,8 @@ class Settings { int getWindowSizeMultiplier() const; + int getMasterVolume() const; + const sf::VideoMode getVideoMode() const; const std::vector>& getSelectedPieces() const; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 0d7def8..486fcaa 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -70,6 +70,10 @@ void resetSettingsFile() { byte = 2; settingsFile.write(&byte, 1); + // master volume + byte = 50; + settingsFile.write(&byte, 1); + // gamemode byte = SPRINT; settingsFile.write(&byte, 1);