Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 2, 2008, 10:34:20 PM (17 years ago)
Author:
rgrieder
Message:

split KeyBinder into multiple files instead of one unreadable monster file

Location:
code/branches/input/src/core
Files:
10 added
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/core/CMakeLists.txt

    r1519 r1520  
    3131  TclThreadManager.cc
    3232  IRC.cc
     33
     34  input/Button.cc
     35  input/CalibratorCallback.cc
     36  input/HalfAxis.cc
    3337  input/InputBuffer.cc
     38  input/InputCommands.cc
    3439  input/InputManager.cc
    3540  input/KeyBinder.cc
     41  input/KeyDetector.cc
     42
    3643  tolua/tolua_bind.cc
    3744)
  • code/branches/input/src/core/CorePrereqs.h

    r1505 r1520  
    162162  // input
    163163  //class GUIInputHandler;
    164   class Calibrator;
     164  class BaseCommand;
     165  class BufferedParamCommand;
     166  class Button;
    165167  class CalibratorCallback;
     168  class HalfAxis;
    166169  class InputBuffer;
    167   class InputBufferListener;
    168170  class InputManager;
    169171  class JoyStickHandler;
     172  class MouseHandler;
    170173  class KeyBinder;
    171174  class KeyDetector;
    172   class MouseHandler;
    173 
     175  class KeyHandler;
     176  class ParamCommand;
     177  class SimpleCommand;
    174178}
    175179
  • code/branches/input/src/core/input/InputManager.cc

    r1519 r1520  
    3535#include "InputManager.h"
    3636
    37 #include "util/Convert.h"
     37#include <limits.h>
    3838#include "core/CoreIncludes.h"
    3939#include "core/ConfigValueIncludes.h"
     
    4141#include "core/CommandExecutor.h"
    4242#include "core/ConsoleCommand.h"
    43 #include "core/Shell.h"
     43#include "core/Shell.h"               // hack!
    4444#include "InputBuffer.h"
    4545#include "KeyBinder.h"
     46#include "KeyDetector.h"
     47#include "CalibratorCallback.h"
    4648
    4749namespace orxonox
  • code/branches/input/src/core/input/KeyBinder.cc

    r1519 r1520  
    3434#include "KeyBinder.h"
    3535#include <fstream>
    36 #include <limits.h>
     36#include <string>
    3737#include "util/Convert.h"
    38 #include "util/SubString.h"
    39 #include "util/String.h"
    4038#include "core/Debug.h"
    4139#include "core/ConfigValueIncludes.h"
    4240#include "core/CoreIncludes.h"
    43 #include "core/CommandExecutor.h"
    44 #include "core/ConsoleCommand.h"
    45 #include "core/Executor.h"
    46 // TODO: only needed by the CalibratorCallback class; move to new file
    47 #include "InputManager.h"
     41#include "InputCommands.h"
    4842
    4943namespace orxonox
    5044{
    51   // ###############################
    52   // ###  BufferedParamCommand   ###
    53   // ###############################
    54 
    55   /**
    56   * Executes a buffered command. This is used for commands with additional
    57   * parameters.
    58   * @return True if command execution was successful or value was zero.
    59   */
    60   bool BufferedParamCommand::execute()
    61   {
    62     if (nValuesAdded_)
    63     {
    64       BufferedParamCommand& cmd = *this;
    65       cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_);
    66       // reset
    67       cmd.nValuesAdded_ = 0;
    68       cmd.value_ = 0;
    69       return cmd.evaluation_.execute();
    70     }
    71     else
    72       return true;
    73   }
    74 
    75   // ###############################
    76   // #####    SimpleCommand    #####
    77   // ###############################
    78 
    79   /**
    80   * Executes a simple command with no additional paramters.
    81   * @return True if command execution was successful, false otherwise.
    82   */
    83   bool SimpleCommand::execute(float abs, float rel)
    84   {
    85     return evaluation_.execute();
    86   }
    87 
    88   // ###############################
    89   // #####    ParamCommand     #####
    90   // ###############################
    91 
    92   /**
    93   * Executes a parameter command. The commmand string is not directly executed,
    94   * but instead stored in a buffer list so that values can be combined.
    95   * @return Always true.
    96   */
    97   bool ParamCommand::execute(float abs, float rel)
    98   {
    99     BufferedParamCommand& cmd = *paramCommand_;
    100     // command has an additional parameter
    101     if (bRelative_)
    102     {
    103       if (rel != 0.0f)
    104       {
    105         // we have to calculate a relative movement.
    106         // paramModifier_ says how much one keystroke is
    107         cmd.value_ += paramModifier_ * rel;
    108       }
    109     }
    110     else if (abs != 0.0f)
    111     {
    112       // Usually, joy sticks create 'noise' (they return values if they're in 0 position)
    113       // and normally this is caught in tickInput(), but that threshold cannot be to high
    114       // in order to preserve accuracy. Instead, we have to catch the problem here. An example:
    115       // Someone only uses buttons with an active joystick. The joy stick value could then
    116       // be 0.05 for instance and the the key value 1. Without handling the problem, the final
    117       // value would be computed to (1+0.05)/2=0.5025 which is not what the user expects.
    118       float absQ = abs * abs;
    119       float valueQ = cmd.value_ * cmd.value_;
    120       if (absQ > 50.0f * valueQ) // ease up comparison by using quadratics
    121       {
    122         cmd.value_ = abs * paramModifier_;
    123         cmd.nValuesAdded_ = 1;
    124       }
    125       else if (absQ * 50.0f < valueQ)
    126       {
    127         // abs is too small, we just don't do anything
    128       }
    129       else
    130       {
    131         // we have to calculate the absolute position of the axis.
    132         // Since there might be another axis that is affected, we have to wait and
    133         // store the result in a temporary place
    134         cmd.value_ = (cmd.value_ * cmd.nValuesAdded_ + paramModifier_ * abs) / ++cmd.nValuesAdded_;
    135       }
    136     }
    137     return true;
    138   }
    139 
    140   // ###############################
    141   // #####       Button        #####
    142   // ###############################
    143 
    144   void Button::clear()
    145   {
    146     for (unsigned int j = 0; j < 3; j++)
    147     {
    148       if (nCommands_[j])
    149       {
    150         // delete all commands and the command pointer array
    151         for (unsigned int i = 0; i < nCommands_[j]; i++)
    152           delete commands_[j][i];
    153         delete[] commands_[j];
    154         commands_[j] = 0;
    155         nCommands_[j] = 0;
    156       }
    157       else
    158       {
    159         commands_[j] = 0;
    160       }
    161     }
    162   }
    163 
    164   void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer)
    165   {
    166     if (isEmpty(bindingString_))
    167     {
    168       clear();
    169       return;
    170     }
    171 
    172     // use std::vector for a temporary dynamic array
    173     std::vector<BaseCommand*> commands[3];
    174 
    175 
    176     // separate the commands
    177     SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false,
    178         '\\', false, '"', false, '(', ')', false, '\0');
    179 
    180     for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++)
    181     {
    182       if (commandStrings[iCommand] != "")
    183       {
    184         SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false,
    185             '\\', false, '"', false, '(', ')', false, '\0');
    186 
    187         unsigned int iToken = 0;
    188 
    189         // for real axes, we can feed a ButtonThreshold argument as entire command
    190         if (getLowercase(tokens[0]) == "buttonthreshold")
    191         {
    192           if (tokens.size() == 1)
    193             continue;
    194           // may fail, but doesn't matter
    195           convertValue(&buttonThreshold_, tokens[1]);
    196           continue;
    197         }
    198 
    199         // first argument can be OnPress, OnHold OnRelease or nothing
    200         KeybindMode::Enum mode = KeybindMode::None;
    201         if (getLowercase(tokens[iToken]) == "onpress")
    202           mode = KeybindMode::OnPress,   iToken++;
    203         if (getLowercase(tokens[iToken]) == "onrelease")
    204           mode = KeybindMode::OnRelease, iToken++;
    205         if (getLowercase(tokens[iToken]) == "onhold")
    206           mode = KeybindMode::OnHold,    iToken++;
    207 
    208         if (iToken == tokens.size())
    209           continue;
    210 
    211         // second argument can be the amplitude for the case it as an axis command
    212         // default amplitude is 1.0f
    213         float paramModifier = 1.0f;
    214         if (getLowercase(tokens[iToken]) == "scale")
    215         {
    216           iToken++;
    217           if (iToken == tokens.size() || !convertValue(&paramModifier, tokens[iToken]))
    218           {
    219             COUT(2) << "Error while parsing key binding " << name_
    220                 << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl;
    221             if (iToken == tokens.size())
    222               continue;
    223           }
    224           iToken++;
    225         }
    226 
    227         // no more arguments expected except for the actual command
    228         if (iToken == tokens.size())
    229           continue;
    230 
    231         std::string commandStr;
    232         while (iToken != tokens.size())
    233           commandStr += tokens[iToken++] + " ";
    234 
    235         // evaluate the command
    236         CommandEvaluation eval = CommandExecutor::evaluate(commandStr);
    237         if (!eval.isValid())
    238           continue;
    239 
    240         // check for param command
    241         int paramIndex = eval.getConsoleCommand()->getAxisParamIndex();
    242         if (paramIndex >= 0)
    243         {
    244           // parameter supported command
    245           ParamCommand* cmd = new ParamCommand();
    246           cmd->paramModifier_ = paramModifier;
    247           cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative();
    248 
    249           // add command to the buffer if not yet existing
    250           for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++)
    251           {
    252             if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getOriginalCommand())
    253                 == getLowercase(commandStr))
    254             {
    255               // already in list
    256               cmd->paramCommand_ = paramCommandBuffer[iParamCmd];
    257               break;
    258             }
    259           }
    260           if (cmd->paramCommand_ == 0)
    261           {
    262             cmd->paramCommand_ = new BufferedParamCommand();
    263             paramCommandBuffer.push_back(cmd->paramCommand_);
    264             cmd->paramCommand_->evaluation_ = eval;
    265             cmd->paramCommand_->paramIndex_ = paramIndex;
    266           }
    267 
    268 
    269           // we don't know whether this is an actual axis or just a button
    270           if (mode == KeybindMode::None)
    271           {
    272             if (!addParamCommand(cmd))
    273             {
    274               mode = eval.getConsoleCommand()->getKeybindMode();
    275               commands[mode].push_back(cmd);
    276             }
    277           }
    278         }
    279         else
    280         {
    281           SimpleCommand* cmd = new SimpleCommand();
    282           cmd->evaluation_ = eval;
    283 
    284           if (mode == KeybindMode::None)
    285             mode = eval.getConsoleCommand()->getKeybindMode();
    286 
    287           commands[mode].push_back(cmd);
    288         }
    289       }
    290     }
    291 
    292     for (unsigned int j = 0; j < 3; j++)
    293     {
    294       nCommands_[j] = commands[j].size();
    295       if (nCommands_[j])
    296       {
    297         commands_[j] = new BaseCommand*[nCommands_[j]];
    298         for (unsigned int i = 0; i < commands[j].size(); i++)
    299           commands_[j][i] = commands[j][i];
    300       }
    301       else
    302         commands_[j] = 0;
    303     }
    304   }
    305 
    306   bool Button::execute(KeybindMode::Enum mode, float abs, float rel)
    307   {
    308     // execute all the parsed commands in the string
    309     for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++)
    310       commands_[mode][iCommand]->execute(abs, rel);
    311     return true;
    312   }
    313 
    314   // ###############################
    315   // #####      HalfAxis       #####
    316   // ###############################
    317 
    318   void HalfAxis::clear()
    319   {
    320     Button::clear();
    321     if (nParamCommands_)
    322     {
    323       // delete all commands and the command pointer array
    324       for (unsigned int i = 0; i < nParamCommands_; i++)
    325         delete paramCommands_[i];
    326       delete[] paramCommands_;
    327       nParamCommands_ = 0;
    328     }
    329     else
    330     {
    331       nParamCommands_ = 0; nParamCommands_ = 0;
    332     }
    333   }
    334 
    335   bool HalfAxis::addParamCommand(ParamCommand* command)
    336   {
    337     ParamCommand** cmds = paramCommands_;
    338     paramCommands_ = new ParamCommand*[++nParamCommands_];
    339     unsigned int i;
    340     for (i = 0; i < nParamCommands_ - 1; i++)
    341       paramCommands_[i] = cmds[i];
    342     paramCommands_[i] = command;
    343     if (nParamCommands_ > 1)
    344       delete[] cmds;
    345     return true;
    346   }
    347 
    348   bool HalfAxis::execute()
    349   {
    350     bool success = true;
    351     for (unsigned int i = 0; i < nParamCommands_; i++)
    352       success = success && paramCommands_[i]->execute(absVal_, relVal_);
    353     return success;
    354   }
    355 
    356 
    357   // ###############################
    358   // ######     KeyBinder     ######
    359   // ###############################
    360 
    36145  /**
    36246    @brief Constructor that does as little as necessary.
     
    854538    }
    855539  }
    856 
    857 
    858   // ###############################
    859   // #####     KeyDetector     #####
    860   // ###############################
    861 
    862   /**
    863     @brief Constructor
    864   */
    865   KeyDetector::KeyDetector()
    866   {
    867     RegisterObject(KeyDetector);
    868   }
    869 
    870   /**
    871     @brief Destructor
    872   */
    873   KeyDetector::~KeyDetector()
    874   {
    875   }
    876 
    877   /**
    878     @brief Loads the key and button bindings.
    879     @return True if loading succeeded.
    880   */
    881   void KeyDetector::loadBindings()
    882   {
    883     clearBindings();
    884     setConfigValues();
    885   }
    886 
    887   void KeyDetector::readTrigger(Button& button)
    888   {
    889     SimpleCommand* cmd = new SimpleCommand();
    890     cmd->evaluation_ = CommandExecutor::evaluate("storeKeyStroke " + button.name_);
    891     button.commands_[KeybindMode::OnPress] = new BaseCommand*[1];
    892     button.commands_[KeybindMode::OnPress][0] = cmd;
    893     button.nCommands_[KeybindMode::OnPress] = 1;
    894   }
    895 
    896 
    897   // ###############################
    898   // ##### CalibratorCallback  #####
    899   // ###############################
    900 
    901   void CalibratorCallback::keyPressed(const orxonox::KeyEvent &evt)
    902   {
    903     if (evt.key == KeyCode::Return)
    904     {
    905       InputManager::setInputState(InputManager::IS_NOCALIBRATE);
    906     }
    907   }
    908540}
  • code/branches/input/src/core/input/KeyBinder.h

    r1519 r1520  
    3737#include "core/CorePrereqs.h"
    3838
    39 #include <string>
    4039#include <vector>
    4140
    42 #include "ois/OIS.h"
    43 #include "util/Math.h"
    4441#include "core/OrxonoxClass.h"
    45 #include "core/CommandEvaluation.h"
    4642#include "InputInterfaces.h"
     43#include "Button.h"
     44#include "HalfAxis.h"
    4745
    4846namespace orxonox
    4947{
    50   class _CoreExport BufferedParamCommand
    51   {
    52   public:
    53     BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { }
    54     bool execute();
    55 
    56     float value_;
    57     unsigned int nValuesAdded_;
    58     int paramIndex_;
    59     CommandEvaluation evaluation_;
    60   };
    61 
    62   class _CoreExport BaseCommand
    63   {
    64   public:
    65     virtual ~BaseCommand() { }
    66     virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;
    67   };
    68 
    69   class _CoreExport SimpleCommand : public BaseCommand
    70   {
    71   public:
    72     bool execute(float abs = 1.0f, float rel = 1.0f);
    73 
    74     CommandEvaluation evaluation_;
    75   };
    76 
    77   class _CoreExport ParamCommand : public BaseCommand
    78   {
    79   public:
    80     ParamCommand() : bRelative_(false), paramModifier_(1.0f), paramCommand_(0) { }
    81     bool execute(float abs = 1.0f, float rel = 1.0f);
    82 
    83     bool bRelative_;
    84     float paramModifier_;
    85     BufferedParamCommand* paramCommand_;
    86   };
    87 
    88   class _CoreExport Button
    89   {
    90   public:
    91     Button() { nCommands_[0]=0; nCommands_[1]=0; nCommands_[2]=0; clear(); }
    92     virtual ~Button() { clear(); }
    93     virtual void clear();
    94     virtual bool addParamCommand(ParamCommand* command) { return false; }
    95     void parse(std::vector<BufferedParamCommand*>& paramCommandBuffer);
    96     bool execute(KeybindMode::Enum mode, float abs = 1.0f, float rel = 1.0f);
    97 
    98     //! The configured string value
    99     std::string bindingString_;
    100     //! Name of the trigger as strings
    101     std::string name_;
    102     //! Basic commands for OnPress, OnHold and OnRelease
    103     BaseCommand** commands_[3];
    104     //! Number of basic commands
    105     unsigned int nCommands_[3];
    106     //! Says how much it takes for an analog axis to trigger a button
    107     //! Note: This variable is here to have only one parse() function.
    108     float buttonThreshold_;
    109   };
    110 
    111 
    112   class _CoreExport HalfAxis : public Button
    113   {
    114   public:
    115     HalfAxis() : relVal_(0.0f), absVal_(0.0f), paramCommands_(0), nParamCommands_(0),
    116                  wasDown_(false), hasChanged_(false) { }
    117     using Button::execute;
    118     bool execute();
    119     //bool execute(KeybindMode::Enum mode) { return Button::execute(mode); }
    120     bool addParamCommand(ParamCommand* command);
    121     void clear();
    122 
    123     // axis related
    124     float relVal_;
    125     float absVal_;
    126     ParamCommand** paramCommands_;
    127     unsigned int nParamCommands_;
    128 
    129     // button related
    130     bool wasDown_;
    131     bool hasChanged_;
    132   };
    133 
    134 
    13548  /**
    13649    @brief Handles mouse, keyboard and joy stick input while in the actual game mode.
     
    226139    bool bClipMouse_;
    227140  };
    228 
    229 
    230   class _CoreExport KeyDetector : public KeyBinder
    231   {
    232   public:
    233     KeyDetector();
    234     ~KeyDetector();
    235     void loadBindings();
    236 
    237   protected:
    238     void readTrigger(Button& button);
    239   };
    240 
    241   class _CoreExport CalibratorCallback : public KeyHandler
    242   {
    243   public:
    244     CalibratorCallback() {}
    245     ~CalibratorCallback() {}
    246 
    247   private:
    248     void keyPressed (const KeyEvent& evt);
    249     void keyReleased(const KeyEvent& evt) {}
    250     void keyHeld    (const KeyEvent& evt) {}
    251 
    252     void tickInput(float dt, const HandlerState &state) { }
    253   };
    254141}
    255142
    256 
    257 
    258 
    259143#endif /* _KeyBinder_H__ */
Note: See TracChangeset for help on using the changeset viewer.