Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

Key.cpp

00001 //Licence: LGPL 00002 //Copyright: Visti Andresen 00003 00004 #include "Key.h" 00005 00006 Key::Key(const string &label, const string &name, Font *font, 00007 SDLKey defaultValue) 00008 { 00009 font_ = font; 00010 name_ = name; 00011 label_ = label; 00012 grabbingKey_ = false; 00013 state_ = false; 00014 press_ = false; 00015 thisKey_ = defaultValue; 00016 } 00017 00018 Key::~Key() 00019 { 00020 } 00021 00022 void Key::load(ifstream &file) 00023 { 00024 if (findLoadPosition(name_, file)) 00025 { 00026 long value; 00027 file >> value; 00028 thisKey_ = (SDLKey)value; 00029 //cout << label_ << " value " << thisKey_ << endl; 00030 } 00031 } 00032 00033 void Key::save(ofstream &file) 00034 { 00035 file << name_ << "=" << (long)thisKey_ << endl; 00036 } 00037 00038 void Key::draw(float x, float y, float zoom) 00039 { 00040 font_->printString(getStringRepresentation(), x, y, zoom); 00041 } 00042 00043 float Key::getWidth(float zoom) 00044 { 00045 return font_->getWidth(getStringRepresentation(), zoom); 00046 } 00047 00048 float Key::getHeight(float zoom) 00049 { 00050 return font_->getHeight(zoom); 00051 } 00052 00053 string Key::getStringRepresentation() 00054 { 00055 string keyText="seeing this is a bug"; 00056 if (grabbingKey_){ 00057 keyText = "press new key"; 00058 } 00059 else { 00060 char buffer[2]; 00061 buffer[0] = (char)thisKey_; 00062 buffer[1] = 0; 00063 if (thisKey_ < 255 && font_->getWidth(buffer, 1)!=-1){ 00064 //font contains char 00065 keyText = buffer; 00066 } 00067 else { 00068 switch (thisKey_){ 00069 case SDLK_UP: 00070 keyText = "arrow up"; 00071 break; 00072 case SDLK_DOWN: 00073 keyText = "arrow down"; 00074 break; 00075 case SDLK_LEFT: 00076 keyText = "arrow left"; 00077 break; 00078 case SDLK_RIGHT: 00079 keyText = "arrow right"; 00080 break; 00081 case SDLK_LCTRL: 00082 keyText = "left control"; 00083 break; 00084 case SDLK_RCTRL: 00085 keyText = "right control"; 00086 break; 00087 case SDLK_LSHIFT: 00088 keyText = "left shift"; 00089 break; 00090 case SDLK_RSHIFT: 00091 keyText = "right shift"; 00092 break; 00093 case SDLK_LALT: 00094 keyText = "left alt"; 00095 break; 00096 case SDLK_RALT: 00097 keyText = "right alt"; 00098 break; 00099 case SDLK_F1: 00100 keyText = "f1"; 00101 break; 00102 case SDLK_F2: 00103 keyText = "f2"; 00104 break; 00105 case SDLK_F3: 00106 keyText = "f3"; 00107 break; 00108 case SDLK_F4: 00109 keyText = "f4"; 00110 break; 00111 case SDLK_F5: 00112 keyText = "f5"; 00113 break; 00114 case SDLK_F6: 00115 keyText = "f6"; 00116 break; 00117 case SDLK_F7: 00118 keyText = "f7"; 00119 break; 00120 case SDLK_F8: 00121 keyText = "f8"; 00122 break; 00123 case SDLK_F9: 00124 keyText = "f9"; 00125 break; 00126 case SDLK_F10: 00127 keyText = "f10"; 00128 break; 00129 case SDLK_F11: 00130 keyText = "f11"; 00131 break; 00132 case SDLK_F12: 00133 keyText = "f12"; 00134 break; 00135 case '\t': 00136 keyText = "tab"; 00137 break; 00138 case '\r': 00139 keyText = "return"; 00140 break; 00141 case '\b': 00142 keyText = "backspace"; 00143 break; 00144 case SDLK_DELETE: 00145 keyText = "delete"; 00146 break; 00147 case SDLK_KP0: 00148 keyText = "keypad 0"; 00149 break; 00150 case SDLK_KP1: 00151 keyText = "keypad 1"; 00152 break; 00153 case SDLK_KP2: 00154 keyText = "keypad 2"; 00155 break; 00156 case SDLK_KP3: 00157 keyText = "keypad 3"; 00158 break; 00159 case SDLK_KP4: 00160 keyText = "keypad 4"; 00161 break; 00162 case SDLK_KP5: 00163 keyText = "keypad 5"; 00164 break; 00165 case SDLK_KP6: 00166 keyText = "keypad 6"; 00167 break; 00168 case SDLK_KP7: 00169 keyText = "keypad 7"; 00170 break; 00171 case SDLK_KP8: 00172 keyText = "keypad 8"; 00173 break; 00174 case SDLK_KP9: 00175 keyText = "keypad 9"; 00176 break; 00177 case SDLK_KP_PERIOD: 00178 keyText = "keypad period"; 00179 break; 00180 case SDLK_KP_DIVIDE: 00181 keyText = "keypad devide"; 00182 break; 00183 case SDLK_KP_MULTIPLY: 00184 keyText = "keypad multiply"; 00185 break; 00186 case SDLK_KP_MINUS: 00187 keyText = "keypad minus"; 00188 break; 00189 case SDLK_KP_PLUS: 00190 keyText = "keypad plus"; 00191 break; 00192 case SDLK_KP_ENTER: 00193 keyText = "keypad enter"; 00194 break; 00195 case SDLK_KP_EQUALS: 00196 keyText = "keypad equals"; 00197 break; 00198 case SDLK_INSERT: 00199 keyText = "insert"; 00200 break; 00201 case SDLK_HOME: 00202 keyText = "home"; 00203 break; 00204 case SDLK_END: 00205 keyText = "end"; 00206 break; 00207 case SDLK_PAGEUP: 00208 keyText = "page up"; 00209 break; 00210 case SDLK_PAGEDOWN: 00211 keyText = "page down"; 00212 break; 00213 case SDLK_NUMLOCK: 00214 keyText = "numlock"; 00215 break; 00216 case SDLK_CAPSLOCK: 00217 keyText = "capslock"; 00218 break; 00219 case SDLK_SCROLLOCK: 00220 keyText = "scrollock"; 00221 break; 00222 case SDLK_RMETA: 00223 keyText = "right meta"; 00224 break; 00225 case SDLK_LMETA: 00226 keyText = "left meta"; 00227 break; 00228 case SDLK_RSUPER: 00229 keyText = "right windows"; 00230 break; 00231 case SDLK_LSUPER: 00232 keyText = "left windows"; 00233 break; 00234 case SDLK_MODE: 00235 keyText = "mode shift"; 00236 break; 00237 case SDLK_HELP: 00238 keyText = "help"; 00239 break; 00240 case SDLK_PRINT: 00241 keyText = "print screen"; 00242 break; 00243 case SDLK_SYSREQ: 00244 keyText = "sys req."; 00245 break; 00246 case SDLK_BREAK: 00247 keyText = "break"; 00248 break; 00249 case SDLK_MENU: 00250 keyText = "menu"; 00251 break; 00252 case SDLK_POWER: 00253 keyText = "power"; 00254 break; 00255 00256 default: 00257 keyText = "no graphics"; 00258 break; 00259 } 00260 } 00261 } 00262 string result = label_ + ": '"+keyText+"'"; 00263 return result; 00264 } 00265 00266 string &Key::getName() 00267 { 00268 return name_; 00269 } 00270 00271 bool Key::isPressed() 00272 { 00273 return state_; 00274 } 00275 00276 bool Key::keyPress() 00277 { 00278 bool result = press_; 00279 press_ = false; 00280 return result; 00281 } 00282 00283 void Key::handleGameKeyboardEvent(SDL_Event *event) 00284 { 00285 if (event->type == SDL_KEYDOWN && thisKey_ == event->key.keysym.sym){ 00286 state_=true; 00287 press_=true; 00288 } 00289 if (event->type == SDL_KEYUP && thisKey_ == event->key.keysym.sym){ 00290 state_=false; 00291 press_=false; 00292 } 00293 } 00294 00295 bool Key::handleKeyEvent(SDL_Event *event) 00296 { 00297 if (event->type != SDL_KEYDOWN) return false; 00298 if (grabbingKey_){ 00299 thisKey_ = event->key.keysym.sym; 00300 grabbingKey_ = false; 00301 return true; 00302 } 00303 else { 00304 if (event->key.keysym.sym == SDLK_RETURN){ 00305 grabbingKey_ = true; 00306 return true; 00307 } 00308 else { 00309 return false; 00310 } 00311 } 00312 }

Generated on Wed Sep 1 14:46:57 2004 by doxygen 1.3.7