home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / input / keyboard.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-08-12  |  13.6 KB  |  462 lines

  1. /***************************************************************************
  2.  * keyboard.cpp  -  keyboard handling class
  3.  *
  4.  * Copyright (C) 2006 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../core/game_core.h"
  17. #include "../core/main.h"
  18. #include "../input/keyboard.h"
  19. #include "../input/mouse.h"
  20. #include "../input/joystick.h"
  21. #include "../player/player.h"
  22. #include "../gui/menu.h"
  23. #include "../overworld/overworld.h"
  24. #include "../core/framerate.h"
  25. #include "../audio/audio.h"
  26. #include "../level/level.h"
  27. #include "../user/preferences.h"
  28. #include "../level/level_editor.h"
  29. #include "../level/level_settings.h"
  30. #include "../overworld/world_editor.h"
  31.  
  32. /* *** *** *** *** *** *** *** *** cKeyboard *** *** *** *** *** *** *** *** *** */
  33.  
  34. cKeyboard :: cKeyboard( void )
  35. {
  36.     Reset_Keys();
  37. }
  38.  
  39. cKeyboard :: ~cKeyboard( void )
  40. {
  41.  
  42. }
  43.  
  44. void cKeyboard :: Reset_Keys( void )
  45. {
  46.     // set all keys to 0
  47.     memset( keys, 0, sizeof( keys ) );
  48. }
  49.  
  50. bool cKeyboard :: CEGUI_Handle_Key_Up( SDLKey key )
  51. {
  52.     // inject the scancode directly
  53.     if( pGuiSystem->injectKeyUp( SDLKey_to_CEGUIKey( key ) ) )
  54.     {
  55.         // input was processed by the gui system
  56.         return 1;
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
  62. bool cKeyboard :: Key_Up( SDLKey key )
  63. {
  64.     // set key to 0
  65.     keys[key] = 0;
  66.  
  67.     // input was processed by the gui system
  68.     if( CEGUI_Handle_Key_Up( key ) )
  69.     {
  70.         return 1;
  71.     }
  72.  
  73.     // handle key in the current mode
  74.     if( Game_Mode == MODE_LEVEL )
  75.     {
  76.         // got processed
  77.         if( pActive_Level->Key_Up( key ) )
  78.         {
  79.             return 1;
  80.         }
  81.     }
  82.     else if( Game_Mode == MODE_MENU )
  83.     {
  84.         // got processed
  85.         if( pMenuCore->Key_Up( key ) )
  86.         {
  87.             return 1;
  88.         }
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94. bool cKeyboard :: CEGUI_Handle_Key_Down( SDLKey key )
  95. {
  96.     // inject the scancode
  97.     if( pGuiSystem->injectKeyDown( SDLKey_to_CEGUIKey( key ) ) == 1 )
  98.     {
  99.         // input got processed by the gui system
  100.         return 1;
  101.     }
  102.  
  103.     // use for translated unicode value
  104.     if( input_event.key.keysym.unicode != 0 )
  105.     {
  106.         if( pGuiSystem->injectChar( input_event.key.keysym.unicode ) )
  107.         {
  108.             // input got processed by the gui system
  109.             return 1;
  110.         }
  111.     }
  112.  
  113.     return 0;
  114. }
  115.  
  116. bool cKeyboard :: Key_Down( SDLKey key )
  117. {
  118.     // input was processed by the gui system
  119.     if( CEGUI_Handle_Key_Down( key ) )
  120.     {
  121.         return 1;
  122.     }
  123.  
  124.     // set key to 1
  125.     keys[key] = 1;
  126.  
  127.     // ## first the internal keys
  128.  
  129.     // game exit
  130.     if( key == SDLK_F4 && input_event.key.keysym.mod & KMOD_ALT )
  131.     {
  132.         game_exit = 1;
  133.         return 1;
  134.     }
  135.     // fullscreen toggle
  136.     else if( key == SDLK_RETURN && input_event.key.keysym.mod & KMOD_ALT )
  137.     {
  138.         pVideo->Toggle_Fullscreen();
  139.         return 1;
  140.     }
  141.     // GUI copy
  142.     else if( key == SDLK_c && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  143.     {
  144.         if( GUI_Copy_To_Clipboard() )
  145.         {
  146.             return 1;
  147.         }
  148.     }
  149.     // GUI cut
  150.     else if( key == SDLK_x && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  151.     {
  152.         if( GUI_Copy_To_Clipboard( 1 ) )
  153.         {
  154.             return 1;
  155.         }
  156.     }
  157.     // GUI paste
  158.     else if( key == SDLK_v && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  159.     {
  160.         if( GUI_Paste_From_Clipboard() )
  161.         {
  162.             return 1;
  163.         }
  164.     }
  165.  
  166.     // handle key in the current mode
  167.     if( Game_Mode == MODE_LEVEL )
  168.     {
  169.         // processed by the level
  170.         if( pActive_Level->Key_Down( key ) )
  171.         {
  172.             return 1;
  173.         }
  174.     }
  175.     else if( Game_Mode == MODE_OVERWORLD )
  176.     {
  177.         // processed by the overworld
  178.         if( pActive_Overworld->Key_Down( key ) )
  179.         {
  180.             return 1;
  181.         }
  182.     }
  183.     else if( Game_Mode == MODE_MENU )
  184.     {
  185.         // processed by the menu
  186.         if( pMenuCore->Key_Down( key ) )
  187.         {
  188.             return 1;
  189.         }
  190.     }
  191.     else if( Game_Mode == MODE_LEVEL_SETTINGS )
  192.     {
  193.         // processed by the level settings
  194.         if( pLevel_Editor->pSettings->Key_Down( key ) )
  195.         {
  196.             return 1;
  197.         }
  198.     }
  199.  
  200.     // set fixed speedfactor mode
  201.     if( key == SDLK_F6 )
  202.     {
  203.         float fixed_speedfactor = string_to_float( Box_Text_Input( float_to_string( pFramerate->force_speedfactor, 4 ), "Set Fixed Speedfactor", 1 ) );
  204.  
  205.         // disable fixed speedfactor mode
  206.         if( fixed_speedfactor <= 0 )
  207.         {
  208.             pFramerate->Set_Fixed_Speedfacor( 0 );
  209.             debugdisplay->Set_Text( "Fixed Speedfactor disabled" );
  210.         }
  211.         // enable fixed speedfactor mode
  212.         else
  213.         {
  214.             pFramerate->Set_Fixed_Speedfacor( fixed_speedfactor );
  215.             debugdisplay->Set_Text( "Fixed Speedfactor enabled" );
  216.         }
  217.     }
  218.     // take a screenshot
  219.     else if( key == SDLK_PRINT )
  220.     {
  221.         pVideo->Save_Screenshot();
  222.     }
  223.     // pause the game
  224.     else if( key == SDLK_PAUSE )
  225.     {
  226.         Draw_Static_Text( "Pause", &yellow, &lightgreyalpha64 );
  227.     }
  228.     // load a level
  229.     else if( key == SDLK_l && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) &&
  230.             !( Game_Mode == MODE_OVERWORLD && pOverworld_Manager->debugmode ) && Game_Mode != MODE_LEVEL_SETTINGS )
  231.     {
  232.         pLevel_Editor->Function_Load();
  233.     }
  234.     // load an overworld
  235.     else if( key == SDLK_w && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) &&
  236.             !( Game_Mode == MODE_OVERWORLD && pOverworld_Manager->debugmode ) && Game_Mode != MODE_LEVEL_SETTINGS )
  237.     {
  238.         pWorld_Editor->Function_Load();
  239.     }
  240.     // sound toggle
  241.     else if( key == SDLK_F10 )
  242.     {
  243.         pAudio->Toggle_Sounds();
  244.  
  245.         if( !pAudio->sound_enabled )
  246.         {
  247.             debugdisplay->Set_Text( "Sound Disabled" );
  248.         }
  249.         else
  250.         {
  251.             debugdisplay->Set_Text( "Sound Enabled" );
  252.         }
  253.     }
  254.     // music toggle
  255.     else if( key == SDLK_F11 )
  256.     {
  257.         pAudio->Toggle_Music();
  258.  
  259.         if( !pAudio->music_enabled )
  260.         {
  261.             debugdisplay->Set_Text( "Music Disabled" );
  262.         }
  263.         else
  264.         {
  265.             debugdisplay->Set_Text( "Music Enabled" );
  266.         }
  267.     }
  268.     // debug mode
  269.     else if( key == SDLK_d && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  270.     {
  271.         if( game_debug )
  272.         {
  273.             debugdisplay->Set_Text( "Debug mode disabled" );
  274.         }
  275.         else
  276.         {
  277.             pFramerate->fps_worst = 1000;
  278.             pFramerate->fps_best = 0;
  279.             debugdisplay->Set_Text( "Debug mode enabled" );
  280.         }
  281.  
  282.         game_debug = !game_debug;
  283.     }
  284.     // performance mode
  285.     else if( key == SDLK_p && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  286.     {
  287.         if( game_debug_performance )
  288.         {
  289.             debugdisplay->Set_Text( "Performance debug mode disabled" );
  290.         }
  291.         else
  292.         {
  293.             pFramerate->fps_worst = 1000;
  294.             pFramerate->fps_best = 0;
  295.             debugdisplay->Set_Text( "Performance debug mode  enabled" );
  296.         }
  297.  
  298.         game_debug_performance = !game_debug_performance;
  299.     }
  300.  
  301.     return 0;
  302. }
  303.  
  304. SDLKey *cKeyboard :: Get_Shortcut( input_identifier shortcut_id )
  305. {
  306.     if( shortcut_id == INP_UP )
  307.     {
  308.         return &pPreferences->key_up;
  309.     }
  310.     else if( shortcut_id == INP_DOWN )
  311.     {
  312.         return &pPreferences->key_down;
  313.     }
  314.     else if( shortcut_id == INP_LEFT )
  315.     {
  316.         return &pPreferences->key_left;
  317.     }
  318.     else if( shortcut_id == INP_RIGHT )
  319.     {
  320.         return &pPreferences->key_right;
  321.     }
  322.     else if( shortcut_id == INP_JUMP )
  323.     {
  324.         return &pPreferences->key_jump;
  325.     }
  326.     else if( shortcut_id == INP_SHOOT )
  327.     {
  328.         return &pPreferences->key_shoot; 
  329.     }
  330.     else if( shortcut_id == INP_ACTION )
  331.     {
  332.         return &pPreferences->key_action; 
  333.     }
  334.  
  335.     return NULL;
  336. }
  337.  
  338. void cKeyboard :: Assign_Shortcut( input_identifier shortcut_id, SDLKey new_key )
  339. {
  340.     SDLKey *key = Get_Shortcut( shortcut_id );
  341.     *key = new_key;
  342. }
  343.  
  344. unsigned int cKeyboard :: SDLKey_to_CEGUIKey( SDLKey key )
  345. {
  346.     switch( key )
  347.     {
  348.     case SDLK_BACKSPACE:    return CEGUI::Key::Backspace;
  349.     case SDLK_TAB:          return CEGUI::Key::Tab;
  350.     case SDLK_RETURN:       return CEGUI::Key::Return;
  351.     case SDLK_PAUSE:        return CEGUI::Key::Pause;
  352.     case SDLK_ESCAPE:       return CEGUI::Key::Escape;
  353.     case SDLK_SPACE:        return CEGUI::Key::Space;
  354.     case SDLK_COMMA:        return CEGUI::Key::Comma;
  355.     case SDLK_MINUS:        return CEGUI::Key::Minus;
  356.     case SDLK_PERIOD:       return CEGUI::Key::Period;
  357.     case SDLK_SLASH:        return CEGUI::Key::Slash;
  358.     case SDLK_0:            return CEGUI::Key::Zero;
  359.     case SDLK_1:            return CEGUI::Key::One;
  360.     case SDLK_2:            return CEGUI::Key::Two;
  361.     case SDLK_3:            return CEGUI::Key::Three;
  362.     case SDLK_4:            return CEGUI::Key::Four;
  363.     case SDLK_5:            return CEGUI::Key::Five;
  364.     case SDLK_6:            return CEGUI::Key::Six;
  365.     case SDLK_7:            return CEGUI::Key::Seven;
  366.     case SDLK_8:            return CEGUI::Key::Eight;
  367.     case SDLK_9:            return CEGUI::Key::Nine;
  368.     case SDLK_COLON:        return CEGUI::Key::Colon;
  369.     case SDLK_SEMICOLON:    return CEGUI::Key::Semicolon;
  370.     case SDLK_EQUALS:       return CEGUI::Key::Equals;
  371.     case SDLK_LEFTBRACKET:  return CEGUI::Key::LeftBracket;
  372.     case SDLK_BACKSLASH:    return CEGUI::Key::Backslash;
  373.     case SDLK_RIGHTBRACKET: return CEGUI::Key::RightBracket;
  374.     case SDLK_a:            return CEGUI::Key::A;
  375.     case SDLK_b:            return CEGUI::Key::B;
  376.     case SDLK_c:            return CEGUI::Key::C;
  377.     case SDLK_d:            return CEGUI::Key::D;
  378.     case SDLK_e:            return CEGUI::Key::E;
  379.     case SDLK_f:            return CEGUI::Key::F;
  380.     case SDLK_g:            return CEGUI::Key::G;
  381.     case SDLK_h:            return CEGUI::Key::H;
  382.     case SDLK_i:            return CEGUI::Key::I;
  383.     case SDLK_j:            return CEGUI::Key::J;
  384.     case SDLK_k:            return CEGUI::Key::K;
  385.     case SDLK_l:            return CEGUI::Key::L;
  386.     case SDLK_m:            return CEGUI::Key::M;
  387.     case SDLK_n:            return CEGUI::Key::N;
  388.     case SDLK_o:            return CEGUI::Key::O;
  389.     case SDLK_p:            return CEGUI::Key::P;
  390.     case SDLK_q:            return CEGUI::Key::Q;
  391.     case SDLK_r:            return CEGUI::Key::R;
  392.     case SDLK_s:            return CEGUI::Key::S;
  393.     case SDLK_t:            return CEGUI::Key::T;
  394.     case SDLK_u:            return CEGUI::Key::U;
  395.     case SDLK_v:            return CEGUI::Key::V;
  396.     case SDLK_w:            return CEGUI::Key::W;
  397.     case SDLK_x:            return CEGUI::Key::X;
  398.     case SDLK_y:            return CEGUI::Key::Y;
  399.     case SDLK_z:            return CEGUI::Key::Z;
  400.     case SDLK_DELETE:       return CEGUI::Key::Delete;
  401.     case SDLK_KP0:          return CEGUI::Key::Numpad0;
  402.     case SDLK_KP1:          return CEGUI::Key::Numpad1;
  403.     case SDLK_KP2:          return CEGUI::Key::Numpad2;
  404.     case SDLK_KP3:          return CEGUI::Key::Numpad3;
  405.     case SDLK_KP4:          return CEGUI::Key::Numpad4;
  406.     case SDLK_KP5:          return CEGUI::Key::Numpad5;
  407.     case SDLK_KP6:          return CEGUI::Key::Numpad6;
  408.     case SDLK_KP7:          return CEGUI::Key::Numpad7;
  409.     case SDLK_KP8:          return CEGUI::Key::Numpad8;
  410.     case SDLK_KP9:          return CEGUI::Key::Numpad9;
  411.     case SDLK_KP_PERIOD:    return CEGUI::Key::Decimal;
  412.     case SDLK_KP_DIVIDE:    return CEGUI::Key::Divide;
  413.     case SDLK_KP_MULTIPLY:  return CEGUI::Key::Multiply;
  414.     case SDLK_KP_MINUS:     return CEGUI::Key::Subtract;
  415.     case SDLK_KP_PLUS:      return CEGUI::Key::Add;
  416.     case SDLK_KP_ENTER:     return CEGUI::Key::NumpadEnter;
  417.     case SDLK_KP_EQUALS:    return CEGUI::Key::NumpadEquals;
  418.     case SDLK_UP:           return CEGUI::Key::ArrowUp;
  419.     case SDLK_DOWN:         return CEGUI::Key::ArrowDown;
  420.     case SDLK_RIGHT:        return CEGUI::Key::ArrowRight;
  421.     case SDLK_LEFT:         return CEGUI::Key::ArrowLeft;
  422.     case SDLK_INSERT:       return CEGUI::Key::Insert;
  423.     case SDLK_HOME:         return CEGUI::Key::Home;
  424.     case SDLK_END:          return CEGUI::Key::End;
  425.     case SDLK_PAGEUP:       return CEGUI::Key::PageUp;
  426.     case SDLK_PAGEDOWN:     return CEGUI::Key::PageDown;
  427.     case SDLK_F1:           return CEGUI::Key::F1;
  428.     case SDLK_F2:           return CEGUI::Key::F2;
  429.     case SDLK_F3:           return CEGUI::Key::F3;
  430.     case SDLK_F4:           return CEGUI::Key::F4;
  431.     case SDLK_F5:           return CEGUI::Key::F5;
  432.     case SDLK_F6:           return CEGUI::Key::F6;
  433.     case SDLK_F7:           return CEGUI::Key::F7;
  434.     case SDLK_F8:           return CEGUI::Key::F8;
  435.     case SDLK_F9:           return CEGUI::Key::F9;
  436.     case SDLK_F10:          return CEGUI::Key::F10;
  437.     case SDLK_F11:          return CEGUI::Key::F11;
  438.     case SDLK_F12:          return CEGUI::Key::F12;
  439.     case SDLK_F13:          return CEGUI::Key::F13;
  440.     case SDLK_F14:          return CEGUI::Key::F14;
  441.     case SDLK_F15:          return CEGUI::Key::F15;
  442.     case SDLK_NUMLOCK:      return CEGUI::Key::NumLock;
  443.     case SDLK_SCROLLOCK:    return CEGUI::Key::ScrollLock;
  444.     case SDLK_RSHIFT:       return CEGUI::Key::RightShift;
  445.     case SDLK_LSHIFT:       return CEGUI::Key::LeftShift;
  446.     case SDLK_RCTRL:        return CEGUI::Key::RightControl;
  447.     case SDLK_LCTRL:        return CEGUI::Key::LeftControl;
  448.     case SDLK_RALT:         return CEGUI::Key::RightAlt;
  449.     case SDLK_LALT:         return CEGUI::Key::LeftAlt;
  450.     case SDLK_LSUPER:       return CEGUI::Key::LeftWindows;
  451.     case SDLK_RSUPER:       return CEGUI::Key::RightWindows;
  452.     case SDLK_SYSREQ:       return CEGUI::Key::SysRq;
  453.     case SDLK_MENU:         return CEGUI::Key::AppMenu;
  454.     case SDLK_POWER:        return CEGUI::Key::Power;
  455.     default:                return 0;
  456.     }
  457. }
  458.  
  459. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  460.  
  461. cKeyboard *pKeyboard = NULL;
  462.