Changeset 1502 for code/trunk/src/core/InputBuffer.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/core/InputBuffer.cc
r1349 r1502 37 37 namespace orxonox 38 38 { 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 /** 176 * This tick() function is called by the InputManager if the InputBuffer is active. 177 * @param dt Delta time 178 */ 179 void InputBuffer::tick(float dt) 180 { 181 timeSinceKeyPressed_ += dt; 182 if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_) 183 { 184 // initial time out has gone by, start repeating keys 185 while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_) 186 { 187 timeSinceKeyRepeated_ += keyRepeatTime_; 188 keysToRepeat_++; 189 } 190 } 191 } 192 193 void InputBuffer::keyPressed(const KeyEvent &evt) 194 { 195 lastKey_ = evt.key; 196 timeSinceKeyPressed_ = 0.0; 197 timeSinceKeyRepeated_ = keyRepeatDeleay_; 198 keysToRepeat_ = 0; 199 200 processKey(evt); 201 } 202 203 void InputBuffer::keyHeld(const KeyEvent& evt) 204 { 205 if (evt.key == lastKey_) 206 { 207 while (keysToRepeat_) 208 { 39 InputBuffer::InputBuffer() 40 { 41 RegisterObject(InputBuffer); 42 43 this->buffer_ = ""; 44 this->cursor_ = 0; 45 this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \ 46 ABCDEFGHIJKLMNOPQRSTUVWXYZ \ 47 äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \ 48 0123456789 \ 49 \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#"; 50 51 this->lastKey_ = KeyCode::Unassigned; 52 this->timeSinceKeyPressed_ = 0.0f; 53 this->timeSinceKeyRepeated_ = 0.0f; 54 this->keysToRepeat_ = 0; 55 56 setConfigValues(); 57 } 58 59 InputBuffer::InputBuffer(const std::string allowedChars) 60 { 61 RegisterObject(InputBuffer); 62 63 this->allowedChars_ = allowedChars; 64 this->buffer_ = ""; 65 this->cursor_ = 0; 66 67 this->lastKey_ = KeyCode::Unassigned; 68 this->timeSinceKeyPressed_ = 0.0f; 69 this->timeSinceKeyRepeated_ = 0.0f; 70 this->keysToRepeat_ = 0; 71 72 setConfigValues(); 73 } 74 75 void InputBuffer::setConfigValues() 76 { 77 SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer"); 78 SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer"); 79 80 if (keyRepeatDeleay_ < 0.0) 81 { 82 ResetConfigValue(keyRepeatDeleay_); 83 } 84 if (keyRepeatTime_ < 0.0) 85 { 86 ResetConfigValue(keyRepeatTime_); 87 } 88 } 89 90 void InputBuffer::set(const std::string& input, bool update) 91 { 92 this->clear(false); 93 this->insert(input, update); 94 } 95 96 void InputBuffer::insert(const std::string& input, bool update) 97 { 98 for (unsigned int i = 0; i < input.size(); ++i) 99 { 100 this->insert(input[i], false); 101 102 if (update) 103 this->updated(input[i], false); 104 } 105 106 if (update) 107 this->updated(); 108 } 109 110 void InputBuffer::insert(const char& input, bool update) 111 { 112 if (this->charIsAllowed(input)) 113 { 114 this->buffer_.insert(this->cursor_, 1, input); 115 ++this->cursor_; 116 } 117 118 if (update) 119 this->updated(input, true); 120 } 121 122 void InputBuffer::clear(bool update) 123 { 124 this->buffer_ = ""; 125 this->cursor_ = 0; 126 127 if (update) 128 this->updated(); 129 } 130 131 void InputBuffer::removeBehindCursor(bool update) 132 { 133 if (this->cursor_ > 0) 134 { 135 --this->cursor_; 136 this->buffer_.erase(this->cursor_, 1); 137 138 if (update) 139 this->updated(); 140 } 141 } 142 143 void InputBuffer::removeAtCursor(bool update) 144 { 145 if (this->cursor_ < this->buffer_.size()) 146 { 147 this->buffer_.erase(this->cursor_, 1); 148 149 if (update) 150 this->updated(); 151 } 152 } 153 154 void InputBuffer::updated() 155 { 156 for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) 157 { 158 if ((*it)->bListenToAllChanges_) 159 (*it)->callFunction(); 160 } 161 } 162 163 void InputBuffer::updated(const char& update, bool bSingleInput) 164 { 165 for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) 166 { 167 if ((!(*it)->trueKeyFalseChar_) && ((*it)->bListenToAllChanges_ || ((*it)->char_ == update)) && (!(*it)->bOnlySingleInput_ || bSingleInput)) 168 (*it)->callFunction(); 169 } 170 } 171 172 bool InputBuffer::charIsAllowed(const char& input) 173 { 174 if (this->allowedChars_ == "") 175 return true; 176 else 177 return (this->allowedChars_.find(input) != std::string::npos); 178 } 179 180 181 void InputBuffer::processKey(const KeyEvent &evt) 182 { 183 if (evt.isModifierDown(KeyboardModifier::Alt) && evt.key == KeyCode::Tab) 184 return; 185 186 for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) 187 { 188 if ((*it)->trueKeyFalseChar_ && ((*it)->key_ == evt.key)) 189 (*it)->callFunction(); 190 } 191 192 if (evt.isModifierDown(KeyboardModifier::Ctrl)) 193 { 194 if (evt.key == KeyCode::V) 195 this->insert(fromClipboard()); 196 else if (evt.key == KeyCode::C) 197 toClipboard(this->buffer_); 198 else if (evt.key == KeyCode::X) 199 { 200 toClipboard(this->buffer_); 201 this->clear(); 202 } 203 } 204 else if (evt.isModifierDown(KeyboardModifier::Shift)) 205 { 206 if (evt.key == KeyCode::Insert) 207 this->insert(fromClipboard()); 208 else if (evt.key == KeyCode::Delete) 209 { 210 toClipboard(this->buffer_); 211 this->clear(); 212 } 213 } 214 215 this->insert((char)evt.text); 216 } 217 218 /** 219 @brief This tick() function is called by the InputManager if the InputBuffer is active. 220 @param dt Delta time 221 */ 222 void InputBuffer::tickInput(float dt, const HandlerState& state) 223 { 224 timeSinceKeyPressed_ += dt; 225 if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_) 226 { 227 // initial time out has gone by, start repeating keys 228 while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_) 229 { 230 timeSinceKeyRepeated_ += keyRepeatTime_; 231 keysToRepeat_++; 232 } 233 } 234 } 235 236 void InputBuffer::keyPressed(const KeyEvent &evt) 237 { 238 lastKey_ = evt.key; 239 timeSinceKeyPressed_ = 0.0; 240 timeSinceKeyRepeated_ = keyRepeatDeleay_; 241 keysToRepeat_ = 0; 242 209 243 processKey(evt); 210 keysToRepeat_--; 211 } 212 } 213 } 214 244 } 245 246 void InputBuffer::keyHeld(const KeyEvent& evt) 247 { 248 if (evt.key == lastKey_) 249 { 250 while (keysToRepeat_) 251 { 252 processKey(evt); 253 keysToRepeat_--; 254 } 255 } 256 } 215 257 }
Note: See TracChangeset
for help on using the changeset viewer.