Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 14, 2008, 11:44:17 AM (16 years ago)
Author:
rgrieder
Message:

merged input branch into merge branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merge/src/core/InputBuffer.cc

    r1220 r1272  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder
    2626 *
    2727 */
     
    3232
    3333#include "util/Clipboard.h"
    34 #include "InputManager.h"
     34#include "CoreIncludes.h"
     35#include "ConfigValueIncludes.h"
    3536
    3637namespace orxonox
    3738{
    38     InputBuffer::InputBuffer()
    39     {
    40         //this->bActivated_ = false;
    41         this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"(){}[]<>.:,;_-+*/=!?|$&%^";
    42         this->keyboard_ = InputManager::getKeyboard();
    43         this->buffer_ = "";
    44 
    45         //this->keyboard_->setEventCallback(this);
    46     }
    47 
    48     InputBuffer::InputBuffer(const std::string allowedChars)
    49     {
    50         //this->bActivated_ = false;
    51         this->allowedChars_ = allowedChars;
    52         this->keyboard_ = InputManager::getKeyboard();
    53         this->buffer_ = "";
    54     }
    55 /*
    56     void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput)
    57     {
    58         struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '};
    59         this->listeners_.insert(this->listeners_.end(), newListener);
    60     }
    61 
    62     void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput)
    63     {
    64         struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_};
    65         this->listeners_.insert(this->listeners_.end(), newListener);
    66     }
    67 */
    68     void InputBuffer::set(const std::string& input)
    69     {
    70         this->buffer_ = "";
    71         this->append(input);
    72     }
    73 
    74     void InputBuffer::append(const std::string& input)
    75     {
    76         for (unsigned int i = 0; i < input.size(); i++)
    77         {
    78             if (this->charIsAllowed(input[i]))
    79                 this->buffer_ += input[i];
    80 
    81             this->updated(input[i], false);
    82         }
    83         this->updated();
    84     }
    85 
    86     void InputBuffer::append(const char& input)
    87     {
    88         if (this->charIsAllowed(input))
    89             this->buffer_ += input;
    90 
    91         this->updated(input, true);
    92     }
    93 
    94     void InputBuffer::clear()
    95     {
    96         this->buffer_ = "";
    97         this->updated();
    98     }
    99 
    100     void InputBuffer::removeLast()
    101     {
    102         this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1);
    103         this->updated();
    104     }
    105 
    106     void InputBuffer::updated()
    107     {
    108         for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
    109         {
    110             if ((*it).bListenToAllChanges_)
    111                 (*(*it).listener_.*(*it).function_)();
    112         }
    113     }
    114 
    115     void InputBuffer::updated(const char& update, bool bSingleInput)
    116     {
    117         for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
    118         {
    119             if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
    120                 (*(*it).listener_.*(*it).function_)();
    121         }
    122     }
    123 
    124     /*void InputBuffer::activityChanged() const
    125     {
    126     }*/
    127 
    128     bool InputBuffer::charIsAllowed(const char& input)
    129     {
    130         if (this->allowedChars_ == "")
    131             return true;
    132         else
    133             return (this->allowedChars_.find(input) != std::string::npos);
    134     }
    135 
    136     bool InputBuffer::keyPressed(const OIS::KeyEvent &e)
    137     {
    138         /*if (e.key == OIS::KC_NUMPADENTER)
    139         {
    140             this->setActivated(!this->isActivated());
    141             this->clear();
    142             return true;
    143         }*/
    144 
    145         if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
    146         {
    147             if (e.key == OIS::KC_V)
    148             {
    149                 this->append(fromClipboard());
    150                 return true;
    151             }
    152             else if (e.key == OIS::KC_C)
    153             {
    154                 toClipboard(this->buffer_);
    155                 return true;
    156             }
    157             else if (e.key == OIS::KC_X)
    158             {
    159                 toClipboard(this->buffer_);
    160                 this->clear();
    161                 return true;
    162             }
    163         }
    164         else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
    165         {
    166             if (e.key == OIS::KC_INSERT)
    167             {
    168                 this->append(fromClipboard());
    169                 return true;
    170             }
    171             else if (e.key == OIS::KC_DELETE)
    172             {
    173                 toClipboard(this->buffer_);
    174                 this->clear();
    175                 return true;
    176             }
    177         }
    178 
    179         //std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl;
    180 
    181         std::string input = this->keyboard_->getAsString(e.key);
    182         /*if (input.size() >= 1)
    183             this->append(input[0]);*/
    184 
    185         this->append((char)e.text);
    186         return true;
    187     }
    188 
    189     bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
    190     {
    191         return true;
    192     }
     39  InputBuffer::InputBuffer(const std::string allowedChars) :
     40    buffer_(""),
     41    allowedChars_(allowedChars),
     42    lastKey_(KeyCode::Unassigned),
     43    timeSinceKeyPressed_(0.0f),
     44    timeSinceKeyRepeated_(0.0f),
     45    keysToRepeat_(0)
     46  {
     47    RegisterObject(InputBuffer);
     48    setConfigValues();
     49  }
     50
     51  void InputBuffer::setConfigValues()
     52  {
     53    SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer");
     54    SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer");
     55    if (keyRepeatDeleay_ < 0.0)
     56    {
     57      ResetConfigValue(keyRepeatDeleay_);
     58    }
     59    if (keyRepeatTime_ < 0.0)
     60    {
     61      ResetConfigValue(keyRepeatTime_);
     62    }
     63  }
     64  /*
     65  void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput)
     66  {
     67    struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '};
     68    listeners_.insert(listeners_.end(), newListener);
     69  }
     70
     71  void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput)
     72  {
     73    struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_};
     74    listeners_.insert(listeners_.end(), newListener);
     75  }
     76  */
     77  void InputBuffer::set(const std::string& input)
     78  {
     79    buffer_ = "";
     80    append(input);
     81  }
     82
     83  void InputBuffer::append(const std::string& input)
     84  {
     85    for (unsigned int i = 0; i < input.size(); i++)
     86    {
     87      if (charIsAllowed(input[i]))
     88        buffer_ += input[i];
     89
     90      updated(input[i], false);
     91    }
     92    updated();
     93  }
     94
     95  void InputBuffer::append(const char& input)
     96  {
     97    if (charIsAllowed(input))
     98      buffer_ += input;
     99
     100    updated(input, true);
     101  }
     102
     103  void InputBuffer::clear()
     104  {
     105    buffer_ = "";
     106    updated();
     107  }
     108
     109  void InputBuffer::removeLast()
     110  {
     111    buffer_ = buffer_.substr(0, buffer_.size() - 1);
     112    updated();
     113  }
     114
     115  void InputBuffer::updated()
     116  {
     117    for (std::list<InputBufferListenerTuple>::iterator it = listeners_.begin(); it != listeners_.end(); ++it)
     118    {
     119      if ((*it).bListenToAllChanges_)
     120        (*(*it).listener_.*(*it).function_)();
     121    }
     122  }
     123
     124  void InputBuffer::updated(const char& update, bool bSingleInput)
     125  {
     126    for (std::list<InputBufferListenerTuple>::iterator it = listeners_.begin(); it != listeners_.end(); ++it)
     127    {
     128      if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
     129        (*(*it).listener_.*(*it).function_)();
     130    }
     131  }
     132
     133  bool InputBuffer::charIsAllowed(const char& input)
     134  {
     135    if (allowedChars_ == "")
     136      return true;
     137    else
     138      return (allowedChars_.find(input) != std::string::npos);
     139  }
     140
     141
     142  void InputBuffer::processKey(const KeyEvent &evt)
     143  {
     144    if (evt.isModifierDown(KeyboardModifier::Ctrl))
     145    {
     146      if (evt.key == KeyCode::V)
     147        append(fromClipboard());
     148      else if (evt.key == KeyCode::C)
     149        toClipboard(buffer_);
     150      else if (evt.key == KeyCode::X)
     151      {
     152        toClipboard(buffer_);
     153        clear();
     154      }
     155    }
     156    else if (evt.isModifierDown(KeyboardModifier::Shift))
     157    {
     158      if (evt.key == KeyCode::Insert)
     159      {
     160        append(fromClipboard());
     161      }
     162      else if (evt.key == KeyCode::Delete)
     163      {
     164        toClipboard(buffer_);
     165        clear();
     166      }
     167    }
     168    //std::string input = keyboard_->getAsString(e.key);
     169    /*if (input.size() >= 1)
     170    append(input[0]);*/
     171
     172    append((char)evt.text);
     173  }
     174
     175  void InputBuffer::tick(float dt)
     176  {
     177    timeSinceKeyPressed_ += dt;
     178    if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
     179    {
     180      // initial time out has gone by, start repeating keys
     181      while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
     182      {
     183        timeSinceKeyRepeated_ += keyRepeatTime_;
     184        keysToRepeat_++;
     185      }
     186    }
     187  }
     188
     189  bool InputBuffer::keyPressed(const KeyEvent &evt)
     190  {
     191    lastKey_ = evt.key;
     192    timeSinceKeyPressed_ = 0.0;
     193    timeSinceKeyRepeated_ = keyRepeatDeleay_;
     194    keysToRepeat_ = 0;
     195
     196    processKey(evt);
     197    return true;
     198  }
     199
     200  bool InputBuffer::keyHeld(const KeyEvent& evt)
     201  {
     202    if (evt.key == lastKey_)
     203    {
     204      while (keysToRepeat_)
     205      {
     206        processKey(evt);
     207        keysToRepeat_--;
     208      }
     209    }
     210    return true;
     211  }
     212
    193213}
Note: See TracChangeset for help on using the changeset viewer.