KeyboardKey.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3