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

  1. #include "InputCCmds.h"
  2.  
  3. #include "Input.h"
  4. #include "Tokenizer.h"
  5.  
  6. CCmdInputBind::CCmdInputBind():CCmd("input.bind"){
  7.     usageStr="input.bind [key] [action]";
  8.     infoStr="binds a action to a key";
  9. }
  10.  
  11. CCmdInputBind::~CCmdInputBind(){
  12.     if(console!=NULL)
  13.         console->unregisterCCmd(this);
  14. }
  15.  
  16. bool CCmdInputBind::exec(int argc, char* argv[]){
  17.     if(argc==2){
  18.         int key=Input::getKeyForString(argv[0]);
  19.         if(key==-1){    // no key -> try mouseButton
  20.             int button=Input::getMouseButtonForString(argv[0]);
  21.             if(button==-1){    // no button -> failure
  22.                 console->print("'%s' is not a valid key\n", argv[0]);
  23.                 return false;
  24.             }else{
  25.  
  26.                 int action=Input::getActionForString(argv[1]);
  27.  
  28.                 if(action!=-1){
  29.                     Input::mouse.buttons[button].action=(action_t)action;
  30.                     console->print("%s bound to %s.\n", argv[1], argv[0]);
  31.                     return true;
  32.                 }else{
  33.                     Input::mouse.buttons[button].consoleString=newString(argv[1]);    // deleted in shutdownInput()
  34.                     console->print("%s bound to %s.\n", argv[1], argv[0]);
  35.                     return true;
  36.                 }
  37.  
  38.             }
  39.         }
  40.  
  41.         int action=Input::getActionForString(argv[1]);
  42.  
  43.         if(action!=-1){
  44.             Input::keyMap[key].action=(action_t)action;
  45.             console->print("%s bound to %s.\n", argv[1], argv[0]);
  46.             return true;
  47.         }else{
  48.             Input::keyMap[key].consoleString=newString(argv[1]);
  49.             console->print("%s bound to %s.\n", argv[1], argv[0]);
  50.             return true;
  51.         }
  52.     }else{
  53.         console->print("usage: %s\n", usageStr);
  54.         return false;
  55.     }
  56. }
  57.  
  58. CCmdInputUnbind::CCmdInputUnbind():CCmd("input.unbind"){
  59.     usageStr="input.unbind [key] or input.unbind all";
  60.     infoStr="unbinds a key/mousebutton (or all keys)";
  61. }
  62.  
  63. CCmdInputUnbind::~CCmdInputUnbind(){
  64.     if(console!=NULL)
  65.         console->unregisterCCmd(this);
  66. }
  67.  
  68. bool CCmdInputUnbind::exec(int argc, char* argv[]){
  69.     if(argc==1){
  70.         if(streq(argv[0], "all")){
  71.             int i;
  72.             for(i=0;i<SDLK_LAST;i++){    // unbind all keys
  73.                 Input::keyMap[i].action=NO_ACTION;
  74.                 if(Input::keyMap[i].consoleString!=NULL){
  75.                     delete[] Input::keyMap[i].consoleString;
  76.                     Input::keyMap[i].consoleString=NULL;
  77.                 }
  78.             }
  79.             for(i=0;i<NUM_MOUSE_BUTTONS;i++){    // and all mouse buttons
  80.                 Input::mouse.buttons[i].action=NO_ACTION;
  81.                 if(Input::mouse.buttons[i].consoleString!=NULL){
  82.                     delete[] Input::mouse.buttons[i].consoleString;
  83.                     Input::mouse.buttons[i].consoleString=NULL;
  84.                 }
  85.             }
  86.             console->print("ALL unbound.\n");
  87.             return true;
  88.         }
  89.  
  90.         // unbind ONE key/button
  91.         int key=Input::getKeyForString(argv[0]);
  92.         if(key==-1){    // no key -> try mouseButton
  93.             int button=Input::getMouseButtonForString(argv[0]);
  94.             if(button==-1){    // no button -> failure
  95.                 console->print("'%s' is not a valid key\n", argv[0]);
  96.                 return false;
  97.             }else{
  98.                 if(Input::mouse.buttons[button].action!=NO_ACTION){
  99.                     Input::mouse.buttons[button].action=NO_ACTION;
  100.                 }
  101.                 if(Input::mouse.buttons[button].consoleString!=NULL){
  102.                     delete[] Input::mouse.buttons[button].consoleString;
  103.                     Input::mouse.buttons[button].consoleString=NULL;
  104.                 }
  105.                 console->print("%s unbound.\n", argv[0]);
  106.                 return true;
  107.             }
  108.         }else{    // unbind key
  109.             Input::keyMap[key].action=NO_ACTION;
  110.             if(Input::keyMap[key].consoleString!=NULL){
  111.                 delete[] Input::keyMap[key].consoleString;
  112.                 Input::keyMap[key].consoleString=NULL;
  113.             }
  114.             console->print("%s unbound.\n", argv[0]);
  115.             return true;
  116.         }
  117.     }else{
  118.         console->print("usage: %s\n", usageStr);
  119.         return false;
  120.     }
  121. }
  122.  
  123.  
  124. CCmdInputBindlist::CCmdInputBindlist():CCmd("input.bindlist"){
  125.     usageStr="input.bindlist";
  126.     infoStr="gives a list with all current bindings";
  127. }
  128.  
  129. CCmdInputBindlist::~CCmdInputBindlist(){
  130.     if(console!=NULL)
  131.         console->unregisterCCmd(this);
  132. }
  133.  
  134. bool CCmdInputBindlist::exec(int argc, char* argv[]){
  135.     if(argc==0){
  136.         console->print("This will be included in the next release...\n");    // TODO
  137.         return true;
  138.     }else{
  139.         console->print("usage: %s\n", usageStr);
  140.         return false;
  141.     }
  142. }
  143.  
  144. CCmdInputGrabMouse::CCmdInputGrabMouse():CCmd("input.grabMouse"){
  145.     usageStr="input.grabMouse";
  146.     infoStr="grabs input focus";
  147. }
  148.  
  149. CCmdInputGrabMouse::~CCmdInputGrabMouse(){
  150.     if(console!=NULL)
  151.         console->unregisterCCmd(this);
  152. }
  153.  
  154. bool CCmdInputGrabMouse::exec(int argc, char* argv[]){
  155.     if(argc==0){
  156.         Input::grabInput();
  157.         Input::hideMouseCursor();
  158.         return true;
  159.     }else{
  160.         console->print("usage: %s\n", usageStr);
  161.         return false;
  162.     }
  163. }
  164.  
  165. CCmdInputFreeMouse::CCmdInputFreeMouse():CCmd("input.freeMouse"){
  166.     usageStr="input.freeMouse";
  167.     infoStr="frees input focus";
  168. }
  169.  
  170. CCmdInputFreeMouse::~CCmdInputFreeMouse(){
  171.     if(console!=NULL)
  172.         console->unregisterCCmd(this);
  173. }
  174.  
  175. bool CCmdInputFreeMouse::exec(int argc, char* argv[]){
  176.     if(argc==0){
  177.         Input::freeInput();
  178.         Input::showMouseCursor();
  179.         return true;
  180.     }else{
  181.         console->print("usage: %s\n", usageStr);
  182.         return false;
  183.     }
  184. }
  185.  
  186.  
  187.