keybinds menu

This commit is contained in:
2025-03-25 17:17:36 +01:00
parent c168cd68d7
commit de8a5e6e34
20 changed files with 252 additions and 7 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 603 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 627 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 622 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 623 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

View File

@@ -42,9 +42,9 @@ static const std::string ACTION_NAMES[] = { // name representation for each a
"Hard drop", "Hard drop",
"Move left", "Move left",
"Move right", "Move right",
"Rotate 0°", "Rotate 0",
"Rotate CW", "Rotate CW",
"Rotate 180°", "Rotate 180",
"Rotate CCW" "Rotate CCW"
}; };

View File

@@ -0,0 +1,87 @@
#include "SettingsKeybindsAppMenu.h"
#include "AppMenu.h"
#include "../PlayerCursor.h"
#include <stack>
#include <memory>
#include <vector>
#include <string>
#include <regex>
#include <filesystem>
#include <SFML/Graphics.hpp>
SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
AppMenu(menuStack, settings, renderWindow),
playerCursor(std::vector<unsigned int>(12, 1)) {
for (Action action : ACTION_LIST_IN_ORDER) {
std::string textureName = ACTION_NAMES[action];
textureName = std::regex_replace(textureName, std::regex(" "), "");
std::filesystem::path texturePath("data/images/keybinds/" + textureName + ".png");
this->iconTextures[action] = sf::Texture(texturePath, false, {{0, 0}, {16, 16}});
}
}
void SettingsKeybindsAppMenu::computeFrame() {
this->updateMetaBinds();
this->playerCursor.updatePosition();
if (this->playerCursor.movedLeft()) {
this->settings->selectPreviousKeybinds();
}
if (this->playerCursor.movedRight()) {
this->settings->selectNextKeybinds();
}
if (this->enterReleased) {
//TODO
}
if (this->escReleased) {
this->menuStack->pop();
}
}
void SettingsKeybindsAppMenu::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, std::optional<PlayerCursor>(), "KEYBINDS SETTINGS", 5.f, std::optional<sf::Vector2u>());
if (this->settings->getKeybindsLayout() == CUSTOMIZABLE_KEYBINDS) {
this->placeText(text, this->playerCursor, "< CUSTOM >", 5.f, 15.f, {0, 0});
}
else {
this->placeText(text, this->playerCursor, "< DEFAULT " + std::to_string(this->settings->getKeybindsLayout() + 1) + " >", 5.f, 15.f, {0, 0});
}
int i = 0;
int firstElem = (this->playerCursor.getPosition().y == 0) ? (unsigned int) 0 : (this->playerCursor.getPosition().y - 1);
for (Action action : ACTION_LIST_IN_ORDER) {
if (i >= firstElem && i < (firstElem + 3)) {
sf::String string;
for (sfKey key : this->settings->getKeybinds().getKeybinds(action)) {
auto keyString = getKeyStringRepresentation(key);
if (keyString.has_value()) {
string += keyString.value() + " ";
}
}
this->placeText(text, this->playerCursor, setStringToUpperCase(ACTION_NAMES[action]), 20.f, ((i - firstElem) * 10) + 25.f, {0, (unsigned int) i + 1});
this->placeText(text, this->playerCursor, string, 50.f, ((i - firstElem) * 10) + 25.f, {0, (unsigned int) i + 1});
sf::Sprite sprite(this->iconTextures[action]);
sprite.setOrigin(sprite.getLocalBounds().getCenter());
sprite.setPosition(sf::Vector2f(10.f, ((i - firstElem) * 10) + 25.f) * (float) this->settings->getWindowSizeMultiplier());
sprite.setScale(sprite.getScale() * ((float) this->settings->getWindowSizeMultiplier() / 2));
this->renderWindow->draw(sprite);
}
i++;
}
this->renderWindow->display();
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "AppMenu.h"
#include "../PlayerCursor.h"
#include <stack>
#include <memory>
#include <SFML/Graphics.hpp>
class SettingsKeybindsAppMenu : public AppMenu {
private:
PlayerCursor playerCursor;
sf::Texture iconTextures[11];
public:
SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow);
void computeFrame() override;
void drawFrame() const override;
};

View File

