From 8a7d87a312b3dc42877577e99533c96d48714368 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 13:10:47 -0500 Subject: Added sliders to the settings menu #7 --- src/input_system.cpp | 8 ++++++++ src/menu.h | 26 ++++++++++++++++++++++--- src/menu_system.cpp | 28 ++++++++++++++++++++++++++ src/menu_system.h | 4 ++++ src/renderer.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 117 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/input_system.cpp b/src/input_system.cpp index 25af7ed..3e4b679 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp @@ -119,12 +119,20 @@ void InputSystem::tick(double dt) { } } } else if (e.key.keysym.sym == SDLK_LEFT) { + if (game_.getSystem().isMenuOpen()) { + game_.getSystem().pressedLeft(); + } + if (game_.isGameplayPaused()) continue; if (game_.getSystem().isChoiceActive()) { game_.getSystem().selectFirstChoice(); } } else if (e.key.keysym.sym == SDLK_RIGHT) { + if (game_.getSystem().isMenuOpen()) { + game_.getSystem().pressedRight(); + } + if (game_.isGameplayPaused()) continue; if (game_.getSystem().isChoiceActive()) { diff --git a/src/menu.h b/src/menu.h index 67c75c0..9ef94be 100644 --- a/src/menu.h +++ b/src/menu.h @@ -9,14 +9,17 @@ class Game; enum class MenuType { - Command + Command, + Slider }; struct MenuItem { MenuType type = MenuType::Command; std::string text; std::function activationFunction; - bool playSfx = false; + bool playSfx = true; + int value = 0; + int maxValue = 0; }; class MenuBuilder { @@ -25,7 +28,6 @@ public: MenuBuilder& Command(std::string text) { result_.type = MenuType::Command; result_.text = std::move(text); - result_.playSfx = true; return *this; } @@ -39,6 +41,22 @@ public: return *this; } + MenuBuilder& Slider(std::string text) { + result_.type = MenuType::Slider; + result_.text = std::move(text); + return *this; + } + + MenuBuilder& InitialValue(int value) { + result_.value = value; + return *this; + } + + MenuBuilder& MaxValue(int mv) { + result_.maxValue = mv; + return *this; + } + MenuItem Build() const { return result_; } @@ -55,6 +73,8 @@ public: const std::vector& getItems() const { return items_; } + std::vector& getItems() { return items_; } + int getCursorPosition() const { return cursor_; } void moveCursorUp(); diff --git a/src/menu_system.cpp b/src/menu_system.cpp index 7ac8af5..e1aca8d 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp @@ -54,6 +54,12 @@ void MenuSystem::openPauseMenu() { MenuBuilder().Command("Settings") .ActivationFunction([this] (Game&) { openSubmenu(Menu({ + MenuBuilder().Slider("Music Volume: ") + .InitialValue(10) + .MaxValue(10), + MenuBuilder().Slider("Sound Volume: ") + .InitialValue(10) + .MaxValue(10), MenuBuilder().Command("Back") .ActivationFunction([this] (Game& game) { closePauseMenu(); @@ -122,6 +128,28 @@ void MenuSystem::pressedDown() { game_.getMixer().playSound("../res/sfx/vertical_menu.wav"); } +void MenuSystem::pressedLeft() { + Menu& curMenu = menus_.back(); + MenuItem& menuItem = curMenu.getItems()[curMenu.getCursorPosition()]; + + if (menuItem.type == MenuType::Slider && menuItem.value > 0) { + menuItem.value--; + + game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); + } +} + +void MenuSystem::pressedRight() { + Menu& curMenu = menus_.back(); + MenuItem& menuItem = curMenu.getItems()[curMenu.getCursorPosition()]; + + if (menuItem.type == MenuType::Slider && menuItem.value < menuItem.maxValue) { + menuItem.value++; + + game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); + } +} + void MenuSystem::activateOption() { Menu& curMenu = menus_.back(); const MenuItem& menuItem = curMenu.getItems()[curMenu.getCursorPosition()]; diff --git a/src/menu_system.h b/src/menu_system.h index c921d0b..71e2b39 100644 --- a/src/menu_system.h +++ b/src/menu_system.h @@ -29,6 +29,10 @@ public: void pressedDown(); + void pressedLeft(); + + void pressedRight(); + void activateOption(); // Info diff --git a/src/renderer.cpp b/src/renderer.cpp index f7644ca..b28e3cb 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,4 +1,5 @@ #include "renderer.h" +#include #include #include "consts.h" #include "game.h" @@ -570,11 +571,33 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { SDL_RenderFillRect(ren_.get(), nullptr); const int lineHeight = 16; + const int sliderBarWidth = 9; int totalHeight = menu.getItems().size() * lineHeight; std::vector positions; + std::deque sliderText; + int maxSliderLineWidth = 0; + int sliderAlignX = 0; + // First, find all of the sliders so we can figure out how to align them. + for (const MenuItem& menuItem : menu.getItems()) { + if (menuItem.type == MenuType::Slider) { + MessageCache output; + renderMessageLine(output, menuItem.text, game); + + int width = output.charIndexToWidth.back() + sliderBarWidth * menuItem.maxValue; + if (width > maxSliderLineWidth) { + maxSliderLineWidth = width; + sliderAlignX = (CANVAS_WIDTH - maxSliderLineWidth) / 2 + output.charIndexToWidth.back(); + } + + sliderText.push_back(std::move(output)); + } + } + + // Now, render all of the items. int index = 0; for (const MenuItem& menuItem : menu.getItems()) { + int lineY = (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index; switch (menuItem.type) { case MenuType::Command: { MessageCache output; @@ -582,7 +605,24 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { SDL_Rect dest { (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, - (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, + lineY, + MESSAGE_TEXT_WIDTH, + game.getFont().getCharacterHeight() + }; + + SDL_SetRenderTarget(ren_.get(), menuTex_.get()); + SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); + + positions.emplace_back(dest.x, dest.y); + + break; + } + case MenuType::Slider: { + MessageCache& output = sliderText.front(); + + SDL_Rect dest { + sliderAlignX - output.charIndexToWidth.back(), + lineY, MESSAGE_TEXT_WIDTH, game.getFont().getCharacterHeight() }; @@ -592,6 +632,19 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { positions.emplace_back(dest.x, dest.y); + for (int j = 0; j < menuItem.maxValue; j++) { + int boxTexId = menuItem.value > j ? loadImageFromFile("../res/slider_on.png") : loadImageFromFile("../res/slider_off.png"); + const SDL_Rect boxDest { + sliderAlignX + j * sliderBarWidth, + lineY + 2, + 8, + 8 }; + SDL_SetRenderTarget(ren_.get(), menuTex_.get()); + SDL_RenderCopy(ren_.get(), textures_.at(boxTexId).get(), nullptr, &boxDest); + } + + sliderText.pop_front(); + break; } } -- cgit 1.4.1