00001 #include "keyman.h" 00002 00003 //no-whitespace version of get string param 00004 string sGetParamNW(ifstream &ffrom) 00005 { 00006 string trashstr; 00007 char trashchar[1024]; 00008 00009 ffrom >> trashstr; 00010 //ffrom.getline(trashchar,1024,'\n'); 00011 00012 while (trashstr.c_str()[0] == '#') 00013 { 00014 ffrom.getline(trashchar, 1024, '\n'); 00015 ffrom >> trashstr; 00016 //ffrom.getline(trashchar,1024,'\n'); 00017 } 00018 00019 return trashstr; 00020 } 00021 00022 void KEYMAN::KeyDown(int key) 00023 { 00024 keys[key] = true; 00025 } 00026 00027 void KEYMAN::KeyUp(int key) 00028 { 00029 keys[key] = false; 00030 } 00031 00032 void KEYMAN::OneTime(int key) 00033 { 00034 if (queuedepth < MAX_ONE_TIME_KEY_DEPTH - 1) 00035 { 00036 queue[queuedepth] = key; 00037 queuedepth++; 00038 } 00039 } 00040 00041 00042 KEYMAN::KEYMAN() 00043 { 00044 for (int i = 0; i < 1024; i++) 00045 { 00046 keys[i] = false; 00047 } 00048 00049 queuedepth = 0; 00050 00051 freecam = false; 00052 } 00053 00054 void KEYMAN::Load() 00055 { 00056 error_log.open((settings.GetSettingsDir() + "/logs/keyman.log").c_str()); 00057 00058 ifstream kdb((settings.GetDataDir() + "/lists/keys").c_str()); 00059 00060 if (!kdb) 00061 error_log << "Couldn't find key database." << endl; 00062 00063 num_keyrecs = utility.iGetParam(kdb); 00064 //error_log << num_keyrecs << endl; 00065 00066 for (int i = 0; i < num_keyrecs; i++) 00067 { 00068 keyrec[i].name = sGetParamNW(kdb); 00069 //error_log << keyrec[i].name << endl; 00070 string nk = sGetParamNW(kdb); 00071 if (strlen(nk.c_str()) == 1) 00072 keyrec[i].key = nk.c_str()[0]; 00073 else 00074 keyrec[i].key = atoi(nk.c_str()); 00075 //error_log << keyrec[i].key << endl; 00076 } 00077 00078 kdb.close(); 00079 } 00080 00081 int KEYMAN::GetKey(string kn) 00082 { 00083 for (int i = 0; i < num_keyrecs; i++) 00084 { 00085 if (strcmp(kn.c_str(), keyrec[i].name.c_str()) == 0) 00086 { 00087 return keyrec[i].key; 00088 } 00089 } 00090 00091 return '!'; 00092 } 00093 00094 extern bool verbose_output; 00095 KEYMAN::~KEYMAN() 00096 { 00097 if (verbose_output) 00098 cout << "keyman deinit" << endl; 00099 00100 error_log.close(); 00101 } 00102 00103 void KEYMAN::DoOneTimeKeys(CAMERA & cam) 00104 { 00105 int i; 00106 00107 for (i = 0; i < queuedepth; i++) 00108 { 00109 00110 } 00111 00112 //flush queue 00113 queuedepth = 0; 00114 00115 //save last key states 00116 for (i = 0; i < 1024; i++) 00117 { 00118 lastkeys[i] = keys[i]; 00119 } 00120 } 00121 00122 void KEYMAN::DoHeldKeys(float timefactor, float fps, CAMERA & cam) 00123 { 00124 //fps camera 00125 00126 float playermovespeedz = 6.0f; 00127 float playermovespeedx = 3.0f; 00128 00129 //camera movement 00130 00131 VERTEX moveamount; 00132 00133 if (keys[GetKey("UP")]) 00134 moveamount.z += playermovespeedz; 00135 if (keys[GetKey("PAGEUP")]) 00136 moveamount.z += 50.0*playermovespeedz; 00137 if (keys[GetKey("INSERT")]) 00138 moveamount.z -= 50.0*playermovespeedz; 00139 if (keys[GetKey("DOWN")]) 00140 moveamount.z += -playermovespeedz; 00141 if (keys[GetKey("RIGHT")]) 00142 moveamount.x += -playermovespeedx; 00143 if (keys[GetKey("LEFT")]) 00144 moveamount.x += playermovespeedx; 00145 00146 moveamount.Scale(3.0f); 00147 00148 moveamount.Scale(timefactor/fps); 00149 00150 if (freecam) 00151 cam.MoveRelative(moveamount.x, moveamount.y, moveamount.z); 00152 VERTEX campos = cam.GetPosition(); 00153 cam.SetPosition(campos); 00154 } 00155 00156 string KEYMAN::GetKeyName(int kc) 00157 { 00158 int i; 00159 for (i = 0; i < num_keyrecs; i++) 00160 { 00161 if (keyrec[i].key == kc) 00162 return keyrec[i].name; 00163 } 00164 00165 string numval; 00166 char tempchar[32]; 00167 sprintf(tempchar, "KeyCode%d", kc); 00168 //return "UNDEFINED"; 00169 numval = tempchar; 00170 return numval; 00171 }
1.4.6