1 #include "Modifiers.h" 2 3 #include "iregistry.h" 4 5 #include "string/string.h" 6 7 // Constructor, loads the modifier nodes from the registry Modifiers()8Modifiers::Modifiers () : _modifierState(0) 9 { 10 loadModifierDefinitions(); 11 } 12 loadModifierDefinitions()13void Modifiers::loadModifierDefinitions () 14 { 15 xml::NodeList modifiers = GlobalRegistry().findXPath("user/ui/input//modifiers"); 16 17 if (!modifiers.empty()) { 18 // Find all button definitions 19 xml::NodeList modifierList = modifiers[0].getNamedChildren("modifier"); 20 21 if (!modifierList.empty()) { 22 globalOutputStream() << "EventManager: Modifiers found: " << modifierList.size() << "\n"; 23 for (unsigned int i = 0; i < modifierList.size(); i++) { 24 const std::string name = modifierList[i].getAttributeValue("name"); 25 26 int bitIndex = string::toInt(modifierList[i].getAttributeValue("bitIndex"), -1); 27 28 if (name != "" && bitIndex >= 0) { 29 // Save the modifier ID into the map 30 _modifierBitIndices[name] = static_cast<unsigned int> (bitIndex); 31 } else { 32 globalOutputStream() << "EventManager: Warning: Invalid modifier definition found.\n"; 33 } 34 } 35 } else { 36 // No Button definitions found! 37 globalOutputStream() << "EventManager: Critical: No modifiers definitions found!\n"; 38 } 39 } else { 40 // No Button definitions found! 41 globalOutputStream() << "EventManager: Critical: No modifiers definitions found!\n"; 42 } 43 } 44 getModifierFlags(const std::string & modifierStr)45unsigned int Modifiers::getModifierFlags (const std::string& modifierStr) 46 { 47 StringParts parts; 48 string::splitBy(modifierStr, parts, "+"); 49 50 // Do we have any modifiers at all? 51 if (!parts.empty()) { 52 unsigned int returnValue = 0; 53 54 // Cycle through all the modifier names and construct the bitfield 55 for (unsigned int i = 0; i < parts.size(); i++) { 56 if (parts[i] == "") 57 continue; 58 59 // Try to find the modifierBitIndex 60 int bitIndex = getModifierBitIndex(parts[i]); 61 62 // Was anything found? 63 if (bitIndex >= 0) { 64 unsigned int bitValue = (1 << static_cast<unsigned int> (bitIndex)); 65 returnValue |= bitValue; 66 } 67 } 68 69 return returnValue; 70 } else { 71 return 0; 72 } 73 } 74 getGdkModifierType(const unsigned int & modifierFlags)75GdkModifierType Modifiers::getGdkModifierType (const unsigned int& modifierFlags) 76 { 77 unsigned int returnValue = 0; 78 79 if ((modifierFlags & (1 << getModifierBitIndex("CONTROL"))) != 0) { 80 returnValue |= GDK_CONTROL_MASK; 81 } 82 83 if ((modifierFlags & (1 << getModifierBitIndex("SHIFT"))) != 0) { 84 returnValue |= GDK_SHIFT_MASK; 85 } 86 87 if ((modifierFlags & (1 << getModifierBitIndex("ALT"))) != 0) { 88 returnValue |= GDK_MOD1_MASK; 89 } 90 91 return static_cast<GdkModifierType> (returnValue); 92 } 93 getModifierBitIndex(const std::string & modifierName)94int Modifiers::getModifierBitIndex (const std::string& modifierName) 95 { 96 ModifierBitIndexMap::iterator it = _modifierBitIndices.find(modifierName); 97 if (it != _modifierBitIndices.end()) { 98 return it->second; 99 } else { 100 globalOutputStream() << "EventManager: Warning: Modifier " << modifierName << " not found, returning -1\n"; 101 return -1; 102 } 103 } 104 105 // Returns a bit field with the according modifier flags set getKeyboardFlags(const unsigned int & state)106unsigned int Modifiers::getKeyboardFlags (const unsigned int& state) 107 { 108 unsigned int returnValue = 0; 109 110 if ((state & GDK_CONTROL_MASK) != 0) { 111 returnValue |= (1 << getModifierBitIndex("CONTROL")); 112 } 113 114 if ((state & GDK_SHIFT_MASK) != 0) { 115 returnValue |= (1 << getModifierBitIndex("SHIFT")); 116 } 117 118 if ((state & GDK_MOD1_MASK) != 0) { 119 returnValue |= (1 << getModifierBitIndex("ALT")); 120 } 121 122 return returnValue; 123 } 124 125 // Returns a string for the given modifier flags set (e.g. "SHIFT+CONTROL") getModifierStr(const unsigned int & modifierFlags,bool forMenu)126std::string Modifiers::getModifierStr (const unsigned int& modifierFlags, bool forMenu) 127 { 128 std::string returnValue = ""; 129 130 const std::string controlStr = (forMenu) ? "Ctrl" : "CONTROL"; 131 const std::string shiftStr = (forMenu) ? "Shift" : "SHIFT"; 132 const std::string altStr = (forMenu) ? "Alt" : "ALT"; 133 const std::string connector = (forMenu) ? "-" : "+"; 134 135 if ((modifierFlags & (1 << getModifierBitIndex("CONTROL"))) != 0) { 136 returnValue += (returnValue != "") ? connector : ""; 137 returnValue += controlStr; 138 } 139 140 if ((modifierFlags & (1 << getModifierBitIndex("SHIFT"))) != 0) { 141 returnValue += (returnValue != "") ? connector : ""; 142 returnValue += shiftStr; 143 } 144 145 if ((modifierFlags & (1 << getModifierBitIndex("ALT"))) != 0) { 146 returnValue += (returnValue != "") ? connector : ""; 147 returnValue += altStr; 148 } 149 150 return returnValue; 151 } 152 getState() const153unsigned int Modifiers::getState() const { 154 return _modifierState; 155 } 156 setState(unsigned int state)157void Modifiers::setState(unsigned int state) { 158 _modifierState = state; 159 } 160 updateState(GdkEventKey * event,bool keyPress)161void Modifiers::updateState(GdkEventKey* event, bool keyPress) { 162 unsigned int mask = 0; 163 164 int ctrlMask = 1 << getModifierBitIndex("CONTROL"); 165 int shiftMask = 1 << getModifierBitIndex("SHIFT"); 166 int altMask = 1 << getModifierBitIndex("ALT"); 167 168 mask |= (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R) ? ctrlMask : 0; 169 mask |= (event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R) ? shiftMask : 0; 170 mask |= (event->keyval == GDK_Alt_L || event->keyval == GDK_Alt_R) ? altMask : 0; 171 172 if (keyPress) { 173 _modifierState |= mask; 174 } 175 else { 176 _modifierState &= ~mask; 177 } 178 } 179