Changeset 1520 for code/branches/input/src/core
- Timestamp:
- Jun 2, 2008, 10:34:20 PM (17 years ago)
- Location:
- code/branches/input/src/core
- Files:
-
- 10 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/core/CMakeLists.txt
r1519 r1520 31 31 TclThreadManager.cc 32 32 IRC.cc 33 34 input/Button.cc 35 input/CalibratorCallback.cc 36 input/HalfAxis.cc 33 37 input/InputBuffer.cc 38 input/InputCommands.cc 34 39 input/InputManager.cc 35 40 input/KeyBinder.cc 41 input/KeyDetector.cc 42 36 43 tolua/tolua_bind.cc 37 44 ) -
code/branches/input/src/core/CorePrereqs.h
r1505 r1520 162 162 // input 163 163 //class GUIInputHandler; 164 class Calibrator; 164 class BaseCommand; 165 class BufferedParamCommand; 166 class Button; 165 167 class CalibratorCallback; 168 class HalfAxis; 166 169 class InputBuffer; 167 class InputBufferListener;168 170 class InputManager; 169 171 class JoyStickHandler; 172 class MouseHandler; 170 173 class KeyBinder; 171 174 class KeyDetector; 172 class MouseHandler; 173 175 class KeyHandler; 176 class ParamCommand; 177 class SimpleCommand; 174 178 } 175 179 -
code/branches/input/src/core/input/InputManager.cc
r1519 r1520 35 35 #include "InputManager.h" 36 36 37 #include "util/Convert.h"37 #include <limits.h> 38 38 #include "core/CoreIncludes.h" 39 39 #include "core/ConfigValueIncludes.h" … … 41 41 #include "core/CommandExecutor.h" 42 42 #include "core/ConsoleCommand.h" 43 #include "core/Shell.h" 43 #include "core/Shell.h" // hack! 44 44 #include "InputBuffer.h" 45 45 #include "KeyBinder.h" 46 #include "KeyDetector.h" 47 #include "CalibratorCallback.h" 46 48 47 49 namespace orxonox -
code/branches/input/src/core/input/KeyBinder.cc
r1519 r1520 34 34 #include "KeyBinder.h" 35 35 #include <fstream> 36 #include < limits.h>36 #include <string> 37 37 #include "util/Convert.h" 38 #include "util/SubString.h"39 #include "util/String.h"40 38 #include "core/Debug.h" 41 39 #include "core/ConfigValueIncludes.h" 42 40 #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" 48 42 49 43 namespace orxonox 50 44 { 51 // ###############################52 // ### BufferedParamCommand ###53 // ###############################54 55 /**56 * Executes a buffered command. This is used for commands with additional57 * 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 // reset67 cmd.nValuesAdded_ = 0;68 cmd.value_ = 0;69 return cmd.evaluation_.execute();70 }71 else72 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 parameter101 if (bRelative_)102 {103 if (rel != 0.0f)104 {105 // we have to calculate a relative movement.106 // paramModifier_ says how much one keystroke is107 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 high114 // 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 then116 // be 0.05 for instance and the the key value 1. Without handling the problem, the final117 // 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 quadratics121 {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 anything128 }129 else130 {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 and133 // store the result in a temporary place134 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 array151 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 else158 {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 array173 std::vector<BaseCommand*> commands[3];174 175 176 // separate the commands177 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 command190 if (getLowercase(tokens[0]) == "buttonthreshold")191 {192 if (tokens.size() == 1)193 continue;194 // may fail, but doesn't matter195 convertValue(&buttonThreshold_, tokens[1]);196 continue;197 }198 199 // first argument can be OnPress, OnHold OnRelease or nothing200 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 command212 // default amplitude is 1.0f213 float paramModifier = 1.0f;214 if (getLowercase(tokens[iToken]) == "scale")215 {216 iToken++;217 if (iToken == tokens.size() || !convertValue(¶mModifier, 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 command228 if (iToken == tokens.size())229 continue;230 231 std::string commandStr;232 while (iToken != tokens.size())233 commandStr += tokens[iToken++] + " ";234 235 // evaluate the command236 CommandEvaluation eval = CommandExecutor::evaluate(commandStr);237 if (!eval.isValid())238 continue;239 240 // check for param command241 int paramIndex = eval.getConsoleCommand()->getAxisParamIndex();242 if (paramIndex >= 0)243 {244 // parameter supported command245 ParamCommand* cmd = new ParamCommand();246 cmd->paramModifier_ = paramModifier;247 cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative();248 249 // add command to the buffer if not yet existing250 for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++)251 {252 if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getOriginalCommand())253 == getLowercase(commandStr))254 {255 // already in list256 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 button270 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 else280 {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 else302 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 string309 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 array324 for (unsigned int i = 0; i < nParamCommands_; i++)325 delete paramCommands_[i];326 delete[] paramCommands_;327 nParamCommands_ = 0;328 }329 else330 {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 361 45 /** 362 46 @brief Constructor that does as little as necessary. … … 854 538 } 855 539 } 856 857 858 // ###############################859 // ##### KeyDetector #####860 // ###############################861 862 /**863 @brief Constructor864 */865 KeyDetector::KeyDetector()866 {867 RegisterObject(KeyDetector);868 }869 870 /**871 @brief Destructor872 */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 }908 540 } -
code/branches/input/src/core/input/KeyBinder.h
r1519 r1520 37 37 #include "core/CorePrereqs.h" 38 38 39 #include <string>40 39 #include <vector> 41 40 42 #include "ois/OIS.h"43 #include "util/Math.h"44 41 #include "core/OrxonoxClass.h" 45 #include "core/CommandEvaluation.h"46 42 #include "InputInterfaces.h" 43 #include "Button.h" 44 #include "HalfAxis.h" 47 45 48 46 namespace orxonox 49 47 { 50 class _CoreExport BufferedParamCommand51 {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 BaseCommand63 {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 BaseCommand70 {71 public:72 bool execute(float abs = 1.0f, float rel = 1.0f);73 74 CommandEvaluation evaluation_;75 };76 77 class _CoreExport ParamCommand : public BaseCommand78 {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 Button89 {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 value99 std::string bindingString_;100 //! Name of the trigger as strings101 std::string name_;102 //! Basic commands for OnPress, OnHold and OnRelease103 BaseCommand** commands_[3];104 //! Number of basic commands105 unsigned int nCommands_[3];106 //! Says how much it takes for an analog axis to trigger a button107 //! Note: This variable is here to have only one parse() function.108 float buttonThreshold_;109 };110 111 112 class _CoreExport HalfAxis : public Button113 {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 related124 float relVal_;125 float absVal_;126 ParamCommand** paramCommands_;127 unsigned int nParamCommands_;128 129 // button related130 bool wasDown_;131 bool hasChanged_;132 };133 134 135 48 /** 136 49 @brief Handles mouse, keyboard and joy stick input while in the actual game mode. … … 226 139 bool bClipMouse_; 227 140 }; 228 229 230 class _CoreExport KeyDetector : public KeyBinder231 {232 public:233 KeyDetector();234 ~KeyDetector();235 void loadBindings();236 237 protected:238 void readTrigger(Button& button);239 };240 241 class _CoreExport CalibratorCallback : public KeyHandler242 {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 };254 141 } 255 142 256 257 258 259 143 #endif /* _KeyBinder_H__ */
Note: See TracChangeset
for help on using the changeset viewer.