Convert keybinding keys to typed keys

This commit is contained in:
Alexander Bock
2020-06-21 23:54:36 +02:00
parent 338c8a9bd5
commit 42e4e0a75b
6 changed files with 42 additions and 9 deletions

View File

@@ -28,6 +28,7 @@
#include <openspace/engine/globals.h>
#include <openspace/interaction/navigationhandler.h>
#include <openspace/properties/propertyowner.h>
#include <openspace/util/keys.h>
#include <optional>
#include <string>
#include <variant>
@@ -70,7 +71,7 @@ public:
std::string value;
};
struct Keybinding {
std::string key; // @TODO (abock, 2020-06-16) change to key+action
KeyWithModifier key;
std::string documentation;
std::string name;
std::string guiPath;

View File

@@ -228,7 +228,12 @@ namespace {
);
}
Profile::Keybinding kb;
kb.key = fields[0];
try {
kb.key = stringToKey(fields[0]);
}
catch (const ghoul::RuntimeError& e) {
throw ProfileParsingError(lineNumber, e.what());
}
kb.documentation = fields[1];
kb.name = fields[2];
kb.guiPath = fields[3];
@@ -566,10 +571,11 @@ std::string Profile::serialize() const {
if (!keybindings.empty()) {
output += fmt::format("\n{}\n", headerKeybinding);
for (const Keybinding& k : keybindings) {
const std::string key = ghoul::to_string(k.key);
const std::string local = k.isLocal ? "true" : "false";
output += fmt::format(
"{}\t{}\t{}\t{}\t{}\t{}\n",
k.key, k.documentation, k.name, k.guiPath, local, k.script
key, k.documentation, k.name, k.guiPath, local, k.script
);
}
}
@@ -801,12 +807,13 @@ std::string Profile::convertToScene() const {
output += "asset.onInitialize(function()\n";
// Keybindings
for (const Keybinding& k : keybindings) {
const std::string name = k.name.empty() ? k.key : k.name;
const std::string key = ghoul::to_string(k.key);
const std::string name = k.name.empty() ? key : k.name;
output += fmt::format(
k.isLocal ?
"openspace.bindKeyLocal(\"{}\", {}, [[{}]], [[{}]], [[{}]]);\n" :
"openspace.bindKey(\"{}\", {}, [[{}]], [[{}]], [[{}]]);\n",
k.key, k.script, k.documentation, k.name.empty() ? k.key : k.name, k.guiPath
key, k.script, k.documentation, name, k.guiPath
);
}

View File

@@ -27,6 +27,7 @@
#include <ghoul/fmt.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/assert.h>
#include <ghoul/misc/exception.h>
#include <ghoul/misc/misc.h>
#include <algorithm>
#include <vector>
@@ -82,11 +83,13 @@ KeyWithModifier stringToKey(std::string str) {
std::vector<std::string> tokens = ghoul::tokenizeString(str, '+');
// default is unknown
Key k = Key::Unknown;
const auto itKey = KeyMapping.find(tokens.back());
if (itKey != KeyMapping.end()) {
k = itKey->second;
if (itKey == KeyMapping.cend()) {
throw ghoul::RuntimeError(
fmt::format("Could not find key for '{}'", tokens.back())
);
}
Key k = itKey->second;
KeyModifier m = KeyModifier::NoModifier;
@@ -102,7 +105,7 @@ KeyWithModifier stringToKey(std::string str) {
);
}
else {
LERROR(fmt::format("Unknown modifier key '{}'", s));
throw ghoul::RuntimeError(fmt::format("Unknown modifier key '{}'", s));
}
}
);

View File

@@ -0,0 +1,7 @@
#Version
12.13
#Keybinding
F50 T documentation T name T Gui-Path true T script
U U documentation U name U Gui-Path false U script
CTRL+V CTRL+V documentation CTRL+V name CTRL+V Gui-Path false CTRL+V script

View File

@@ -0,0 +1,7 @@
#Version
12.13
#Keybinding
Keykey+F T documentation T name T Gui-Path true T script
U U documentation U name U Gui-Path false U script
CTRL+V CTRL+V documentation CTRL+V name CTRL+V Gui-Path false CTRL+V script

View File

@@ -597,6 +597,14 @@ TEST_CASE("Error keybinding too many parameters", "[profile]") {
);
}
TEST_CASE("Error keybinding wrong parameter value 'key'", "[profile]") {
error_keybinding_wrong_parameter_value_key
}
TEST_CASE("Error keybinding wrong parameter value 'key, modifier'", "[profile]") {
error_keybinding_wrong_parameter_value_modifier
}
TEST_CASE("Error keybinding wrong parameter type 'local'", "[profile]") {
constexpr const char* TestFile =
"${TESTDIR}/profile/error_keybinding_wrong_parameter_type_local.profile";