1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

writing shortcuts

This commit is contained in:
Laserlicht
2025-07-19 15:19:09 +02:00
parent fb45c80621
commit 72e60c6600
9 changed files with 38 additions and 29 deletions

View File

@@ -91,8 +91,6 @@ void QuickSpellPanel::create()
{ {
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig");
labels.clear(); labels.clear();
buttons.clear(); buttons.clear();
buttonsDisabled.clear(); buttonsDisabled.clear();
@@ -132,7 +130,7 @@ void QuickSpellPanel::create()
buttonsDisabled.push_back(std::make_shared<TransparentFilledRectangle>(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172))); buttonsDisabled.push_back(std::make_shared<TransparentFilledRectangle>(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172)));
} }
if(ENGINE->input().getCurrentInputMode() == InputMode::KEYBOARD_AND_MOUSE) if(ENGINE->input().getCurrentInputMode() == InputMode::KEYBOARD_AND_MOUSE)
labels.push_back(std::make_shared<CLabel>(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<CLabel>(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, shortcutsConfig["keyboard"]["battleSpellShortcut" + std::to_string(i)].String()));
buttons.push_back(button); buttons.push_back(button);
} }

View File

@@ -13,21 +13,20 @@
#include "ShortcutHandler.h" #include "ShortcutHandler.h"
#include "Shortcut.h" #include "Shortcut.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/json/JsonUtils.h" #include "../../lib/json/JsonUtils.h"
ShortcutHandler::ShortcutHandler() ShortcutHandler::ShortcutHandler()
{ {
const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig"); mappedKeyboardShortcuts = loadShortcuts(shortcutsConfig["keyboard"]);
mappedJoystickShortcuts = loadShortcuts(shortcutsConfig["joystickButtons"]);
mappedKeyboardShortcuts = loadShortcuts(config["keyboard"]); mappedJoystickAxes = loadShortcuts(shortcutsConfig["joystickAxes"]);
mappedJoystickShortcuts = loadShortcuts(config["joystickButtons"]);
mappedJoystickAxes = loadShortcuts(config["joystickAxes"]);
#ifndef ENABLE_GOLDMASTER #ifndef ENABLE_GOLDMASTER
std::vector<EShortcut> assignedShortcuts; std::vector<EShortcut> assignedShortcuts;
std::vector<EShortcut> missingShortcuts; std::vector<EShortcut> missingShortcuts;
for (auto const & entry : config["keyboard"].Struct()) for (auto const & entry : shortcutsConfig["keyboard"].Struct())
{ {
EShortcut shortcutID = findShortcut(entry.first); EShortcut shortcutID = findShortcut(entry.first);
assert(!vstd::contains(assignedShortcuts, shortcutID)); assert(!vstd::contains(assignedShortcuts, shortcutID));

View File

@@ -23,6 +23,7 @@
#include "../../widgets/Slider.h" #include "../../widgets/Slider.h"
#include "../../windows/InfoWindows.h" #include "../../windows/InfoWindows.h"
#include "../../../lib/CConfigHandler.h"
#include "../../../lib/texts/MetaString.h" #include "../../../lib/texts/MetaString.h"
#include "../../../lib/json/JsonNode.h" #include "../../../lib/json/JsonNode.h"
#include "../../../lib/json/JsonUtils.h" #include "../../../lib/json/JsonUtils.h"
@@ -44,10 +45,8 @@ ShortcutsWindow::ShortcutsWindow()
); );
backgroundRect = std::make_shared<TransparentFilledRectangle>(Rect(8, 48, pos.w - 16, 348), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1); backgroundRect = std::make_shared<TransparentFilledRectangle>(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; int count = 0;
for(auto & group : shortcuts.Struct()) for(auto & group : shortcutsConfig.toJsonNode().Struct())
{ {
count++; count++;
count += group.second.Struct().size(); count += group.second.Struct().size();
@@ -75,7 +74,7 @@ void ShortcutsWindow::fillList(int start)
listElements.clear(); listElements.clear();
int i = 0; 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) if(i >= start)
listElements.push_back(std::make_shared<ShortcutElement>(group->first, listElements.size())); listElements.push_back(std::make_shared<ShortcutElement>(group->first, listElements.size()));
@@ -85,15 +84,15 @@ void ShortcutsWindow::fillList(int start)
for(auto & elem : group->second.Struct()) for(auto & elem : group->second.Struct())
{ {
if(i >= start) if(i >= start)
listElements.push_back(std::make_shared<ShortcutElement>(elem.first, elem.second, listElements.size(), [this](const std::string & id, const std::string & keyName){ listElements.push_back(std::make_shared<ShortcutElement>(elem.first, elem.second, listElements.size(), [this, group](const std::string & id, const std::string & keyName){
auto str = MetaString::createFromTextID("vcmi.shortcuts.inputSet"); auto str = MetaString::createFromTextID("vcmi.shortcuts.inputSet");
str.replaceTextID("vcmi.shortcuts.shortcut." + id); str.replaceTextID("vcmi.shortcuts.shortcut." + id);
str.replaceRawString(keyName); str.replaceRawString(keyName);
GAME->interface()->showYesNoDialog(str.toString(), [this, id, keyName](){ GAME->interface()->showYesNoDialog(str.toString(), [this, group, id, keyName](){
setKeyBinding(id, keyName, true); setKeyBinding(id, group->first, keyName, true);
}, [this, id, keyName](){ }, [this, group, id, keyName](){
setKeyBinding(id, keyName, false); setKeyBinding(id, group->first, keyName, false);
}); });
})); }));
i++; 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 auto existing = shortcutsConfig[group][id];
std::cout << id << " " << keyName << " " << append << "\n"; 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()); fillList(slider->getValue());
} }
void ShortcutsWindow::resetKeyBinding() 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()); fillList(slider->getValue());
} }

