mapping for standalone and combined modifiers fixed

This commit is contained in:
thrust26 2019-05-26 19:53:14 +02:00
parent 308cc1864d
commit 54decb2cdf
2 changed files with 90 additions and 59 deletions

View File

@ -14,7 +14,6 @@
// See the file "License.txt" for information on usage and redistribution of // See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================ //============================================================================
#include "KeyMap.hxx" #include "KeyMap.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -23,9 +22,9 @@ KeyMap::KeyMap(void)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::add(const Event::Type event, const Mapping& input) void KeyMap::add(const Event::Type event, const Mapping& mapping)
{ {
myMap[convertMod(input)] = event; myMap[convertMod(mapping)] = event;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -35,9 +34,9 @@ void KeyMap::add(const Event::Type event, const int mode, const int key, const i
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::erase(const Mapping& input) void KeyMap::erase(const Mapping& mapping)
{ {
myMap.erase(convertMod(input)); myMap.erase(convertMod(mapping));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -47,10 +46,18 @@ void KeyMap::erase(const int mode, const int key, const int mod)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type KeyMap::get(const Mapping& input) const Event::Type KeyMap::get(const Mapping& mapping) const
{ {
auto find = myMap.find(convertMod(input)); Mapping m = convertMod(mapping);
auto find = myMap.find(m);
if (find != myMap.end())
return find->second;
// mapping not found, try without modifiers
m.mod = StellaMod(0);
find = myMap.find(m);
if (find != myMap.end()) if (find != myMap.end())
return find->second; return find->second;
@ -63,6 +70,42 @@ Event::Type KeyMap::get(const int mode, const int key, const int mod) const
return get(Mapping(mode, key, mod)); return get(Mapping(mode, key, mod));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string KeyMap::getDesc(const Mapping& mapping) const
{
ostringstream buf;
#ifndef BSPF_MACOS
string modifier = "Ctrl";
#else
string modifier = "Cmd";
#endif
if ((mapping.mod & KBDM_CTRL) == KBDM_CTRL) buf << modifier;
else if (mapping.mod & KBDM_LCTRL) buf << "Left " << modifier;
else if (mapping.mod & KBDM_RCTRL) buf << "Right " << modifier;
if ((mapping.mod & KBDM_ALT) && buf.tellp()) buf << "+";
if ((mapping.mod & KBDM_ALT) == KBDM_ALT) buf << "Alt";
else if (mapping.mod & KBDM_LALT) buf << "Left Alt";
else if (mapping.mod & KBDM_RALT) buf << "Right Alt";
if ((mapping.mod & KBDM_SHIFT) && buf.tellp()) buf << "+";
if ((mapping.mod & KBDM_SHIFT) == KBDM_SHIFT) buf << "Shift";
else if (mapping.mod & KBDM_LSHIFT) buf << "Left Shift";
else if (mapping.mod & KBDM_RSHIFT) buf << "Right Shift";
if (buf.tellp()) buf << "+";
buf << StellaKeyName::forKey(mapping.key);
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string KeyMap::getDesc(const int mode, const int key, const int mod) const
{
return getDesc(Mapping(mode, key, mod));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string KeyMap::getEventMappingDesc(const Event::Type event, const int mode) const string KeyMap::getEventMappingDesc(const Event::Type event, const int mode) const
{ {
@ -79,10 +122,7 @@ string KeyMap::getEventMappingDesc(const Event::Type event, const int mode) cons
{ {
if (buf.str() != "") if (buf.str() != "")
buf << ", "; buf << ", ";
if (item.first.mod & StellaMod::KBDM_CTRL) buf << modifier << "+"; buf << getDesc(item.first);
if (item.first.mod & StellaMod::KBDM_ALT) buf << "Alt+";
if (item.first.mod & StellaMod::KBDM_SHIFT) buf << "Shift+";
buf << StellaKeyName::forKey(item.first.key);
} }
} }
return buf.str(); return buf.str();
@ -134,52 +174,39 @@ int KeyMap::loadMapping(string& list, const int mode)
return i; return i;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string KeyMap::getDesc(const Mapping& input) const
{
return "TODO";
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string KeyMap::getDesc(const int mode, const int key, const int mod) const
{
return getDesc(Mapping(mode, key, mod));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::eraseMode(const int mode) void KeyMap::eraseMode(const int mode)
{ {
for (auto i = myMap.begin(); i != myMap.end();) for (auto item = myMap.begin(); item != myMap.end();)
if (i->first.mode == mode) { if (item->first.mode == mode) {
auto _i = i++; auto _item = item++;
erase(_i->first); erase(_item->first);
} }
else i++; else item++;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::eraseEvent(const Event::Type event, const int mode) void KeyMap::eraseEvent(const Event::Type event, const int mode)
{ {
for (auto i = myMap.begin(); i != myMap.end();) for (auto item = myMap.begin(); item != myMap.end();)
if (i->second == event && i->first.mode == mode) { if (item->second == event && item->first.mode == mode) {
auto _i = i++; auto _item = item++;
erase(_i->first); erase(_item->first);
} }
else i++; else item++;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
KeyMap::Mapping KeyMap::convertMod(const Mapping& input) const KeyMap::Mapping KeyMap::convertMod(const Mapping& mapping) const
{ {
Mapping i = input; Mapping m = mapping;
if (m.key >= KBDK_LCTRL && m.key <= KBDK_RGUI)
// handle solo modifier keys differently
m.mod = KBDM_NONE;
else
// limit to modifiers we want to support // limit to modifiers we want to support
i.mod = StellaMod(i.mod & (StellaMod::KBDM_SHIFT | StellaMod::KBDM_ALT | StellaMod::KBDM_CTRL)); m.mod = StellaMod(m.mod & (KBDM_SHIFT | KBDM_ALT | KBDM_CTRL));
// merge left and right modifiers return m;
if (i.mod & KBDM_CTRL) i.mod = StellaMod(i.mod | KBDM_CTRL);
if (i.mod & KBDM_SHIFT) i.mod = StellaMod(i.mod | KBDM_SHIFT);
if (i.mod & KBDM_ALT) i.mod = StellaMod(i.mod | KBDM_ALT);
return i;
} }

View File

@ -39,7 +39,7 @@ class KeyMap
StellaMod mod; StellaMod mod;
Mapping() : mode(EventMode(0)), key(StellaKey(0)), mod(StellaMod(0)) { } Mapping() : mode(EventMode(0)), key(StellaKey(0)), mod(StellaMod(0)) { }
Mapping(const Mapping& k) : mode(k.mode), key(k.key), mod(k.mod) { } Mapping(const Mapping& m) : mode(m.mode), key(m.key), mod(m.mod) { }
explicit Mapping(EventMode c_mode, StellaKey c_key, StellaMod c_mod) explicit Mapping(EventMode c_mode, StellaKey c_key, StellaMod c_mod)
: mode(c_mode), key(c_key), mod(c_mod) { } : mode(c_mode), key(c_key), mod(c_mod) { }
explicit Mapping(int c_mode, int c_key, int c_mod) explicit Mapping(int c_mode, int c_key, int c_mod)
@ -47,9 +47,10 @@ class KeyMap
bool operator==(const Mapping& other) const bool operator==(const Mapping& other) const
{ {
return (mode == other.mode return (//&& mod == other.mod
&& key == other.key (mod | other.mod ? mod & other.mod : true)
&& mod == other.mod); && mode == other.mode
&& key == other.key);
} }
}; };
@ -57,24 +58,24 @@ class KeyMap
virtual ~KeyMap() = default; virtual ~KeyMap() = default;
/** Add new mapping for given event */ /** Add new mapping for given event */
void add(const Event::Type event, const Mapping& input); void add(const Event::Type event, const Mapping& mapping);
void add(const Event::Type event, const int mode, const int key, const int mod); void add(const Event::Type event, const int mode, const int key, const int mod);
/** Erase mapping */ /** Erase mapping */
void erase(const Mapping& input); void erase(const Mapping& mapping);
void erase(const int mode, const int key, const int mod); void erase(const int mode, const int key, const int mod);
/** Get event for mapping */ /** Get event for mapping */
Event::Type get(const Mapping& input) const; Event::Type get(const Mapping& mapping) const;
Event::Type get(const int mode, const int key, const int mod) const; Event::Type get(const int mode, const int key, const int mod) const;
/** Get the mapping(s) description for given event and mode */
string getEventMappingDesc(const Event::Type event, const int mode) const;
/** Get mapping description */ /** Get mapping description */
string getDesc(const Mapping& input) const; string getDesc(const Mapping& mapping) const;
string getDesc(const int mode, const int key, const int mod) const; string getDesc(const int mode, const int key, const int mod) const;
/** Get the mapping description(s) for given event and mode */
string getEventMappingDesc(const Event::Type event, const int mode) const;
std::vector<Mapping> getEventMapping(const Event::Type event, const int mode) const; std::vector<Mapping> getEventMapping(const Event::Type event, const int mode) const;
string saveMapping(const int mode) const; string saveMapping(const int mode) const;
@ -90,13 +91,16 @@ class KeyMap
private: private:
//** Convert modifiers */ //** Convert modifiers */
Mapping convertMod(const Mapping& input) const; Mapping convertMod(const Mapping& mapping) const;
struct KeyHash { struct KeyHash {
size_t operator()(const Mapping& k)const { size_t operator()(const Mapping& m)const {
return std::hash<uInt64>()((uInt64(k.mode)) return std::hash<uInt64>()((uInt64(m.mode)) // 1 bit
^ ((uInt64(k.key)) << 16) ^ ((uInt64(m.key)) << 1) // 8 bits
^ ((uInt64(k.mod)) << 32)); // no mod in hash to allow mapping left and right modifiers as one
// also see '==' above
/* ^ ((uInt64(m.mod)) << 9)*/); // 15 bits
} }
}; };