Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1344 for code/branches


Ignore:
Timestamp:
May 21, 2008, 6:26:15 PM (17 years ago)
Author:
rgrieder
Message:
  • added scroll wheel support
  • improved mouse input handling
  • added support for mouse input derivation (allows to use the track stick like a joy stick)

About the new key binder in general:
You can assign any button/key/axis any combination of command strings the way you want.
When using simple commands (shortcuts) without parameters depending on the input, there are 3 keybind modes: OnPress, OnRelease and OnHold. For the case you assign such a command to a mouse/joystick axis, the input is translated to switch-like behavior (can be configured by feeding ButtonThreshold argument as ONE key binding).
With parameter controlled commands, keys are simply 1 or 0, axis are of course continuous. There are two ways a command can expect a value: relative or absolute input. For instance the joystick axis state (-1 to 1) is an absolute value, mouse movement is relative. Once again, everything is translated in the right way.
There's a lot more to explain, but I guess I'm gonna write a wiki page for that…

one last note: I haven't yet changed the way input is handled in the space ship. I shall do that after the merge tomorrow.

Location:
code/branches/input
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/bin/keybindings.ini

    r1340 r1344  
    154154MouseButton6=
    155155MouseButton7=
    156 MouseXPos=exit | ButtonThreshold 0.85
     156MouseWheel1Up=AxisAmp 0.1 slomo
     157MouseWheel1Down=AxisAmp 0.1 slomo
     158MouseWheel2Up=
     159MouseWheel2Down=
     160MouseXPos= #exit | ButtonThreshold 0.85
    157161MouseXNeg=
    158162MouseYPos=
    159163MouseYNeg=
    160 MouseWheel1Pos=
    161 MouseWheel1Neg=
    162 MouseWheel2Pos=
    163 MouseWheel2Neg=
     164Empty1Pos=
     165Empty1Neg=
     166Empty2Pos=
     167Empty2Neg=
    164168JoyAxis1Pos=
    165169JoyAxis1Neg=
  • code/branches/input/src/core/InputHandler.cc

    r1340 r1344  
    7272    BufferedParamCommand& paramCommand = *paramCommand_;
    7373    // command has an additional parameter
    74     if (bRelative_)
     74    if (bRelative_ && (rel > 0 || rel < 0))
    7575    {
    7676      // we have to calculate a relative movement.
    77       // amplitude says how much one keystroke is
     77      // paramModifier_ says how much one keystroke is
    7878      paramCommand.value_ += paramModifier_ * rel;
    7979    }
    80     else
     80    else if (abs > 0 || abs < 0)
    8181    {
    8282      // we have to calculate absolute position of the axis.
    83       // for a key this simply is 1, but multiplied by a user defined factor
    84       // since there might be another axis that is affected, we have to wait and
     83      // Since there might be another axis that is affected, we have to wait and
    8584      // store the result in a temporary place
    8685      paramCommand.value_ = (paramCommand.value_ * paramCommand.nValuesAdded_ + paramModifier_ * abs)
     
    254253  }
    255254
    256   bool Button::execute(KeybindMode::Enum mode)
     255  bool Button::execute(KeybindMode::Enum mode, float abs, float rel)
    257256  {
    258257    // execute all the parsed commands in the string
    259258    for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++)
    260       commands_[mode][iCommand]->execute();
     259      commands_[mode][iCommand]->execute(abs, rel);
    261260    return true;
    262261  }
     
    306305    @brief Constructor that does as little as necessary.
    307306  */
    308   KeyBinder::KeyBinder()
     307  KeyBinder::KeyBinder() : deriveTime_(0.0f)
    309308  {
    310309    RegisterObject(KeyBinder);
     
    413412      "MouseLeft", "MouseRight", "MouseMiddle",
    414413      "MouseButton3", "MouseButton4", "MouseButton5",
    415       "MouseButton6", "MouseButton7" };
     414      "MouseButton6", "MouseButton7",
     415      "MouseWheel1Up", "MouseWheel1Down",
     416      "MouseWheel2Up", "MouseWheel2Down" };
    416417    for (int i = 0; i < nMouseButtons_s; i++)
    417418      mouseButtons_[i].name_ = mouseButtonNames[i];
     
    432433    rawNames[0] = "MouseX";
    433434    rawNames[1] = "MouseY";
    434     rawNames[2] = "MouseWheel1";
    435     rawNames[3] = "MouseWheel2";
     435    rawNames[2] = "Empty1";
     436    rawNames[3] = "Empty2";
    436437    for (unsigned int i = 4; i < nHalfAxes_s/2; i++)
    437438      rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3);
     
    475476  void KeyBinder::setConfigValues()
    476477  {
    477     SetConfigValue(analogThreshold_, 0.01f).description("Threshold for analog axes until which the state is 0.");
     478    SetConfigValue(analogThreshold_, 0.01f)  .description("Threshold for analog axes until which the state is 0.");
     479    SetConfigValue(bDeriveMouseInput_, false).description("Whether or not to derive moues movement for the absolute value.");
     480    SetConfigValue(derivePeriod_, 0.1f)      .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
     481    SetConfigValue(mouseSensitivity_, 1.0f)  .description("Mouse sensitivity.");
     482
    478483    float oldThresh = buttonThreshold_;
    479484    SetConfigValue(buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed.");
     
    573578    }
    574579
     580    if (bDeriveMouseInput_)
     581    {
     582      if (deriveTime_ > derivePeriod_)
     583      {
     584        deriveTime_ = 0.0f;
     585        CCOUT(3) << "mouse abs: ";
     586        for (int i = 0; i < 2; i++)
     587        {
     588          if (mouseRelative_[i] > 0)
     589          {
     590            halfAxes_[2*i + 0].absVal_ = mouseRelative_[i] * derivePeriod_ / 500;
     591            halfAxes_[2*i + 1].absVal_ = 0.0f;
     592          }
     593          else if (mouseRelative_[0] < 0)
     594          {
     595            halfAxes_[2*i + 0].absVal_ = 0.0f;
     596            halfAxes_[2*i + 1].absVal_ = -mouseRelative_[i] * derivePeriod_ / 500;
     597          }
     598          COUT(3) << mouseRelative_[i] << " | ";
     599          mouseRelative_[i] = 0.0f;
     600        }
     601        COUT(3) << std::endl;
     602      }
     603      else
     604        deriveTime_ += dt;
     605    }
     606
    575607    // execute all buffered bindings (addional parameter)
    576608    for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
     
    578610
    579611    // always reset the relative movement of the mouse
    580     for (unsigned int i = 0; i < 4; i++)
    581       halfAxes_[i].relVal_ = 0;
     612    for (unsigned int i = 0; i < 8; i++)
     613      halfAxes_[i].relVal_ = 0.0f;
    582614  }
    583615
     
    615647    @param e Mouse state information
    616648  */
    617   void KeyBinder::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
    618   {
    619     // translate absolute mouse position into joystick like behaviour
    620     if (clippingSize.x > clippingSize.y)
    621     {
    622       int margin = (clippingSize.x - clippingSize.y) / 2;
    623       if (abs.x - margin > clippingSize.y)
     649  void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
     650  {
     651    if (!bDeriveMouseInput_)
     652    {
     653      // y axis of mouse input is inverted
     654      int rel[] = { rel_.x * mouseSensitivity_, -rel_.y * mouseSensitivity_ };
     655
     656      COUT(3) << rel[0] << " | " << rel[1] << std::endl;
     657
     658      for (int i = 0; i < 2; i++)
    624659      {
    625         halfAxes_[0].absVal_ = 1.0f;
    626         halfAxes_[1].absVal_ = 0.0f;
    627       }
    628       else if (abs.x < margin)
    629       {
    630         halfAxes_[0].absVal_ = 0.0f;
    631         halfAxes_[1].absVal_ = 1.0f;
    632       }
    633       else
    634       {
    635         float temp = ((float)abs.x) / clippingSize.y * 2 - 1;
    636         if (temp > 0)
     660        if (rel[i])
    637661        {
    638           halfAxes_[0].absVal_ = temp;
    639           halfAxes_[1].absVal_ = 0.0f;
    640         }
    641         else
    642         {
    643           halfAxes_[0].absVal_ = 0.0f;
    644           halfAxes_[1].absVal_ = -temp;
     662          // absolute
     663          if (mousePosition_[i] >= 0)
     664          {
     665            mousePosition_[i] += rel[i];
     666            halfAxes_[0 + 2*i].hasChanged_ = true;
     667            if (mousePosition_[i] < 0)
     668            {
     669              halfAxes_[1 + 2*i].hasChanged_ = true;
     670              halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024;
     671              halfAxes_[0 + 2*i].absVal_ =  0.0f;
     672            }
     673            else
     674              halfAxes_[1 + 2*i].absVal_ =  ((float)mousePosition_[i])/1024;
     675          }
     676          else
     677          {
     678            mousePosition_[i] += rel[i];
     679            halfAxes_[1 + 2*i].hasChanged_ = true;
     680            if (mousePosition_[i] > 0)
     681            {
     682              halfAxes_[0 + 2*i].hasChanged_ = true;
     683              halfAxes_[0 + 2*i].absVal_ =  ((float)mousePosition_[i])/1024;
     684              halfAxes_[1 + 2*i].absVal_ =  0.0f;
     685            }
     686            else
     687              halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024;
     688          }
     689
     690          // relative
     691          if (rel[i] > 0)
     692            halfAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/1024;
     693          else
     694            halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024;
    645695        }
    646696      }
    647 
    648       float temp = -((float)abs.y) / clippingSize.y * 2 + 1;
    649       if (temp > 0)
    650       {
    651         halfAxes_[2].absVal_ = temp;
    652         halfAxes_[3].absVal_ = 0.0;
    653       }
    654       else
    655       {
    656         halfAxes_[2].absVal_ = 0.0;
    657         halfAxes_[3].absVal_ = -temp;
    658       }
    659697    }
    660698    else
    661699    {
    662       float temp = ((float)abs.x) / clippingSize.x * 2 - 1;
    663       if (temp > 0)
    664       {
    665         halfAxes_[0].absVal_ = temp;
    666         halfAxes_[1].absVal_ = 0.0;
    667       }
    668       else
    669       {
    670         halfAxes_[0].absVal_ = 0.0;
    671         halfAxes_[1].absVal_ = -temp;
    672       }
    673 
    674       int margin = (clippingSize.y - clippingSize.x) / 2;
    675       if (abs.y - margin > clippingSize.x)
    676       {
    677         halfAxes_[2].absVal_ = 0.0;
    678         halfAxes_[3].absVal_ = 1.0;
    679       }
    680       else if (abs.y < margin)
    681       {
    682         halfAxes_[2].absVal_ = 1.0;
    683         halfAxes_[3].absVal_ = 0.0;
    684       }
    685       else
    686       {
    687         float temp = -((float)abs.y) / clippingSize.x * 2 + 1;
    688         if (temp > 0)
    689         {
    690           halfAxes_[2].absVal_ = temp;
    691           halfAxes_[3].absVal_ = 0.0;
    692         }
    693         else
    694         {
    695           halfAxes_[2].absVal_ = 0.0;
    696           halfAxes_[3].absVal_ = -temp;
    697         }
    698       }
    699     }
    700 
    701     // relative movements
    702     if (rel.x > 0)
    703     {
    704       halfAxes_[0].hasChanged_ = true;
    705       halfAxes_[1].hasChanged_ = true;
    706       halfAxes_[0].relVal_ = rel.x;
    707       halfAxes_[1].relVal_ = 0.0;
    708     }
    709     else if (rel.x < 0)
    710     {
    711       halfAxes_[0].hasChanged_ = true;
    712       halfAxes_[1].hasChanged_ = true;
    713       halfAxes_[0].relVal_ = 0.0;
    714       halfAxes_[1].relVal_ = rel.x;
    715     }
    716 
    717     if (rel.y /*!*/ < /*!*/ 0)
    718     {
    719       halfAxes_[2].hasChanged_ = true;
    720       halfAxes_[3].hasChanged_ = true;
    721       halfAxes_[0].relVal_ = -rel.y;
    722       halfAxes_[1].relVal_ = 0.0;
    723     }
    724     else if (rel.y > 0)
    725     {
    726       halfAxes_[2].hasChanged_ = true;
    727       halfAxes_[3].hasChanged_ = true;
    728       halfAxes_[0].relVal_ = 0.0;
    729       halfAxes_[1].relVal_ = -rel.y;
     700      mouseRelative_[0] += rel_.x * mouseSensitivity_;
     701      mouseRelative_[1] -= rel_.y * mouseSensitivity_;
    730702    }
    731703  }
     
    737709  void KeyBinder::mouseScrolled(int abs, int rel)
    738710  {
    739     // TODO: obvious...
     711    COUT(3) << mouseButtons_[8].name_ << "   " << abs << " | " << rel << std::endl;
     712
     713    if (rel > 0)
     714      for (int i = 0; i < rel/120; i++)
     715        mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
     716    else
     717      for (int i = 0; i < -rel/120; i++)
     718        mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
    740719  }
    741720
     
    743722  {
    744723    // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows)
    745     //CCOUT(3) << axis << std::endl;
     724    CCOUT(3) << halfAxes_[8 + axis].name_ << std::endl;
    746725    if (value >= 0)
    747726    {
  • code/branches/input/src/core/InputHandler.h

    r1340 r1344  
    6060    BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { }
    6161    bool execute();
     62
    6263    float value_;
    6364    unsigned int nValuesAdded_;
     
    9394    virtual bool addParamCommand(ParamCommand* command) { return false; }
    9495    void parse(std::vector<BufferedParamCommand*>& paramCommandBuffer);
    95     bool execute(KeybindMode::Enum mode);
     96    bool execute(KeybindMode::Enum mode, float abs = 1.0f, float rel = 1.0f);
    9697
    9798    //! The configured string value
     
    114115    HalfAxis() : relVal_(0.0f), absVal_(0.0f), paramCommands_(0), nParamCommands_(0),
    115116                 wasDown_(false), hasChanged_(false) { }
     117    using Button::execute;
    116118    bool execute();
    117     bool execute(KeybindMode::Enum mode) { return Button::execute(mode); }
     119    //bool execute(KeybindMode::Enum mode) { return Button::execute(mode); }
    118120    bool addParamCommand(ParamCommand* command);
    119121    void clear();
     
    176178
    177179    //! denotes the number of different mouse buttons there are in OIS.
    178     static const unsigned int nMouseButtons_s = 8;
     180    static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels
    179181    //! Actual key bindings as bundle for Press, Hold and Release
    180182    Button mouseButtons_ [nMouseButtons_s];
     
    193195    * Sequence is as follows:
    194196    *  0 -  3: Mouse x and y
    195     *  4 -  7: Mouse scroll wheels 1 and 2 (2 not yet supported)
     197    *  4 -  7: empty
    196198    *  8 - 23: joy stick (slider) axes 1 to 8
    197199    * 24 - 55: joy stick axes 1 - 16
     
    205207    std::vector<BufferedParamCommand*> paramCommandBuffer_;
    206208
     209    //! Keeps track of the absolute mouse value (incl. scroll wheel)
     210    int mousePosition_[3];
     211    //! Used to derive mouse input if requested
     212    int mouseRelative_[2];
     213    float deriveTime_;
     214
     215    //**** ConfigValues *****\\
    207216    //! Threshold for analog triggers until which the state is 0.
    208217    float analogThreshold_;
    209218    //! Threshold for analog triggers until which the button is not pressed.
    210219    float buttonThreshold_;
     220    //! Derive mouse input for absolute values?
     221    bool bDeriveMouseInput_;
     222    //! Accuracy of the mouse input deriver. The higher the more precise, but laggier.
     223    float derivePeriod_;
     224    //! mouse sensitivity
     225    float mouseSensitivity_;
    211226  };
    212227
  • code/branches/input/src/util/Math.h

    r1323 r1344  
    156156};
    157157
     158class _UtilExport IntVector3
     159{
     160public:
     161  IntVector3() : x(0), y(0), z(0) { }
     162  IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
     163  int x;
     164  int y;
     165  int z;
     166};
     167
    158168#endif /* _Util_Math_H__ */
Note: See TracChangeset for help on using the changeset viewer.