Convert legacy keyboard mappings.

This commit is contained in:
Christian Speckner 2020-11-28 12:46:48 +01:00
parent 234a2745c4
commit 9ce8e9a02a
5 changed files with 59 additions and 44 deletions

View File

@ -16,6 +16,7 @@
//============================================================================
#include "KeyMap.hxx"
#include "Logger.hxx"
#include "jsonDefinitions.hxx"
#include <map>
@ -198,35 +199,48 @@ int KeyMap::loadMapping(const json& mappings, const EventMode mode) {
int i = 0;
for (const json& mapping: mappings) {
add(
mapping.at("event").get<Event::Type>(),
mode,
mapping.at("key").get<StellaKey>(),
mapping.contains("mod") ? mapping.at("mod").get<StellaMod>() : StellaMod::KBDM_NONE
);
try {
add(
mapping.at("event").get<Event::Type>(),
mode,
mapping.at("key").get<StellaKey>(),
mapping.contains("mod") ? mapping.at("mod").get<StellaMod>() : StellaMod::KBDM_NONE
);
i++;
i++;
} catch (json::exception) {
Logger::error("ignoring bad keyboard mapping");
}
}
return i;
}
/*
int KeyMap::loadMapping(string& list, const EventMode mode)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
json KeyMap::convertLegacyMapping(string list)
{
json convertedMapping = json::array();
// Since istringstream swallows whitespace, we have to make the
// delimiters be spaces
std::replace(list.begin(), list.end(), '|', ' ');
std::replace(list.begin(), list.end(), ':', ' ');
std::replace(list.begin(), list.end(), ',', ' ');
istringstream buf(list);
int event, key, mod, i = 0;
int event, key, mod;
while (buf >> event && buf >> key && buf >> mod && ++i)
add(Event::Type(event), mode, key, mod);
while (buf >> event && buf >> key && buf >> mod) {
json mapping = json::object();
return i;
mapping["event"] = Event::Type(event);
mapping["key"] = StellaKey(key);
mapping["mod"] = StellaMod(mod);
convertedMapping.push_back(mapping);
}
return convertedMapping;
}
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::eraseMode(const EventMode mode)

View File

@ -90,6 +90,8 @@ class KeyMap
nlohmann::json saveMapping(const EventMode mode) const;
int loadMapping(const nlohmann::json& mapping, const EventMode mode);
static nlohmann::json convertLegacyMapping(string list);
/** Erase all mappings for given mode */
void eraseMode(const EventMode mode);
/** Erase given event's mapping for given mode */

View File

@ -55,7 +55,7 @@ PhysicalJoystickHandler::PhysicalJoystickHandler(
for (const json& mapping: mappings) {
if (!mapping.contains("name")) {
Logger::error("igmoring bad joystick mapping");
Logger::error("ignoring bad joystick mapping");
continue;
}

View File

@ -49,38 +49,15 @@ PhysicalKeyboardHandler::PhysicalKeyboardHandler(OSystem& system, EventHandler&
// Compare if event list version has changed so that key maps became invalid
if (version == Event::VERSION)
{
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_emu")), EventMode::kCommonMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: common");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_joy")), EventMode::kJoystickMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: joystick");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_pad")), EventMode::kPaddlesMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: paddles");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_key")), EventMode::kKeypadMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: keypad");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_ui")), EventMode::kMenuMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: UI");
}
loadSerializedMappings(myOSystem.settings().getString("keymap_emu"), EventMode::kCommonMode);
loadSerializedMappings(myOSystem.settings().getString("keymap_joy"), EventMode::kJoystickMode);
loadSerializedMappings(myOSystem.settings().getString("keymap_pad"), EventMode::kPaddlesMode);
loadSerializedMappings(myOSystem.settings().getString("keymap_key"), EventMode::kKeypadMode);
loadSerializedMappings(myOSystem.settings().getString("keymap_ui"), EventMode::kMenuMode);
updateDefaults = true;
}
myKeyMap.enableMod() = myOSystem.settings().getBool("modcombo");
setDefaultMapping(Event::NoType, EventMode::kEmulationMode, updateDefaults);
@ -90,6 +67,26 @@ PhysicalKeyboardHandler::PhysicalKeyboardHandler(OSystem& system, EventHandler&
#endif // DEBUG
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::loadSerializedMappings(const string& serializedMapping, EventMode mode)
{
json mapping;
try {
mapping = json::parse(serializedMapping);
} catch (json::exception) {
Logger::info("converting legacy keyboard mappings");
mapping = KeyMap::convertLegacyMapping(serializedMapping);
}
try {
myKeyMap.loadMapping(mapping, mode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings");
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PhysicalKeyboardHandler::isMappingUsed(EventMode mode, const EventMapping& map) const
{

View File

@ -45,6 +45,8 @@ class PhysicalKeyboardHandler
PhysicalKeyboardHandler(OSystem& system, EventHandler& handler);
void loadSerializedMappings(const string& serializedMappings, EventMode mode);
void setDefaultMapping(Event::Type type, EventMode mode, bool updateDefaults = false);
/** define mappings for current controllers */