View File

@@ -49,10 +49,8 @@ private:
std::vector<std::shared_ptr<ShortcutElement>> listElements; std::vector<std::shared_ptr<ShortcutElement>> listElements;
std::shared_ptr<CButton> buttonReset; std::shared_ptr<CButton> buttonReset;
JsonNode shortcuts;
void fillList(int start); 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(); void resetKeyBinding();
public: public:

View File

@@ -1,9 +1,5 @@
// This file defines all shortcuts used by VCMI // 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 // 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" : { "keyboard" : {

View File

@@ -37,6 +37,7 @@ ConfigEditorDialog::ConfigEditorDialog(QWidget *parent):
"settings.json", "settings.json",
"persistentStorage.json", "persistentStorage.json",
"modSettings.json", "modSettings.json",
"shortcutsConfig.json",
}; };
ui->comboBox->addItems(files); ui->comboBox->addItems(files);

View File

@@ -19,6 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings; SettingsStorage settings;
SettingsStorage persistentStorage; SettingsStorage persistentStorage;
SettingsStorage shortcutsConfig;
template<typename Accessor> template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path): SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):

View File

@@ -122,5 +122,6 @@ public:
extern DLL_LINKAGE SettingsStorage settings; extern DLL_LINKAGE SettingsStorage settings;
extern DLL_LINKAGE SettingsStorage persistentStorage; extern DLL_LINKAGE SettingsStorage persistentStorage;
extern DLL_LINKAGE SettingsStorage shortcutsConfig;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@@ -160,6 +160,7 @@ void GameLibrary::initializeFilesystem(bool extractArchives)
loadFilesystem(extractArchives); loadFilesystem(extractArchives);
settings.init("config/settings.json", "vcmi:settings"); settings.init("config/settings.json", "vcmi:settings");
persistentStorage.init("config/persistentStorage.json", ""); persistentStorage.init("config/persistentStorage.json", "");
shortcutsConfig.init("config/shortcutsConfig.json", "");
loadModFilesystem(); loadModFilesystem();
} }