@@ -1,6 +1,7 @@
#include "SettingsMainAppMenu.h" #include "SettingsMainAppMenu.h"
#include "AppMenu.h" #include "AppMenu.h"
#include "SettingsKeybindsAppMenu.h"
#include "SettingsControlsAppMenu.h" #include "SettingsControlsAppMenu.h"
#include "../PlayerCursor.h" #include "../PlayerCursor.h"
@@ -47,7 +48,7 @@ void SettingsMainAppMenu::computeFrame() {
if (this->enterReleased) { if (this->enterReleased) {
if (this->playerCursor.getPosition().y == 0) { if (this->playerCursor.getPosition().y == 0) {
//TODO this->menuStack->push(std::make_shared<SettingsKeybindsAppMenu>(this->menuStack, this->settings, this->renderWindow));
} }
if (this->playerCursor.getPosition().y == 1) { if (this->playerCursor.getPosition().y == 1) {
this->menuStack->push(std::make_shared<SettingsControlsAppMenu>(this->menuStack, this->settings, this->renderWindow)); this->menuStack->push(std::make_shared<SettingsControlsAppMenu>(this->menuStack, this->settings, this->renderWindow));
@@ -69,7 +70,7 @@ void SettingsMainAppMenu::drawFrame() const {
sf::Vector2u windowSize = this->renderWindow->getSize(); sf::Vector2u windowSize = this->renderWindow->getSize();
this->placeText(text, this->playerCursor, "CHANGE KEYBINDS (TODO)", 5.f, 15.f, {0, 0}); this->placeText(text, this->playerCursor, "CHANGE KEYBINDS", 5.f, 15.f, {0, 0});
this->placeText(text, this->playerCursor, "CHANGE CONTROLS", 5.f, 25.f, {0, 1}); this->placeText(text, this->playerCursor, "CHANGE CONTROLS", 5.f, 25.f, {0, 1});
this->placeText(text, this->playerCursor, "< WINDOW SIZE: " + std::to_string(windowSize.x) + "x" + std::to_string(windowSize.y) + " >", 5.f, 35.f, {0, 2}); this->placeText(text, this->playerCursor, "< WINDOW SIZE: " + std::to_string(windowSize.x) + "x" + std::to_string(windowSize.y) + " >", 5.f, 35.f, {0, 2});
this->placeText(text, this->playerCursor, "< VOLUME: " + std::to_string(this->settings->getMasterVolume()) + "% >", 5.f, 45.f, {0, 3}); this->placeText(text, this->playerCursor, "< VOLUME: " + std::to_string(this->settings->getMasterVolume()) + "% >", 5.f, 45.f, {0, 3});

View File

@@ -66,6 +66,7 @@ void Keybinds::saveKeybindsToFile() const {
void Keybinds::addKey(Action action, sfKey key) { void Keybinds::addKey(Action action, sfKey key) {
if (!this->modifiable) return; if (!this->modifiable) return;
if (!getKeyStringRepresentation(key).has_value()) return;
this->keybinds.at(action).insert(key); this->keybinds.at(action).insert(key);
} }

View File

@@ -4,6 +4,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <optional>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
using sfKey = sf::Keyboard::Key; using sfKey = sf::Keyboard::Key;
@@ -35,3 +36,132 @@ class Keybinds {
const std::set<sfKey>& getKeybinds(Action action) const; const std::set<sfKey>& getKeybinds(Action action) const;
}; };
inline std::string setStringToUpperCase(std::string&& str) {
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
inline std::string setStringToUpperCase(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::toupper);
return result;
}
inline const std::optional<sf::String> getKeyStringRepresentation(sfKey key) {
#define INSERT_MAPPING(identifier) {sfKey::identifier, setStringToUpperCase(#identifier)}
static const std::map<sfKey, sf::String> keysRepresentation {
INSERT_MAPPING(A),
INSERT_MAPPING(B),
INSERT_MAPPING(C),
INSERT_MAPPING(D),
INSERT_MAPPING(E),
INSERT_MAPPING(F),
INSERT_MAPPING(G),
INSERT_MAPPING(H),
INSERT_MAPPING(I),
INSERT_MAPPING(J),
INSERT_MAPPING(K),
INSERT_MAPPING(L),
INSERT_MAPPING(M),
INSERT_MAPPING(N),
INSERT_MAPPING(O),
INSERT_MAPPING(P),
INSERT_MAPPING(Q),
INSERT_MAPPING(R),
INSERT_MAPPING(S),
INSERT_MAPPING(T),
INSERT_MAPPING(U),
INSERT_MAPPING(V),
INSERT_MAPPING(W),
INSERT_MAPPING(X),
INSERT_MAPPING(Y),
INSERT_MAPPING(Z),
INSERT_MAPPING(Num0),
INSERT_MAPPING(Num1),
INSERT_MAPPING(Num2),
INSERT_MAPPING(Num3),
INSERT_MAPPING(Num4),
INSERT_MAPPING(Num5),
INSERT_MAPPING(Num6),
INSERT_MAPPING(Num7),
INSERT_MAPPING(Num8),
INSERT_MAPPING(Num9),
INSERT_MAPPING(Escape),
INSERT_MAPPING(LControl),
INSERT_MAPPING(LShift),
INSERT_MAPPING(LAlt),
INSERT_MAPPING(LSystem),
INSERT_MAPPING(RControl),
INSERT_MAPPING(RShift),
INSERT_MAPPING(RAlt),
INSERT_MAPPING(RSystem),
INSERT_MAPPING(Menu),
INSERT_MAPPING(LBracket),
INSERT_MAPPING(RBracket),
INSERT_MAPPING(Semicolon),
INSERT_MAPPING(Comma),
INSERT_MAPPING(Period),
INSERT_MAPPING(Apostrophe),
INSERT_MAPPING(Slash),
INSERT_MAPPING(Backslash),
INSERT_MAPPING(Grave),
INSERT_MAPPING(Equal),
INSERT_MAPPING(Hyphen),
INSERT_MAPPING(Space),
INSERT_MAPPING(Enter),
INSERT_MAPPING(Backspace),
INSERT_MAPPING(Tab),
INSERT_MAPPING(PageUp),
INSERT_MAPPING(PageDown),
INSERT_MAPPING(End),
INSERT_MAPPING(Home),
INSERT_MAPPING(Insert),
INSERT_MAPPING(Delete),
INSERT_MAPPING(Add),
INSERT_MAPPING(Subtract),
INSERT_MAPPING(Multiply),
INSERT_MAPPING(Divide),
INSERT_MAPPING(Left),
INSERT_MAPPING(Right),
INSERT_MAPPING(Up),
INSERT_MAPPING(Down),
INSERT_MAPPING(Numpad0),
INSERT_MAPPING(Numpad1),
INSERT_MAPPING(Numpad2),
INSERT_MAPPING(Numpad3),
INSERT_MAPPING(Numpad4),
INSERT_MAPPING(Numpad5),
INSERT_MAPPING(Numpad6),
INSERT_MAPPING(Numpad7),
INSERT_MAPPING(Numpad8),
INSERT_MAPPING(Numpad9),
INSERT_MAPPING(F1),
INSERT_MAPPING(F2),
INSERT_MAPPING(F3),
INSERT_MAPPING(F4),
INSERT_MAPPING(F5),
INSERT_MAPPING(F6),
INSERT_MAPPING(F7),
INSERT_MAPPING(F8),
INSERT_MAPPING(F9),
INSERT_MAPPING(F10),
INSERT_MAPPING(F11),
INSERT_MAPPING(F12),
INSERT_MAPPING(F13),
INSERT_MAPPING(F14),
INSERT_MAPPING(F15),
INSERT_MAPPING(Pause)
};
#undef INSERT_MAPPING
if (keysRepresentation.contains(key)) {
return keysRepresentation.at(key);
}
else {
return {};
}
}

View File

@@ -142,7 +142,7 @@ void Settings::saveSettingsToFile() const {
} }
bool Settings::selectNextKeybinds() { bool Settings::selectNextKeybinds() {
if (this->chosenKeybinds < NUMBER_OF_KEYBINDS) { if (this->chosenKeybinds < (NUMBER_OF_KEYBINDS - 1)) {
this->chosenKeybinds++; this->chosenKeybinds++;
return true; return true;
} }
@@ -246,6 +246,10 @@ Keybinds& Settings::getKeybinds() {
return this->keybinds.at(this->chosenKeybinds); return this->keybinds.at(this->chosenKeybinds);
} }
int Settings::getKeybindsLayout() const {
return this->chosenKeybinds;
}
Gamemode Settings::getGamemode() const { Gamemode Settings::getGamemode() const {
return this->gamemode; return this->gamemode;
} }

View File

@@ -63,6 +63,8 @@ class Settings {
Keybinds& getKeybinds(); Keybinds& getKeybinds();
int getKeybindsLayout() const;
Gamemode getGamemode() const; Gamemode getGamemode() const;
int getWindowSizeMultiplier() const; int getWindowSizeMultiplier() const;

View File

@@ -6,8 +6,6 @@ set_languages("c++20")
set_rundir(".") set_rundir(".")
set_optimize("fastest")
target("core") target("core")
set_kind("$(kind)") set_kind("$(kind)")
add_files("src/Pieces/*.cpp") add_files("src/Pieces/*.cpp")