home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Input.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-16  |  11.6 KB  |  462 lines

  1. #include "Input.h"
  2.  
  3. #include "Display.h"
  4. #include "ConsoleFrontEnd.h"
  5. #include "log.h"
  6. #include "Tokenizer.h"
  7. #include "collision.h" // fⁿr castRay()
  8. #include "Sound.h"    // fⁿr playSound
  9. #include "Game.h"
  10. #include "Gui.h"
  11. #include "Network.h"
  12.  
  13. const char* Input::keyNames[SDLK_LAST];
  14. const char* Input::actionNames[NUM_ACTIONS];
  15.  
  16. mouse_t Input::mouse;
  17. keyMap_t Input::keyMap;
  18. inputArray_t Input::inputArray;
  19.  
  20. bool Input::initialized = false;
  21.  
  22. bool Input::init(){
  23.     if(initialized){
  24.         error("(in Input::init()): Input is already initialized.\n\n");
  25.         return false;
  26.     }
  27.  
  28.     if(Gui::loadingMenu!=NULL)
  29.         Gui::loadingMenu->updateStatusBar("initializing input");
  30.  
  31.     log("\n");
  32.     log("**********************************\n");
  33.     log("*** Initializing Input Devices ***\n");
  34.     log("**********************************\n");
  35.     log("\n");
  36.  
  37.     int i;
  38.  
  39.     initKeyNames();
  40.     initActionNames();
  41.  
  42.     for(i=0;i<NUM_ACTIONS;i++){
  43.         inputArray[i].pressed=false;
  44.  
  45.     }
  46.     for(i=0;i<SDLK_LAST;i++){
  47.         keyMap[i].action=NO_ACTION;
  48.         keyMap[i].consoleString=NULL;
  49.         keyMap[i].pressed=false;
  50.     }
  51.     log("initializing keyboard... done.\n");
  52.  
  53.  
  54.     for(i=0;i<NUM_MOUSE_BUTTONS;i++){
  55.         mouse.buttons[i].action=NO_ACTION;
  56.         mouse.buttons[i].consoleString=NULL;
  57.         mouse.buttons[i].pressed=false;
  58.     }
  59. /*
  60.     hideMouseCursor();
  61.     grabInput();
  62.  */
  63.     log("initializing mouse... done.\n");
  64.  
  65.     initialized=true;
  66.  
  67.     return true;
  68. }
  69.  
  70. bool Input::shutdown(){
  71.     int i;
  72.  
  73.     if(!initialized){
  74.         error("(in Input::shutdown()): Input is not initialized.\n\n");
  75.         return false;
  76.     }
  77.  
  78.     log("\n");
  79.     log("===================================\n");
  80.     log("=== Shutting down input devices ===\n");
  81.     log("===================================\n");
  82.     log("\n");
  83.  
  84.     showMouseCursor();
  85.     freeInput();
  86.  
  87.     for(i=0;i<SDLK_LAST;i++){
  88.         if(keyMap[i].consoleString!=NULL){
  89.             delete[] keyMap[i].consoleString;
  90.             keyMap[i].consoleString=NULL;
  91.         }
  92.     }
  93.     
  94.     for(i=0;i<NUM_MOUSE_BUTTONS;i++){
  95.         //inp.mouse.buttons[i].action=NO_ACTION;
  96.         if(mouse.buttons[i].consoleString!=NULL){
  97.             delete[] mouse.buttons[i].consoleString;
  98.             mouse.buttons[i].consoleString=NULL;
  99.         }
  100.     }
  101.  
  102.     initialized=false;
  103.  
  104.     return true;
  105. }
  106.  
  107. bool Input::wasInit(){
  108.     return initialized;
  109. }
  110.  
  111. bool Input::registerCVarsAndCCmds(){
  112.     return info.registerCVarsAndCCmds();
  113. }
  114.  
  115. bool Input::unregisterCVarsAndCCmds(){
  116.     return info.unregisterCVarsAndCCmds();
  117. }
  118.  
  119.  
  120.  
  121.  
  122. void Input::handleKeyboardEvent(SDL_KeyboardEvent* event){
  123.     SDLKey sym=event->keysym.sym;
  124.  
  125.     //printf("sym: %d, scancode: %d, unicode: %d, mod: %d\n",sym, sym, sym, sym);
  126.  
  127.     if(event->type==SDL_KEYDOWN){
  128.         keyMap[sym].pressed=true;
  129.  
  130.         if(keyMap[sym].action==TOGGLE_CONSOLE){
  131.             if(consoleFrontEnd->isActive)
  132.                 consoleFrontEnd->deactivate();
  133.             else
  134.                 consoleFrontEnd->activate();
  135.             return;
  136.         }
  137.         if(consoleFrontEnd->isActive){
  138.             consoleFrontEnd->receiveKey(&event->keysym);
  139.             return;        // actions and parsing are skipped when console is active!
  140.         }
  141.         if(Gui::hud->chatPrompt->isActive){
  142.             Gui::hud->chatPrompt->receiveKey(&event->keysym);
  143.             return;
  144.         }
  145.         
  146.  
  147.         if(keyMap[sym].action!=NO_ACTION){
  148.             inputArray[keyMap[sym].action].pressed=true;
  149.         }else if(keyMap[sym].consoleString!=NULL){
  150.             console->parse(keyMap[sym].consoleString);
  151.         }else{
  152.         }
  153.  
  154.     }else if(event->type==SDL_KEYUP){
  155.         keyMap[sym].pressed=false;
  156.         if(keyMap[sym].action!=NO_ACTION){
  157.             inputArray[keyMap[sym].action].pressed=false;
  158.         }
  159.     }
  160.  
  161. }
  162.  
  163. void Input::handleMouseMotionEvent(SDL_MouseMotionEvent* event){
  164. //    printf("MOUSE MOTION: xrel: %i, yRel: %i\n", event->xrel, event->yrel);
  165.     mouse.x=event->x;
  166.     mouse.y=event->y;
  167.  
  168.  
  169.     float xRel = event->xrel*mouse.sensitivity*Display::info.var.scaleX;
  170.     float yRel = event->yrel*mouse.sensitivity*Display::info.var.scaleX;
  171.  
  172.     if( (Game::cam.mode == CAMERA_MODE_FIRST_PERSON || Game::cam.mode == CAMERA_MODE_THIRD_PERSON) ){
  173.         if( Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
  174.             if(xRel<0.0f)
  175.                 Network::client->vehicle->turnLeft(xRel* -0.0001f);
  176.             if(xRel>0.0f)
  177.                 Network::client->vehicle->turnRight(xRel*0.0001f);
  178.  
  179.             // turn up/down
  180.             if(yRel<0.0f)
  181.                 Network::client->vehicle->turnUp(yRel*-0.0001f);
  182.             if(yRel>0.0f)
  183.                 Network::client->vehicle->turnDown(yRel* 0.0001f);
  184.         }else{    // following someone else or dead
  185.         // turn left/right
  186.             if(xRel<0.0f)
  187.                 Game::cam.turnLeft(xRel* -0.0001f);
  188.             if(xRel>0.0f)
  189.                 Game::cam.turnRight(xRel*0.0001f);
  190.  
  191.             // turn up/down
  192.             if(yRel<0.0f)
  193.                 Game::cam.turnUp(yRel*-0.0001f);
  194.             if(yRel>0.0f)
  195.                 Game::cam.turnDown(yRel* 0.0001f);
  196.  
  197.         }
  198.     }else{
  199.         // turn left/right
  200.         if(xRel<0.0f)
  201.             Game::cam.turnLeft(xRel* -0.0001f);
  202.         if(xRel>0.0f)
  203.             Game::cam.turnRight(xRel*0.0001f);
  204.  
  205.         // turn up/down
  206.         if(yRel<0.0f)
  207.             Game::cam.turnUp(yRel*-0.0001f);
  208.         if(yRel>0.0f)
  209.             Game::cam.turnDown(yRel* 0.0001f);
  210.     }
  211.  
  212. }
  213.  
  214. void Input::handleMouseButtonEvent(SDL_MouseButtonEvent* event){
  215. //    printf("MOUSE BUTTON: %i\n", event->button);
  216.     mouse.x=event->x;
  217.     mouse.y=event->y;
  218.  
  219.     int button=event->button;    // THINKABOUTME: extra was fⁿr mausrad fummeln??
  220.  
  221.     if(event->type==SDL_MOUSEBUTTONDOWN){
  222.         mouse.buttons[button].pressed=true;
  223.         if(mouse.buttons[button].action!=NO_ACTION){
  224.             inputArray[mouse.buttons[button].action].pressed=true;
  225.         }else if(mouse.buttons[button].consoleString!=NULL){
  226.             console->parse(mouse.buttons[button].consoleString);
  227.         }
  228.  
  229.     }else if(event->type==SDL_MOUSEBUTTONUP){
  230.         mouse.buttons[button].pressed=false;
  231.         if(mouse.buttons[button].action!=NO_ACTION){
  232.             inputArray[mouse.buttons[button].action].pressed=false;
  233.         }
  234.     }
  235. }
  236.  
  237. void Input::showMouseCursor(){
  238.     SDL_ShowCursor(SDL_ENABLE);
  239. }
  240. void Input::hideMouseCursor(){
  241.     SDL_ShowCursor(SDL_DISABLE);
  242. }
  243. void Input::grabInput(){
  244.     SDL_WM_GrabInput(SDL_GRAB_ON);
  245. }
  246. void Input::freeInput(){
  247.     SDL_WM_GrabInput(SDL_GRAB_OFF);
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254. void Input::processInputArray(){
  255. /* OLD VERSION
  256.     if(Game::cam.mode == CAMERA_MODE_FIRST_PERSON
  257.             && Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
  258.         Network::client->vehicle->processInputArray(inputArray);
  259.     }else{
  260.         Game::cam.processInputArray(inputArray);
  261.     }
  262. */
  263.     // always process cam input
  264.     Game::cam.processInputArray(inputArray);
  265.  
  266.     // process vehicle input if first person and cam.target == player
  267.     if( (Game::cam.mode == CAMERA_MODE_FIRST_PERSON || Game::cam.mode == CAMERA_MODE_THIRD_PERSON)
  268.             && Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
  269.         Network::client->vehicle->processInputArray(inputArray);
  270.     }
  271. }
  272.  
  273.  
  274. void Input::initKeyNames(){
  275.     for(int i=0;i<SDLK_LAST;i++){
  276.         keyNames[i] = "<unknown key>";
  277.     }
  278.  
  279.     keyNames[SDLK_ESCAPE] = "escape";
  280.     keyNames[SDLK_F1] = "f1";
  281.     keyNames[SDLK_F2] = "f2";
  282.     keyNames[SDLK_F3] = "f3";
  283.     keyNames[SDLK_F4] = "f4";
  284.     keyNames[SDLK_F5] = "f5";
  285.     keyNames[SDLK_F6] = "f6";
  286.     keyNames[SDLK_F7] = "f7";
  287.     keyNames[SDLK_F8] = "f8";
  288.     keyNames[SDLK_F9] = "f9";
  289.     keyNames[SDLK_F10] = "f10";
  290.     keyNames[SDLK_F11] = "f11";
  291.     keyNames[SDLK_F12] = "f12";
  292.  
  293.     keyNames[SDLK_CARET] = "^";
  294.     keyNames[SDLK_1] = "1";
  295.     keyNames[SDLK_2] = "2";
  296.     keyNames[SDLK_3] = "3";
  297.     keyNames[SDLK_4] = "4";
  298.     keyNames[SDLK_5] = "5";
  299.     keyNames[SDLK_6] = "6";
  300.     keyNames[SDLK_7] = "7";
  301.     keyNames[SDLK_8] = "8";
  302.     keyNames[SDLK_9] = "9";
  303.     keyNames[SDLK_0] = "0";
  304.     keyNames[SDLK_BACKQUOTE] = "`";
  305.     keyNames[SDLK_BACKSPACE] = "backspace";
  306.     keyNames[SDLK_TAB] = "tab";
  307.     keyNames[SDLK_PLUS] = "plus";
  308.     keyNames[SDLK_MINUS] = "-";
  309.     keyNames[SDLK_ASTERISK] = "*";
  310.     keyNames[SDLK_SLASH] = "/";
  311.     keyNames[SDLK_PERIOD] = ".";
  312.     keyNames[SDLK_COMMA] = ",";
  313.     keyNames[SDLK_HASH] = "#";
  314.     keyNames[SDLK_RETURN] = "enter";
  315.  
  316.     keyNames[SDLK_LSHIFT] = "lshift";
  317.     keyNames[SDLK_LCTRL] = "lctrl";
  318.     keyNames[SDLK_LALT] = "lalt";
  319.     keyNames[SDLK_RSHIFT] = "rshift";
  320.     keyNames[SDLK_RCTRL] = "rctrl";
  321.     keyNames[SDLK_RALT] = "ralt";
  322.     keyNames[SDLK_SPACE] = "space";
  323.  
  324.     keyNames[SDLK_INSERT] = "insert";
  325.     keyNames[SDLK_DELETE] = "delete";
  326.     keyNames[SDLK_HOME] = "home";
  327.     keyNames[SDLK_END] = "end";
  328.     keyNames[SDLK_PAGEUP] = "page_up";
  329.     keyNames[SDLK_PAGEDOWN] = "page_down";
  330.  
  331.     keyNames[SDLK_LEFT] = "left";
  332.     keyNames[SDLK_RIGHT] = "right";
  333.     keyNames[SDLK_UP] = "up";
  334.     keyNames[SDLK_DOWN] = "down";
  335.  
  336.     keyNames[SDLK_KP0] = "kp_0";
  337.     keyNames[SDLK_KP1] = "kp_1";
  338.     keyNames[SDLK_KP2] = "kp_2";
  339.     keyNames[SDLK_KP3] = "kp_3";
  340.     keyNames[SDLK_KP4] = "kp_4";
  341.     keyNames[SDLK_KP5] = "kp_5";
  342.     keyNames[SDLK_KP6] = "kp_6";
  343.     keyNames[SDLK_KP7] = "kp_7";
  344.     keyNames[SDLK_KP8] = "kp_8";
  345.     keyNames[SDLK_KP9] = "kp_9";
  346.     keyNames[SDLK_KP_PERIOD] = "kp_period";
  347.     keyNames[SDLK_KP_ENTER] = "kp_enter";
  348.     keyNames[SDLK_KP_PLUS] = "kp_plus";
  349.     keyNames[SDLK_KP_MINUS] = "kp_minus";
  350.     keyNames[SDLK_KP_MULTIPLY] = "kp_multiply";
  351.     keyNames[SDLK_KP_DIVIDE] = "kp_divide";
  352.  
  353.     keyNames[SDLK_a] = "a";
  354.     keyNames[SDLK_b] = "b";
  355.     keyNames[SDLK_c] = "c";
  356.     keyNames[SDLK_d] = "d";
  357.     keyNames[SDLK_e] = "e";
  358.     keyNames[SDLK_f] = "f";
  359.     keyNames[SDLK_g] = "g";
  360.     keyNames[SDLK_h] = "h";
  361.     keyNames[SDLK_i] = "i";
  362.     keyNames[SDLK_j] = "j";
  363.     keyNames[SDLK_k] = "k";
  364.     keyNames[SDLK_l] = "l";
  365.     keyNames[SDLK_m] = "m";
  366.     keyNames[SDLK_n] = "n";
  367.     keyNames[SDLK_o] = "o";
  368.     keyNames[SDLK_p] = "p";
  369.     keyNames[SDLK_q] = "q";
  370.     keyNames[SDLK_r] = "r";
  371.     keyNames[SDLK_s] = "s";
  372.     keyNames[SDLK_t] = "t";
  373.     keyNames[SDLK_u] = "u";
  374.     keyNames[SDLK_v] = "v";
  375.     keyNames[SDLK_w] = "w";
  376.     keyNames[SDLK_x] = "x";
  377.     keyNames[SDLK_y] = "y";
  378.     keyNames[SDLK_z] = "z";
  379. }
  380.  
  381. void Input::initActionNames(){
  382.     for(int i=0;i<NUM_ACTIONS;i++){
  383.         actionNames[i] = "<unknown action>";
  384.     }
  385.  
  386.     actionNames[NO_ACTION] = "no_action";
  387.     actionNames[MOVE_FORWARD] = "move_forward";
  388.     actionNames[MOVE_BACKWARD] = "move_backward";
  389.     actionNames[MOVE_LEFT] = "move_left";
  390.     actionNames[MOVE_RIGHT] = "move_right";
  391.     actionNames[MOVE_UP] = "move_up";
  392.     actionNames[MOVE_DOWN] = "move_down";
  393.  
  394.     actionNames[TURN_LEFT] = "turn_left";
  395.     actionNames[TURN_RIGHT] = "turn_right";
  396.     actionNames[TURN_UP] = "turn_up";
  397.     actionNames[TURN_DOWN] = "turn_down";
  398.  
  399.     actionNames[FIRE_WEAPON_1] = "fire_weapon_1";
  400.     actionNames[FIRE_WEAPON_2] = "fire_weapon_2";
  401.     actionNames[FIRE_WEAPON_3] = "fire_weapon_3";
  402.     actionNames[FIRE_WEAPON_4] = "fire_weapon_4";
  403.  
  404.     actionNames[CAMERA_ZOOM_IN] = "camera_zoom_in";
  405.     actionNames[CAMERA_ZOOM_OUT] = "camera_zoom_out";
  406.     actionNames[CAMERA_ROTATE_LEFT] = "camera_rotate_left";
  407.     actionNames[CAMERA_ROTATE_RIGHT] = "camera_rotate_right";
  408.     actionNames[CAMERA_ROTATE_UP] = "camera_rotate_up";
  409.     actionNames[CAMERA_ROTATE_DOWN] = "camera_rotate_down";
  410.     actionNames[CAMERA_RESET] = "camera_reset";
  411.  
  412.     actionNames[TOGGLE_CONSOLE] = "toggle_console";
  413.  
  414. }
  415.  
  416. int Input::getMouseButtonForString(char* str){
  417.     if(streq(str, "button1") || streq(str, "mouse1") || streq(str, "mouse_leftbutton")){
  418.         return 1;
  419.     }else if(streq(str, "button2") || streq(str, "mouse2") || streq(str, "mouse_middlebutton")){
  420.         return 2;
  421.     }else if(streq(str, "button3") || streq(str, "mouse3") || streq(str, "mouse_rightbutton")){
  422.         return 3;
  423.     }else if(streq(str, "button4") || streq(str, "mouse4") || streq(str, "mouse_wheelup")){
  424.         return 4;
  425.     }else if(streq(str, "button5") || streq(str, "mouse5") || streq(str, "mouse_wheeldown")){
  426.         return 5;
  427.     }else{
  428.         return -1;
  429.     }
  430. }
  431.  
  432. int Input::getKeyForString(char* str){
  433.     for(int i=0;i<SDLK_LAST;i++){
  434.         if( streq(keyNames[i], str) ){
  435.             return i;
  436.         }
  437.     }
  438.     
  439.     return -1;
  440.  
  441. }
  442.  
  443. int Input::getActionForString(char* str){
  444.     for(int i=0;i<NUM_ACTIONS;i++){
  445.         if( streq(actionNames[i], str) ){
  446.             return i;
  447.         }
  448.     }
  449.     
  450.     return -1;
  451.  
  452. }
  453.  
  454.  
  455. const char* Input::getKeyName(int keysym){
  456.     return keyNames[keysym];
  457. }
  458.  
  459. const char* Input::getActionName(int action){
  460.     return actionNames[action];
  461. }
  462.