refactor controller dependent mappings

This commit is contained in:
thrust26 2019-06-15 12:10:00 +02:00
parent d969d9152b
commit 65cb17e736
6 changed files with 80 additions and 48 deletions

View File

@ -55,7 +55,7 @@ PhysicalKeyboardHandler::PhysicalKeyboardHandler(
if (version == Event::VERSION)
{
string list = myOSystem.settings().getString("keymap_emu");
myKeyMap.loadMapping(list, kEmulationMode);
myKeyMap.loadMapping(list, kCommonMode);
list = myOSystem.settings().getString("keymap_joy");
myKeyMap.loadMapping(list, kJoystickMode);
list = myOSystem.settings().getString("keymap_pad");
@ -111,8 +111,8 @@ void PhysicalKeyboardHandler::setDefaultMapping(Event::Type event, EventMode mod
switch(mode)
{
case kEmulationMode:
for (const auto& item: DefaultEmuMapping)
setDefaultKey(item, event, kEmulationMode, updateDefaults);
for (const auto& item: DefaultCommonMapping)
setDefaultKey(item, event, kCommonMode, updateDefaults);
// put all controller events into their own mode's mappings
for (const auto& item: DefaultJoystickMapping)
setDefaultKey(item, event, kJoystickMode, updateDefaults);
@ -133,8 +133,9 @@ void PhysicalKeyboardHandler::setDefaultMapping(Event::Type event, EventMode mod
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::enableControllerEvents(const string& controllerName, Controller::Jack port)
void PhysicalKeyboardHandler::defineControllerMappings(const string& controllerName, Controller::Jack port)
{
// determine controller events to use
if ((controllerName == "KEYBOARD") || (controllerName == "KEYPAD"))
{
if (port == Controller::Jack::Left)
@ -142,7 +143,7 @@ void PhysicalKeyboardHandler::enableControllerEvents(const string& controllerNam
else
myRightMode = kKeypadMode;
}
else if(BSPF::startsWithIgnoreCase(controllerName, "PADDLES"))
else if (BSPF::startsWithIgnoreCase(controllerName, "PADDLES"))
{
if (port == Controller::Jack::Left)
myLeftMode = kPaddlesMode;
@ -156,13 +157,15 @@ void PhysicalKeyboardHandler::enableControllerEvents(const string& controllerNam
else
myRightMode = kJoystickMode;
}
enableControllerEvents();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::enableControllerEvents()
void PhysicalKeyboardHandler::enableEmulationMappings()
{
// start from scratch and enable common mappings
myKeyMap.eraseMode(kEmulationMode);
enableCommonMappings();
// enable right mode first, so that in case of mapping clashes the left controller has preference
switch (myRightMode)
{
@ -196,34 +199,51 @@ void PhysicalKeyboardHandler::enableControllerEvents()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::enableMappings(const EventSet events, EventMode mode, bool enable)
void PhysicalKeyboardHandler::enableCommonMappings()
{
for (int i = Event::NoType + 1; i < Event::LastType; i++)
{
Event::Type event = static_cast<Event::Type>(i);
if (isCommonEvent(event))
enableMapping(event, kCommonMode);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::enableMappings(const EventSet events, EventMode mode)
{
for (const auto& event : events)
{
// copy from controller mode into emulation mode
std::vector<KeyMap::Mapping> mappings = myKeyMap.getEventMapping(event, mode);
enableMapping(event, mode);
}
for (const auto& mapping : mappings)
{
if (enable)
myKeyMap.add(event, kEmulationMode, mapping.key, mapping.mod);
else
myKeyMap.eraseEvent(event, kEmulationMode);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::enableMapping(const Event::Type event, EventMode mode)
{
// copy from controller mode into emulation mode
std::vector<KeyMap::Mapping> mappings = myKeyMap.getEventMapping(event, mode);
for (const auto& mapping : mappings)
myKeyMap.add(event, kEmulationMode, mapping.key, mapping.mod);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
EventMode PhysicalKeyboardHandler::getEventMode(const Event::Type event, const EventMode mode) const
{
if (isJoystickEvent(event))
return kJoystickMode;
if (mode == kEmulationMode)
{
if (isJoystickEvent(event))
return kJoystickMode;
if (isPaddleEvent(event))
return kPaddlesMode;
if (isPaddleEvent(event))
return kPaddlesMode;
if (isKeypadEvent(event))
return kKeypadMode;
if (isKeypadEvent(event))
return kKeypadMode;
if (isCommonEvent(event))
return kCommonMode;
}
return mode;
}
@ -249,6 +269,12 @@ bool PhysicalKeyboardHandler::isKeypadEvent(const Event::Type event) const
|| RightKeypadEvents.find(event) != RightKeypadEvents.end();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PhysicalKeyboardHandler::isCommonEvent(const Event::Type event) const
{
return !(isJoystickEvent(event) || isPaddleEvent(event) || isKeypadEvent(event));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::eraseMapping(Event::Type event, EventMode mode)
{
@ -260,20 +286,12 @@ void PhysicalKeyboardHandler::eraseMapping(Event::Type event, EventMode mode)
void PhysicalKeyboardHandler::saveMapping()
{
myOSystem.settings().setValue("event_ver", Event::VERSION);
// remove controller specific events in emulation mode mapping
enableMappings(LeftJoystickEvents, kJoystickMode, false);
enableMappings(LeftPaddlesEvents, kPaddlesMode, false);
enableMappings(LeftKeypadEvents, kKeypadMode, false);
enableMappings(RightJoystickEvents, kJoystickMode, false);
enableMappings(RightPaddlesEvents, kPaddlesMode, false);
enableMappings(RightKeypadEvents, kKeypadMode, false);
myOSystem.settings().setValue("keymap_emu", myKeyMap.saveMapping(kEmulationMode));
// restore current controller's event mappings
enableControllerEvents();
myOSystem.settings().setValue("keymap_emu", myKeyMap.saveMapping(kCommonMode));
myOSystem.settings().setValue("keymap_joy", myKeyMap.saveMapping(kJoystickMode));
myOSystem.settings().setValue("keymap_pad", myKeyMap.saveMapping(kPaddlesMode));
myOSystem.settings().setValue("keymap_key", myKeyMap.saveMapping(kKeypadMode));
myOSystem.settings().setValue("keymap_ui", myKeyMap.saveMapping(kMenuMode));
enableEmulationMappings();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -405,7 +423,7 @@ EventSet PhysicalKeyboardHandler::RightKeypadEvents = {
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PhysicalKeyboardHandler::EventMappingArray PhysicalKeyboardHandler::DefaultEmuMapping = {
PhysicalKeyboardHandler::EventMappingArray PhysicalKeyboardHandler::DefaultCommonMapping = {
{Event::ConsoleSelect, KBDK_F1},
{Event::ConsoleReset, KBDK_F2},
{Event::ConsoleColor, KBDK_F3},

View File

@ -50,8 +50,10 @@ class PhysicalKeyboardHandler
void setDefaultMapping(Event::Type type, EventMode mode, bool updateDefaults = false);
/** enable mappings for current controllers */
void enableControllerEvents(const string& controllerName, Controller::Jack port);
/** define mappings for current controllers */
void defineControllerMappings(const string& controllerName, Controller::Jack port);
/** enable mappings for emulation mode */
void enableEmulationMappings();
void eraseMapping(Event::Type event, EventMode mode);
void saveMapping();
@ -100,10 +102,12 @@ class PhysicalKeyboardHandler
bool isJoystickEvent(const Event::Type event) const;
bool isPaddleEvent(const Event::Type event) const;
bool isKeypadEvent(const Event::Type event) const;
bool isCommonEvent(const Event::Type event) const;
void enableControllerEvents();
void enableCommonMappings();
void enableMappings(const EventSet events, EventMode mode, bool enable = true);
void enableMappings(const EventSet events, EventMode mode);
void enableMapping(const Event::Type event, EventMode mode);
OSystem& myOSystem;
EventHandler& myHandler;
@ -136,8 +140,8 @@ class PhysicalKeyboardHandler
static EventSet LeftKeypadEvents;
static EventSet RightKeypadEvents;
static EventMappingArray DefaultEmuMapping;
static EventMappingArray DefaultMenuMapping;
static EventMappingArray DefaultCommonMapping;
static EventMappingArray DefaultJoystickMapping;
static EventMappingArray DefaultPaddleMapping;
static EventMappingArray DefaultKeypadMapping;

View File

@ -133,6 +133,8 @@ Console::Console(OSystem& osystem, unique_ptr<Cartridge>& cart,
// This must be done before the debugger is initialized
const string& md5 = myProperties.get(PropType::Cart_MD5);
setControllers(md5);
// now that we know the controllers, enable the event mappings
myOSystem.eventHandler().enableEmulationKeyMappings();
// Mute audio and clear framebuffer while autodetection runs
myOSystem.sound().mute(1);
@ -845,7 +847,7 @@ unique_ptr<Controller> Console::getControllerPort(const string& rommd5,
{
unique_ptr<Controller> controller = std::move(myLeftControl);
myOSystem.eventHandler().enableKeyControllerEvents(controllerName, port);
myOSystem.eventHandler().defineKeyControllerMappings(controllerName, port);
if(controllerName == "JOYSTICK")
{

View File

@ -193,10 +193,17 @@ class EventHandler
bool addKeyMapping(Event::Type event, EventMode mode, StellaKey key, StellaMod mod);
/**
Enable controller specific keyboard events.
Enable controller specific keyboard event mappings.
*/
void enableKeyControllerEvents(const string& controllerName, Controller::Jack port) {
myPKeyHandler->enableControllerEvents(controllerName, port);
void defineKeyControllerMappings(const string& controllerName, Controller::Jack port) {
myPKeyHandler->defineControllerMappings(controllerName, port);
}
/**
Enable emulation keyboard event mappings.
*/
void enableEmulationKeyMappings() {
myPKeyHandler->enableEmulationMappings();
}
/**

View File

@ -73,7 +73,8 @@ enum EventMode {
kNumModes = 2,
kJoystickMode = kNumModes, // 3 extra modes for mapping controller keys separately
kPaddlesMode = kNumModes + 1,
kKeypadMode = kNumModes + 2
kKeypadMode = kNumModes + 2,
kCommonMode = kNumModes + 3
};

View File

@ -111,7 +111,7 @@ Settings::Settings()
setPermanent("ss1x", "false");
setPermanent("ssinterval", "2");
setPermanent("autoslot", "false");
setPermanent("saveonexit", "ALL");
setPermanent("saveonexit", "none");
// Config files and paths
setPermanent("romdir", "");