diff --git a/client/battle/QuickSpellPanel.cpp b/client/battle/QuickSpellPanel.cpp index 1f5382f99..2feec1c47 100644 --- a/client/battle/QuickSpellPanel.cpp +++ b/client/battle/QuickSpellPanel.cpp @@ -91,8 +91,6 @@ void QuickSpellPanel::create() { OBJECT_CONSTRUCTION; - const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig"); - labels.clear(); buttons.clear(); buttonsDisabled.clear(); @@ -132,7 +130,7 @@ void QuickSpellPanel::create() buttonsDisabled.push_back(std::make_shared(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172))); } if(ENGINE->input().getCurrentInputMode() == InputMode::KEYBOARD_AND_MOUSE) - labels.push_back(std::make_shared(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, config["keyboard"]["battleSpellShortcut" + std::to_string(i)].String())); + labels.push_back(std::make_shared(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, shortcutsConfig["keyboard"]["battleSpellShortcut" + std::to_string(i)].String())); buttons.push_back(button); } diff --git a/client/gui/ShortcutHandler.cpp b/client/gui/ShortcutHandler.cpp index ee9561c95..133ee8fce 100644 --- a/client/gui/ShortcutHandler.cpp +++ b/client/gui/ShortcutHandler.cpp @@ -13,21 +13,20 @@ #include "ShortcutHandler.h" #include "Shortcut.h" +#include "../../lib/CConfigHandler.h" #include "../../lib/json/JsonUtils.h" ShortcutHandler::ShortcutHandler() { - const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig"); - - mappedKeyboardShortcuts = loadShortcuts(config["keyboard"]); - mappedJoystickShortcuts = loadShortcuts(config["joystickButtons"]); - mappedJoystickAxes = loadShortcuts(config["joystickAxes"]); + mappedKeyboardShortcuts = loadShortcuts(shortcutsConfig["keyboard"]); + mappedJoystickShortcuts = loadShortcuts(shortcutsConfig["joystickButtons"]); + mappedJoystickAxes = loadShortcuts(shortcutsConfig["joystickAxes"]); #ifndef ENABLE_GOLDMASTER std::vector assignedShortcuts; std::vector missingShortcuts; - for (auto const & entry : config["keyboard"].Struct()) + for (auto const & entry : shortcutsConfig["keyboard"].Struct()) { EShortcut shortcutID = findShortcut(entry.first); assert(!vstd::contains(assignedShortcuts, shortcutID)); diff --git a/client/windows/settings/ShortcutsWindow.cpp b/client/windows/settings/ShortcutsWindow.cpp index 4f3caf3c3..dfdc2a14b 100644 --- a/client/windows/settings/ShortcutsWindow.cpp +++ b/client/windows/settings/ShortcutsWindow.cpp @@ -23,6 +23,7 @@ #include "../../widgets/Slider.h" #include "../../windows/InfoWindows.h" +#include "../../../lib/CConfigHandler.h" #include "../../../lib/texts/MetaString.h" #include "../../../lib/json/JsonNode.h" #include "../../../lib/json/JsonUtils.h" @@ -44,10 +45,8 @@ ShortcutsWindow::ShortcutsWindow() ); backgroundRect = std::make_shared(Rect(8, 48, pos.w - 16, 348), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1); - shortcuts = JsonUtils::assembleFromFiles("config/shortcutsConfig"); - int count = 0; - for(auto & group : shortcuts.Struct()) + for(auto & group : shortcutsConfig.toJsonNode().Struct()) { count++; count += group.second.Struct().size(); @@ -75,7 +74,7 @@ void ShortcutsWindow::fillList(int start) listElements.clear(); int i = 0; [&]{ - for(auto group = shortcuts.Struct().rbegin(); group != shortcuts.Struct().rend(); ++group) + for(auto group = shortcutsConfig.toJsonNode().Struct().rbegin(); group != shortcutsConfig.toJsonNode().Struct().rend(); ++group) { if(i >= start) listElements.push_back(std::make_shared(group->first, listElements.size())); @@ -85,15 +84,15 @@ void ShortcutsWindow::fillList(int start) for(auto & elem : group->second.Struct()) { if(i >= start) - listElements.push_back(std::make_shared(elem.first, elem.second, listElements.size(), [this](const std::string & id, const std::string & keyName){ + listElements.push_back(std::make_shared(elem.first, elem.second, listElements.size(), [this, group](const std::string & id, const std::string & keyName){ auto str = MetaString::createFromTextID("vcmi.shortcuts.inputSet"); str.replaceTextID("vcmi.shortcuts.shortcut." + id); str.replaceRawString(keyName); - GAME->interface()->showYesNoDialog(str.toString(), [this, id, keyName](){ - setKeyBinding(id, keyName, true); - }, [this, id, keyName](){ - setKeyBinding(id, keyName, false); + GAME->interface()->showYesNoDialog(str.toString(), [this, group, id, keyName](){ + setKeyBinding(id, group->first, keyName, true); + }, [this, group, id, keyName](){ + setKeyBinding(id, group->first, keyName, false); }); })); i++; @@ -104,17 +103,32 @@ void ShortcutsWindow::fillList(int start) }(); } -void ShortcutsWindow::setKeyBinding(const std::string & id, const std::string & keyName, bool append) +void ShortcutsWindow::setKeyBinding(const std::string & id, const std::string & group, const std::string & keyName, bool append) { - // TODO - std::cout << id << " " << keyName << " " << append << "\n"; + auto existing = shortcutsConfig[group][id]; + Settings existingWrite = shortcutsConfig.write[group][id]; + if((existing.isVector() || (existing.isString() && !existing.String().empty())) && append) + { + JsonVector tmp; + if(existing.isVector()) + tmp = existing.Vector(); + if(existing.isString()) + tmp.push_back(existing); + tmp.push_back(JsonNode(keyName)); + existingWrite->Vector() = tmp; + } + else + existingWrite->String() = keyName; fillList(slider->getValue()); } void ShortcutsWindow::resetKeyBinding() { - // TODO + // FIXME: Not working yet + Settings write = shortcutsConfig.write; + write->clear(); + write->Struct() = JsonUtils::assembleFromFiles("config/shortcutsConfig.json").Struct(); fillList(slider->getValue()); } diff --git a/client/windows/settings/ShortcutsWindow.h b/client/windows/settings/ShortcutsWindow.h index 1e5533c6d..211d9bbca 100644 --- a/client/windows/settings/ShortcutsWindow.h +++ b/client/windows/settings/ShortcutsWindow.h @@ -49,10 +49,8 @@ private: std::vector> listElements; std::shared_ptr buttonReset; - JsonNode shortcuts; - void fillList(int start); - void setKeyBinding(const std::string & id, const std::string & keyName, bool append); + void setKeyBinding(const std::string & id, const std::string & group, const std::string & keyName, bool append); void resetKeyBinding(); public: diff --git a/config/shortcutsConfig.json b/config/shortcutsConfig.json index c5b532cd7..14ebad395 100644 --- a/config/shortcutsConfig.json +++ b/config/shortcutsConfig.json @@ -1,9 +1,5 @@ // This file defines all shortcuts used by VCMI -// For modders: create file with same name (Content/config/shortcutsConfig.json) to modify this set in your mod -// For players (Windows): create file Documents/My Games/vcmi/config/shortcutsConfig.json to modify this set -// For players (Linux): create file ~/.config/vcmi/shortcutsConfig.json (or ~/.var/app/eu.vcmi.VCMI/config for Flatpak) to modify this set // -// When creating your own config, you can remove all hotkeys that you have not changed and game will read them from this file // It is possible to add modifiers to keys: Ctrl, Shift, or Alt. For example, "Ctrl+Tab" hotkey will only activate if Ctrl is pressed { "keyboard" : { diff --git a/launcher/settingsView/configeditordialog_moc.cpp b/launcher/settingsView/configeditordialog_moc.cpp index f45822657..01a2229d0 100644 --- a/launcher/settingsView/configeditordialog_moc.cpp +++ b/launcher/settingsView/configeditordialog_moc.cpp @@ -37,6 +37,7 @@ ConfigEditorDialog::ConfigEditorDialog(QWidget *parent): "settings.json", "persistentStorage.json", "modSettings.json", + "shortcutsConfig.json", }; ui->comboBox->addItems(files); diff --git a/lib/CConfigHandler.cpp b/lib/CConfigHandler.cpp index 4fbfc57fe..886ee8a4e 100644 --- a/lib/CConfigHandler.cpp +++ b/lib/CConfigHandler.cpp @@ -19,6 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN SettingsStorage settings; SettingsStorage persistentStorage; +SettingsStorage shortcutsConfig; template SettingsStorage::NodeAccessor::NodeAccessor(SettingsStorage & _parent, std::vector _path): diff --git a/lib/CConfigHandler.h b/lib/CConfigHandler.h index 4822db41c..8ba62b188 100644 --- a/lib/CConfigHandler.h +++ b/lib/CConfigHandler.h @@ -122,5 +122,6 @@ public: extern DLL_LINKAGE SettingsStorage settings; extern DLL_LINKAGE SettingsStorage persistentStorage; +extern DLL_LINKAGE SettingsStorage shortcutsConfig; VCMI_LIB_NAMESPACE_END diff --git a/lib/GameLibrary.cpp b/lib/GameLibrary.cpp index 00c11db94..ef1948df8 100644 --- a/lib/GameLibrary.cpp +++ b/lib/GameLibrary.cpp @@ -160,6 +160,7 @@ void GameLibrary::initializeFilesystem(bool extractArchives) loadFilesystem(extractArchives); settings.init("config/settings.json", "vcmi:settings"); persistentStorage.init("config/persistentStorage.json", ""); + shortcutsConfig.init("config/shortcutsConfig.json", ""); loadModFilesystem(); }