home *** CD-ROM | disk | FTP | other *** search
- #include "Input.h"
-
- #include "Display.h"
- #include "ConsoleFrontEnd.h"
- #include "log.h"
- #include "Tokenizer.h"
- #include "collision.h" // fⁿr castRay()
- #include "Sound.h" // fⁿr playSound
- #include "Game.h"
- #include "Gui.h"
- #include "Network.h"
-
- const char* Input::keyNames[SDLK_LAST];
- const char* Input::actionNames[NUM_ACTIONS];
-
- mouse_t Input::mouse;
- keyMap_t Input::keyMap;
- inputArray_t Input::inputArray;
-
- bool Input::initialized = false;
-
- bool Input::init(){
- if(initialized){
- error("(in Input::init()): Input is already initialized.\n\n");
- return false;
- }
-
- if(Gui::loadingMenu!=NULL)
- Gui::loadingMenu->updateStatusBar("initializing input");
-
- log("\n");
- log("**********************************\n");
- log("*** Initializing Input Devices ***\n");
- log("**********************************\n");
- log("\n");
-
- int i;
-
- initKeyNames();
- initActionNames();
-
- for(i=0;i<NUM_ACTIONS;i++){
- inputArray[i].pressed=false;
-
- }
- for(i=0;i<SDLK_LAST;i++){
- keyMap[i].action=NO_ACTION;
- keyMap[i].consoleString=NULL;
- keyMap[i].pressed=false;
- }
- log("initializing keyboard... done.\n");
-
-
- for(i=0;i<NUM_MOUSE_BUTTONS;i++){
- mouse.buttons[i].action=NO_ACTION;
- mouse.buttons[i].consoleString=NULL;
- mouse.buttons[i].pressed=false;
- }
- /*
- hideMouseCursor();
- grabInput();
- */
- log("initializing mouse... done.\n");
-
- initialized=true;
-
- return true;
- }
-
- bool Input::shutdown(){
- int i;
-
- if(!initialized){
- error("(in Input::shutdown()): Input is not initialized.\n\n");
- return false;
- }
-
- log("\n");
- log("===================================\n");
- log("=== Shutting down input devices ===\n");
- log("===================================\n");
- log("\n");
-
- showMouseCursor();
- freeInput();
-
- for(i=0;i<SDLK_LAST;i++){
- if(keyMap[i].consoleString!=NULL){
- delete[] keyMap[i].consoleString;
- keyMap[i].consoleString=NULL;
- }
- }
-
- for(i=0;i<NUM_MOUSE_BUTTONS;i++){
- //inp.mouse.buttons[i].action=NO_ACTION;
- if(mouse.buttons[i].consoleString!=NULL){
- delete[] mouse.buttons[i].consoleString;
- mouse.buttons[i].consoleString=NULL;
- }
- }
-
- initialized=false;
-
- return true;
- }
-
- bool Input::wasInit(){
- return initialized;
- }
-
- bool Input::registerCVarsAndCCmds(){
- return info.registerCVarsAndCCmds();
- }
-
- bool Input::unregisterCVarsAndCCmds(){
- return info.unregisterCVarsAndCCmds();
- }
-
-
-
-
- void Input::handleKeyboardEvent(SDL_KeyboardEvent* event){
- SDLKey sym=event->keysym.sym;
-
- //printf("sym: %d, scancode: %d, unicode: %d, mod: %d\n",sym, sym, sym, sym);
-
- if(event->type==SDL_KEYDOWN){
- keyMap[sym].pressed=true;
-
- if(keyMap[sym].action==TOGGLE_CONSOLE){
- if(consoleFrontEnd->isActive)
- consoleFrontEnd->deactivate();
- else
- consoleFrontEnd->activate();
- return;
- }
- if(consoleFrontEnd->isActive){
- consoleFrontEnd->receiveKey(&event->keysym);
- return; // actions and parsing are skipped when console is active!
- }
- if(Gui::hud->chatPrompt->isActive){
- Gui::hud->chatPrompt->receiveKey(&event->keysym);
- return;
- }
-
-
- if(keyMap[sym].action!=NO_ACTION){
- inputArray[keyMap[sym].action].pressed=true;
- }else if(keyMap[sym].consoleString!=NULL){
- console->parse(keyMap[sym].consoleString);
- }else{
- }
-
- }else if(event->type==SDL_KEYUP){
- keyMap[sym].pressed=false;
- if(keyMap[sym].action!=NO_ACTION){
- inputArray[keyMap[sym].action].pressed=false;
- }
- }
-
- }
-
- void Input::handleMouseMotionEvent(SDL_MouseMotionEvent* event){
- // printf("MOUSE MOTION: xrel: %i, yRel: %i\n", event->xrel, event->yrel);
- mouse.x=event->x;
- mouse.y=event->y;
-
-
- float xRel = event->xrel*mouse.sensitivity*Display::info.var.scaleX;
- float yRel = event->yrel*mouse.sensitivity*Display::info.var.scaleX;
-
- if( (Game::cam.mode == CAMERA_MODE_FIRST_PERSON || Game::cam.mode == CAMERA_MODE_THIRD_PERSON) ){
- if( Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
- if(xRel<0.0f)
- Network::client->vehicle->turnLeft(xRel* -0.0001f);
- if(xRel>0.0f)
- Network::client->vehicle->turnRight(xRel*0.0001f);
-
- // turn up/down
- if(yRel<0.0f)
- Network::client->vehicle->turnUp(yRel*-0.0001f);
- if(yRel>0.0f)
- Network::client->vehicle->turnDown(yRel* 0.0001f);
- }else{ // following someone else or dead
- // turn left/right
- if(xRel<0.0f)
- Game::cam.turnLeft(xRel* -0.0001f);
- if(xRel>0.0f)
- Game::cam.turnRight(xRel*0.0001f);
-
- // turn up/down
- if(yRel<0.0f)
- Game::cam.turnUp(yRel*-0.0001f);
- if(yRel>0.0f)
- Game::cam.turnDown(yRel* 0.0001f);
-
- }
- }else{
- // turn left/right
- if(xRel<0.0f)
- Game::cam.turnLeft(xRel* -0.0001f);
- if(xRel>0.0f)
- Game::cam.turnRight(xRel*0.0001f);
-
- // turn up/down
- if(yRel<0.0f)
- Game::cam.turnUp(yRel*-0.0001f);
- if(yRel>0.0f)
- Game::cam.turnDown(yRel* 0.0001f);
- }
-
- }
-
- void Input::handleMouseButtonEvent(SDL_MouseButtonEvent* event){
- // printf("MOUSE BUTTON: %i\n", event->button);
- mouse.x=event->x;
- mouse.y=event->y;
-
- int button=event->button; // THINKABOUTME: extra was fⁿr mausrad fummeln??
-
- if(event->type==SDL_MOUSEBUTTONDOWN){
- mouse.buttons[button].pressed=true;
- if(mouse.buttons[button].action!=NO_ACTION){
- inputArray[mouse.buttons[button].action].pressed=true;
- }else if(mouse.buttons[button].consoleString!=NULL){
- console->parse(mouse.buttons[button].consoleString);
- }
-
- }else if(event->type==SDL_MOUSEBUTTONUP){
- mouse.buttons[button].pressed=false;
- if(mouse.buttons[button].action!=NO_ACTION){
- inputArray[mouse.buttons[button].action].pressed=false;
- }
- }
- }
-
- void Input::showMouseCursor(){
- SDL_ShowCursor(SDL_ENABLE);
- }
- void Input::hideMouseCursor(){
- SDL_ShowCursor(SDL_DISABLE);
- }
- void Input::grabInput(){
- SDL_WM_GrabInput(SDL_GRAB_ON);
- }
- void Input::freeInput(){
- SDL_WM_GrabInput(SDL_GRAB_OFF);
- }
-
-
-
-
-
- void Input::processInputArray(){
- /* OLD VERSION
- if(Game::cam.mode == CAMERA_MODE_FIRST_PERSON
- && Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
- Network::client->vehicle->processInputArray(inputArray);
- }else{
- Game::cam.processInputArray(inputArray);
- }
- */
- // always process cam input
- Game::cam.processInputArray(inputArray);
-
- // process vehicle input if first person and cam.target == player
- if( (Game::cam.mode == CAMERA_MODE_FIRST_PERSON || Game::cam.mode == CAMERA_MODE_THIRD_PERSON)
- && Network::client->vehicle != NULL && Game::cam.target == Network::client->vehicle ){
- Network::client->vehicle->processInputArray(inputArray);
- }
- }
-
-
- void Input::initKeyNames(){
- for(int i=0;i<SDLK_LAST;i++){
- keyNames[i] = "<unknown key>";
- }
-
- keyNames[SDLK_ESCAPE] = "escape";
- keyNames[SDLK_F1] = "f1";
- keyNames[SDLK_F2] = "f2";
- keyNames[SDLK_F3] = "f3";
- keyNames[SDLK_F4] = "f4";
- keyNames[SDLK_F5] = "f5";
- keyNames[SDLK_F6] = "f6";
- keyNames[SDLK_F7] = "f7";
- keyNames[SDLK_F8] = "f8";
- keyNames[SDLK_F9] = "f9";
- keyNames[SDLK_F10] = "f10";
- keyNames[SDLK_F11] = "f11";
- keyNames[SDLK_F12] = "f12";
-
- keyNames[SDLK_CARET] = "^";
- keyNames[SDLK_1] = "1";
- keyNames[SDLK_2] = "2";
- keyNames[SDLK_3] = "3";
- keyNames[SDLK_4] = "4";
- keyNames[SDLK_5] = "5";
- keyNames[SDLK_6] = "6";
- keyNames[SDLK_7] = "7";
- keyNames[SDLK_8] = "8";
- keyNames[SDLK_9] = "9";
- keyNames[SDLK_0] = "0";
- keyNames[SDLK_BACKQUOTE] = "`";
- keyNames[SDLK_BACKSPACE] = "backspace";
- keyNames[SDLK_TAB] = "tab";
- keyNames[SDLK_PLUS] = "plus";
- keyNames[SDLK_MINUS] = "-";
- keyNames[SDLK_ASTERISK] = "*";
- keyNames[SDLK_SLASH] = "/";
- keyNames[SDLK_PERIOD] = ".";
- keyNames[SDLK_COMMA] = ",";
- keyNames[SDLK_HASH] = "#";
- keyNames[SDLK_RETURN] = "enter";
-
- keyNames[SDLK_LSHIFT] = "lshift";
- keyNames[SDLK_LCTRL] = "lctrl";
- keyNames[SDLK_LALT] = "lalt";
- keyNames[SDLK_RSHIFT] = "rshift";
- keyNames[SDLK_RCTRL] = "rctrl";
- keyNames[SDLK_RALT] = "ralt";
- keyNames[SDLK_SPACE] = "space";
-
- keyNames[SDLK_INSERT] = "insert";
- keyNames[SDLK_DELETE] = "delete";
- keyNames[SDLK_HOME] = "home";
- keyNames[SDLK_END] = "end";
- keyNames[SDLK_PAGEUP] = "page_up";
- keyNames[SDLK_PAGEDOWN] = "page_down";
-
- keyNames[SDLK_LEFT] = "left";
- keyNames[SDLK_RIGHT] = "right";
- keyNames[SDLK_UP] = "up";
- keyNames[SDLK_DOWN] = "down";
-
- keyNames[SDLK_KP0] = "kp_0";
- keyNames[SDLK_KP1] = "kp_1";
- keyNames[SDLK_KP2] = "kp_2";
- keyNames[SDLK_KP3] = "kp_3";
- keyNames[SDLK_KP4] = "kp_4";
- keyNames[SDLK_KP5] = "kp_5";
- keyNames[SDLK_KP6] = "kp_6";
- keyNames[SDLK_KP7] = "kp_7";
- keyNames[SDLK_KP8] = "kp_8";
- keyNames[SDLK_KP9] = "kp_9";
- keyNames[SDLK_KP_PERIOD] = "kp_period";
- keyNames[SDLK_KP_ENTER] = "kp_enter";
- keyNames[SDLK_KP_PLUS] = "kp_plus";
- keyNames[SDLK_KP_MINUS] = "kp_minus";
- keyNames[SDLK_KP_MULTIPLY] = "kp_multiply";
- keyNames[SDLK_KP_DIVIDE] = "kp_divide";
-
- keyNames[SDLK_a] = "a";
- keyNames[SDLK_b] = "b";
- keyNames[SDLK_c] = "c";
- keyNames[SDLK_d] = "d";
- keyNames[SDLK_e] = "e";
- keyNames[SDLK_f] = "f";
- keyNames[SDLK_g] = "g";
- keyNames[SDLK_h] = "h";
- keyNames[SDLK_i] = "i";
- keyNames[SDLK_j] = "j";
- keyNames[SDLK_k] = "k";
- keyNames[SDLK_l] = "l";
- keyNames[SDLK_m] = "m";
- keyNames[SDLK_n] = "n";
- keyNames[SDLK_o] = "o";
- keyNames[SDLK_p] = "p";
- keyNames[SDLK_q] = "q";
- keyNames[SDLK_r] = "r";
- keyNames[SDLK_s] = "s";
- keyNames[SDLK_t] = "t";
- keyNames[SDLK_u] = "u";
- keyNames[SDLK_v] = "v";
- keyNames[SDLK_w] = "w";
- keyNames[SDLK_x] = "x";
- keyNames[SDLK_y] = "y";
- keyNames[SDLK_z] = "z";
- }
-
- void Input::initActionNames(){
- for(int i=0;i<NUM_ACTIONS;i++){
- actionNames[i] = "<unknown action>";
- }
-
- actionNames[NO_ACTION] = "no_action";
- actionNames[MOVE_FORWARD] = "move_forward";
- actionNames[MOVE_BACKWARD] = "move_backward";
- actionNames[MOVE_LEFT] = "move_left";
- actionNames[MOVE_RIGHT] = "move_right";
- actionNames[MOVE_UP] = "move_up";
- actionNames[MOVE_DOWN] = "move_down";
-
- actionNames[TURN_LEFT] = "turn_left";
- actionNames[TURN_RIGHT] = "turn_right";
- actionNames[TURN_UP] = "turn_up";
- actionNames[TURN_DOWN] = "turn_down";
-
- actionNames[FIRE_WEAPON_1] = "fire_weapon_1";
- actionNames[FIRE_WEAPON_2] = "fire_weapon_2";
- actionNames[FIRE_WEAPON_3] = "fire_weapon_3";
- actionNames[FIRE_WEAPON_4] = "fire_weapon_4";
-
- actionNames[CAMERA_ZOOM_IN] = "camera_zoom_in";
- actionNames[CAMERA_ZOOM_OUT] = "camera_zoom_out";
- actionNames[CAMERA_ROTATE_LEFT] = "camera_rotate_left";
- actionNames[CAMERA_ROTATE_RIGHT] = "camera_rotate_right";
- actionNames[CAMERA_ROTATE_UP] = "camera_rotate_up";
- actionNames[CAMERA_ROTATE_DOWN] = "camera_rotate_down";
- actionNames[CAMERA_RESET] = "camera_reset";
-
- actionNames[TOGGLE_CONSOLE] = "toggle_console";
-
- }
-
- int Input::getMouseButtonForString(char* str){
- if(streq(str, "button1") || streq(str, "mouse1") || streq(str, "mouse_leftbutton")){
- return 1;
- }else if(streq(str, "button2") || streq(str, "mouse2") || streq(str, "mouse_middlebutton")){
- return 2;
- }else if(streq(str, "button3") || streq(str, "mouse3") || streq(str, "mouse_rightbutton")){
- return 3;
- }else if(streq(str, "button4") || streq(str, "mouse4") || streq(str, "mouse_wheelup")){
- return 4;
- }else if(streq(str, "button5") || streq(str, "mouse5") || streq(str, "mouse_wheeldown")){
- return 5;
- }else{
- return -1;
- }
- }
-
- int Input::getKeyForString(char* str){
- for(int i=0;i<SDLK_LAST;i++){
- if( streq(keyNames[i], str) ){
- return i;
- }
- }
-
- return -1;
-
- }
-
- int Input::getActionForString(char* str){
- for(int i=0;i<NUM_ACTIONS;i++){
- if( streq(actionNames[i], str) ){
- return i;
- }
- }
-
- return -1;
-
- }
-
-
- const char* Input::getKeyName(int keysym){
- return keyNames[keysym];
- }
-
- const char* Input::getActionName(int action){
- return actionNames[action];
- }
-