00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <common/KeyboardKey.h>
00022 #include <common/KeyTranslate.h>
00023 #include <common/KeyStateTranslate.h>
00024 #include <common/Defines.h>
00025
00026 KeyboardKey::KeyboardKey(const char *name,
00027 const char *title,
00028 const char *description,
00029 int group,
00030 bool command) :
00031 name_(name), title_(title), description_(description),
00032 keyToogle_(false), command_(command), group_(group),
00033 changed_(false)
00034 {
00035 }
00036
00037 KeyboardKey::~KeyboardKey()
00038 {
00039 }
00040
00041 bool KeyboardKey::addKeys(std::list<std::string> &keyNames,
00042 std::list<std::string> &keyStates)
00043 {
00044 DIALOG_ASSERT(keyNames.size() == keyStates.size());
00045 keys_.clear();
00046
00047 unsigned int i = 0;
00048 std::list<std::string>::iterator itor;
00049 std::list<std::string>::iterator itorState;
00050 for (itor = keyNames.begin(), itorState = keyStates.begin();
00051 itor != keyNames.end() && itorState != keyStates.end();
00052 itor++, i++, itorState++)
00053 {
00054 unsigned int keyValue = 0;
00055 std::string &name = *itor;
00056 if (!translateKeyName(name.c_str(), keyValue))
00057 {
00058 S3D::dialogMessage("KeyboardKey", S3D::formatStringBuffer(
00059 "Failed to find key names \"%s\" for \"%s\"",
00060 name.c_str(),
00061 name_.c_str()));
00062 return false;
00063 }
00064
00065 unsigned int keyState = 0;
00066 std::string &state = *itorState;
00067 if (!translateKeyState(state.c_str(), keyState))
00068 {
00069 S3D::dialogMessage("KeyboardKey", S3D::formatStringBuffer(
00070 "Failed to find key state \"%s\" for \"%s\"",
00071 state.c_str(),
00072 name_.c_str()));
00073 return false;
00074 }
00075
00076 addKey(i, keyValue, keyState);
00077 }
00078 return true;
00079 }
00080
00081 int KeyboardKey::keyIndex(unsigned int key, unsigned int state)
00082 {
00083 int i=0;
00084 std::vector<KeyEntry>::iterator itor;
00085 for (itor = keys_.begin();
00086 itor != keys_.end();
00087 itor++, i++)
00088 {
00089 KeyEntry &entry = *itor;
00090 if (entry.key == key &&
00091 entry.state == state) return i;
00092 }
00093 return -1;
00094 }
00095
00096 void KeyboardKey::addKey(unsigned int position,
00097 unsigned int key, unsigned int state)
00098 {
00099 KeyEntry entry;
00100 entry.key = key;
00101 entry.state = state;
00102
00103 if (position < keys_.size())
00104 {
00105 keys_[position] = entry;
00106 }
00107 else
00108 {
00109 keys_.push_back(entry);
00110 }
00111 changed_ = true;
00112 }
00113
00114 void KeyboardKey::removeKey(unsigned int position)
00115 {
00116 if (position < keys_.size())
00117 {
00118 unsigned int i = 0;
00119 std::vector<KeyEntry>::iterator itor;
00120 for (itor = keys_.begin();
00121 itor != keys_.end();
00122 itor++, i++)
00123 {
00124 if (i == position)
00125 {
00126 keys_.erase(itor);
00127 break;
00128 }
00129 }
00130 }
00131 changed_ = true;
00132 }
00133
00134 bool KeyboardKey::translateKeyName(const char *name, unsigned int &key)
00135 {
00136 for (int i=0; i<sizeof(KeyTranslationTable)/sizeof(KeyTranslation); i++)
00137 {
00138 if (strcmp(KeyTranslationTable[i].keyName, name)==0)
00139 {
00140 key = KeyTranslationTable[i].keySym;
00141 return true;
00142 }
00143 }
00144 return false;
00145 }
00146
00147 bool KeyboardKey::translateKeyState(const char *name, unsigned int &state)
00148 {
00149 for (int i=0; i<sizeof(KeyStateTranslationTable)/sizeof(KeyStateTranslation); i++)
00150 {
00151 if (strcmp(KeyStateTranslationTable[i].keyStateName, name)==0)
00152 {
00153 state = KeyStateTranslationTable[i].keyStateSym;
00154 return true;
00155 }
00156 }
00157 return false;
00158 }
00159
00160 bool KeyboardKey::translateKeyNameValue(unsigned int key, const char *&name)
00161 {
00162 for (int i=0; i<sizeof(KeyTranslationTable)/sizeof(KeyTranslation); i++)
00163 {
00164 if (KeyTranslationTable[i].keySym == key)
00165 {
00166 name = KeyTranslationTable[i].keyName;
00167 }
00168 }
00169 return false;
00170 }
00171
00172 bool KeyboardKey::translateKeyStateValue(unsigned int state, const char *&name)
00173 {
00174 for (int i=0; i<sizeof(KeyStateTranslationTable)/sizeof(KeyStateTranslation); i++)
00175 {
00176 if (KeyStateTranslationTable[i].keyStateSym == state)
00177 {
00178 name = KeyStateTranslationTable[i].keyStateName;
00179 }
00180 }
00181 return false;
00182 }
00183
00184 bool KeyboardKey::keyDown(char *buffer, unsigned int keyState, bool repeat)
00185 {
00186 for (unsigned int i=0; i<keys_.size(); i++)
00187 {
00188 if (keyState == keys_[i].state && buffer[keys_[i].key])
00189 {
00190 if (!repeat && keyToogle_) return false;
00191
00192 keyToogle_ = true;
00193 return true;
00194 }
00195 }
00196
00197 keyToogle_ = false;
00198 return false;
00199 }
00200
00201 bool KeyboardKey::keyMatch(unsigned key)
00202 {
00203 for (unsigned int i=0; i<keys_.size(); i++)
00204 {
00205 if (keys_[i].key == key) return true;
00206 }
00207 return false;
00208 }