fini menu keybinds

This commit is contained in:
2025-03-25 20:06:02 +01:00
parent de8a5e6e34
commit b2567844fc
10 changed files with 199 additions and 161 deletions

View File

@@ -9,6 +9,7 @@
#include <string>
#include <regex>
#include <filesystem>
#include <algorithm>
#include <SFML/Graphics.hpp>
@@ -16,6 +17,8 @@ SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menu
AppMenu(menuStack, settings, renderWindow),
playerCursor(std::vector<unsigned int>(12, 1)) {
this->selectedAnAction = false;
for (Action action : ACTION_LIST_IN_ORDER) {
std::string textureName = ACTION_NAMES[action];
textureName = std::regex_replace(textureName, std::regex(" "), "");
@@ -27,20 +30,44 @@ SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr<MenuStack> menu
void SettingsKeybindsAppMenu::computeFrame() {
this->updateMetaBinds();
this->playerCursor.updatePosition();
if (this->playerCursor.movedLeft()) {
this->settings->selectPreviousKeybinds();
if (!this->selectedAnAction) {
this->playerCursor.updatePosition();
if (this->playerCursor.movedLeft()) {
this->settings->selectPreviousKeybinds();
}
if (this->playerCursor.movedRight()) {
this->settings->selectNextKeybinds();
}
}
if (this->playerCursor.movedRight()) {
this->settings->selectNextKeybinds();
else {
bool addedKeybind = false;
for (const auto& [key, string] : KEYS_TO_STRING) {
if (sf::Keyboard::isKeyPressed(key) && (key != sfKey::Enter) && (key != sfKey::Escape)) {
this->settings->getKeybinds().addKey(this->actionSelected, key);
addedKeybind = true;
}
if (addedKeybind) {
this->selectedAnAction = false;
break;
}
}
}
if (this->enterReleased) {
//TODO
if (this->enterReleased && this->settings->getKeybinds().isModifiable()) {
this->selectedAnAction = !selectedAnAction;
this->actionSelected = ACTION_LIST_IN_ORDER[this->playerCursor.getPosition().y - 1];
}
if (this->escReleased) {
this->menuStack->pop();
if (this->selectedAnAction) {
this->settings->getKeybinds().clearKeys(this->actionSelected);
this->selectedAnAction = false;
}
else {
this->menuStack->pop();
}
}
}
@@ -51,32 +78,45 @@ void SettingsKeybindsAppMenu::drawFrame() const {
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>());
this->placeTitle(text, {}, "KEYBINDS SETTINGS", 5.f, {});
if (this->settings->getKeybindsLayout() == CUSTOMIZABLE_KEYBINDS) {
this->placeText(text, this->playerCursor, "< CUSTOM >", 5.f, 15.f, {0, 0});
this->placeText(text, this->playerCursor, "< CUSTOM >", 5.f, 15.f, sf::Vector2u{0, 0});
}
else {
this->placeText(text, this->playerCursor, "< DEFAULT " + std::to_string(this->settings->getKeybindsLayout() + 1) + " >", 5.f, 15.f, {0, 0});
this->placeText(text, this->playerCursor, "< DEFAULT " + std::to_string(this->settings->getKeybindsLayout() + 1) + " >", 5.f, 15.f, sf::Vector2u{0, 0});
}
if (this->selectedAnAction) {
text.setOutlineColor(sf::Color(255, 0, 0));
}
int i = 0;
int firstElem = (this->playerCursor.getPosition().y == 0) ? (unsigned int) 0 : (this->playerCursor.getPosition().y - 1);
int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 1, 0, 8);
for (Action action : ACTION_LIST_IN_ORDER) {
if (i >= firstElem && i < (firstElem + 3)) {
sf::String string;
bool firstKey = true;
for (sfKey key : this->settings->getKeybinds().getKeybinds(action)) {
auto keyString = getKeyStringRepresentation(key);
if (keyString.has_value()) {
string += keyString.value() + " ";
if (KEYS_TO_STRING.contains(key)) {
std::string keyString = KEYS_TO_STRING.at(key);
if (firstKey) {
string += keyString;
firstKey = false;
}
else {
string += ", " + keyString;
}
}
}
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});
this->placeText(text, this->playerCursor, setStringToUpperCase(ACTION_NAMES[action]), 15.f, ((i - firstElem) * 10) + 25.f, sf::Vector2u{0, (unsigned int) i + 1});
this->placeText(text, {}, string, 40.f, ((i - firstElem) * 10) + 25.f, {});
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.setPosition(sf::Vector2f(8.f, ((i - firstElem) * 10) + 25.f) * (float) this->settings->getWindowSizeMultiplier());
sprite.setScale(sprite.getScale() * ((float) this->settings->getWindowSizeMultiplier() / 2));
this->renderWindow->draw(sprite);
}