Changeset 1630
- Timestamp:
- Jun 27, 2008, 10:36:53 AM (16 years ago)
- Location:
- code/branches/input/src
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/core/input/Button.cc
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the different input handlers. 32 */ 30 @file 31 @brief 32 Implementation of the different input handlers. 33 */ 33 34 34 35 #include "Button.h" … … 44 45 namespace orxonox 45 46 { 46 void Button::clear() 47 { 48 for (unsigned int j = 0; j < 3; j++) 47 void Button::clear() 49 48 { 50 if (nCommands_[j]) 51 { 52 // delete all commands and the command pointer array 53 for (unsigned int i = 0; i < nCommands_[j]; i++) 54 delete commands_[j][i]; 55 delete[] commands_[j]; 56 commands_[j] = 0; 57 nCommands_[j] = 0; 58 } 59 else 60 { 61 commands_[j] = 0; 62 } 49 for (unsigned int j = 0; j < 3; j++) 50 { 51 if (nCommands_[j]) 52 { 53 // delete all commands and the command pointer array 54 for (unsigned int i = 0; i < nCommands_[j]; i++) 55 delete commands_[j][i]; 56 delete[] commands_[j]; 57 commands_[j] = 0; 58 nCommands_[j] = 0; 59 } 60 else 61 { 62 commands_[j] = 0; 63 } 64 } 63 65 } 64 } 65 66 void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer) 67 { 68 if (isEmpty(bindingString_)) 66 67 void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer) 69 68 { 70 clear(); 71 return; 69 if (isEmpty(bindingString_)) 70 { 71 clear(); 72 return; 73 } 74 75 // use std::vector for a temporary dynamic array 76 std::vector<BaseCommand*> commands[3]; 77 78 79 // separate the commands 80 SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, 81 '\\', false, '"', false, '(', ')', false, '\0'); 82 83 for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) 84 { 85 if (commandStrings[iCommand] != "") 86 { 87 SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, 88 '\\', false, '"', false, '(', ')', false, '\0'); 89 90 unsigned int iToken = 0; 91 92 // for real axes, we can feed a ButtonThreshold argument as entire command 93 if (getLowercase(tokens[0]) == "buttonthreshold") 94 { 95 if (tokens.size() == 1) 96 continue; 97 // may fail, but doesn't matter 98 convertValue(&buttonThreshold_, tokens[1]); 99 continue; 100 } 101 102 // first argument can be OnPress, OnHold OnRelease or nothing 103 KeybindMode::Enum mode = KeybindMode::None; 104 if (getLowercase(tokens[iToken]) == "onpress") 105 mode = KeybindMode::OnPress, iToken++; 106 if (getLowercase(tokens[iToken]) == "onrelease") 107 mode = KeybindMode::OnRelease, iToken++; 108 if (getLowercase(tokens[iToken]) == "onhold") 109 mode = KeybindMode::OnHold, iToken++; 110 111 if (iToken == tokens.size()) 112 continue; 113 114 // second argument can be the amplitude for the case it as an axis command 115 // default amplitude is 1.0f 116 float paramModifier = 1.0f; 117 if (getLowercase(tokens[iToken]) == "scale") 118 { 119 iToken++; 120 if (iToken == tokens.size() || !convertValue(¶mModifier, tokens[iToken])) 121 { 122 COUT(2) << "Error while parsing key binding " << name_ 123 << ". Numeric expression expected afer 'AxisAmp', switching to default value" 124 << std::endl; 125 if (iToken == tokens.size()) 126 continue; 127 } 128 iToken++; 129 } 130 131 // no more arguments expected except for the actual command 132 if (iToken == tokens.size()) 133 continue; 134 135 std::string commandStr; 136 while (iToken != tokens.size()) 137 commandStr += tokens[iToken++] + " "; 138 139 // evaluate the command 140 CommandEvaluation eval = CommandExecutor::evaluate(commandStr); 141 if (!eval.isValid()) 142 continue; 143 144 // check for param command 145 int paramIndex = eval.getConsoleCommand()->getAxisParamIndex(); 146 if (paramIndex >= 0) 147 { 148 // parameter supported command 149 ParamCommand* cmd = new ParamCommand(); 150 cmd->paramModifier_ = paramModifier; 151 cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative(); 152 153 // add command to the buffer if not yet existing 154 for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++) 155 { 156 if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getOriginalCommand()) 157 == getLowercase(commandStr)) 158 { 159 // already in list 160 cmd->paramCommand_ = paramCommandBuffer[iParamCmd]; 161 break; 162 } 163 } 164 if (cmd->paramCommand_ == 0) 165 { 166 cmd->paramCommand_ = new BufferedParamCommand(); 167 paramCommandBuffer.push_back(cmd->paramCommand_); 168 cmd->paramCommand_->evaluation_ = eval; 169 cmd->paramCommand_->paramIndex_ = paramIndex; 170 } 171 172 173 // we don't know whether this is an actual axis or just a button 174 if (mode == KeybindMode::None) 175 { 176 if (!addParamCommand(cmd)) 177 { 178 mode = eval.getConsoleCommand()->getKeybindMode(); 179 commands[mode].push_back(cmd); 180 } 181 } 182 } 183 else 184 { 185 SimpleCommand* cmd = new SimpleCommand(); 186 cmd->evaluation_ = eval; 187 188 if (mode == KeybindMode::None) 189 mode = eval.getConsoleCommand()->getKeybindMode(); 190 191 commands[mode].push_back(cmd); 192 } 193 } 194 } 195 196 for (unsigned int j = 0; j < 3; j++) 197 { 198 nCommands_[j] = commands[j].size(); 199 if (nCommands_[j]) 200 { 201 commands_[j] = new BaseCommand*[nCommands_[j]]; 202 for (unsigned int i = 0; i < commands[j].size(); i++) 203 commands_[j][i] = commands[j][i]; 204 } 205 else 206 commands_[j] = 0; 207 } 72 208 } 73 209 74 // use std::vector for a temporary dynamic array 75 std::vector<BaseCommand*> commands[3]; 76 77 78 // separate the commands 79 SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, 80 '\\', false, '"', false, '(', ')', false, '\0'); 81 82 for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) 210 bool Button::execute(KeybindMode::Enum mode, float abs, float rel) 83 211 { 84 if (commandStrings[iCommand] != "") 85 { 86 SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, 87 '\\', false, '"', false, '(', ')', false, '\0'); 88 89 unsigned int iToken = 0; 90 91 // for real axes, we can feed a ButtonThreshold argument as entire command 92 if (getLowercase(tokens[0]) == "buttonthreshold") 93 { 94 if (tokens.size() == 1) 95 continue; 96 // may fail, but doesn't matter 97 convertValue(&buttonThreshold_, tokens[1]); 98 continue; 99 } 100 101 // first argument can be OnPress, OnHold OnRelease or nothing 102 KeybindMode::Enum mode = KeybindMode::None; 103 if (getLowercase(tokens[iToken]) == "onpress") 104 mode = KeybindMode::OnPress, iToken++; 105 if (getLowercase(tokens[iToken]) == "onrelease") 106 mode = KeybindMode::OnRelease, iToken++; 107 if (getLowercase(tokens[iToken]) == "onhold") 108 mode = KeybindMode::OnHold, iToken++; 109 110 if (iToken == tokens.size()) 111 continue; 112 113 // second argument can be the amplitude for the case it as an axis command 114 // default amplitude is 1.0f 115 float paramModifier = 1.0f; 116 if (getLowercase(tokens[iToken]) == "scale") 117 { 118 iToken++; 119 if (iToken == tokens.size() || !convertValue(¶mModifier, tokens[iToken])) 120 { 121 COUT(2) << "Error while parsing key binding " << name_ 122 << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl; 123 if (iToken == tokens.size()) 124 continue; 125 } 126 iToken++; 127 } 128 129 // no more arguments expected except for the actual command 130 if (iToken == tokens.size()) 131 continue; 132 133 std::string commandStr; 134 while (iToken != tokens.size()) 135 commandStr += tokens[iToken++] + " "; 136 137 // evaluate the command 138 CommandEvaluation eval = CommandExecutor::evaluate(commandStr); 139 if (!eval.isValid()) 140 continue; 141 142 // check for param command 143 int paramIndex = eval.getConsoleCommand()->getAxisParamIndex(); 144 if (paramIndex >= 0) 145 { 146 // parameter supported command 147 ParamCommand* cmd = new ParamCommand(); 148 cmd->paramModifier_ = paramModifier; 149 cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative(); 150 151 // add command to the buffer if not yet existing 152 for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++) 153 { 154 if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getOriginalCommand()) 155 == getLowercase(commandStr)) 156 { 157 // already in list 158 cmd->paramCommand_ = paramCommandBuffer[iParamCmd]; 159 break; 160 } 161 } 162 if (cmd->paramCommand_ == 0) 163 { 164 cmd->paramCommand_ = new BufferedParamCommand(); 165 paramCommandBuffer.push_back(cmd->paramCommand_); 166 cmd->paramCommand_->evaluation_ = eval; 167 cmd->paramCommand_->paramIndex_ = paramIndex; 168 } 169 170 171 // we don't know whether this is an actual axis or just a button 172 if (mode == KeybindMode::None) 173 { 174 if (!addParamCommand(cmd)) 175 { 176 mode = eval.getConsoleCommand()->getKeybindMode(); 177 commands[mode].push_back(cmd); 178 } 179 } 180 } 181 else 182 { 183 SimpleCommand* cmd = new SimpleCommand(); 184 cmd->evaluation_ = eval; 185 186 if (mode == KeybindMode::None) 187 mode = eval.getConsoleCommand()->getKeybindMode(); 188 189 commands[mode].push_back(cmd); 190 } 191 } 212 // execute all the parsed commands in the string 213 for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++) 214 commands_[mode][iCommand]->execute(abs, rel); 215 return true; 192 216 } 193 194 for (unsigned int j = 0; j < 3; j++)195 {196 nCommands_[j] = commands[j].size();197 if (nCommands_[j])198 {199 commands_[j] = new BaseCommand*[nCommands_[j]];200 for (unsigned int i = 0; i < commands[j].size(); i++)201 commands_[j][i] = commands[j][i];202 }203 else204 commands_[j] = 0;205 }206 }207 208 bool Button::execute(KeybindMode::Enum mode, float abs, float rel)209 {210 // execute all the parsed commands in the string211 for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++)212 commands_[mode][iCommand]->execute(abs, rel);213 return true;214 }215 217 } -
code/branches/input/src/core/input/Button.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _Button_H__ … … 42 43 namespace orxonox 43 44 { 44 class _CoreExport Button45 {46 public:47 Button() { nCommands_[0]=0; nCommands_[1]=0; nCommands_[2]=0; clear(); }48 virtual ~Button() { clear(); }49 virtual void clear();50 virtual bool addParamCommand(ParamCommand* command) { return false; }51 void parse(std::vector<BufferedParamCommand*>& paramCommandBuffer);52 bool execute(KeybindMode::Enum mode, float abs = 1.0f, float rel = 1.0f);45 class _CoreExport Button 46 { 47 public: 48 Button() { nCommands_[0]=0; nCommands_[1]=0; nCommands_[2]=0; clear(); } 49 virtual ~Button() { clear(); } 50 virtual void clear(); 51 virtual bool addParamCommand(ParamCommand* command) { return false; } 52 void parse(std::vector<BufferedParamCommand*>& paramCommandBuffer); 53 bool execute(KeybindMode::Enum mode, float abs = 1.0f, float rel = 1.0f); 53 54 54 //! The configured string value55 std::string bindingString_;56 //! Name of the trigger as strings57 std::string name_;58 //! Basic commands for OnPress, OnHold and OnRelease59 BaseCommand** commands_[3];60 //! Number of basic commands61 unsigned int nCommands_[3];62 //! Says how much it takes for an analog axis to trigger a button63 //! Note: This variable is here to have only one parse() function.64 float buttonThreshold_;65 };55 //! The configured string value 56 std::string bindingString_; 57 //! Name of the trigger as strings 58 std::string name_; 59 //! Basic commands for OnPress, OnHold and OnRelease 60 BaseCommand** commands_[3]; 61 //! Number of basic commands 62 unsigned int nCommands_[3]; 63 //! Says how much it takes for an analog axis to trigger a button 64 //! Note: This variable is here to have only one parse() function. 65 float buttonThreshold_; 66 }; 66 67 } 67 68 -
code/branches/input/src/core/input/CalibratorCallback.cc
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the different input handlers. 32 */ 30 @file 31 @brief 32 Implementation of the different input handlers. 33 */ 33 34 34 35 #include "CalibratorCallback.h" … … 37 38 namespace orxonox 38 39 { 39 void CalibratorCallback::keyPressed(const orxonox::KeyEvent &evt) 40 { 41 if (evt.key == KeyCode::Return) 40 void CalibratorCallback::keyPressed(const orxonox::KeyEvent &evt) 42 41 { 43 InputManager::setInputState(InputManager::IS_NOCALIBRATE); 42 if (evt.key == KeyCode::Return) 43 { 44 InputManager::setInputState(InputManager::IS_NOCALIBRATE); 45 } 44 46 } 45 }46 47 } -
code/branches/input/src/core/input/CalibratorCallback.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _CalibratorCallback_H__ … … 36 37 37 38 #include "core/CorePrereqs.h" 38 39 39 #include "InputInterfaces.h" 40 40 41 41 namespace orxonox 42 42 { 43 class _CoreExport CalibratorCallback : public KeyHandler44 {45 public:46 CalibratorCallback() {}47 ~CalibratorCallback() {}43 class _CoreExport CalibratorCallback : public KeyHandler 44 { 45 public: 46 CalibratorCallback() { } 47 ~CalibratorCallback() { } 48 48 49 private:50 void keyPressed (const KeyEvent& evt);51 void keyReleased(const KeyEvent& evt) {}52 void keyHeld (const KeyEvent& evt) {}49 private: 50 void keyPressed (const KeyEvent& evt); 51 void keyReleased(const KeyEvent& evt) { } 52 void keyHeld (const KeyEvent& evt) { } 53 53 54 void tickInput(float dt, const HandlerState &state) { }55 };54 void tickInput(float dt, const HandlerState &state) { } 55 }; 56 56 } 57 57 -
code/branches/input/src/core/input/HalfAxis.cc
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the different input handlers. 32 */ 30 @file 31 @brief 32 Implementation of the different input handlers. 33 */ 33 34 34 35 #include "HalfAxis.h" … … 38 39 namespace orxonox 39 40 { 40 void HalfAxis::clear() 41 { 42 Button::clear(); 43 if (nParamCommands_) 41 void HalfAxis::clear() 44 42 { 45 // delete all commands and the command pointer array 46 for (unsigned int i = 0; i < nParamCommands_; i++) 47 delete paramCommands_[i]; 48 delete[] paramCommands_; 49 nParamCommands_ = 0; 43 Button::clear(); 44 if (nParamCommands_) 45 { 46 // delete all commands and the command pointer array 47 for (unsigned int i = 0; i < nParamCommands_; i++) 48 delete paramCommands_[i]; 49 delete[] paramCommands_; 50 nParamCommands_ = 0; 51 } 52 else 53 { 54 nParamCommands_ = 0; nParamCommands_ = 0; 55 } 50 56 } 51 else 57 58 bool HalfAxis::addParamCommand(ParamCommand* command) 52 59 { 53 nParamCommands_ = 0; nParamCommands_ = 0; 60 ParamCommand** cmds = paramCommands_; 61 paramCommands_ = new ParamCommand*[++nParamCommands_]; 62 unsigned int i; 63 for (i = 0; i < nParamCommands_ - 1; i++) 64 paramCommands_[i] = cmds[i]; 65 paramCommands_[i] = command; 66 if (nParamCommands_ > 1) 67 delete[] cmds; 68 return true; 54 69 } 55 }56 70 57 bool HalfAxis::addParamCommand(ParamCommand* command) 58 { 59 ParamCommand** cmds = paramCommands_; 60 paramCommands_ = new ParamCommand*[++nParamCommands_]; 61 unsigned int i; 62 for (i = 0; i < nParamCommands_ - 1; i++) 63 paramCommands_[i] = cmds[i]; 64 paramCommands_[i] = command; 65 if (nParamCommands_ > 1) 66 delete[] cmds; 67 return true; 68 } 69 70 bool HalfAxis::execute() 71 { 72 bool success = true; 73 for (unsigned int i = 0; i < nParamCommands_; i++) 74 success = success && paramCommands_[i]->execute(absVal_, relVal_); 75 return success; 76 } 71 bool HalfAxis::execute() 72 { 73 bool success = true; 74 for (unsigned int i = 0; i < nParamCommands_; i++) 75 success = success && paramCommands_[i]->execute(absVal_, relVal_); 76 return success; 77 } 77 78 } -
code/branches/input/src/core/input/HalfAxis.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _HalfAxis_H__ … … 40 41 namespace orxonox 41 42 { 42 class _CoreExport HalfAxis : public Button 43 { 44 public: 45 HalfAxis() : relVal_(0.0f), absVal_(0.0f), paramCommands_(0), nParamCommands_(0), 46 wasDown_(false), hasChanged_(false) { } 47 using Button::execute; 48 bool execute(); 49 bool addParamCommand(ParamCommand* command); 50 void clear(); 43 class _CoreExport HalfAxis : public Button 44 { 45 public: 46 HalfAxis() 47 : relVal_(0.0f) 48 , absVal_(0.0f) 49 , paramCommands_(0) 50 , nParamCommands_(0) 51 , wasDown_(false) 52 , hasChanged_(false) 53 { } 54 using Button::execute; 55 bool execute(); 56 bool addParamCommand(ParamCommand* command); 57 void clear(); 51 58 52 // axis related53 float relVal_;54 float absVal_;55 ParamCommand** paramCommands_;56 unsigned int nParamCommands_;59 // axis related 60 float relVal_; 61 float absVal_; 62 ParamCommand** paramCommands_; 63 unsigned int nParamCommands_; 57 64 58 // button related59 bool wasDown_;60 bool hasChanged_;61 };65 // button related 66 bool wasDown_; 67 bool hasChanged_; 68 }; 62 69 } 63 70 -
code/branches/input/src/core/input/InputCommands.cc
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the different input handlers. 32 */ 30 @file 31 @brief 32 Implementation of the different input handlers. 33 */ 33 34 34 35 #include "InputCommands.h" … … 37 38 namespace orxonox 38 39 { 39 // ###############################40 // ### BufferedParamCommand ###41 // ###############################40 // ############################### 41 // ### BufferedParamCommand ### 42 // ############################### 42 43 43 /**44 * Executes a buffered command. This is used for commands with additional45 * parameters.46 * @return True if command execution was successful or value was zero.47 */48 bool BufferedParamCommand::execute()49 {50 if (nValuesAdded_)44 /** 45 @brief 46 Executes a buffered command. This is used for commands with additional 47 parameters. 48 @return 49 True if command execution was successful or value was zero. 50 */ 51 bool BufferedParamCommand::execute() 51 52 { 52 BufferedParamCommand& cmd = *this; 53 cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_); 54 // reset 55 cmd.nValuesAdded_ = 0; 56 cmd.value_ = 0; 57 return cmd.evaluation_.execute(); 53 if (nValuesAdded_) 54 { 55 BufferedParamCommand& cmd = *this; 56 cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_); 57 // reset 58 cmd.nValuesAdded_ = 0; 59 cmd.value_ = 0; 60 return cmd.evaluation_.execute(); 61 } 62 else 63 return true; 58 64 } 59 else60 return true;61 }62 65 63 // ###############################64 // ##### SimpleCommand #####65 // ###############################66 // ############################### 67 // ##### SimpleCommand ##### 68 // ############################### 66 69 67 /** 68 * Executes a simple command with no additional paramters. 69 * @return True if command execution was successful, false otherwise. 70 */ 71 bool SimpleCommand::execute(float abs, float rel) 72 { 73 return evaluation_.execute(); 74 } 70 /** 71 @brief 72 Executes a simple command with no additional paramters. 73 @return 74 True if command execution was successful, false otherwise. 75 */ 76 bool SimpleCommand::execute(float abs, float rel) 77 { 78 return evaluation_.execute(); 79 } 75 80 76 // ###############################77 // ##### ParamCommand #####78 // ###############################81 // ############################### 82 // ##### ParamCommand ##### 83 // ############################### 79 84 80 /** 81 * Executes a parameter command. The commmand string is not directly executed, 82 * but instead stored in a buffer list so that values can be combined. 83 * @return Always true. 84 */ 85 bool ParamCommand::execute(float abs, float rel) 86 { 87 BufferedParamCommand& cmd = *paramCommand_; 88 // command has an additional parameter 89 if (bRelative_) 85 /** 86 @brief 87 Executes a parameter command. The commmand string is not directly executed, 88 but instead stored in a buffer list so that values can be combined. 89 @return 90 Always true. 91 */ 92 bool ParamCommand::execute(float abs, float rel) 90 93 { 91 if (rel != 0.0f) 92 { 93 // we have to calculate a relative movement. 94 // paramModifier_ says how much one keystroke is 95 cmd.value_ += paramModifier_ * rel; 96 } 94 BufferedParamCommand& cmd = *paramCommand_; 95 // command has an additional parameter 96 if (bRelative_) 97 { 98 if (rel != 0.0f) 99 { 100 // we have to calculate a relative movement. 101 // paramModifier_ says how much one keystroke is 102 cmd.value_ += paramModifier_ * rel; 103 } 104 } 105 else if (abs != 0.0f) 106 { 107 // Usually, joy sticks create 'noise' (they return values if they're in 0 position) 108 // and normally this is caught in tickInput(), but that threshold cannot be to high 109 // in order to preserve accuracy. Instead, we have to catch the problem here. An example: 110 // Someone only uses buttons with an active joystick. The joy stick value could then 111 // be 0.05 for instance and the the key value 1. Without handling the problem, the final 112 // value would be computed to (1+0.05)/2=0.5025 which is not what the user expects. 113 float absQ = abs * abs; 114 float valueQ = cmd.value_ * cmd.value_; 115 if (absQ > 50.0f * valueQ) // ease up comparison by using quadratics 116 { 117 cmd.value_ = abs * paramModifier_; 118 cmd.nValuesAdded_ = 1; 119 } 120 else if (absQ * 50.0f < valueQ) 121 { 122 // abs is too small, we just don't do anything 123 } 124 else 125 { 126 // we have to calculate the absolute position of the axis. 127 // Since there might be another axis that is affected, we have to wait and 128 // store the result in a temporary place 129 cmd.value_ = (cmd.value_ * cmd.nValuesAdded_ + paramModifier_ * abs) / ++cmd.nValuesAdded_; 130 } 131 } 132 return true; 97 133 } 98 else if (abs != 0.0f)99 {100 // Usually, joy sticks create 'noise' (they return values if they're in 0 position)101 // and normally this is caught in tickInput(), but that threshold cannot be to high102 // in order to preserve accuracy. Instead, we have to catch the problem here. An example:103 // Someone only uses buttons with an active joystick. The joy stick value could then104 // be 0.05 for instance and the the key value 1. Without handling the problem, the final105 // value would be computed to (1+0.05)/2=0.5025 which is not what the user expects.106 float absQ = abs * abs;107 float valueQ = cmd.value_ * cmd.value_;108 if (absQ > 50.0f * valueQ) // ease up comparison by using quadratics109 {110 cmd.value_ = abs * paramModifier_;111 cmd.nValuesAdded_ = 1;112 }113 else if (absQ * 50.0f < valueQ)114 {115 // abs is too small, we just don't do anything116 }117 else118 {119 // we have to calculate the absolute position of the axis.120 // Since there might be another axis that is affected, we have to wait and121 // store the result in a temporary place122 cmd.value_ = (cmd.value_ * cmd.nValuesAdded_ + paramModifier_ * abs) / ++cmd.nValuesAdded_;123 }124 }125 return true;126 }127 134 } -
code/branches/input/src/core/input/InputCommands.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _InputCommands_H__ … … 40 41 namespace orxonox 41 42 { 42 class _CoreExport BufferedParamCommand43 {44 public:45 BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { }46 bool execute();43 class _CoreExport BufferedParamCommand 44 { 45 public: 46 BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { } 47 bool execute(); 47 48 48 float value_;49 unsigned int nValuesAdded_;50 int paramIndex_;51 CommandEvaluation evaluation_;52 };49 float value_; 50 unsigned int nValuesAdded_; 51 int paramIndex_; 52 CommandEvaluation evaluation_; 53 }; 53 54 54 class _CoreExport BaseCommand55 {56 public:57 virtual ~BaseCommand() { }58 virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;59 };55 class _CoreExport BaseCommand 56 { 57 public: 58 virtual ~BaseCommand() { } 59 virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0; 60 }; 60 61 61 class _CoreExport SimpleCommand : public BaseCommand62 {63 public:64 bool execute(float abs = 1.0f, float rel = 1.0f);62 class _CoreExport SimpleCommand : public BaseCommand 63 { 64 public: 65 bool execute(float abs = 1.0f, float rel = 1.0f); 65 66 66 CommandEvaluation evaluation_;67 };67 CommandEvaluation evaluation_; 68 }; 68 69 69 class _CoreExport ParamCommand : public BaseCommand70 {71 public:72 ParamCommand() : bRelative_(false), paramModifier_(1.0f), paramCommand_(0) { }73 bool execute(float abs = 1.0f, float rel = 1.0f);70 class _CoreExport ParamCommand : public BaseCommand 71 { 72 public: 73 ParamCommand() : bRelative_(false), paramModifier_(1.0f), paramCommand_(0) { } 74 bool execute(float abs = 1.0f, float rel = 1.0f); 74 75 75 bool bRelative_;76 float paramModifier_;77 BufferedParamCommand* paramCommand_;78 };76 bool bRelative_; 77 float paramModifier_; 78 BufferedParamCommand* paramCommand_; 79 }; 79 80 } 80 81 -
code/branches/input/src/core/input/InputInterfaces.h
r1629 r1630 28 28 29 29 /** 30 @file 31 @brief Declarations of various interface classes for the input management. 30 @file 31 @brief 32 Declarations of various interface classes for the input management. 32 33 */ 33 34 … … 44 45 namespace orxonox 45 46 { 46 namespace KeyCode 47 { 48 // note: KeyCode comments were directly taken from OISKeyboard.h 49 enum Enum 50 { 51 Unassigned = OIS::KC_UNASSIGNED, 52 Escape = OIS::KC_ESCAPE, 53 NumRow1 = OIS::KC_1, 54 NumRow2 = OIS::KC_2, 55 NumRow3 = OIS::KC_3, 56 NumRow4 = OIS::KC_4, 57 NumRow5 = OIS::KC_5, 58 NumRow6 = OIS::KC_6, 59 NumRow7 = OIS::KC_7, 60 NumRow8 = OIS::KC_8, 61 NumRow9 = OIS::KC_9, 62 NumRow0 = OIS::KC_0, 63 Minus = OIS::KC_MINUS, // - on main keyboard 64 Equals = OIS::KC_EQUALS, 65 Back = OIS::KC_BACK, // backspace 66 Tab = OIS::KC_TAB, 67 Q = OIS::KC_Q, 68 W = OIS::KC_W, 69 E = OIS::KC_E, 70 R = OIS::KC_R, 71 T = OIS::KC_T, 72 Y = OIS::KC_Y, 73 U = OIS::KC_U, 74 I = OIS::KC_I, 75 O = OIS::KC_O, 76 P = OIS::KC_P, 77 LeftBracket = OIS::KC_LBRACKET, 78 RightBracket = OIS::KC_RBRACKET, 79 Return = OIS::KC_RETURN, // Enter on main keyboard 80 LeftControl = OIS::KC_LCONTROL, 81 A = OIS::KC_A, 82 S = OIS::KC_S, 83 D = OIS::KC_D, 84 F = OIS::KC_F, 85 G = OIS::KC_G, 86 H = OIS::KC_H, 87 J = OIS::KC_J, 88 K = OIS::KC_K, 89 L = OIS::KC_L, 90 Semicolon = OIS::KC_SEMICOLON, 91 Apostrophe = OIS::KC_APOSTROPHE, 92 Grave = OIS::KC_GRAVE, // accent 93 LeftShift = OIS::KC_LSHIFT, 94 Backslash = OIS::KC_BACKSLASH, 95 Z = OIS::KC_Z, 96 X = OIS::KC_X, 97 C = OIS::KC_C, 98 V = OIS::KC_V, 99 B = OIS::KC_B, 100 N = OIS::KC_N, 101 M = OIS::KC_M, 102 Comma = OIS::KC_COMMA, 103 Period = OIS::KC_PERIOD, // . on main keyboard 104 Slash = OIS::KC_SLASH, // / on main keyboard 105 RightShift = OIS::KC_RSHIFT, 106 Multiply = OIS::KC_MULTIPLY, // * on numeric keypad 107 LeftAlt = OIS::KC_LMENU, // left Alt 108 Space = OIS::KC_SPACE, 109 CapsLock = OIS::KC_CAPITAL, 110 F1 = OIS::KC_F1, 111 F2 = OIS::KC_F2, 112 F3 = OIS::KC_F3, 113 F4 = OIS::KC_F4, 114 F5 = OIS::KC_F5, 115 F6 = OIS::KC_F6, 116 F7 = OIS::KC_F7, 117 F8 = OIS::KC_F8, 118 F9 = OIS::KC_F9, 119 F10 = OIS::KC_F10, 120 Numlock = OIS::KC_NUMLOCK, 121 Scrolllock = OIS::KC_SCROLL, // Scroll Lock 122 Numpad7 = OIS::KC_NUMPAD7, 123 Numpad8 = OIS::KC_NUMPAD8, 124 Numpad9 = OIS::KC_NUMPAD9, 125 NumpadSubtract= OIS::KC_SUBTRACT, // - on numeric keypad 126 Numpad4 = OIS::KC_NUMPAD4, 127 Numpad5 = OIS::KC_NUMPAD5, 128 Numpad6 = OIS::KC_NUMPAD6, 129 NumpadAdd = OIS::KC_ADD, // + on numeric keypad 130 Numpad1 = OIS::KC_NUMPAD1, 131 Numpad2 = OIS::KC_NUMPAD2, 132 Numpad3 = OIS::KC_NUMPAD3, 133 Numpad0 = OIS::KC_NUMPAD0, 134 NumpadPeriod = OIS::KC_DECIMAL, // . on numeric keypad 135 LessThan = OIS::KC_OEM_102, // < > | on UK/Germany keyboards 136 F11 = OIS::KC_F11, 137 F12 = OIS::KC_F12, 138 F13 = OIS::KC_F13, // (NEC PC98) 139 F14 = OIS::KC_F14, // (NEC PC98) 140 F15 = OIS::KC_F15, // (NEC PC98) 141 Kana = OIS::KC_KANA, // (Japanese keyboard) 142 ABNT_C1 = OIS::KC_ABNT_C1, // / ? on Portugese (Brazilian) keyboards 143 Convert = OIS::KC_CONVERT, // (Japanese keyboard) 144 NoConvert = OIS::KC_NOCONVERT, // (Japanese keyboard) 145 Yen = OIS::KC_YEN, // (Japanese keyboard) 146 ABNT_C2 = OIS::KC_ABNT_C2, // Numpad . on Portugese (Brazilian) keyboards 147 NumpadEquals = OIS::KC_NUMPADEQUALS, // = on numeric keypad (NEC PC98) 148 PreviousTrack = OIS::KC_PREVTRACK, // Previous Track (KC_CIRCUMFLEX on Japanese keyboard) 149 AT = OIS::KC_AT, // (NEC PC98) 150 Colon = OIS::KC_COLON, // (NEC PC98) 151 Underline = OIS::KC_UNDERLINE, // (NEC PC98) 152 Kanji = OIS::KC_KANJI, // (Japanese keyboard) 153 Stop = OIS::KC_STOP, // (NEC PC98) 154 AX = OIS::KC_AX, // (Japan AX) 155 Unlabeled = OIS::KC_UNLABELED, // (J3100) 156 NextTrack = OIS::KC_NEXTTRACK, // Next Track 157 NumpadEnter = OIS::KC_NUMPADENTER, // Enter on numeric keypad 158 RightControl = OIS::KC_RCONTROL, 159 Mute = OIS::KC_MUTE, // Mute 160 Calculator = OIS::KC_CALCULATOR, // Calculator 161 PlayPause = OIS::KC_PLAYPAUSE, // Play / Pause 162 MediaStop = OIS::KC_MEDIASTOP, // Media Stop 163 VolumeDown = OIS::KC_VOLUMEDOWN, // Volume - 164 VolumeUp = OIS::KC_VOLUMEUP, // Volume + 165 WebHome = OIS::KC_WEBHOME, // Web home 166 NumpadComma = OIS::KC_NUMPADCOMMA, // , on numeric keypad (NEC PC98) 167 Divide = OIS::KC_DIVIDE, // / on numeric keypad 168 SYSRQ = OIS::KC_SYSRQ, 169 RightAlt = OIS::KC_RMENU, // right Alt 170 Pause = OIS::KC_PAUSE, // Pause 171 Home = OIS::KC_HOME, // Home on arrow keypad 172 Up = OIS::KC_UP, // UpArrow on arrow keypad 173 PageUp = OIS::KC_PGUP, // PgUp on arrow keypad 174 Left = OIS::KC_LEFT, // LeftArrow on arrow keypad 175 Right = OIS::KC_RIGHT, // RightArrow on arrow keypad 176 End = OIS::KC_END, // End on arrow keypad 177 Down = OIS::KC_DOWN, // DownArrow on arrow keypad 178 PageDown = OIS::KC_PGDOWN, // PgDn on arrow keypad 179 Insert = OIS::KC_INSERT, // Insert on arrow keypad 180 Delete = OIS::KC_DELETE, // Delete on arrow keypad 181 LeftWindows = OIS::KC_LWIN, // Left Windows key 182 RightWindows = OIS::KC_RWIN, // Right Windows key 183 Apps = OIS::KC_APPS, // AppMenu key 184 Power = OIS::KC_POWER, // System Power 185 Sleep = OIS::KC_SLEEP, // System Sleep 186 Wake = OIS::KC_WAKE, // System Wake 187 WebSearch = OIS::KC_WEBSEARCH, // Web Search 188 WebFavorites = OIS::KC_WEBFAVORITES, // Web Favorites 189 WebRefresh = OIS::KC_WEBREFRESH, // Web Refresh 190 WebStop = OIS::KC_WEBSTOP, // Web Stop 191 WebForward = OIS::KC_WEBFORWARD, // Web Forward 192 WebBack = OIS::KC_WEBBACK, // Web Back 193 MyComputer = OIS::KC_MYCOMPUTER, // My Computer 194 Mail = OIS::KC_MAIL, // Mail 195 MediaSelect = OIS::KC_MEDIASELECT // Media Select 196 }; 197 } 198 199 namespace MouseButton 200 { 201 enum Enum 202 { 203 Left = OIS::MB_Left, 204 Right = OIS::MB_Right, 205 Middle = OIS::MB_Middle, 206 Button3 = OIS::MB_Button3, 207 Button4 = OIS::MB_Button4, 208 Button5 = OIS::MB_Button5, 209 Button6 = OIS::MB_Button6, 210 Button7 = OIS::MB_Button7 211 }; 212 } 213 214 namespace KeyboardModifier 215 { 216 enum Enum 217 { 218 Shift = 0x0000001, 219 Ctrl = 0x0000010, 220 Alt = 0x0000100 221 }; 222 } 223 224 struct _CoreExport Key 225 { 226 Key(const OIS::KeyEvent& evt) : key((KeyCode::Enum)evt.key), text(evt.text) { } 227 KeyCode::Enum key; 228 unsigned int text; 229 }; 230 231 class _CoreExport KeyEvent 232 { 233 public: 234 KeyEvent(KeyCode::Enum key, unsigned int text) : key(key), text(text) { } 235 KeyEvent(const OIS::KeyEvent& evt, unsigned int mod) : key((KeyCode::Enum)evt.key), text(evt.text), modifiers(mod) { } 236 KeyEvent(const Key& key, unsigned int mod) : key(key.key), text(key.text), modifiers(mod) { } 237 bool isModifierDown(KeyboardModifier::Enum modifier) const { return (KeyboardModifier::Enum)modifier&modifiers; } 238 239 const KeyCode::Enum key; 240 unsigned int text; 241 unsigned int modifiers; 242 }; 243 244 //typedef OIS::MouseState MouseState; 245 246 /*class _CoreExport JoyStickState 247 { 248 public: 249 JoyStickState(const OIS::JoyStickState& state, int ID) : OIS::JoyStickState(state), mJoyStickID(ID) { } 250 JoyStickState() { clear(); } 251 int mJoyStickID; 252 JoyStickState() { clear(); } 253 254 std::vector<bool> mButtons; 255 int axes[16]; 256 std::vector<Vector3> mVectors; 257 };*/ 258 259 /** 260 * Helper struct to determine which handlers of an object (can implement 261 * multiple handlers) are active. 262 */ 263 struct HandlerState 264 { 265 HandlerState() : key(false), mouse(false), joyStick(false) { } 266 bool key; 267 bool mouse; 268 bool joyStick; 269 }; 270 271 class _CoreExport InputTickable 272 { 273 public: 274 virtual ~InputTickable() { } 275 virtual void tickInput(float dt, const HandlerState& state) = 0; 276 }; 277 278 /** 279 @brief Interface class used for key input listeners. 280 */ 281 class _CoreExport KeyHandler : virtual public InputTickable 282 { 283 public: 284 virtual ~KeyHandler() { } 285 virtual void keyPressed (const KeyEvent& evt) = 0; 286 virtual void keyReleased(const KeyEvent& evt) = 0; 287 virtual void keyHeld (const KeyEvent& evt) = 0; 288 //virtual void tickKey (float dt) { } 289 }; 290 291 /** 292 @brief Interface class used for mouse input listeners. 293 */ 294 class _CoreExport MouseHandler : virtual public InputTickable 295 { 296 public: 297 virtual ~MouseHandler() { } 298 virtual void mouseButtonPressed (MouseButton::Enum id) = 0; 299 virtual void mouseButtonReleased(MouseButton::Enum id) = 0; 300 virtual void mouseButtonHeld (MouseButton::Enum id) = 0; 301 virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) = 0; 302 virtual void mouseScrolled (int abs, int rel) = 0; 303 //virtual void tickMouse (float dt) { } 304 }; 305 306 307 /** 308 @brief Interface class used for joy stick input listeners. 309 */ 310 class _CoreExport JoyStickHandler : virtual public InputTickable 311 { 312 public: 313 virtual ~JoyStickHandler() { } 314 virtual void joyStickButtonPressed (int joyStickID, int button) = 0; 315 virtual void joyStickButtonReleased(int joyStickID, int button) = 0; 316 virtual void joyStickButtonHeld (int joyStickID, int button) = 0; 317 virtual void joyStickAxisMoved (int joyStickID, int axis, float value) = 0; 318 //virtual bool joyStickVector3Moved (int joyStickID, int index /*, fill list*/) {return true;} 319 //virtual void tickJoyStick (float dt) { } 320 }; 47 namespace KeyCode 48 { 49 // note: KeyCode comments were directly copied from OISKeyboard.h 50 enum Enum 51 { 52 Unassigned = OIS::KC_UNASSIGNED, 53 Escape = OIS::KC_ESCAPE, 54 NumRow1 = OIS::KC_1, 55 NumRow2 = OIS::KC_2, 56 NumRow3 = OIS::KC_3, 57 NumRow4 = OIS::KC_4, 58 NumRow5 = OIS::KC_5, 59 NumRow6 = OIS::KC_6, 60 NumRow7 = OIS::KC_7, 61 NumRow8 = OIS::KC_8, 62 NumRow9 = OIS::KC_9, 63 NumRow0 = OIS::KC_0, 64 Minus = OIS::KC_MINUS, // - on main keyboard 65 Equals = OIS::KC_EQUALS, 66 Back = OIS::KC_BACK, // backspace 67 Tab = OIS::KC_TAB, 68 Q = OIS::KC_Q, 69 W = OIS::KC_W, 70 E = OIS::KC_E, 71 R = OIS::KC_R, 72 T = OIS::KC_T, 73 Y = OIS::KC_Y, 74 U = OIS::KC_U, 75 I = OIS::KC_I, 76 O = OIS::KC_O, 77 P = OIS::KC_P, 78 LeftBracket = OIS::KC_LBRACKET, 79 RightBracket = OIS::KC_RBRACKET, 80 Return = OIS::KC_RETURN, // Enter on main keyboard 81 LeftControl = OIS::KC_LCONTROL, 82 A = OIS::KC_A, 83 S = OIS::KC_S, 84 D = OIS::KC_D, 85 F = OIS::KC_F, 86 G = OIS::KC_G, 87 H = OIS::KC_H, 88 J = OIS::KC_J, 89 K = OIS::KC_K, 90 L = OIS::KC_L, 91 Semicolon = OIS::KC_SEMICOLON, 92 Apostrophe = OIS::KC_APOSTROPHE, 93 Grave = OIS::KC_GRAVE, // accent 94 LeftShift = OIS::KC_LSHIFT, 95 Backslash = OIS::KC_BACKSLASH, 96 Z = OIS::KC_Z, 97 X = OIS::KC_X, 98 C = OIS::KC_C, 99 V = OIS::KC_V, 100 B = OIS::KC_B, 101 N = OIS::KC_N, 102 M = OIS::KC_M, 103 Comma = OIS::KC_COMMA, 104 Period = OIS::KC_PERIOD, // . on main keyboard 105 Slash = OIS::KC_SLASH, // / on main keyboard 106 RightShift = OIS::KC_RSHIFT, 107 Multiply = OIS::KC_MULTIPLY, // * on numeric keypad 108 LeftAlt = OIS::KC_LMENU, // left Alt 109 Space = OIS::KC_SPACE, 110 CapsLock = OIS::KC_CAPITAL, 111 F1 = OIS::KC_F1, 112 F2 = OIS::KC_F2, 113 F3 = OIS::KC_F3, 114 F4 = OIS::KC_F4, 115 F5 = OIS::KC_F5, 116 F6 = OIS::KC_F6, 117 F7 = OIS::KC_F7, 118 F8 = OIS::KC_F8, 119 F9 = OIS::KC_F9, 120 F10 = OIS::KC_F10, 121 Numlock = OIS::KC_NUMLOCK, 122 Scrolllock = OIS::KC_SCROLL, // Scroll Lock 123 Numpad7 = OIS::KC_NUMPAD7, 124 Numpad8 = OIS::KC_NUMPAD8, 125 Numpad9 = OIS::KC_NUMPAD9, 126 NumpadSubtract= OIS::KC_SUBTRACT, // - on numeric keypad 127 Numpad4 = OIS::KC_NUMPAD4, 128 Numpad5 = OIS::KC_NUMPAD5, 129 Numpad6 = OIS::KC_NUMPAD6, 130 NumpadAdd = OIS::KC_ADD, // + on numeric keypad 131 Numpad1 = OIS::KC_NUMPAD1, 132 Numpad2 = OIS::KC_NUMPAD2, 133 Numpad3 = OIS::KC_NUMPAD3, 134 Numpad0 = OIS::KC_NUMPAD0, 135 NumpadPeriod = OIS::KC_DECIMAL, // . on numeric keypad 136 LessThan = OIS::KC_OEM_102, // < > | on UK/Germany keyboards 137 F11 = OIS::KC_F11, 138 F12 = OIS::KC_F12, 139 F13 = OIS::KC_F13, // (NEC PC98) 140 F14 = OIS::KC_F14, // (NEC PC98) 141 F15 = OIS::KC_F15, // (NEC PC98) 142 Kana = OIS::KC_KANA, // (Japanese keyboard) 143 ABNT_C1 = OIS::KC_ABNT_C1, // / ? on Portugese (Brazilian) keyboards 144 Convert = OIS::KC_CONVERT, // (Japanese keyboard) 145 NoConvert = OIS::KC_NOCONVERT, // (Japanese keyboard) 146 Yen = OIS::KC_YEN, // (Japanese keyboard) 147 ABNT_C2 = OIS::KC_ABNT_C2, // Numpad . on Portugese (Brazilian) keyboards 148 NumpadEquals = OIS::KC_NUMPADEQUALS, // = on numeric keypad (NEC PC98) 149 PreviousTrack = OIS::KC_PREVTRACK, // Previous Track (KC_CIRCUMFLEX on Japanese keyboard) 150 AT = OIS::KC_AT, // (NEC PC98) 151 Colon = OIS::KC_COLON, // (NEC PC98) 152 Underline = OIS::KC_UNDERLINE, // (NEC PC98) 153 Kanji = OIS::KC_KANJI, // (Japanese keyboard) 154 Stop = OIS::KC_STOP, // (NEC PC98) 155 AX = OIS::KC_AX, // (Japan AX) 156 Unlabeled = OIS::KC_UNLABELED, // (J3100) 157 NextTrack = OIS::KC_NEXTTRACK, // Next Track 158 NumpadEnter = OIS::KC_NUMPADENTER, // Enter on numeric keypad 159 RightControl = OIS::KC_RCONTROL, 160 Mute = OIS::KC_MUTE, // Mute 161 Calculator = OIS::KC_CALCULATOR, // Calculator 162 PlayPause = OIS::KC_PLAYPAUSE, // Play / Pause 163 MediaStop = OIS::KC_MEDIASTOP, // Media Stop 164 VolumeDown = OIS::KC_VOLUMEDOWN, // Volume - 165 VolumeUp = OIS::KC_VOLUMEUP, // Volume + 166 WebHome = OIS::KC_WEBHOME, // Web home 167 NumpadComma = OIS::KC_NUMPADCOMMA, // , on numeric keypad (NEC PC98) 168 Divide = OIS::KC_DIVIDE, // / on numeric keypad 169 SYSRQ = OIS::KC_SYSRQ, 170 RightAlt = OIS::KC_RMENU, // right Alt 171 Pause = OIS::KC_PAUSE, // Pause 172 Home = OIS::KC_HOME, // Home on arrow keypad 173 Up = OIS::KC_UP, // UpArrow on arrow keypad 174 PageUp = OIS::KC_PGUP, // PgUp on arrow keypad 175 Left = OIS::KC_LEFT, // LeftArrow on arrow keypad 176 Right = OIS::KC_RIGHT, // RightArrow on arrow keypad 177 End = OIS::KC_END, // End on arrow keypad 178 Down = OIS::KC_DOWN, // DownArrow on arrow keypad 179 PageDown = OIS::KC_PGDOWN, // PgDn on arrow keypad 180 Insert = OIS::KC_INSERT, // Insert on arrow keypad 181 Delete = OIS::KC_DELETE, // Delete on arrow keypad 182 LeftWindows = OIS::KC_LWIN, // Left Windows key 183 RightWindows = OIS::KC_RWIN, // Right Windows key 184 Apps = OIS::KC_APPS, // AppMenu key 185 Power = OIS::KC_POWER, // System Power 186 Sleep = OIS::KC_SLEEP, // System Sleep 187 Wake = OIS::KC_WAKE, // System Wake 188 WebSearch = OIS::KC_WEBSEARCH, // Web Search 189 WebFavorites = OIS::KC_WEBFAVORITES, // Web Favorites 190 WebRefresh = OIS::KC_WEBREFRESH, // Web Refresh 191 WebStop = OIS::KC_WEBSTOP, // Web Stop 192 WebForward = OIS::KC_WEBFORWARD, // Web Forward 193 WebBack = OIS::KC_WEBBACK, // Web Back 194 MyComputer = OIS::KC_MYCOMPUTER, // My Computer 195 Mail = OIS::KC_MAIL, // Mail 196 MediaSelect = OIS::KC_MEDIASELECT // Media Select 197 }; 198 } 199 200 namespace MouseButton 201 { 202 enum Enum 203 { 204 Left = OIS::MB_Left, 205 Right = OIS::MB_Right, 206 Middle = OIS::MB_Middle, 207 Button3 = OIS::MB_Button3, 208 Button4 = OIS::MB_Button4, 209 Button5 = OIS::MB_Button5, 210 Button6 = OIS::MB_Button6, 211 Button7 = OIS::MB_Button7 212 }; 213 } 214 215 namespace KeyboardModifier 216 { 217 enum Enum 218 { 219 Shift = 0x0000001, 220 Ctrl = 0x0000010, 221 Alt = 0x0000100 222 }; 223 } 224 225 struct _CoreExport Key 226 { 227 Key(const OIS::KeyEvent& evt) : key((KeyCode::Enum)evt.key), text(evt.text) { } 228 KeyCode::Enum key; 229 unsigned int text; 230 }; 231 232 class _CoreExport KeyEvent 233 { 234 public: 235 KeyEvent(KeyCode::Enum key, unsigned int text) : key(key), text(text) { } 236 KeyEvent(const OIS::KeyEvent& evt, unsigned int mod) 237 : key((KeyCode::Enum)evt.key), text(evt.text), modifiers(mod) { } 238 KeyEvent(const Key& key, unsigned int mod) : key(key.key), text(key.text), modifiers(mod) { } 239 bool isModifierDown(KeyboardModifier::Enum modifier) const 240 { return (KeyboardModifier::Enum)modifier&modifiers; } 241 242 const KeyCode::Enum key; 243 unsigned int text; 244 unsigned int modifiers; 245 }; 246 247 //typedef OIS::MouseState MouseState; 248 249 /*class _CoreExport JoyStickState 250 { 251 public: 252 JoyStickState(const OIS::JoyStickState& state, int ID) : OIS::JoyStickState(state), mJoyStickID(ID) { } 253 JoyStickState() { clear(); } 254 int mJoyStickID; 255 JoyStickState() { clear(); } 256 257 std::vector<bool> mButtons; 258 int axes[16]; 259 std::vector<Vector3> mVectors; 260 };*/ 261 262 /** 263 @brief 264 Helper struct to determine which handlers of an object (can implement 265 multiple handlers) are active. 266 */ 267 struct HandlerState 268 { 269 HandlerState() : key(false), mouse(false), joyStick(false) { } 270 bool key; 271 bool mouse; 272 bool joyStick; 273 }; 274 275 class _CoreExport InputTickable 276 { 277 public: 278 virtual ~InputTickable() { } 279 virtual void tickInput(float dt, const HandlerState& state) = 0; 280 }; 281 282 /** 283 @brief 284 Interface class used for key input listeners. 285 */ 286 class _CoreExport KeyHandler : virtual public InputTickable 287 { 288 public: 289 virtual ~KeyHandler() { } 290 virtual void keyPressed (const KeyEvent& evt) = 0; 291 virtual void keyReleased(const KeyEvent& evt) = 0; 292 virtual void keyHeld (const KeyEvent& evt) = 0; 293 //virtual void tickKey (float dt) { } 294 }; 295 296 /** 297 @brief 298 Interface class used for mouse input listeners. 299 */ 300 class _CoreExport MouseHandler : virtual public InputTickable 301 { 302 public: 303 virtual ~MouseHandler() { } 304 virtual void mouseButtonPressed (MouseButton::Enum id) = 0; 305 virtual void mouseButtonReleased(MouseButton::Enum id) = 0; 306 virtual void mouseButtonHeld (MouseButton::Enum id) = 0; 307 virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) = 0; 308 virtual void mouseScrolled (int abs, int rel) = 0; 309 //virtual void tickMouse (float dt) { } 310 }; 311 312 313 /** 314 @brief 315 Interface class used for joy stick input listeners. 316 */ 317 class _CoreExport JoyStickHandler : virtual public InputTickable 318 { 319 public: 320 virtual ~JoyStickHandler() { } 321 virtual void joyStickButtonPressed (int joyStickID, int button) = 0; 322 virtual void joyStickButtonReleased(int joyStickID, int button) = 0; 323 virtual void joyStickButtonHeld (int joyStickID, int button) = 0; 324 virtual void joyStickAxisMoved (int joyStickID, int axis, float value) = 0; 325 //virtual bool joyStickVector3Moved (int joyStickID, int index /*, fill list*/) {return true;} 326 //virtual void tickJoyStick (float dt) { } 327 }; 321 328 322 329 } -
code/branches/input/src/core/input/InputManager.cc
r1629 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the InputManager that captures all the input from OIS 32 and redirects it to handlers. 30 @file 31 @brief 32 Implementation of the InputManager that captures all the input from OIS 33 and redirects it to handlers. 33 34 */ 34 35 … … 54 55 namespace orxonox 55 56 { 56 SetConsoleCommandShortcut(InputManager, keyBind); 57 SetConsoleCommandShortcut(InputManager, storeKeyStroke); 58 SetConsoleCommandShortcut(InputManager, calibrate); 59 60 // ############################### 61 // ### Internal Methods ### 62 // ############################### 63 // ############################### 64 65 /** 66 @brief Constructor only sets member fields to initial zero values 67 and registers the class in the class hierarchy. 68 */ 69 InputManager::InputManager() : 70 inputSystem_(0), keyboard_(0), mouse_(0), 71 joySticksSize_(0), 72 keyBinder_(0), keyDetector_(0), buffer_(0), calibratorCallback_(0), 73 state_(IS_UNINIT), stateRequest_(IS_UNINIT), savedState_(IS_UNINIT), 74 keyboardModifiers_(0) 75 { 76 RegisterRootObject(InputManager); 77 } 78 79 /** 80 @brief The one instance of the InputManager is stored in this function. 81 @return A reference to the only instance of the InputManager 82 */ 83 InputManager& InputManager::_getSingleton() 84 { 85 static InputManager theOnlyInstance; 86 return theOnlyInstance; 87 } 88 89 /** 90 @brief Destructor only called at the end of the program, after main. 91 */ 92 InputManager::~InputManager() 93 { 94 _destroy(); 95 } 96 97 /** 98 @brief Creates the OIS::InputMananger, the keyboard, the mouse and 99 the joysticks and assigns the key bindings. 100 @param windowHnd The window handle of the render window 101 @param windowWidth The width of the render window 102 @param windowHeight The height of the render window 103 */ 104 bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight, 57 SetConsoleCommandShortcut(InputManager, keyBind); 58 SetConsoleCommandShortcut(InputManager, storeKeyStroke); 59 SetConsoleCommandShortcut(InputManager, calibrate); 60 61 // ############################### 62 // ### Internal Methods ### 63 // ############################### 64 // ############################### 65 66 /** 67 @brief 68 Constructor only sets member fields to initial zero values 69 and registers the class in the class hierarchy. 70 */ 71 InputManager::InputManager() 72 : inputSystem_(0) 73 , keyboard_(0) 74 , mouse_(0) 75 , joySticksSize_(0) 76 , keyBinder_(0) 77 , keyDetector_(0) 78 , buffer_(0) 79 , calibratorCallback_(0) 80 , state_(IS_UNINIT) 81 , stateRequest_(IS_UNINIT) 82 , savedState_(IS_UNINIT) 83 , keyboardModifiers_(0) 84 { 85 RegisterRootObject(InputManager); 86 } 87 88 /** 89 @brief 90 The one instance of the InputManager is stored in this function. 91 @return 92 A reference to the only instance of the InputManager 93 */ 94 InputManager& InputManager::_getSingleton() 95 { 96 static InputManager theOnlyInstance; 97 return theOnlyInstance; 98 } 99 100 /** 101 @brief 102 Destructor only called at the end of the program, after main. 103 */ 104 InputManager::~InputManager() 105 { 106 _destroy(); 107 } 108 109 /** 110 @brief 111 Creates the OIS::InputMananger, the keyboard, the mouse and 112 the joysticks and assigns the key bindings. 113 @param windowHnd 114 The window handle of the render window 115 @param windowWidth 116 The width of the render window 117 @param windowHeight 118 The height of the render window 119 */ 120 bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight, 121 bool createKeyboard, bool createMouse, bool createJoySticks) 122 { 123 if (state_ == IS_UNINIT) 124 { 125 CCOUT(3) << "Initialising Input System..." << std::endl; 126 CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl; 127 128 OIS::ParamList paramList; 129 std::ostringstream windowHndStr; 130 131 // Fill parameter list 132 windowHndStr << (unsigned int)windowHnd; 133 paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); 134 //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); 135 //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); 136 //#if defined OIS_LINUX_PLATFORM 137 //paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); 138 //#endif 139 140 try 141 { 142 inputSystem_ = OIS::InputManager::createInputSystem(paramList); 143 CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; 144 } 145 catch (OIS::Exception ex) 146 { 147 CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." 148 << "OIS message: \"" << ex.eText << "\"" << std::endl; 149 inputSystem_ = 0; 150 return false; 151 } 152 153 if (createKeyboard) 154 _initialiseKeyboard(); 155 156 if (createMouse) 157 _initialiseMouse(); 158 159 if (createJoySticks) 160 _initialiseJoySticks(); 161 162 // Set mouse/joystick region 163 if (mouse_) 164 { 165 setWindowExtents(windowWidth, windowHeight); 166 } 167 168 state_ = IS_NONE; 169 CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; 170 171 // InputManager holds the input buffer --> create one and add it. 172 buffer_ = new InputBuffer(); 173 addKeyHandler(buffer_, "buffer"); 174 Shell::getInstance().setInputBuffer(buffer_); 175 176 keyBinder_ = new KeyBinder(); 177 keyBinder_->loadBindings(); 178 addKeyHandler(keyBinder_, "keybinder"); 179 addMouseHandler(keyBinder_, "keybinder"); 180 addJoyStickHandler(keyBinder_, "keybinder"); 181 182 keyDetector_ = new KeyDetector(); 183 keyDetector_->loadBindings(); 184 addKeyHandler(keyDetector_, "keydetector"); 185 addMouseHandler(keyDetector_, "keydetector"); 186 addJoyStickHandler(keyDetector_, "keydetector"); 187 188 calibratorCallback_ = new CalibratorCallback(); 189 addKeyHandler(calibratorCallback_, "calibratorcallback"); 190 191 setConfigValues(); 192 193 CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; 194 } 195 else 196 { 197 CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl; 198 } 199 return true; 200 } 201 202 /** 203 @brief 204 Creates a keyboard and sets the event handler. 205 @return 206 False if keyboard stays uninitialised, true otherwise. 207 */ 208 bool InputManager::_initialiseKeyboard() 209 { 210 if (keyboard_ != 0) 211 { 212 CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; 213 return true; 214 } 215 try 216 { 217 if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) 218 { 219 keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); 220 // register our listener in OIS. 221 keyboard_->setEventCallback(this); 222 // note: OIS will not detect keys that have already been down when the keyboard was created. 223 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 224 return true; 225 } 226 else 227 { 228 CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; 229 return false; 230 } 231 } 232 catch (OIS::Exception ex) 233 { 234 // TODO: Test this output regarding formatting 235 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" 236 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 237 keyboard_ = 0; 238 return false; 239 } 240 } 241 242 /** 243 @brief 244 Creates a mouse and sets the event handler. 245 @return 246 False if mouse stays uninitialised, true otherwise. 247 */ 248 bool InputManager::_initialiseMouse() 249 { 250 if (mouse_ != 0) 251 { 252 CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; 253 return true; 254 } 255 try 256 { 257 if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) 258 { 259 mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); 260 // register our listener in OIS. 261 mouse_->setEventCallback(this); 262 CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; 263 return true; 264 } 265 else 266 { 267 CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; 268 return false; 269 } 270 } 271 catch (OIS::Exception ex) 272 { 273 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" 274 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 275 mouse_ = 0; 276 return false; 277 } 278 } 279 280 /** 281 @brief 282 Creates all joy sticks and sets the event handler. 283 @return 284 False joy stick stay uninitialised, true otherwise. 285 */ 286 bool InputManager::_initialiseJoySticks() 287 { 288 if (joySticksSize_ > 0) 289 { 290 CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; 291 return true; 292 } 293 bool success = false; 294 if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) 295 { 296 for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) 297 { 298 try 299 { 300 OIS::JoyStick* stig = static_cast<OIS::JoyStick*> 301 (inputSystem_->createInputObject(OIS::OISJoyStick, true)); 302 joySticks_.push_back(stig); 303 // register our listener in OIS. 304 stig->setEventCallback(this); 305 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; 306 success = true; 307 } 308 catch (OIS::Exception ex) 309 { 310 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" 311 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 312 } 313 } 314 } 315 else 316 { 317 CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 318 return false; 319 } 320 joySticksSize_ = joySticks_.size(); 321 activeJoyStickHandlers_.resize(joySticksSize_); 322 joyStickButtonsDown_.resize(joySticksSize_); 323 povStates_.resize(joySticksSize_); 324 sliderStates_.resize(joySticksSize_); 325 joySticksCalibration_.resize(joySticksSize_); 326 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 327 { 328 // reset the calibration with default values 329 for (unsigned int i = 0; i < 24; i++) 330 { 331 joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; 332 joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; 333 joySticksCalibration_[iJoyStick].zeroStates[i] = 0; 334 } 335 } 336 return success; 337 } 338 339 /** 340 @brief 341 Sets the configurable values. Use keybindings.ini as file.. 342 */ 343 void InputManager::setConfigValues() 344 { 345 if (joySticksSize_) 346 { 347 std::vector<MultiTypeMath> coeffPos; 348 std::vector<MultiTypeMath> coeffNeg; 349 std::vector<MultiTypeMath> zero; 350 coeffPos.resize(24); 351 coeffNeg.resize(24); 352 zero.resize(24); 353 for (unsigned int i = 0; i < 24; i++) 354 { 355 coeffPos[i] = 1.0f/32767.0f; 356 coeffNeg[i] = 1.0f/32768.0f; 357 zero[i] = 0; 358 } 359 360 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 361 if (!cont) 362 { 363 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffPos", coeffPos); 364 getIdentifier()->addConfigValueContainer("CoeffPos", cont); 365 } 366 cont->getValue(&coeffPos); 367 368 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 369 if (!cont) 370 { 371 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffNeg", coeffNeg); 372 getIdentifier()->addConfigValueContainer("CoeffNeg", cont); 373 } 374 cont->getValue(&coeffNeg); 375 376 cont = getIdentifier()->getConfigValueContainer("Zero"); 377 if (!cont) 378 { 379 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "Zero", zero); 380 getIdentifier()->addConfigValueContainer("Zero", cont); 381 } 382 cont->getValue(&zero); 383 384 // copy values to our own variables 385 for (unsigned int i = 0; i < 24; i++) 386 { 387 joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; 388 joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; 389 joySticksCalibration_[0].zeroStates[i] = zero[i]; 390 } 391 } 392 } 393 394 /** 395 @brief 396 Destroys all the created input devices and sets the InputManager to construction state. 397 */ 398 void InputManager::_destroy() 399 { 400 if (state_ != IS_UNINIT) 401 { 402 CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl; 403 404 if (buffer_) 405 delete buffer_; 406 407 if (keyBinder_) 408 delete keyBinder_; 409 410 if (keyDetector_) 411 delete keyDetector_; 412 413 if (calibratorCallback_) 414 delete calibratorCallback_; 415 416 keyHandlers_.clear(); 417 mouseHandlers_.clear(); 418 joyStickHandlers_.clear(); 419 420 _destroyKeyboard(); 421 _destroyMouse(); 422 _destroyJoySticks(); 423 424 activeHandlers_.clear(); 425 426 // inputSystem_ can never be 0, or else the code is mistaken 427 OIS::InputManager::destroyInputSystem(inputSystem_); 428 inputSystem_ = 0; 429 430 state_ = IS_UNINIT; 431 CCOUT(ORX_DEBUG) << "Destroying done." << std::endl; 432 } 433 } 434 435 /** 436 @brief 437 Destroys the keyboard and sets it to 0. 438 */ 439 void InputManager::_destroyKeyboard() 440 { 441 if (keyboard_) 442 inputSystem_->destroyInputObject(keyboard_); 443 keyboard_ = 0; 444 activeKeyHandlers_.clear(); 445 keysDown_.clear(); 446 CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; 447 } 448 449 /** 450 @brief 451 Destroys the mouse and sets it to 0. 452 */ 453 void InputManager::_destroyMouse() 454 { 455 if (mouse_) 456 inputSystem_->destroyInputObject(mouse_); 457 mouse_ = 0; 458 activeMouseHandlers_.clear(); 459 mouseButtonsDown_.clear(); 460 CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; 461 } 462 463 /** 464 @brief 465 Destroys all the joy sticks and resizes the lists to 0. 466 */ 467 void InputManager::_destroyJoySticks() 468 { 469 if (joySticksSize_ > 0) 470 { 471 // note: inputSystem_ can never be 0, or else the code is mistaken 472 for (unsigned int i = 0; i < joySticksSize_; i++) 473 if (joySticks_[i] != 0) 474 inputSystem_->destroyInputObject(joySticks_[i]); 475 476 joySticks_.clear(); 477 joySticksSize_ = 0; 478 activeJoyStickHandlers_.clear(); 479 joyStickButtonsDown_.clear(); 480 povStates_.clear(); 481 sliderStates_.clear(); 482 joySticksCalibration_.clear(); 483 } 484 CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; 485 } 486 487 void InputManager::_saveState() 488 { 489 savedHandlers_.activeHandlers_ = activeHandlers_; 490 savedHandlers_.activeJoyStickHandlers_ = activeJoyStickHandlers_; 491 savedHandlers_.activeKeyHandlers_ = activeKeyHandlers_; 492 savedHandlers_.activeMouseHandlers_ = activeMouseHandlers_; 493 } 494 495 void InputManager::_restoreState() 496 { 497 activeHandlers_ = savedHandlers_.activeHandlers_; 498 activeJoyStickHandlers_ = savedHandlers_.activeJoyStickHandlers_; 499 activeKeyHandlers_ = savedHandlers_.activeKeyHandlers_; 500 activeMouseHandlers_ = savedHandlers_.activeMouseHandlers_; 501 } 502 503 void InputManager::_updateTickables() 504 { 505 // we can use a map to have a list of unique pointers (an object can implement all 3 handlers) 506 std::map<InputTickable*, HandlerState> tempSet; 507 for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) 508 tempSet[activeKeyHandlers_[iHandler]].joyStick = true; 509 for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) 510 tempSet[activeMouseHandlers_[iHandler]].mouse = true; 511 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 512 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 513 tempSet[activeJoyStickHandlers_[iJoyStick][iHandler]].joyStick = true; 514 515 // copy the content of the map back to the actual vector 516 activeHandlers_.clear(); 517 for (std::map<InputTickable*, HandlerState>::const_iterator itHandler = tempSet.begin(); 518 itHandler != tempSet.end(); itHandler++) 519 activeHandlers_.push_back(std::pair<InputTickable*, HandlerState>((*itHandler).first, (*itHandler).second)); 520 } 521 522 523 // ################################# 524 // ### Private Interface Methods ### 525 // ################################# 526 // ################################# 527 528 /** 529 @brief 530 Updates the InputManager. Tick is called by Orxonox. 531 @param dt 532 Delta time 533 */ 534 void InputManager::_tick(float dt) 535 { 536 if (state_ == IS_UNINIT) 537 return; 538 539 if (state_ != stateRequest_) 540 { 541 InputState sr = stateRequest_; 542 switch (sr) 543 { 544 case IS_NORMAL: 545 activeKeyHandlers_.clear(); 546 activeMouseHandlers_.clear(); 547 for (unsigned int i = 0; i < joySticksSize_; i++) 548 activeJoyStickHandlers_[i].clear(); 549 550 // normal play mode 551 // note: we assume that the handlers exist since otherwise, something's wrong anyway. 552 enableKeyHandler("keybinder"); 553 enableMouseHandler("keybinder"); 554 enableJoyStickHandler("keybinder", 0); 555 stateRequest_ = IS_NORMAL; 556 state_ = IS_NORMAL; 557 break; 558 559 case IS_GUI: 560 state_ = IS_GUI; 561 break; 562 563 case IS_CONSOLE: 564 activeKeyHandlers_.clear(); 565 activeMouseHandlers_.clear(); 566 for (unsigned int i = 0; i < joySticksSize_; i++) 567 activeJoyStickHandlers_[i].clear(); 568 569 enableMouseHandler("keybinder"); 570 enableJoyStickHandler("keybinder", 0); 571 enableKeyHandler("buffer"); 572 stateRequest_ = IS_CONSOLE; 573 state_ = IS_CONSOLE; 574 break; 575 576 case IS_DETECT: 577 savedState_ = state_; 578 _saveState(); 579 580 activeKeyHandlers_.clear(); 581 activeMouseHandlers_.clear(); 582 for (unsigned int i = 0; i < joySticksSize_; i++) 583 activeJoyStickHandlers_[i].clear(); 584 585 enableKeyHandler("keydetector"); 586 enableMouseHandler("keydetector"); 587 enableJoyStickHandler("keydetector", 0); 588 589 stateRequest_ = IS_DETECT; 590 state_ = IS_DETECT; 591 break; 592 593 case IS_NODETECT: 594 _restoreState(); 595 keysDown_.clear(); 596 mouseButtonsDown_.clear(); 597 for (unsigned int i = 0; i < joySticksSize_; i++) 598 joyStickButtonsDown_[i].clear(); 599 state_ = IS_NODETECT; 600 stateRequest_ = savedState_; 601 break; 602 603 case IS_CALIBRATE: 604 if (joySticksSize_) 605 { 606 savedState_ = _getSingleton().state_; 607 for (unsigned int i = 0; i < 24; i++) 608 { 609 marginalsMax_[i] = INT_MIN; 610 marginalsMin_[i] = INT_MAX; 611 } 612 COUT(0) << "Move all joy stick axes in all directions a few times. " 613 << "Then put all axes in zero state and hit enter." << std::endl; 614 615 savedState_ = state_; 616 _saveState(); 617 618 activeKeyHandlers_.clear(); 619 activeMouseHandlers_.clear(); 620 for (unsigned int i = 0; i < joySticksSize_; i++) 621 activeJoyStickHandlers_[i].clear(); 622 623 enableKeyHandler("calibratorcallback"); 624 stateRequest_ = IS_CALIBRATE; 625 state_ = IS_CALIBRATE; 626 } 627 else 628 { 629 COUT(3) << "Connot calibrate, no joy stick found!" << std::endl; 630 stateRequest_ = state_; 631 } 632 break; 633 634 case IS_NOCALIBRATE: 635 _completeCalibration(); 636 _restoreState(); 637 keyBinder_->resetJoyStickAxes(); 638 state_ = IS_NOCALIBRATE; 639 stateRequest_ = savedState_; 640 break; 641 642 case IS_NONE: 643 activeKeyHandlers_.clear(); 644 activeMouseHandlers_.clear(); 645 for (unsigned int i = 0; i < joySticksSize_; i++) 646 activeJoyStickHandlers_[i].clear(); 647 state_ = IS_NONE; 648 649 default: 650 break; 651 } 652 } 653 654 // Capture all the input. This calls the event handlers in InputManager. 655 if (mouse_) 656 mouse_->capture(); 657 if (keyboard_) 658 keyboard_->capture(); 659 for (unsigned int i = 0; i < joySticksSize_; i++) 660 joySticks_[i]->capture(); 661 662 if (state_ != IS_CALIBRATE) 663 { 664 // call all the handlers for the held key events 665 for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) 666 for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) 667 activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_)); 668 669 // call all the handlers for the held mouse button events 670 for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) 671 for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) 672 activeMouseHandlers_[iHandler]->mouseButtonHeld(mouseButtonsDown_[iButton]); 673 674 // call all the handlers for the held joy stick button events 675 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 676 for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) 677 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 678 { 679 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld( 680 iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); 681 } 682 } 683 684 // call the ticks for the handlers (need to be treated specially) 685 for (unsigned int iHandler = 0; iHandler < activeHandlers_.size(); iHandler++) 686 activeHandlers_[iHandler].first->tickInput(dt, activeHandlers_[iHandler].second); 687 } 688 689 void InputManager::_completeCalibration() 690 { 691 for (unsigned int i = 0; i < 24; i++) 692 { 693 // positive coefficient 694 if (marginalsMax_[i] == INT_MIN) 695 marginalsMax_[i] = 32767; 696 // coefficients 697 if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) 698 { 699 joySticksCalibration_[0].positiveCoeff[i] 700 = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); 701 } 702 else 703 joySticksCalibration_[0].positiveCoeff[i] = 1.0f; 704 705 // config value 706 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 707 assert(cont); 708 cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); 709 710 // negative coefficient 711 if (marginalsMin_[i] == INT_MAX) 712 marginalsMin_[i] = -32768; 713 // coefficients 714 if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) 715 { 716 joySticksCalibration_[0].negativeCoeff[i] = -1.0f 717 / (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); 718 } 719 else 720 joySticksCalibration_[0].negativeCoeff[i] = 1.0f; 721 // config value 722 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 723 assert(cont); 724 cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); 725 726 // zero states 727 if (i < 8) 728 { 729 if (!(i & 1)) 730 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; 731 else 732 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; 733 } 734 else 735 { 736 if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) 737 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; 738 else 739 joySticksCalibration_[0].zeroStates[i] = 0; 740 } 741 // config value 742 cont = getIdentifier()->getConfigValueContainer("Zero"); 743 assert(cont); 744 cont->set(i, joySticksCalibration_[0].zeroStates[i]); 745 } 746 } 747 748 // ###### Key Events ###### 749 750 /** 751 @brief 752 Event handler for the keyPressed Event. 753 @param e 754 Event information 755 */ 756 bool InputManager::keyPressed(const OIS::KeyEvent &e) 757 { 758 // check whether the key already is in the list (can happen when focus was lost) 759 unsigned int iKey = 0; 760 while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key) 761 iKey++; 762 if (iKey == keysDown_.size()) 763 keysDown_.push_back(Key(e)); 764 765 // update modifiers 766 if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) 767 keyboardModifiers_ |= KeyboardModifier::Alt; // alt key 768 if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) 769 keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key 770 if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) 771 keyboardModifiers_ |= KeyboardModifier::Shift; // shift key 772 773 for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) 774 activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_)); 775 776 return true; 777 } 778 779 /** 780 @brief 781 Event handler for the keyReleased Event. 782 @param e 783 Event information 784 */ 785 bool InputManager::keyReleased(const OIS::KeyEvent &e) 786 { 787 // remove the key from the keysDown_ list 788 for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) 789 { 790 if (keysDown_[iKey].key == (KeyCode::Enum)e.key) 791 { 792 keysDown_.erase(keysDown_.begin() + iKey); 793 break; 794 } 795 } 796 797 // update modifiers 798 if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) 799 keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key 800 if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) 801 keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key 802 if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) 803 keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key 804 805 for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) 806 activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_)); 807 808 return true; 809 } 810 811 812 // ###### Mouse Events ###### 813 814 /** 815 @brief 816 Event handler for the mouseMoved Event. 817 @param e 818 Event information 819 */ 820 bool InputManager::mouseMoved(const OIS::MouseEvent &e) 821 { 822 // check for actual moved event 823 if (e.state.X.rel != 0 || e.state.Y.rel != 0) 824 { 825 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 826 { 827 activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs), 828 IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height)); 829 } 830 } 831 832 // check for mouse scrolled event 833 if (e.state.Z.rel != 0) 834 { 835 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 836 activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); 837 } 838 839 return true; 840 } 841 842 /** 843 @brief 844 Event handler for the mousePressed Event. 845 @param e 846 Event information 847 @param id 848 The ID of the mouse button 849 */ 850 bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 851 { 852 // check whether the button already is in the list (can happen when focus was lost) 853 unsigned int iButton = 0; 854 while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id) 855 iButton++; 856 if (iButton == mouseButtonsDown_.size()) 857 mouseButtonsDown_.push_back((MouseButton::Enum)id); 858 859 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 860 activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id); 861 862 return true; 863 } 864 865 /** 866 @brief 867 Event handler for the mouseReleased Event. 868 @param e 869 Event information 870 @param id 871 The ID of the mouse button 872 */ 873 bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 874 { 875 // remove the button from the keysDown_ list 876 for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) 877 { 878 if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id) 879 { 880 mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); 881 break; 882 } 883 } 884 885 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 886 activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id); 887 888 return true; 889 } 890 891 892 // ###### Joy Stick Events ###### 893 894 inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) 895 { 896 // use the device to identify which one called the method 897 OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; 898 unsigned int iJoyStick = 0; 899 while (joySticks_[iJoyStick] != joyStick) 900 { 901 iJoyStick++; 902 if (iJoyStick == joySticksSize_) 903 { 904 CCOUT(3) << "Unknown joystick fired an event. This means there is a bug somewhere! Aborting." << std::endl; 905 abort(); 906 } 907 } 908 return iJoyStick; 909 } 910 911 bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) 912 { 913 unsigned int iJoyStick = _getJoystick(arg); 914 915 // check whether the button already is in the list (can happen when focus was lost) 916 std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; 917 unsigned int iButton = 0; 918 while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) 919 iButton++; 920 if (iButton == buttonsDown.size()) 921 buttonsDown.push_back(button); 922 923 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 924 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button); 925 926 return true; 927 } 928 929 bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) 930 { 931 unsigned int iJoyStick = _getJoystick(arg); 932 933 // remove the button from the joyStickButtonsDown_ list 934 std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; 935 for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) 936 { 937 if (buttonsDown[iButton] == button) 938 { 939 buttonsDown.erase(buttonsDown.begin() + iButton); 940 break; 941 } 942 } 943 944 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 945 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button); 946 947 return true; 948 } 949 950 void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) 951 { 952 if (state_ == IS_CALIBRATE) 953 { 954 if (value > marginalsMax_[axis]) 955 marginalsMax_[axis] = value; 956 if (value < marginalsMin_[axis]) 957 marginalsMin_[axis] = value; 958 } 959 else 960 { 961 float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis]; 962 if (fValue > 0.0f) 963 fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis]; 964 else 965 fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis]; 966 967 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 968 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue); 969 } 970 } 971 972 bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) 973 { 974 //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000) 975 //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;} 976 977 unsigned int iJoyStick = _getJoystick(arg); 978 979 // keep in mind that the first 8 axes are reserved for the sliders 980 _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs); 981 982 return true; 983 } 984 985 bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) 986 { 987 //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000) 988 //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;} 989 //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl; 990 991 unsigned int iJoyStick = _getJoystick(arg); 992 993 if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) 994 _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); 995 else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) 996 _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); 997 998 return true; 999 } 1000 1001 bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) 1002 { 1003 unsigned int iJoyStick = _getJoystick(arg); 1004 1005 // translate the POV into 8 simple buttons 1006 int lastState = povStates_[iJoyStick][id]; 1007 if (lastState & OIS::Pov::North) 1008 buttonReleased(arg, 32 + id * 4 + 0); 1009 if (lastState & OIS::Pov::South) 1010 buttonReleased(arg, 32 + id * 4 + 1); 1011 if (lastState & OIS::Pov::East) 1012 buttonReleased(arg, 32 + id * 4 + 2); 1013 if (lastState & OIS::Pov::West) 1014 buttonReleased(arg, 32 + id * 4 + 3); 1015 1016 povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; 1017 1018 int currentState = povStates_[iJoyStick][id]; 1019 if (currentState & OIS::Pov::North) 1020 buttonPressed(arg, 32 + id * 4 + 0); 1021 if (currentState & OIS::Pov::South) 1022 buttonPressed(arg, 32 + id * 4 + 1); 1023 if (currentState & OIS::Pov::East) 1024 buttonPressed(arg, 32 + id * 4 + 2); 1025 if (currentState & OIS::Pov::West) 1026 buttonPressed(arg, 32 + id * 4 + 3); 1027 1028 return true; 1029 } 1030 1031 /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id) 1032 { 1033 unsigned int iJoyStick = _getJoystick(arg); 1034 1035 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 1036 { 1037 activeJoyStickHandlers_[iJoyStick][iHandler] 1038 ->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id); 1039 } 1040 1041 return true; 1042 }*/ 1043 1044 1045 // ################################ 1046 // ### Static Interface Methods ### 1047 // ################################ 1048 // ################################ 1049 1050 std::string InputManager::bindingCommmandString_s = ""; 1051 1052 bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight, 105 1053 bool createKeyboard, bool createMouse, bool createJoySticks) 106 { 107 if (state_ == IS_UNINIT) 108 { 109 CCOUT(3) << "Initialising Input System..." << std::endl; 110 CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl; 111 112 OIS::ParamList paramList; 113 std::ostringstream windowHndStr; 114 115 // Fill parameter list 116 windowHndStr << (unsigned int)windowHnd; 117 paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); 118 //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); 119 //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); 120 //#if defined OIS_LINUX_PLATFORM 121 // paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); 122 //#endif 123 124 try 125 { 126 inputSystem_ = OIS::InputManager::createInputSystem(paramList); 127 CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; 128 } 129 catch (OIS::Exception ex) 130 { 131 CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." 132 << "OIS message: \"" << ex.eText << "\"" << std::endl; 133 inputSystem_ = 0; 1054 { 1055 return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, 1056 createKeyboard, createMouse, createJoySticks); 1057 } 1058 1059 bool InputManager::initialiseKeyboard() 1060 { 1061 return _getSingleton()._initialiseKeyboard(); 1062 } 1063 1064 bool InputManager::initialiseMouse() 1065 { 1066 return _getSingleton()._initialiseMouse(); 1067 } 1068 1069 bool InputManager::initialiseJoySticks() 1070 { 1071 return _getSingleton()._initialiseJoySticks(); 1072 } 1073 1074 int InputManager::numberOfKeyboards() 1075 { 1076 if (_getSingleton().keyboard_ != 0) 1077 return 1; 1078 else 1079 return 0; 1080 } 1081 1082 int InputManager::numberOfMice() 1083 { 1084 if (_getSingleton().mouse_ != 0) 1085 return 1; 1086 else 1087 return 0; 1088 } 1089 1090 int InputManager::numberOfJoySticks() 1091 { 1092 return _getSingleton().joySticksSize_; 1093 } 1094 1095 /*bool InputManager::isKeyDown(KeyCode::Enum key) 1096 { 1097 if (_getSingleton().keyboard_) 1098 return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key); 1099 else 134 1100 return false; 135 } 136 137 if (createKeyboard) 138 _initialiseKeyboard(); 139 140 if (createMouse) 141 _initialiseMouse(); 142 143 if (createJoySticks) 144 _initialiseJoySticks(); 145 146 // Set mouse/joystick region 147 if (mouse_) 148 { 149 setWindowExtents(windowWidth, windowHeight); 150 } 151 152 state_ = IS_NONE; 153 CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; 154 155 // InputManager holds the input buffer --> create one and add it. 156 buffer_ = new InputBuffer(); 157 addKeyHandler(buffer_, "buffer"); 158 Shell::getInstance().setInputBuffer(buffer_); 159 160 keyBinder_ = new KeyBinder(); 161 keyBinder_->loadBindings(); 162 addKeyHandler(keyBinder_, "keybinder"); 163 addMouseHandler(keyBinder_, "keybinder"); 164 addJoyStickHandler(keyBinder_, "keybinder"); 165 166 keyDetector_ = new KeyDetector(); 167 keyDetector_->loadBindings(); 168 addKeyHandler(keyDetector_, "keydetector"); 169 addMouseHandler(keyDetector_, "keydetector"); 170 addJoyStickHandler(keyDetector_, "keydetector"); 171 172 calibratorCallback_ = new CalibratorCallback(); 173 addKeyHandler(calibratorCallback_, "calibratorcallback"); 174 175 setConfigValues(); 176 177 CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; 178 } 1101 }*/ 1102 1103 /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier) 1104 { 1105 if (_getSingleton().keyboard_) 1106 return isModifierDown(modifier); 179 1107 else 180 {181 CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;182 }183 return true;184 }185 186 /**187 @brief Creates a keyboard and sets the event handler.188 @return False if keyboard stays uninitialised, true otherwise.189 */190 bool InputManager::_initialiseKeyboard()191 {192 if (keyboard_ != 0)193 {194 CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;195 return true;196 }197 try198 {199 if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)200 {201 keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);202 // register our listener in OIS.203 keyboard_->setEventCallback(this);204 // note: OIS will not detect keys that have already been down when the keyboard was created.205 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;206 return true;207 }208 else209 {210 CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;211 1108 return false; 212 } 213 } 214 catch (OIS::Exception ex) 215 { 216 // TODO: Test this output regarding formatting 217 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" 218 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 219 keyboard_ = 0; 220 return false; 221 } 222 } 223 224 /** 225 @brief Creates a mouse and sets the event handler. 226 @return False if mouse stays uninitialised, true otherwise. 227 */ 228 bool InputManager::_initialiseMouse() 229 { 230 if (mouse_ != 0) 231 { 232 CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; 233 return true; 234 } 235 try 236 { 237 if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) 238 { 239 mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); 240 // register our listener in OIS. 241 mouse_->setEventCallback(this); 242 CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; 243 return true; 244 } 245 else 246 { 247 CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; 248 return false; 249 } 250 } 251 catch (OIS::Exception ex) 252 { 253 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" 254 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 255 mouse_ = 0; 256 return false; 257 } 258 } 259 260 /** 261 @brief Creates all joy sticks and sets the event handler. 262 @return False joy stick stay uninitialised, true otherwise. 263 */ 264 bool InputManager::_initialiseJoySticks() 265 { 266 if (joySticksSize_ > 0) 267 { 268 CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; 269 return true; 270 } 271 bool success = false; 272 if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) 273 { 274 for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) 275 { 276 try 277 { 278 OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); 279 joySticks_.push_back(stig); 280 // register our listener in OIS. 281 stig->setEventCallback(this); 282 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; 283 success = true; 284 } 285 catch (OIS::Exception ex) 286 { 287 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" 288 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 289 } 290 } 291 } 1109 }*/ 1110 1111 /*const MouseState InputManager::getMouseState() 1112 { 1113 if (_getSingleton().mouse_) 1114 return _getSingleton().mouse_->getMouseState(); 292 1115 else 293 { 294 CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 295 return false; 296 } 297 joySticksSize_ = joySticks_.size(); 298 activeJoyStickHandlers_.resize(joySticksSize_); 299 joyStickButtonsDown_.resize(joySticksSize_); 300 povStates_.resize(joySticksSize_); 301 sliderStates_.resize(joySticksSize_); 302 joySticksCalibration_.resize(joySticksSize_); 303 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 304 { 305 // reset the calibration with default values 306 for (unsigned int i = 0; i < 24; i++) 307 { 308 joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; 309 joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; 310 joySticksCalibration_[iJoyStick].zeroStates[i] = 0; 311 } 312 } 313 return success; 314 } 315 316 /** 317 @brief Sets the configurable values. Use keybindings.ini as file.. 318 */ 319 void InputManager::setConfigValues() 320 { 321 if (joySticksSize_) 322 { 323 std::vector<MultiTypeMath> coeffPos; 324 std::vector<MultiTypeMath> coeffNeg; 325 std::vector<MultiTypeMath> zero; 326 coeffPos.resize(24); 327 coeffNeg.resize(24); 328 zero.resize(24); 329 for (unsigned int i = 0; i < 24; i++) 330 { 331 coeffPos[i] = 1.0f/32767.0f; 332 coeffNeg[i] = 1.0f/32768.0f; 333 zero[i] = 0; 334 } 335 336 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 337 if (!cont) 338 { 339 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffPos", coeffPos); 340 getIdentifier()->addConfigValueContainer("CoeffPos", cont); 341 } 342 cont->getValue(&coeffPos); 343 344 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 345 if (!cont) 346 { 347 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffNeg", coeffNeg); 348 getIdentifier()->addConfigValueContainer("CoeffNeg", cont); 349 } 350 cont->getValue(&coeffNeg); 351 352 cont = getIdentifier()->getConfigValueContainer("Zero"); 353 if (!cont) 354 { 355 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "Zero", zero); 356 getIdentifier()->addConfigValueContainer("Zero", cont); 357 } 358 cont->getValue(&zero); 359 360 // copy values to our own variables 361 for (unsigned int i = 0; i < 24; i++) 362 { 363 joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; 364 joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; 365 joySticksCalibration_[0].zeroStates[i] = zero[i]; 366 } 367 } 368 } 369 370 /** 371 @brief Destroys all the created input devices and sets the InputManager to construction state. 372 */ 373 void InputManager::_destroy() 374 { 375 if (state_ != IS_UNINIT) 376 { 377 CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl; 378 379 if (buffer_) 380 delete buffer_; 381 382 if (keyBinder_) 383 delete keyBinder_; 384 385 if (keyDetector_) 386 delete keyDetector_; 387 388 if (calibratorCallback_) 389 delete calibratorCallback_; 390 391 keyHandlers_.clear(); 392 mouseHandlers_.clear(); 393 joyStickHandlers_.clear(); 394 395 _destroyKeyboard(); 396 _destroyMouse(); 397 _destroyJoySticks(); 398 399 activeHandlers_.clear(); 400 401 // inputSystem_ can never be 0, or else the code is mistaken 402 OIS::InputManager::destroyInputSystem(inputSystem_); 403 inputSystem_ = 0; 404 405 state_ = IS_UNINIT; 406 CCOUT(ORX_DEBUG) << "Destroying done." << std::endl; 407 } 408 } 409 410 /** 411 @brief Destroys the keyboard and sets it to 0. 412 */ 413 void InputManager::_destroyKeyboard() 414 { 415 if (keyboard_) 416 // inputSystem_ can never be 0, or else the code is mistaken 417 inputSystem_->destroyInputObject(keyboard_); 418 keyboard_ = 0; 419 activeKeyHandlers_.clear(); 420 keysDown_.clear(); 421 CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; 422 } 423 424 /** 425 @brief Destroys the mouse and sets it to 0. 426 */ 427 void InputManager::_destroyMouse() 428 { 429 if (mouse_) 430 // inputSystem_ can never be 0, or else the code is mistaken 431 inputSystem_->destroyInputObject(mouse_); 432 mouse_ = 0; 433 activeMouseHandlers_.clear(); 434 mouseButtonsDown_.clear(); 435 CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; 436 } 437 438 /** 439 @brief Destroys all the joy sticks and resizes the lists to 0. 440 */ 441 void InputManager::_destroyJoySticks() 442 { 443 if (joySticksSize_ > 0) 444 { 445 // note: inputSystem_ can never be 0, or else the code is mistaken 446 for (unsigned int i = 0; i < joySticksSize_; i++) 447 if (joySticks_[i] != 0) 448 inputSystem_->destroyInputObject(joySticks_[i]); 449 450 joySticks_.clear(); 451 joySticksSize_ = 0; 452 activeJoyStickHandlers_.clear(); 453 joyStickButtonsDown_.clear(); 454 povStates_.clear(); 455 sliderStates_.clear(); 456 joySticksCalibration_.clear(); 457 } 458 CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; 459 } 460 461 void InputManager::_saveState() 462 { 463 savedHandlers_.activeHandlers_ = activeHandlers_; 464 savedHandlers_.activeJoyStickHandlers_ = activeJoyStickHandlers_; 465 savedHandlers_.activeKeyHandlers_ = activeKeyHandlers_; 466 savedHandlers_.activeMouseHandlers_ = activeMouseHandlers_; 467 } 468 469 void InputManager::_restoreState() 470 { 471 activeHandlers_ = savedHandlers_.activeHandlers_; 472 activeJoyStickHandlers_ = savedHandlers_.activeJoyStickHandlers_; 473 activeKeyHandlers_ = savedHandlers_.activeKeyHandlers_; 474 activeMouseHandlers_ = savedHandlers_.activeMouseHandlers_; 475 } 476 477 void InputManager::_updateTickables() 478 { 479 // we can use a map to have a list of unique pointers (an object can implement all 3 handlers) 480 std::map<InputTickable*, HandlerState> tempSet; 481 for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) 482 tempSet[activeKeyHandlers_[iHandler]].joyStick = true; 483 for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) 484 tempSet[activeMouseHandlers_[iHandler]].mouse = true; 485 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 486 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 487 tempSet[activeJoyStickHandlers_[iJoyStick][iHandler]].joyStick = true; 488 489 // copy the content of the map back to the actual vector 490 activeHandlers_.clear(); 491 for (std::map<InputTickable*, HandlerState>::const_iterator itHandler = tempSet.begin(); 492 itHandler != tempSet.end(); itHandler++) 493 activeHandlers_.push_back(std::pair<InputTickable*, HandlerState>((*itHandler).first, (*itHandler).second)); 494 } 495 496 497 // ################################# 498 // ### Private Interface Methods ### 499 // ################################# 500 // ################################# 501 502 /** 503 @brief Updates the InputManager. Tick is called by Orxonox. 504 @param dt Delta time 505 */ 506 void InputManager::_tick(float dt) 507 { 508 if (state_ == IS_UNINIT) 509 return; 510 511 if (state_ != stateRequest_) 512 { 513 InputState sr = stateRequest_; 514 switch (sr) 515 { 516 case IS_NORMAL: 517 activeKeyHandlers_.clear(); 518 activeMouseHandlers_.clear(); 519 for (unsigned int i = 0; i < joySticksSize_; i++) 520 activeJoyStickHandlers_[i].clear(); 521 522 // normal play mode 523 // note: we assume that the handlers exist since otherwise, something's wrong anyway. 524 enableKeyHandler("keybinder"); 525 enableMouseHandler("keybinder"); 526 enableJoyStickHandler("keybinder", 0); 527 stateRequest_ = IS_NORMAL; 528 state_ = IS_NORMAL; 529 break; 530 531 case IS_GUI: 532 state_ = IS_GUI; 533 break; 534 535 case IS_CONSOLE: 536 activeKeyHandlers_.clear(); 537 activeMouseHandlers_.clear(); 538 for (unsigned int i = 0; i < joySticksSize_; i++) 539 activeJoyStickHandlers_[i].clear(); 540 541 enableMouseHandler("keybinder"); 542 enableJoyStickHandler("keybinder", 0); 543 enableKeyHandler("buffer"); 544 stateRequest_ = IS_CONSOLE; 545 state_ = IS_CONSOLE; 546 break; 547 548 case IS_DETECT: 549 savedState_ = state_; 550 _saveState(); 551 552 activeKeyHandlers_.clear(); 553 activeMouseHandlers_.clear(); 554 for (unsigned int i = 0; i < joySticksSize_; i++) 555 activeJoyStickHandlers_[i].clear(); 556 557 enableKeyHandler("keydetector"); 558 enableMouseHandler("keydetector"); 559 enableJoyStickHandler("keydetector", 0); 560 561 stateRequest_ = IS_DETECT; 562 state_ = IS_DETECT; 563 break; 564 565 case IS_NODETECT: 566 _restoreState(); 567 keysDown_.clear(); 568 mouseButtonsDown_.clear(); 569 for (unsigned int i = 0; i < joySticksSize_; i++) 570 joyStickButtonsDown_[i].clear(); 571 state_ = IS_NODETECT; 572 stateRequest_ = savedState_; 573 break; 574 575 case IS_CALIBRATE: 576 if (joySticksSize_) 577 { 578 savedState_ = _getSingleton().state_; 579 for (unsigned int i = 0; i < 24; i++) 580 { 581 marginalsMax_[i] = INT_MIN; 582 marginalsMin_[i] = INT_MAX; 583 } 584 COUT(0) << "Move all joy stick axes in all directions a few times. " 585 << "Then put all axes in zero state and hit enter." << std::endl; 586 587 savedState_ = state_; 588 _saveState(); 589 590 activeKeyHandlers_.clear(); 591 activeMouseHandlers_.clear(); 592 for (unsigned int i = 0; i < joySticksSize_; i++) 593 activeJoyStickHandlers_[i].clear(); 594 595 enableKeyHandler("calibratorcallback"); 596 stateRequest_ = IS_CALIBRATE; 597 state_ = IS_CALIBRATE; 1116 return MouseState(); 1117 }*/ 1118 1119 /*const JoyStickState InputManager::getJoyStickState(unsigned int ID) 1120 { 1121 if (ID < _getSingleton().joySticksSize_) 1122 return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID); 1123 else 1124 return JoyStickState(); 1125 }*/ 1126 1127 void InputManager::destroy() 1128 { 1129 _getSingleton()._destroy(); 1130 } 1131 1132 void InputManager::destroyKeyboard() 1133 { 1134 return _getSingleton()._destroyKeyboard(); 1135 } 1136 1137 void InputManager::destroyMouse() 1138 { 1139 return _getSingleton()._destroyMouse(); 1140 } 1141 1142 void InputManager::destroyJoySticks() 1143 { 1144 return _getSingleton()._destroyJoySticks(); 1145 } 1146 1147 1148 /** 1149 @brief 1150 Adjusts the mouse window metrics. 1151 This method has to be called every time the size of the window changes. 1152 @param width 1153 The new width of the render window 1154 @param^height 1155 The new height of the render window 1156 */ 1157 void InputManager::setWindowExtents(const int width, const int height) 1158 { 1159 if (_getSingleton().mouse_) 1160 { 1161 // Set mouse region (if window resizes, we should alter this to reflect as well) 1162 const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); 1163 mouseState.width = width; 1164 mouseState.height = height; 1165 } 1166 } 1167 1168 /** 1169 @brief 1170 Sets the input mode to either GUI, inGame or Buffer 1171 @param mode 1172 The new input mode 1173 @remarks 1174 Only has an affect if the mode actually changes 1175 */ 1176 void InputManager::setInputState(const InputState state) 1177 { 1178 _getSingleton().stateRequest_ = state; 1179 } 1180 1181 /** 1182 @brief 1183 Returns the current input handling method 1184 @return 1185 The current input mode. 1186 */ 1187 InputManager::InputState InputManager::getInputState() 1188 { 1189 return _getSingleton().state_; 1190 } 1191 1192 void InputManager::storeKeyStroke(const std::string& name) 1193 { 1194 setInputState(IS_NODETECT); 1195 COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl; 1196 CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false); 1197 } 1198 1199 void InputManager::keyBind(const std::string& command) 1200 { 1201 bindingCommmandString_s = command; 1202 setInputState(IS_DETECT); 1203 COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; 1204 } 1205 1206 void InputManager::calibrate() 1207 { 1208 _getSingleton().setInputState(IS_CALIBRATE); 1209 } 1210 1211 void InputManager::tick(float dt) 1212 { 1213 _getSingleton()._tick(dt); 1214 } 1215 1216 // ###### KeyHandler ###### 1217 1218 /** 1219 @brief 1220 Adds a new key handler. 1221 @param handler 1222 Pointer to the handler object. 1223 @param name 1224 Unique name of the handler. 1225 @return 1226 True if added, false if name already existed. 1227 */ 1228 bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name) 1229 { 1230 if (!handler) 1231 return false; 1232 if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end()) 1233 { 1234 _getSingleton().keyHandlers_[name] = handler; 1235 return true; 598 1236 } 599 1237 else 600 { 601 COUT(3) << "Connot calibrate, no joy stick found!" << std::endl; 602 stateRequest_ = state_; 603 } 604 break; 605 606 case IS_NOCALIBRATE: 607 _completeCalibration(); 608 _restoreState(); 609 keyBinder_->resetJoyStickAxes(); 610 state_ = IS_NOCALIBRATE; 611 stateRequest_ = savedState_; 612 break; 613 614 case IS_NONE: 615 activeKeyHandlers_.clear(); 616 activeMouseHandlers_.clear(); 617 for (unsigned int i = 0; i < joySticksSize_; i++) 618 activeJoyStickHandlers_[i].clear(); 619 state_ = IS_NONE; 620 621 default: 622 break; 623 } 624 } 625 626 // Capture all the input. This calls the event handlers in InputManager. 627 if (mouse_) 628 mouse_->capture(); 629 if (keyboard_) 630 keyboard_->capture(); 631 for (unsigned int i = 0; i < joySticksSize_; i++) 632 joySticks_[i]->capture(); 633 634 if (state_ != IS_CALIBRATE) 635 { 636 // call all the handlers for the held key events 637 for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) 638 for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) 639 activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_)); 640 641 // call all the handlers for the held mouse button events 642 for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) 643 for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) 644 activeMouseHandlers_[iHandler]->mouseButtonHeld(mouseButtonsDown_[iButton]); 645 646 // call all the handlers for the held joy stick button events 647 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 648 for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) 649 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 650 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); 651 } 652 653 // call the ticks for the handlers (need to be treated specially) 654 for (unsigned int iHandler = 0; iHandler < activeHandlers_.size(); iHandler++) 655 activeHandlers_[iHandler].first->tickInput(dt, activeHandlers_[iHandler].second); 656 } 657 658 void InputManager::_completeCalibration() 659 { 660 for (unsigned int i = 0; i < 24; i++) 661 { 662 // positive coefficient 663 if (marginalsMax_[i] == INT_MIN) 664 marginalsMax_[i] = 32767; 665 // coefficients 666 if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) 667 joySticksCalibration_[0].positiveCoeff[i] = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); 668 else 669 joySticksCalibration_[0].positiveCoeff[i] = 1.0f; 670 671 // config value 672 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 673 assert(cont); 674 cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); 675 676 // negative coefficient 677 if (marginalsMin_[i] == INT_MAX) 678 marginalsMin_[i] = -32768; 679 // coefficients 680 if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) 681 joySticksCalibration_[0].negativeCoeff[i] = -1.0f/(marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); 682 else 683 joySticksCalibration_[0].negativeCoeff[i] = 1.0f; 684 // config value 685 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 686 assert(cont); 687 cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); 688 689 // zero states 690 if (i < 8) 691 { 692 if (!(i & 1)) 693 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; 1238 return false; 1239 } 1240 1241 /** 1242 @brief 1243 Removes a Key handler from the list. 1244 @param name 1245 Unique name of the handler. 1246 @return 1247 True if removal was successful, false if name was not found. 1248 */ 1249 bool InputManager::removeKeyHandler(const std::string &name) 1250 { 1251 disableKeyHandler(name); 1252 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); 1253 if (it != _getSingleton().keyHandlers_.end()) 1254 { 1255 _getSingleton().keyHandlers_.erase(it); 1256 return true; 1257 } 694 1258 else 695 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; 696 } 697 else 698 { 699 if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) 700 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; 1259 return false; 1260 } 1261 1262 /** 1263 @brief 1264 Returns the pointer to a handler. 1265 @param name 1266 Unique name of the handler. 1267 @return 1268 Pointer to the instance, 0 if name was not found. 1269 */ 1270 KeyHandler* InputManager::getKeyHandler(const std::string& name) 1271 { 1272 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); 1273 if (it != _getSingleton().keyHandlers_.end()) 1274 return (*it).second; 701 1275 else 702 joySticksCalibration_[0].zeroStates[i] = 0; 703 } 704 // config value 705 cont = getIdentifier()->getConfigValueContainer("Zero"); 706 assert(cont); 707 cont->set(i, joySticksCalibration_[0].zeroStates[i]); 708 } 709 } 710 711 // ###### Key Events ###### 712 713 /** 714 @brief Event handler for the keyPressed Event. 715 @param e Event information 716 */ 717 bool InputManager::keyPressed(const OIS::KeyEvent &e) 718 { 719 // check whether the key already is in the list (can happen when focus was lost) 720 unsigned int iKey = 0; 721 while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key) 722 iKey++; 723 if (iKey == keysDown_.size()) 724 keysDown_.push_back(Key(e)); 725 726 // update modifiers 727 if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) 728 keyboardModifiers_ |= KeyboardModifier::Alt; // alt key 729 if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) 730 keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key 731 if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) 732 keyboardModifiers_ |= KeyboardModifier::Shift; // shift key 733 734 for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) 735 activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_)); 736 737 return true; 738 } 739 740 /** 741 @brief Event handler for the keyReleased Event. 742 @param e Event information 743 */ 744 bool InputManager::keyReleased(const OIS::KeyEvent &e) 745 { 746 // remove the key from the keysDown_ list 747 for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) 748 { 749 if (keysDown_[iKey].key == (KeyCode::Enum)e.key) 750 { 751 keysDown_.erase(keysDown_.begin() + iKey); 752 break; 753 } 754 } 755 756 // update modifiers 757 if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) 758 keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key 759 if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) 760 keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key 761 if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) 762 keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key 763 764 for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) 765 activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_)); 766 767 return true; 768 } 769 770 771 // ###### Mouse Events ###### 772 773 /** 774 @brief Event handler for the mouseMoved Event. 775 @param e Event information 776 */ 777 bool InputManager::mouseMoved(const OIS::MouseEvent &e) 778 { 779 // check for actual moved event 780 if (e.state.X.rel != 0 || e.state.Y.rel != 0) 781 { 782 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 783 activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs), 784 IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height)); 785 } 786 787 // check for mouse scrolled event 788 if (e.state.Z.rel != 0) 789 { 790 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 791 activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); 792 } 793 794 return true; 795 } 796 797 /** 798 @brief Event handler for the mousePressed Event. 799 @param e Event information 800 @param id The ID of the mouse button 801 */ 802 bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 803 { 804 // check whether the button already is in the list (can happen when focus was lost) 805 unsigned int iButton = 0; 806 while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id) 807 iButton++; 808 if (iButton == mouseButtonsDown_.size()) 809 mouseButtonsDown_.push_back((MouseButton::Enum)id); 810 811 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 812 activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id); 813 814 return true; 815 } 816 817 /** 818 @brief Event handler for the mouseReleased Event. 819 @param e Event information 820 @param id The ID of the mouse button 821 */ 822 bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 823 { 824 // remove the button from the keysDown_ list 825 for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) 826 { 827 if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id) 828 { 829 mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); 830 break; 831 } 832 } 833 834 for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) 835 activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id); 836 837 return true; 838 } 839 840 841 // ###### Joy Stick Events ###### 842 843 inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) 844 { 845 // use the device to identify which one called the method 846 OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; 847 unsigned int iJoyStick = 0; 848 while (joySticks_[iJoyStick] != joyStick) 849 { 850 iJoyStick++; 851 if (iJoyStick == joySticksSize_) 852 { 853 CCOUT(3) << "Unknown joystick fired an event. This means there is a bug somewhere! Aborting." << std::endl; 854 abort(); 855 } 856 } 857 return iJoyStick; 858 } 859 860 bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) 861 { 862 unsigned int iJoyStick = _getJoystick(arg); 863 864 // check whether the button already is in the list (can happen when focus was lost) 865 std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; 866 unsigned int iButton = 0; 867 while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) 868 iButton++; 869 if (iButton == buttonsDown.size()) 870 buttonsDown.push_back(button); 871 872 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 873 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button); 874 875 return true; 876 } 877 878 bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) 879 { 880 unsigned int iJoyStick = _getJoystick(arg); 881 882 // remove the button from the joyStickButtonsDown_ list 883 std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; 884 for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) 885 { 886 if (buttonsDown[iButton] == button) 887 { 888 buttonsDown.erase(buttonsDown.begin() + iButton); 889 break; 890 } 891 } 892 893 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 894 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button); 895 896 return true; 897 } 898 899 void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) 900 { 901 if (state_ == IS_CALIBRATE) 902 { 903 if (value > marginalsMax_[axis]) 904 marginalsMax_[axis] = value; 905 if (value < marginalsMin_[axis]) 906 marginalsMin_[axis] = value; 907 } 908 else 909 { 910 float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis]; 911 if (fValue > 0.0f) 912 fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis]; 913 else 914 fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis]; 915 916 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 917 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue); 918 } 919 } 920 921 bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) 922 { 923 //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000) 924 //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;} 925 926 unsigned int iJoyStick = _getJoystick(arg); 927 928 // keep in mind that the first 8 axes are reserved for the sliders 929 _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs); 930 931 return true; 932 } 933 934 bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) 935 { 936 //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000) 937 //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;} 938 //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl; 939 940 unsigned int iJoyStick = _getJoystick(arg); 941 942 if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) 943 _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); 944 else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) 945 _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); 946 947 return true; 948 } 949 950 bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) 951 { 952 unsigned int iJoyStick = _getJoystick(arg); 953 954 // translate the POV into 8 simple buttons 955 int lastState = povStates_[iJoyStick][id]; 956 if (lastState & OIS::Pov::North) 957 buttonReleased(arg, 32 + id * 4 + 0); 958 if (lastState & OIS::Pov::South) 959 buttonReleased(arg, 32 + id * 4 + 1); 960 if (lastState & OIS::Pov::East) 961 buttonReleased(arg, 32 + id * 4 + 2); 962 if (lastState & OIS::Pov::West) 963 buttonReleased(arg, 32 + id * 4 + 3); 964 965 povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; 966 967 int currentState = povStates_[iJoyStick][id]; 968 if (currentState & OIS::Pov::North) 969 buttonPressed(arg, 32 + id * 4 + 0); 970 if (currentState & OIS::Pov::South) 971 buttonPressed(arg, 32 + id * 4 + 1); 972 if (currentState & OIS::Pov::East) 973 buttonPressed(arg, 32 + id * 4 + 2); 974 if (currentState & OIS::Pov::West) 975 buttonPressed(arg, 32 + id * 4 + 3); 976 977 return true; 978 } 979 980 /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id) 981 { 982 unsigned int iJoyStick = _getJoystick(arg); 983 984 for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) 985 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id); 986 987 return true; 988 }*/ 989 990 991 // ################################ 992 // ### Static Interface Methods ### 993 // ################################ 994 // ################################ 995 996 std::string InputManager::bindingCommmandString_s = ""; 997 998 bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight, 999 bool createKeyboard, bool createMouse, bool createJoySticks) 1000 { 1001 return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, 1002 createKeyboard, createMouse, createJoySticks); 1003 } 1004 1005 bool InputManager::initialiseKeyboard() 1006 { 1007 return _getSingleton()._initialiseKeyboard(); 1008 } 1009 1010 bool InputManager::initialiseMouse() 1011 { 1012 return _getSingleton()._initialiseMouse(); 1013 } 1014 1015 bool InputManager::initialiseJoySticks() 1016 { 1017 return _getSingleton()._initialiseJoySticks(); 1018 } 1019 1020 int InputManager::numberOfKeyboards() 1021 { 1022 if (_getSingleton().keyboard_ != 0) 1023 return 1; 1024 else 1025 return 0; 1026 } 1027 1028 int InputManager::numberOfMice() 1029 { 1030 if (_getSingleton().mouse_ != 0) 1031 return 1; 1032 else 1033 return 0; 1034 } 1035 1036 int InputManager::numberOfJoySticks() 1037 { 1038 return _getSingleton().joySticksSize_; 1039 } 1040 1041 /*bool InputManager::isKeyDown(KeyCode::Enum key) 1042 { 1043 if (_getSingleton().keyboard_) 1044 return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key); 1045 else 1046 return false; 1047 }*/ 1048 1049 /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier) 1050 { 1051 if (_getSingleton().keyboard_) 1052 return isModifierDown(modifier); 1053 else 1054 return false; 1055 }*/ 1056 1057 /*const MouseState InputManager::getMouseState() 1058 { 1059 if (_getSingleton().mouse_) 1060 return _getSingleton().mouse_->getMouseState(); 1061 else 1062 return MouseState(); 1063 }*/ 1064 1065 /*const JoyStickState InputManager::getJoyStickState(unsigned int ID) 1066 { 1067 if (ID < _getSingleton().joySticksSize_) 1068 return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID); 1069 else 1070 return JoyStickState(); 1071 }*/ 1072 1073 void InputManager::destroy() 1074 { 1075 _getSingleton()._destroy(); 1076 } 1077 1078 void InputManager::destroyKeyboard() 1079 { 1080 return _getSingleton()._destroyKeyboard(); 1081 } 1082 1083 void InputManager::destroyMouse() 1084 { 1085 return _getSingleton()._destroyMouse(); 1086 } 1087 1088 void InputManager::destroyJoySticks() 1089 { 1090 return _getSingleton()._destroyJoySticks(); 1091 } 1092 1093 1094 /** 1095 @brief Adjusts the mouse window metrics. 1096 This method has to be called every time the size of the window changes. 1097 @param width The new width of the render window 1098 @param height the new height of the render window 1099 */ 1100 void InputManager::setWindowExtents(const int width, const int height) 1101 { 1102 if (_getSingleton().mouse_) 1103 { 1104 // Set mouse region (if window resizes, we should alter this to reflect as well) 1105 const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); 1106 mouseState.width = width; 1107 mouseState.height = height; 1108 } 1109 } 1110 1111 /** 1112 @brief Sets the input mode to either GUI, inGame or Buffer 1113 @param mode The new input mode 1114 @remark Only has an affect if the mode actually changes 1115 */ 1116 void InputManager::setInputState(const InputState state) 1117 { 1118 _getSingleton().stateRequest_ = state; 1119 } 1120 1121 /** 1122 @brief Returns the current input handling method 1123 @return The current input mode. 1124 */ 1125 InputManager::InputState InputManager::getInputState() 1126 { 1127 return _getSingleton().state_; 1128 } 1129 1130 void InputManager::storeKeyStroke(const std::string& name) 1131 { 1132 setInputState(IS_NODETECT); 1133 COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl; 1134 CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false); 1135 } 1136 1137 void InputManager::keyBind(const std::string& command) 1138 { 1139 bindingCommmandString_s = command; 1140 setInputState(IS_DETECT); 1141 COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; 1142 } 1143 1144 void InputManager::calibrate() 1145 { 1146 _getSingleton().setInputState(IS_CALIBRATE); 1147 } 1148 1149 void InputManager::tick(float dt) 1150 { 1151 _getSingleton()._tick(dt); 1152 } 1153 1154 // ###### KeyHandler ###### 1155 1156 /** 1157 @brief Adds a new key handler. 1158 @param handler Pointer to the handler object. 1159 @param name Unique name of the handler. 1160 @return True if added, false if name already existed. 1161 */ 1162 bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name) 1163 { 1164 if (!handler) 1165 return false; 1166 if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end()) 1167 { 1168 _getSingleton().keyHandlers_[name] = handler; 1169 return true; 1170 } 1171 else 1172 return false; 1173 } 1174 1175 /** 1176 @brief Removes a Key handler from the list. 1177 @param name Unique name of the handler. 1178 @return True if removal was successful, false if name was not found. 1179 */ 1180 bool InputManager::removeKeyHandler(const std::string &name) 1181 { 1182 disableKeyHandler(name); 1183 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); 1184 if (it != _getSingleton().keyHandlers_.end()) 1185 { 1186 _getSingleton().keyHandlers_.erase(it); 1187 return true; 1188 } 1189 else 1190 return false; 1191 } 1192 1193 /** 1194 @brief Returns the pointer to a handler. 1195 @param name Unique name of the handler. 1196 @return Pointer to the instance, 0 if name was not found. 1197 */ 1198 KeyHandler* InputManager::getKeyHandler(const std::string& name) 1199 { 1200 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); 1201 if (it != _getSingleton().keyHandlers_.end()) 1202 { 1203 return (*it).second; 1204 } 1205 else 1206 return 0; 1207 } 1208 1209 /** 1210 @brief Enables a specific key handler that has already been added. 1211 @param name Unique name of the handler. 1212 @return False if name was not found, true otherwise. 1213 */ 1214 bool InputManager::enableKeyHandler(const std::string& name) 1215 { 1216 // get pointer from the map with all stored handlers 1217 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1218 if (mapIt == _getSingleton().keyHandlers_.end()) 1219 return false; 1220 // see whether the handler already is in the list 1221 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1222 it != _getSingleton().activeKeyHandlers_.end(); it++) 1223 { 1224 if ((*it) == (*mapIt).second) 1225 { 1226 return true; 1227 } 1228 } 1229 _getSingleton().activeKeyHandlers_.push_back((*mapIt).second); 1230 _getSingleton().stateRequest_ = IS_CUSTOM; 1231 _getSingleton()._updateTickables(); 1232 return true; 1233 } 1234 1235 /** 1236 @brief Disables a specific key handler. 1237 @param name Unique name of the handler. 1238 @return False if name was not found, true otherwise. 1239 */ 1240 bool InputManager::disableKeyHandler(const std::string &name) 1241 { 1242 // get pointer from the map with all stored handlers 1243 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1244 if (mapIt == _getSingleton().keyHandlers_.end()) 1245 return false; 1246 // look for the handler in the list 1247 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1248 it != _getSingleton().activeKeyHandlers_.end(); it++) 1249 { 1250 if ((*it) == (*mapIt).second) 1251 { 1252 _getSingleton().activeKeyHandlers_.erase(it); 1276 return 0; 1277 } 1278 1279 /** 1280 @brief 1281 Enables a specific key handler that has already been added. 1282 @param name 1283 Unique name of the handler. 1284 @return 1285 False if name was not found, true otherwise. 1286 */ 1287 bool InputManager::enableKeyHandler(const std::string& name) 1288 { 1289 // get pointer from the map with all stored handlers 1290 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1291 if (mapIt == _getSingleton().keyHandlers_.end()) 1292 return false; 1293 // see whether the handler already is in the list 1294 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1295 it != _getSingleton().activeKeyHandlers_.end(); it++) 1296 { 1297 if ((*it) == (*mapIt).second) 1298 { 1299 return true; 1300 } 1301 } 1302 _getSingleton().activeKeyHandlers_.push_back((*mapIt).second); 1253 1303 _getSingleton().stateRequest_ = IS_CUSTOM; 1254 1304 _getSingleton()._updateTickables(); 1255 1305 return true; 1256 } 1257 } 1258 return true; 1259 } 1260 1261 /** 1262 @brief Checks whether a key handler is active 1263 @param name Unique name of the handler. 1264 @return False if key handler is not active or doesn't exist, true otherwise. 1265 */ 1266 bool InputManager::isKeyHandlerActive(const std::string& name) 1267 { 1268 // get pointer from the map with all stored handlers 1269 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1270 if (mapIt == _getSingleton().keyHandlers_.end()) 1271 return false; 1272 // see whether the handler already is in the list 1273 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1274 it != _getSingleton().activeKeyHandlers_.end(); it++) 1275 { 1276 if ((*it) == (*mapIt).second) 1277 return true; 1278 } 1279 return false; 1280 } 1281 1282 1283 // ###### MouseHandler ###### 1284 /** 1285 @brief Adds a new mouse handler. 1286 @param handler Pointer to the handler object. 1287 @param name Unique name of the handler. 1288 @return True if added, false if name already existed. 1289 */ 1290 bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name) 1291 { 1292 if (!handler) 1293 return false; 1294 if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end()) 1295 { 1296 _getSingleton().mouseHandlers_[name] = handler; 1297 return true; 1298 } 1299 else 1300 return false; 1301 } 1302 1303 /** 1304 @brief Removes a Mouse handler from the list. 1305 @param name Unique name of the handler. 1306 @return True if removal was successful, false if name was not found. 1307 */ 1308 bool InputManager::removeMouseHandler(const std::string &name) 1309 { 1310 disableMouseHandler(name); 1311 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); 1312 if (it != _getSingleton().mouseHandlers_.end()) 1313 { 1314 _getSingleton().mouseHandlers_.erase(it); 1315 return true; 1316 } 1317 else 1318 return false; 1319 } 1320 1321 /** 1322 @brief Returns the pointer to a handler. 1323 @param name Unique name of the handler. 1324 @return Pointer to the instance, 0 if name was not found. 1325 */ 1326 MouseHandler* InputManager::getMouseHandler(const std::string& name) 1327 { 1328 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); 1329 if (it != _getSingleton().mouseHandlers_.end()) 1330 { 1331 return (*it).second; 1332 } 1333 else 1334 return 0; 1335 } 1336 1337 /** 1338 @brief Enables a specific mouse handler that has already been added. 1339 @param name Unique name of the handler. 1340 @return False if name was not found, true otherwise. 1341 */ 1342 bool InputManager::enableMouseHandler(const std::string& name) 1343 { 1344 // get pointer from the map with all stored handlers 1345 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1346 if (mapIt == _getSingleton().mouseHandlers_.end()) 1347 return false; 1348 // see whether the handler already is in the list 1349 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1350 it != _getSingleton().activeMouseHandlers_.end(); it++) 1351 { 1352 if ((*it) == (*mapIt).second) 1353 { 1354 return true; 1355 } 1356 } 1357 _getSingleton().activeMouseHandlers_.push_back((*mapIt).second); 1358 _getSingleton().stateRequest_ = IS_CUSTOM; 1359 _getSingleton()._updateTickables(); 1360 return true; 1361 } 1362 1363 /** 1364 @brief Disables a specific mouse handler. 1365 @param name Unique name of the handler. 1366 @return False if name was not found, true otherwise. 1367 */ 1368 bool InputManager::disableMouseHandler(const std::string &name) 1369 { 1370 // get pointer from the map with all stored handlers 1371 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1372 if (mapIt == _getSingleton().mouseHandlers_.end()) 1373 return false; 1374 // look for the handler in the list 1375 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1376 it != _getSingleton().activeMouseHandlers_.end(); it++) 1377 { 1378 if ((*it) == (*mapIt).second) 1379 { 1380 _getSingleton().activeMouseHandlers_.erase(it); 1306 } 1307 1308 /** 1309 @brief 1310 Disables a specific key handler. 1311 @param name 1312 Unique name of the handler. 1313 @return 1314 False if name was not found, true otherwise. 1315 */ 1316 bool InputManager::disableKeyHandler(const std::string &name) 1317 { 1318 // get pointer from the map with all stored handlers 1319 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1320 if (mapIt == _getSingleton().keyHandlers_.end()) 1321 return false; 1322 // look for the handler in the list 1323 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1324 it != _getSingleton().activeKeyHandlers_.end(); it++) 1325 { 1326 if ((*it) == (*mapIt).second) 1327 { 1328 _getSingleton().activeKeyHandlers_.erase(it); 1329 _getSingleton().stateRequest_ = IS_CUSTOM; 1330 _getSingleton()._updateTickables(); 1331 return true; 1332 } 1333 } 1334 return true; 1335 } 1336 1337 /** 1338 @brief 1339 Checks whether a key handler is active 1340 @param name 1341 Unique name of the handler. 1342 @return 1343 False if key handler is not active or doesn't exist, true otherwise. 1344 */ 1345 bool InputManager::isKeyHandlerActive(const std::string& name) 1346 { 1347 // get pointer from the map with all stored handlers 1348 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); 1349 if (mapIt == _getSingleton().keyHandlers_.end()) 1350 return false; 1351 // see whether the handler already is in the list 1352 for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); 1353 it != _getSingleton().activeKeyHandlers_.end(); it++) 1354 { 1355 if ((*it) == (*mapIt).second) 1356 return true; 1357 } 1358 return false; 1359 } 1360 1361 1362 // ###### MouseHandler ###### 1363 /** 1364 @brief 1365 Adds a new mouse handler. 1366 @param handler 1367 Pointer to the handler object. 1368 @param name 1369 Unique name of the handler. 1370 @return 1371 True if added, false if name already existed. 1372 */ 1373 bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name) 1374 { 1375 if (!handler) 1376 return false; 1377 if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end()) 1378 { 1379 _getSingleton().mouseHandlers_[name] = handler; 1380 return true; 1381 } 1382 else 1383 return false; 1384 } 1385 1386 /** 1387 @brief 1388 Removes a Mouse handler from the list. 1389 @param name 1390 Unique name of the handler. 1391 @return 1392 True if removal was successful, false if name was not found. 1393 */ 1394 bool InputManager::removeMouseHandler(const std::string &name) 1395 { 1396 disableMouseHandler(name); 1397 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); 1398 if (it != _getSingleton().mouseHandlers_.end()) 1399 { 1400 _getSingleton().mouseHandlers_.erase(it); 1401 return true; 1402 } 1403 else 1404 return false; 1405 } 1406 1407 /** 1408 @brief 1409 Returns the pointer to a handler. 1410 @param name 1411 Unique name of the handler. 1412 @return 1413 Pointer to the instance, 0 if name was not found. 1414 */ 1415 MouseHandler* InputManager::getMouseHandler(const std::string& name) 1416 { 1417 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); 1418 if (it != _getSingleton().mouseHandlers_.end()) 1419 { 1420 return (*it).second; 1421 } 1422 else 1423 return 0; 1424 } 1425 1426 /** 1427 @brief 1428 Enables a specific mouse handler that has already been added. 1429 @param name 1430 Unique name of the handler. 1431 @return 1432 False if name was not found, true otherwise. 1433 */ 1434 bool InputManager::enableMouseHandler(const std::string& name) 1435 { 1436 // get pointer from the map with all stored handlers 1437 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1438 if (mapIt == _getSingleton().mouseHandlers_.end()) 1439 return false; 1440 // see whether the handler already is in the list 1441 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1442 it != _getSingleton().activeMouseHandlers_.end(); it++) 1443 { 1444 if ((*it) == (*mapIt).second) 1445 { 1446 return true; 1447 } 1448 } 1449 _getSingleton().activeMouseHandlers_.push_back((*mapIt).second); 1381 1450 _getSingleton().stateRequest_ = IS_CUSTOM; 1382 1451 _getSingleton()._updateTickables(); 1383 1452 return true; 1384 } 1385 } 1386 return true; 1387 } 1388 1389 /** 1390 @brief Checks whether a mouse handler is active 1391 @param name Unique name of the handler. 1392 @return False if key handler is not active or doesn't exist, true otherwise. 1393 */ 1394 bool InputManager::isMouseHandlerActive(const std::string& name) 1395 { 1396 // get pointer from the map with all stored handlers 1397 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1398 if (mapIt == _getSingleton().mouseHandlers_.end()) 1399 return false; 1400 // see whether the handler already is in the list 1401 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1402 it != _getSingleton().activeMouseHandlers_.end(); it++) 1403 { 1404 if ((*it) == (*mapIt).second) 1405 return true; 1406 } 1407 return false; 1408 } 1409 1410 1411 // ###### JoyStickHandler ###### 1412 1413 /** 1414 @brief Adds a new joy stick handler. 1415 @param handler Pointer to the handler object. 1416 @param name Unique name of the handler. 1417 @return True if added, false if name already existed. 1418 */ 1419 bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name) 1420 { 1421 if (!handler) 1422 return false; 1423 if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end()) 1424 { 1425 _getSingleton().joyStickHandlers_[name] = handler; 1426 return true; 1427 } 1428 else 1429 return false; 1430 } 1431 1432 /** 1433 @brief Removes a JoyStick handler from the list. 1434 @param name Unique name of the handler. 1435 @return True if removal was successful, false if name was not found. 1436 */ 1437 bool InputManager::removeJoyStickHandler(const std::string &name) 1438 { 1439 for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); 1440 itstick != _getSingleton().joySticks_.end(); itstick++) 1441 disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin()); 1442 1443 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); 1444 if (it != _getSingleton().joyStickHandlers_.end()) 1445 { 1446 _getSingleton().joyStickHandlers_.erase(it); 1447 return true; 1448 } 1449 else 1450 return false; 1451 } 1452 1453 /** 1454 @brief Returns the pointer to a handler. 1455 @param name Unique name of the handler. 1456 @return Pointer to the instance, 0 if name was not found. 1457 */ 1458 JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name) 1459 { 1460 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); 1461 if (it != _getSingleton().joyStickHandlers_.end()) 1462 { 1463 return (*it).second; 1464 } 1465 else 1466 return 0; 1467 } 1468 1469 /** 1470 @brief Enables a specific joy stick handler that has already been added. 1471 @param name Unique name of the handler. 1472 @return False if name or id was not found, true otherwise. 1473 */ 1474 bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID) 1475 { 1476 // get handler pointer from the map with all stored handlers 1477 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1478 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1479 return false; 1480 1481 // check for existence of the ID 1482 if (ID >= _getSingleton().joySticksSize_) 1483 return false; 1484 1485 // see whether the handler already is in the list 1486 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1487 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1488 { 1489 if ((*it) == (*handlerIt).second) 1490 { 1491 return true; 1492 } 1493 } 1494 _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second); 1495 _getSingleton().stateRequest_ = IS_CUSTOM; 1496 _getSingleton()._updateTickables(); 1497 return true; 1498 } 1499 1500 /** 1501 @brief Disables a specific joy stick handler. 1502 @param name Unique name of the handler. 1503 @return False if name or id was not found, true otherwise. 1504 */ 1505 bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID) 1506 { 1507 // get handler pointer from the map with all stored handlers 1508 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1509 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1510 return false; 1511 1512 // check for existence of the ID 1513 if (ID >= _getSingleton().joySticksSize_) 1514 return false; 1515 1516 // look for the handler in the list 1517 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1518 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1519 { 1520 if ((*it) == (*handlerIt).second) 1521 { 1522 _getSingleton().activeJoyStickHandlers_[ID].erase(it); 1453 } 1454 1455 /** 1456 @brief 1457 Disables a specific mouse handler. 1458 @param name 1459 Unique name of the handler. 1460 @return 1461 False if name was not found, true otherwise. 1462 */ 1463 bool InputManager::disableMouseHandler(const std::string &name) 1464 { 1465 // get pointer from the map with all stored handlers 1466 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1467 if (mapIt == _getSingleton().mouseHandlers_.end()) 1468 return false; 1469 // look for the handler in the list 1470 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1471 it != _getSingleton().activeMouseHandlers_.end(); it++) 1472 { 1473 if ((*it) == (*mapIt).second) 1474 { 1475 _getSingleton().activeMouseHandlers_.erase(it); 1476 _getSingleton().stateRequest_ = IS_CUSTOM; 1477 _getSingleton()._updateTickables(); 1478 return true; 1479 } 1480 } 1481 return true; 1482 } 1483 1484 /** 1485 @brief 1486 Checks whether a mouse handler is active 1487 @param name 1488 Unique name of the handler. 1489 @return 1490 False if key handler is not active or doesn't exist, true otherwise. 1491 */ 1492 bool InputManager::isMouseHandlerActive(const std::string& name) 1493 { 1494 // get pointer from the map with all stored handlers 1495 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); 1496 if (mapIt == _getSingleton().mouseHandlers_.end()) 1497 return false; 1498 // see whether the handler already is in the list 1499 for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); 1500 it != _getSingleton().activeMouseHandlers_.end(); it++) 1501 { 1502 if ((*it) == (*mapIt).second) 1503 return true; 1504 } 1505 return false; 1506 } 1507 1508 1509 // ###### JoyStickHandler ###### 1510 1511 /** 1512 @brief 1513 Adds a new joy stick handler. 1514 @param handler 1515 Pointer to the handler object. 1516 @param name 1517 Unique name of the handler. 1518 @return 1519 True if added, false if name already existed. 1520 */ 1521 bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name) 1522 { 1523 if (!handler) 1524 return false; 1525 if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end()) 1526 { 1527 _getSingleton().joyStickHandlers_[name] = handler; 1528 return true; 1529 } 1530 else 1531 return false; 1532 } 1533 1534 /** 1535 @brief 1536 Removes a JoyStick handler from the list. 1537 @param name 1538 Unique name of the handler. 1539 @return 1540 True if removal was successful, false if name was not found. 1541 */ 1542 bool InputManager::removeJoyStickHandler(const std::string &name) 1543 { 1544 for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); 1545 itstick != _getSingleton().joySticks_.end(); itstick++) 1546 disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin()); 1547 1548 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); 1549 if (it != _getSingleton().joyStickHandlers_.end()) 1550 { 1551 _getSingleton().joyStickHandlers_.erase(it); 1552 return true; 1553 } 1554 else 1555 return false; 1556 } 1557 1558 /** 1559 @brief 1560 Returns the pointer to a handler. 1561 @param name 1562 Unique name of the handler. 1563 @return 1564 Pointer to the instance, 0 if name was not found. 1565 */ 1566 JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name) 1567 { 1568 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); 1569 if (it != _getSingleton().joyStickHandlers_.end()) 1570 { 1571 return (*it).second; 1572 } 1573 else 1574 return 0; 1575 } 1576 1577 /** 1578 @brief 1579 Enables a specific joy stick handler that has already been added. 1580 @param name 1581 Unique name of the handler. 1582 @return 1583 False if name or id was not found, true otherwise. 1584 */ 1585 bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID) 1586 { 1587 // get handler pointer from the map with all stored handlers 1588 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1589 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1590 return false; 1591 1592 // check for existence of the ID 1593 if (ID >= _getSingleton().joySticksSize_) 1594 return false; 1595 1596 // see whether the handler already is in the list 1597 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1598 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1599 { 1600 if ((*it) == (*handlerIt).second) 1601 { 1602 return true; 1603 } 1604 } 1605 _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second); 1523 1606 _getSingleton().stateRequest_ = IS_CUSTOM; 1524 1607 _getSingleton()._updateTickables(); 1525 1608 return true; 1526 } 1527 } 1528 return true; 1529 } 1530 1531 /** 1532 @brief Checks whether a joy stick handler is active 1533 @param name Unique name of the handler. 1534 @return False if key handler is not active or doesn't exist, true otherwise. 1535 */ 1536 bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID) 1537 { 1538 // get handler pointer from the map with all stored handlers 1539 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1540 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1541 return false; 1542 1543 // check for existence of the ID 1544 if (ID >= _getSingleton().joySticksSize_) 1545 return false; 1546 1547 // see whether the handler already is in the list 1548 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1549 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1550 { 1551 if ((*it) == (*handlerIt).second) 1552 return true; 1553 } 1554 return false; 1555 } 1609 } 1610 1611 /** 1612 @brief 1613 Disables a specific joy stick handler. 1614 @param name 1615 Unique name of the handler. 1616 @return 1617 False if name or id was not found, true otherwise. 1618 */ 1619 bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID) 1620 { 1621 // get handler pointer from the map with all stored handlers 1622 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1623 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1624 return false; 1625 1626 // check for existence of the ID 1627 if (ID >= _getSingleton().joySticksSize_) 1628 return false; 1629 1630 // look for the handler in the list 1631 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1632 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1633 { 1634 if ((*it) == (*handlerIt).second) 1635 { 1636 _getSingleton().activeJoyStickHandlers_[ID].erase(it); 1637 _getSingleton().stateRequest_ = IS_CUSTOM; 1638 _getSingleton()._updateTickables(); 1639 return true; 1640 } 1641 } 1642 return true; 1643 } 1644 1645 /** 1646 @brief 1647 Checks whether a joy stick handler is active 1648 @param name 1649 Unique name of the handler. 1650 @return 1651 False if key handler is not active or doesn't exist, true otherwise. 1652 */ 1653 bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID) 1654 { 1655 // get handler pointer from the map with all stored handlers 1656 std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); 1657 if (handlerIt == _getSingleton().joyStickHandlers_.end()) 1658 return false; 1659 1660 // check for existence of the ID 1661 if (ID >= _getSingleton().joySticksSize_) 1662 return false; 1663 1664 // see whether the handler already is in the list 1665 for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); 1666 it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) 1667 { 1668 if ((*it) == (*handlerIt).second) 1669 return true; 1670 } 1671 return false; 1672 } 1556 1673 1557 1674 } -
code/branches/input/src/core/input/InputManager.h
r1629 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of a little Input handler that distributes everything 32 coming from OIS. 33 */ 30 @file 31 @brief 32 Implementation of a little Input handler that distributes everything 33 coming from OIS. 34 */ 34 35 35 36 #ifndef _InputManager_H__ … … 40 41 #include <map> 41 42 #include <vector> 42 43 43 #include "util/Math.h" 44 44 #include "core/OrxonoxClass.h" … … 47 47 namespace orxonox 48 48 { 49 /** 50 * Helper class to realise a vector<int[4]> 51 */ 52 class POVStates 53 { 54 public: 55 int operator[](unsigned int index) { return povStates[index]; } 56 int povStates[4]; 57 }; 58 59 /** 60 * Helper class to realise a vector< {int[4], int[4]} > 61 */ 62 class SliderStates 63 { 64 public: 65 IntVector2 sliderStates[4]; 66 }; 67 68 /** 69 * Struct for storing a custom input state 70 */ 71 struct StoredState 72 { 73 std::vector<KeyHandler*> activeKeyHandlers_; 74 std::vector<MouseHandler*> activeMouseHandlers_; 75 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 76 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 77 }; 78 79 struct JoyStickCalibration 80 { 81 int zeroStates[24]; 82 float positiveCoeff[24]; 83 float negativeCoeff[24]; 84 }; 85 86 /** 87 @brief Captures and distributes mouse and keyboard input. 88 */ 89 class _CoreExport InputManager 49 /** 50 @brief 51 Helper class to realise a vector<int[4]> 52 */ 53 class POVStates 54 { 55 public: 56 int operator[](unsigned int index) { return povStates[index]; } 57 int povStates[4]; 58 }; 59 60 /** 61 @brief 62 Helper class to realise a vector< {int[4], int[4]} > 63 */ 64 class SliderStates 65 { 66 public: 67 IntVector2 sliderStates[4]; 68 }; 69 70 /** 71 @brief 72 Struct for storing a custom input state 73 */ 74 struct StoredState 75 { 76 std::vector<KeyHandler*> activeKeyHandlers_; 77 std::vector<MouseHandler*> activeMouseHandlers_; 78 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 79 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 80 }; 81 82 struct JoyStickCalibration 83 { 84 int zeroStates[24]; 85 float positiveCoeff[24]; 86 float negativeCoeff[24]; 87 }; 88 89 /** 90 @brief 91 Captures and distributes mouse and keyboard input. 92 */ 93 class _CoreExport InputManager 90 94 : public OrxonoxClass, 91 public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener 92 { 93 public: // enumerations 94 /** 95 @brief Designates the way input is handled and redirected. 96 */ 97 enum InputState 98 { 99 IS_UNINIT, //!< InputManager has not yet been initialised. 100 IS_NONE, //!< Input is discarded. 101 IS_NORMAL, //!< Normal play state. Key and button bindings are active. 102 IS_GUI, //!< All OIS input events are passed to CEGUI. 103 IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. 104 IS_DETECT, //!< All the input additionally goes to the KeyDetector 105 IS_NODETECT, //!< remove KeyDetector 106 IS_NOCALIBRATE, 107 IS_CALIBRATE, 108 IS_CUSTOM //!< Any possible configuration. 109 }; 110 111 public: // member functions 112 void setConfigValues(); 113 114 public: // static functions 115 static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, 116 bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); 117 static bool initialiseKeyboard(); 118 static bool initialiseMouse(); 119 static bool initialiseJoySticks(); 120 static int numberOfKeyboards(); 121 static int numberOfMice(); 122 static int numberOfJoySticks(); 123 124 static void destroy(); 125 static void destroyKeyboard(); 126 static void destroyMouse(); 127 static void destroyJoySticks(); 128 129 //static bool isModifierDown(KeyboardModifier::Enum modifier); 130 //static bool isKeyDown(KeyCode::Enum key); 131 //static const MouseState getMouseState(); 132 //static const JoyStickState getJoyStickState(unsigned int ID); 133 134 static void setWindowExtents(const int width, const int height); 135 136 static void setInputState(const InputState state); 137 static InputState getInputState(); 138 139 static void storeKeyStroke(const std::string& name); 140 static void keyBind(const std::string& command); 141 142 static void calibrate(); 143 144 static void tick(float dt); 145 146 static bool addKeyHandler (KeyHandler* handler, const std::string& name); 147 static bool removeKeyHandler (const std::string& name); 148 static KeyHandler* getKeyHandler (const std::string& name); 149 static bool enableKeyHandler (const std::string& name); 150 static bool disableKeyHandler (const std::string& name); 151 static bool isKeyHandlerActive (const std::string& name); 152 153 static bool addMouseHandler (MouseHandler* handler, const std::string& name); 154 static bool removeMouseHandler (const std::string& name); 155 static MouseHandler* getMouseHandler (const std::string& name); 156 static bool enableMouseHandler (const std::string& name); 157 static bool disableMouseHandler (const std::string& name); 158 static bool isMouseHandlerActive (const std::string& name); 159 160 static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); 161 static bool removeJoyStickHandler (const std::string& name); 162 static JoyStickHandler* getJoyStickHandler(const std::string& name); 163 static bool enableJoyStickHandler (const std::string& name, unsigned int id); 164 static bool disableJoyStickHandler (const std::string& name, unsigned int id); 165 static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); 166 167 private: // functions 168 // don't mess with a Singleton 169 InputManager (); 170 InputManager (const InputManager&); 171 ~InputManager(); 172 173 // Intenal methods 174 bool _initialise(const size_t, int, int, bool, bool, bool); 175 bool _initialiseKeyboard(); 176 bool _initialiseMouse(); 177 bool _initialiseJoySticks(); 178 179 void _destroy(); 180 void _destroyKeyboard(); 181 void _destroyMouse(); 182 void _destroyJoySticks(); 183 184 void _updateTickables(); 185 186 void _saveState(); 187 void _restoreState(); 188 189 void _completeCalibration(); 190 191 void _fireAxis(unsigned int iJoyStick, int axis, int value); 192 unsigned int _getJoystick(const OIS::JoyStickEvent& arg); 193 194 void _tick(float dt); 195 196 // input events 197 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 198 bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 199 bool mouseMoved (const OIS::MouseEvent &arg); 200 bool keyPressed (const OIS::KeyEvent &arg); 201 bool keyReleased (const OIS::KeyEvent &arg); 202 bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 203 bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 204 bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 205 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 206 bool povMoved (const OIS::JoyStickEvent &arg, int id); 207 //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); 208 209 static InputManager& _getSingleton(); 210 static InputManager* _getSingletonPtr() { return &_getSingleton(); } 211 212 private: // variables 213 OIS::InputManager* inputSystem_; //!< OIS input manager 214 OIS::Keyboard* keyboard_; //!< OIS mouse 215 OIS::Mouse* mouse_; //!< OIS keyboard 216 std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks 217 unsigned int joySticksSize_; 218 219 KeyBinder* keyBinder_; //!< KeyBinder instance 220 KeyDetector* keyDetector_; //!< KeyDetector instance 221 InputBuffer* buffer_; //!< InputBuffer instance 222 CalibratorCallback* calibratorCallback_; 223 224 InputState state_; 225 InputState stateRequest_; 226 InputState savedState_; 227 unsigned int keyboardModifiers_; 228 StoredState savedHandlers_; 229 230 // joystick calibration 231 //std::vector<int> marginalsMaxConfig_; 232 //std::vector<int> marginalsMinConfig_; 233 int marginalsMax_[24]; 234 int marginalsMin_[24]; 235 bool bCalibrated_; 236 237 //! Keeps track of the joy stick POV states 238 std::vector<POVStates> povStates_; 239 //! Keeps track of the possibly two slider axes 240 std::vector<SliderStates> sliderStates_; 241 std::vector<JoyStickCalibration> joySticksCalibration_; 242 243 std::map<std::string, KeyHandler*> keyHandlers_; 244 std::map<std::string, MouseHandler*> mouseHandlers_; 245 std::map<std::string, JoyStickHandler*> joyStickHandlers_; 246 247 std::vector<KeyHandler*> activeKeyHandlers_; 248 std::vector<MouseHandler*> activeMouseHandlers_; 249 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 250 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 251 252 std::vector<Key> keysDown_; 253 std::vector<MouseButton::Enum> mouseButtonsDown_; 254 std::vector<std::vector<int> > joyStickButtonsDown_; 255 256 static std::string bindingCommmandString_s; 257 }; 95 public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener 96 { 97 public: // enumerations 98 /** 99 @brief 100 Designates the way input is handled and redirected. 101 */ 102 enum InputState 103 { 104 IS_UNINIT, //!< InputManager has not yet been initialised. 105 IS_NONE, //!< Input is discarded. 106 IS_NORMAL, //!< Normal play state. Key and button bindings are active. 107 IS_GUI, //!< All OIS input events are passed to CEGUI. 108 IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. 109 IS_DETECT, //!< All the input additionally goes to the KeyDetector 110 IS_NODETECT, //!< remove KeyDetector 111 IS_NOCALIBRATE, 112 IS_CALIBRATE, 113 IS_CUSTOM //!< Any possible configuration. 114 }; 115 116 public: // member functions 117 void setConfigValues(); 118 119 public: // static functions 120 static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, 121 bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); 122 static bool initialiseKeyboard(); 123 static bool initialiseMouse(); 124 static bool initialiseJoySticks(); 125 static int numberOfKeyboards(); 126 static int numberOfMice(); 127 static int numberOfJoySticks(); 128 129 static void destroy(); 130 static void destroyKeyboard(); 131 static void destroyMouse(); 132 static void destroyJoySticks(); 133 134 //static bool isModifierDown(KeyboardModifier::Enum modifier); 135 //static bool isKeyDown(KeyCode::Enum key); 136 //static const MouseState getMouseState(); 137 //static const JoyStickState getJoyStickState(unsigned int ID); 138 139 static void setWindowExtents(const int width, const int height); 140 141 static void setInputState(const InputState state); 142 static InputState getInputState(); 143 144 static void storeKeyStroke(const std::string& name); 145 static void keyBind(const std::string& command); 146 147 static void calibrate(); 148 149 static void tick(float dt); 150 151 static bool addKeyHandler (KeyHandler* handler, const std::string& name); 152 static bool removeKeyHandler (const std::string& name); 153 static KeyHandler* getKeyHandler (const std::string& name); 154 static bool enableKeyHandler (const std::string& name); 155 static bool disableKeyHandler (const std::string& name); 156 static bool isKeyHandlerActive (const std::string& name); 157 158 static bool addMouseHandler (MouseHandler* handler, const std::string& name); 159 static bool removeMouseHandler (const std::string& name); 160 static MouseHandler* getMouseHandler (const std::string& name); 161 static bool enableMouseHandler (const std::string& name); 162 static bool disableMouseHandler (const std::string& name); 163 static bool isMouseHandlerActive (const std::string& name); 164 165 static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); 166 static bool removeJoyStickHandler (const std::string& name); 167 static JoyStickHandler* getJoyStickHandler(const std::string& name); 168 static bool enableJoyStickHandler (const std::string& name, unsigned int id); 169 static bool disableJoyStickHandler (const std::string& name, unsigned int id); 170 static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); 171 172 private: // functions 173 // don't mess with a Singleton 174 InputManager (); 175 InputManager (const InputManager&); 176 ~InputManager(); 177 178 // Intenal methods 179 bool _initialise(const size_t, int, int, bool, bool, bool); 180 bool _initialiseKeyboard(); 181 bool _initialiseMouse(); 182 bool _initialiseJoySticks(); 183 184 void _destroy(); 185 void _destroyKeyboard(); 186 void _destroyMouse(); 187 void _destroyJoySticks(); 188 189 void _updateTickables(); 190 191 void _saveState(); 192 void _restoreState(); 193 194 void _completeCalibration(); 195 196 void _fireAxis(unsigned int iJoyStick, int axis, int value); 197 unsigned int _getJoystick(const OIS::JoyStickEvent& arg); 198 199 void _tick(float dt); 200 201 // input events 202 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 203 bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 204 bool mouseMoved (const OIS::MouseEvent &arg); 205 bool keyPressed (const OIS::KeyEvent &arg); 206 bool keyReleased (const OIS::KeyEvent &arg); 207 bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 208 bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 209 bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 210 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 211 bool povMoved (const OIS::JoyStickEvent &arg, int id); 212 //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); 213 214 static InputManager& _getSingleton(); 215 static InputManager* _getSingletonPtr() { return &_getSingleton(); } 216 217 private: // variables 218 OIS::InputManager* inputSystem_; //!< OIS input manager 219 OIS::Keyboard* keyboard_; //!< OIS mouse 220 OIS::Mouse* mouse_; //!< OIS keyboard 221 std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks 222 unsigned int joySticksSize_; 223 224 KeyBinder* keyBinder_; //!< KeyBinder instance 225 KeyDetector* keyDetector_; //!< KeyDetector instance 226 InputBuffer* buffer_; //!< InputBuffer instance 227 CalibratorCallback* calibratorCallback_; 228 229 InputState state_; 230 InputState stateRequest_; 231 InputState savedState_; 232 unsigned int keyboardModifiers_; 233 StoredState savedHandlers_; 234 235 // joystick calibration 236 //std::vector<int> marginalsMaxConfig_; 237 //std::vector<int> marginalsMinConfig_; 238 int marginalsMax_[24]; 239 int marginalsMin_[24]; 240 bool bCalibrated_; 241 242 //! Keeps track of the joy stick POV states 243 std::vector<POVStates> povStates_; 244 //! Keeps track of the possibly two slider axes 245 std::vector<SliderStates> sliderStates_; 246 std::vector<JoyStickCalibration> joySticksCalibration_; 247 248 std::map<std::string, KeyHandler*> keyHandlers_; 249 std::map<std::string, MouseHandler*> mouseHandlers_; 250 std::map<std::string, JoyStickHandler*> joyStickHandlers_; 251 252 std::vector<KeyHandler*> activeKeyHandlers_; 253 std::vector<MouseHandler*> activeMouseHandlers_; 254 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 255 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 256 257 std::vector<Key> keysDown_; 258 std::vector<MouseButton::Enum> mouseButtonsDown_; 259 std::vector<std::vector<int> > joyStickButtonsDown_; 260 261 static std::string bindingCommmandString_s; 262 }; 258 263 259 264 } -
code/branches/input/src/core/input/KeyBinder.cc
r1629 r1630 43 43 namespace orxonox 44 44 { 45 /** 46 @brief Constructor that does as little as necessary. 47 */ 48 KeyBinder::KeyBinder() : deriveTime_(0.0f) 49 { 50 mouseRelative_[0] = 0; 51 mouseRelative_[1] = 0; 52 mousePosition_[0] = 0; 53 mousePosition_[1] = 0; 54 55 RegisterRootObject(KeyBinder); 56 57 // keys 58 std::string keyNames[] = { 59 "UNASSIGNED", 60 "ESCAPE", 61 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 62 "MINUS", "EQUALS", "BACK", "TAB", 63 "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", 64 "LBRACKET", "RBRACKET", 65 "RETURN", "LCONTROL", 66 "A", "S", "D", "F", "G", "H", "J", "K", "L", 67 "SEMICOLON", "APOSTROPHE", "GRAVE", 68 "LSHIFT", "BACKSLASH", 69 "Z", "X", "C", "V", "B", "N", "M", 70 "COMMA", "PERIOD", "SLASH", 71 "RSHIFT", 72 "MULTIPLY", 73 "LMENU", 74 "SPACE", 75 "CAPITAL", 76 "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", 77 "NUMLOCK", "SCROLL", 78 "NUMPAD7", "NUMPAD8", "NUMPAD9", 79 "SUBTRACT", 80 "NUMPAD4", "NUMPAD5", "NUMPAD6", 81 "ADD", 82 "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0", 83 "DECIMAL", 84 "","", 85 "OEM_102", 86 "F11", "F12", 87 "","","","","","","","","","","", 88 "F13", "F14", "F15", 89 "","","","","","","","","","", 90 "KANA", 91 "","", 92 "ABNT_C1", 93 "","","","","", 94 "CONVERT", 95 "", 96 "NOCONVERT", 97 "", 98 "YEN", 99 "ABNT_C2", 100 "","","","","","","","","","","","","","", 101 "NUMPADEQUALS", 102 "","", 103 "PREVTRACK", 104 "AT", 105 "COLON", "UNDERLINE", 106 "KANJI", 107 "STOP", 108 "AX", 109 "UNLABELED", 110 "NEXTTRACK", 111 "","", 112 "NUMPADENTER", 113 "RCONTROL", 114 "","", 115 "MUTE", 116 "CALCULATOR", 117 "PLAYPAUSE", 118 "", 119 "MEDIASTOP", 120 "","","","","","","","","", 121 "VOLUMEDOWN", 122 "", 123 "VOLUMEUP", 124 "", 125 "WEBHOME", 126 "NUMPADCOMMA", 127 "", 128 "DIVIDE", 129 "", 130 "SYSRQ", 131 "RMENU", 132 "","","","","","","","","","","","", 133 "PAUSE", 134 "", 135 "HOME", 136 "UP", 137 "PGUP", 138 "", 139 "LEFT", 140 "", 141 "RIGHT", 142 "", 143 "END", "DOWN", "PGDOWN", "INSERT", "DELETE", 144 "","","","","","","", 145 "LWIN", "RWIN", "APPS", 146 "POWER", "SLEEP", 147 "","","", 148 "WAKE", 149 "", 150 "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK", 151 "MYCOMPUTER", "MAIL", "MEDIASELECT" 152 }; 153 for (unsigned int i = 0; i < nKeys_s; i++) 154 keys_[i].name_ = "Key" + keyNames[i]; 155 156 // mouse buttons 157 std::string mouseButtonNames[] = { 158 "MouseLeft", "MouseRight", "MouseMiddle", 159 "MouseButton3", "MouseButton4", "MouseButton5", 160 "MouseButton6", "MouseButton7", 161 "MouseWheel1Up", "MouseWheel1Down", 162 "MouseWheel2Up", "MouseWheel2Down" }; 163 for (unsigned int i = 0; i < nMouseButtons_s; i++) 164 mouseButtons_[i].name_ = mouseButtonNames[i]; 165 166 // joy stick buttons 167 for (unsigned int i = 0; i < 32; i++) 168 joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i); 169 for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4) 170 { 171 joyStickButtons_[i + 0].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North"; 172 joyStickButtons_[i + 1].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South"; 173 joyStickButtons_[i + 2].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East"; 174 joyStickButtons_[i + 3].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West"; 175 } 176 177 // half axes 178 std::string rawNames[nHalfAxes_s/2]; 179 rawNames[0] = "MouseX"; 180 rawNames[1] = "MouseY"; 181 rawNames[2] = "Empty1"; 182 rawNames[3] = "Empty2"; 183 for (unsigned int i = 4; i < nHalfAxes_s/2; i++) 184 rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3); 185 for (unsigned int i = 0; i < nHalfAxes_s/2; i++) 186 { 187 halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos"; 188 halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg"; 189 } 190 191 for (unsigned int i = 0; i < this->nHalfAxes_s; i++) 192 halfAxes_[i].buttonThreshold_ = buttonThreshold_; 193 } 194 195 /** 196 @brief Destructor 197 */ 198 KeyBinder::~KeyBinder() 199 { 200 // almost no destructors required because most of the arrays are static. 201 clearBindings(); // does some destruction work 202 } 203 204 /** 205 @brief Loads the key and button bindings. 206 @return True if loading succeeded. 207 */ 208 void KeyBinder::loadBindings() 209 { 210 COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; 211 212 clearBindings(); 213 214 std::ifstream infile; 215 infile.open("keybindings.ini"); 216 if (!infile) 217 { 218 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "def_keybindings.ini"); 219 ConfigFileManager::getSingleton()->save(CFT_Keybindings, "keybindings.ini"); 220 } 221 else 222 infile.close(); 223 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); 224 225 // parse key bindings 226 setConfigValues(); 227 228 COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; 229 } 230 231 /** 232 @brief Loader for the key bindings, managed by config values. 233 */ 234 void KeyBinder::setConfigValues() 235 { 236 SetConfigValueGeneric(KeyBinder, analogThreshold_, 0.05f) .description("Threshold for analog axes until which the state is 0."); 237 SetConfigValueGeneric(KeyBinder, mouseSensitivity_, 1.0f) .description("Mouse sensitivity."); 238 SetConfigValueGeneric(KeyBinder, bDeriveMouseInput_, false).description("Whether or not to derive moues movement for the absolute value."); 239 SetConfigValueGeneric(KeyBinder, derivePeriod_, 0.05f).description("Accuracy of the mouse input deriver. The higher the more precise, but laggier."); 240 SetConfigValueGeneric(KeyBinder, mouseSensitivityDerived_, 1.0f).description("Mouse sensitivity if mouse input is derived."); 241 SetConfigValueGeneric(KeyBinder, bClipMouse_, true).description("Whether or not to clip absolute value of mouse in non derive mode."); 242 243 float oldThresh = buttonThreshold_; 244 SetConfigValueGeneric(KeyBinder, buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed."); 245 if (oldThresh != buttonThreshold_) 246 for (unsigned int i = 0; i < nHalfAxes_s; i++) 247 if (halfAxes_[i].buttonThreshold_ == oldThresh) 248 halfAxes_[i].buttonThreshold_ = buttonThreshold_; 249 250 // keys 251 for (unsigned int i = 0; i < nKeys_s; i++) 252 readTrigger(keys_[i]); 253 // mouse buttons 254 for (unsigned int i = 0; i < nMouseButtons_s; i++) 255 readTrigger(mouseButtons_[i]); 256 // joy stick buttons 257 for (unsigned int i = 0; i < nJoyStickButtons_s; i++) 258 readTrigger(joyStickButtons_[i]); 259 // half axes 260 for (unsigned int i = 0; i < nHalfAxes_s; i++) 261 readTrigger(halfAxes_[i]); 262 } 263 264 void KeyBinder::readTrigger(Button& button) 265 { 266 // config value stuff 267 ConfigValueContainer* cont = ClassIdentifier<KeyBinder>::getIdentifier()->getConfigValueContainer(button.name_); 268 if (!cont) 269 { 270 cont = new ConfigValueContainer(CFT_Keybindings, ClassIdentifier<KeyBinder>::getIdentifier(), button.name_, ""); 271 ClassIdentifier<KeyBinder>::getIdentifier()->addConfigValueContainer(button.name_, cont); 272 } 273 std::string old = button.bindingString_; 274 cont->getValue(&button.bindingString_); 275 276 // keybinder stuff 277 if (old != button.bindingString_) 278 { 279 // clear everything so we don't get old axis ParamCommands mixed up 280 button.clear(); 281 282 // binding has changed 283 button.parse(paramCommandBuffer_); 284 } 285 } 286 287 /** 288 @brief Overwrites all bindings with "" 289 */ 290 void KeyBinder::clearBindings() 291 { 292 for (unsigned int i = 0; i < nKeys_s; i++) 293 keys_[i].clear(); 294 295 for (unsigned int i = 0; i < nMouseButtons_s; i++) 296 mouseButtons_[i].clear(); 297 298 for (unsigned int i = 0; i < nJoyStickButtons_s; i++) 299 joyStickButtons_[i].clear(); 300 301 for (unsigned int i = 0; i < nHalfAxes_s; i++) 302 halfAxes_[i].clear(); 303 304 for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) 305 delete paramCommandBuffer_[i]; 306 paramCommandBuffer_.clear(); 307 } 308 309 void KeyBinder::resetJoyStickAxes() 310 { 311 for (unsigned int i = 8; i < nHalfAxes_s; i++) 312 { 313 halfAxes_[i].absVal_ = 0.0f; 314 halfAxes_[i].relVal_ = 0.0f; 315 } 316 } 317 318 void KeyBinder::tickInput(float dt, const HandlerState& state) 319 { 320 // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event. 321 unsigned int iBegin = 8; 322 unsigned int iEnd = 8; 323 if (state.joyStick) 324 iEnd = nHalfAxes_s; 325 if (state.mouse) 326 iBegin = 0; 327 for (unsigned int i = iBegin; i < iEnd; i++) 328 { 329 if (halfAxes_[i].hasChanged_) 330 { 331 if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_) 332 { 333 halfAxes_[i].wasDown_ = true; 334 if (halfAxes_[i].nCommands_[KeybindMode::OnPress]) 335 halfAxes_[i].execute(KeybindMode::OnPress); 336 } 337 else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_) 338 { 339 halfAxes_[i].wasDown_ = false; 340 if (halfAxes_[i].nCommands_[KeybindMode::OnRelease]) 341 halfAxes_[i].execute(KeybindMode::OnRelease); 342 } 343 halfAxes_[i].hasChanged_ = false; 344 } 345 346 if (halfAxes_[i].wasDown_) 347 { 348 if (halfAxes_[i].nCommands_[KeybindMode::OnHold]) 349 halfAxes_[i].execute(KeybindMode::OnHold); 350 } 351 352 // these are the actually useful axis bindings for analog input AND output 353 if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) 354 { 355 //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl; 356 halfAxes_[i].execute(); 357 } 358 } 359 360 if (bDeriveMouseInput_ && state.mouse) 361 { 362 if (deriveTime_ > derivePeriod_) 363 { 364 //CCOUT(3) << "mouse abs: "; 45 /** 46 @brief 47 Constructor that does as little as necessary. 48 */ 49 KeyBinder::KeyBinder() 50 : deriveTime_(0.0f) 51 { 52 mouseRelative_[0] = 0; 53 mouseRelative_[1] = 0; 54 mousePosition_[0] = 0; 55 mousePosition_[1] = 0; 56 57 RegisterRootObject(KeyBinder); 58 59 // keys 60 std::string keyNames[] = { 61 "UNASSIGNED", 62 "ESCAPE", 63 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 64 "MINUS", "EQUALS", "BACK", "TAB", 65 "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", 66 "LBRACKET", "RBRACKET", 67 "RETURN", "LCONTROL", 68 "A", "S", "D", "F", "G", "H", "J", "K", "L", 69 "SEMICOLON", "APOSTROPHE", "GRAVE", 70 "LSHIFT", "BACKSLASH", 71 "Z", "X", "C", "V", "B", "N", "M", 72 "COMMA", "PERIOD", "SLASH", 73 "RSHIFT", 74 "MULTIPLY", 75 "LMENU", 76 "SPACE", 77 "CAPITAL", 78 "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", 79 "NUMLOCK", "SCROLL", 80 "NUMPAD7", "NUMPAD8", "NUMPAD9", 81 "SUBTRACT", 82 "NUMPAD4", "NUMPAD5", "NUMPAD6", 83 "ADD", 84 "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0", 85 "DECIMAL", 86 "","", 87 "OEM_102", 88 "F11", "F12", 89 "","","","","","","","","","","", 90 "F13", "F14", "F15", 91 "","","","","","","","","","", 92 "KANA", 93 "","", 94 "ABNT_C1", 95 "","","","","", 96 "CONVERT", 97 "", 98 "NOCONVERT", 99 "", 100 "YEN", 101 "ABNT_C2", 102 "","","","","","","","","","","","","","", 103 "NUMPADEQUALS", 104 "","", 105 "PREVTRACK", 106 "AT", 107 "COLON", "UNDERLINE", 108 "KANJI", 109 "STOP", 110 "AX", 111 "UNLABELED", 112 "NEXTTRACK", 113 "","", 114 "NUMPADENTER", 115 "RCONTROL", 116 "","", 117 "MUTE", 118 "CALCULATOR", 119 "PLAYPAUSE", 120 "", 121 "MEDIASTOP", 122 "","","","","","","","","", 123 "VOLUMEDOWN", 124 "", 125 "VOLUMEUP", 126 "", 127 "WEBHOME", 128 "NUMPADCOMMA", 129 "", 130 "DIVIDE", 131 "", 132 "SYSRQ", 133 "RMENU", 134 "","","","","","","","","","","","", 135 "PAUSE", 136 "", 137 "HOME", 138 "UP", 139 "PGUP", 140 "", 141 "LEFT", 142 "", 143 "RIGHT", 144 "", 145 "END", "DOWN", "PGDOWN", "INSERT", "DELETE", 146 "","","","","","","", 147 "LWIN", "RWIN", "APPS", 148 "POWER", "SLEEP", 149 "","","", 150 "WAKE", 151 "", 152 "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK", 153 "MYCOMPUTER", "MAIL", "MEDIASELECT" 154 }; 155 for (unsigned int i = 0; i < nKeys_s; i++) 156 keys_[i].name_ = "Key" + keyNames[i]; 157 158 // mouse buttons 159 std::string mouseButtonNames[] = { 160 "MouseLeft", "MouseRight", "MouseMiddle", 161 "MouseButton3", "MouseButton4", "MouseButton5", 162 "MouseButton6", "MouseButton7", 163 "MouseWheel1Up", "MouseWheel1Down", 164 "MouseWheel2Up", "MouseWheel2Down" 165 }; 166 for (unsigned int i = 0; i < nMouseButtons_s; i++) 167 mouseButtons_[i].name_ = mouseButtonNames[i]; 168 169 // joy stick buttons 170 for (unsigned int i = 0; i < 32; i++) 171 joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i); 172 for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4) 173 { 174 joyStickButtons_[i + 0].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "North"; 175 joyStickButtons_[i + 1].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "South"; 176 joyStickButtons_[i + 2].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "East"; 177 joyStickButtons_[i + 3].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "West"; 178 } 179 180 // half axes 181 std::string rawNames[nHalfAxes_s/2]; 182 rawNames[0] = "MouseX"; 183 rawNames[1] = "MouseY"; 184 rawNames[2] = "Empty1"; 185 rawNames[3] = "Empty2"; 186 for (unsigned int i = 4; i < nHalfAxes_s/2; i++) 187 rawNames[i] = "JoyAxis" + convertToString(i - 3); 188 for (unsigned int i = 0; i < nHalfAxes_s/2; i++) 189 { 190 halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos"; 191 halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg"; 192 } 193 194 for (unsigned int i = 0; i < this->nHalfAxes_s; i++) 195 halfAxes_[i].buttonThreshold_ = buttonThreshold_; 196 } 197 198 /** 199 @brief 200 Destructor 201 */ 202 KeyBinder::~KeyBinder() 203 { 204 // almost no destructors required because most of the arrays are static. 205 clearBindings(); // does some destruction work 206 } 207 208 /** 209 @brief 210 Loads the key and button bindings. 211 @return 212 True if loading succeeded. 213 */ 214 void KeyBinder::loadBindings() 215 { 216 COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; 217 218 clearBindings(); 219 220 std::ifstream infile; 221 infile.open("keybindings.ini"); 222 if (!infile) 223 { 224 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "def_keybindings.ini"); 225 ConfigFileManager::getSingleton()->save(CFT_Keybindings, "keybindings.ini"); 226 } 227 else 228 infile.close(); 229 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); 230 231 // parse key bindings 232 setConfigValues(); 233 234 COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; 235 } 236 237 /** 238 @brief 239 Loader for the key bindings, managed by config values. 240 */ 241 void KeyBinder::setConfigValues() 242 { 243 SetConfigValueGeneric(KeyBinder, analogThreshold_, 0.05f) 244 .description("Threshold for analog axes until which the state is 0."); 245 SetConfigValueGeneric(KeyBinder, mouseSensitivity_, 1.0f) 246 .description("Mouse sensitivity."); 247 SetConfigValueGeneric(KeyBinder, bDeriveMouseInput_, false) 248 .description("Whether or not to derive moues movement for the absolute value."); 249 SetConfigValueGeneric(KeyBinder, derivePeriod_, 0.05f) 250 .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier."); 251 SetConfigValueGeneric(KeyBinder, mouseSensitivityDerived_, 1.0f) 252 .description("Mouse sensitivity if mouse input is derived."); 253 SetConfigValueGeneric(KeyBinder, bClipMouse_, true) 254 .description("Whether or not to clip absolute value of mouse in non derive mode."); 255 256 float oldThresh = buttonThreshold_; 257 SetConfigValueGeneric(KeyBinder, buttonThreshold_, 0.80f) 258 .description("Threshold for analog axes until which the button is not pressed."); 259 if (oldThresh != buttonThreshold_) 260 for (unsigned int i = 0; i < nHalfAxes_s; i++) 261 if (halfAxes_[i].buttonThreshold_ == oldThresh) 262 halfAxes_[i].buttonThreshold_ = buttonThreshold_; 263 264 // keys 265 for (unsigned int i = 0; i < nKeys_s; i++) 266 readTrigger(keys_[i]); 267 // mouse buttons 268 for (unsigned int i = 0; i < nMouseButtons_s; i++) 269 readTrigger(mouseButtons_[i]); 270 // joy stick buttons 271 for (unsigned int i = 0; i < nJoyStickButtons_s; i++) 272 readTrigger(joyStickButtons_[i]); 273 // half axes 274 for (unsigned int i = 0; i < nHalfAxes_s; i++) 275 readTrigger(halfAxes_[i]); 276 } 277 278 void KeyBinder::readTrigger(Button& button) 279 { 280 // config value stuff 281 ConfigValueContainer* cont 282 = ClassIdentifier<KeyBinder>::getIdentifier()->getConfigValueContainer(button.name_); 283 if (!cont) 284 { 285 cont = new ConfigValueContainer 286 (CFT_Keybindings, ClassIdentifier<KeyBinder>::getIdentifier(), button.name_, ""); 287 ClassIdentifier<KeyBinder>::getIdentifier()->addConfigValueContainer(button.name_, cont); 288 } 289 std::string old = button.bindingString_; 290 cont->getValue(&button.bindingString_); 291 292 // keybinder stuff 293 if (old != button.bindingString_) 294 { 295 // clear everything so we don't get old axis ParamCommands mixed up 296 button.clear(); 297 298 // binding has changed 299 button.parse(paramCommandBuffer_); 300 } 301 } 302 303 /** 304 @brief 305 Overwrites all bindings with "" 306 */ 307 void KeyBinder::clearBindings() 308 { 309 for (unsigned int i = 0; i < nKeys_s; i++) 310 keys_[i].clear(); 311 312 for (unsigned int i = 0; i < nMouseButtons_s; i++) 313 mouseButtons_[i].clear(); 314 315 for (unsigned int i = 0; i < nJoyStickButtons_s; i++) 316 joyStickButtons_[i].clear(); 317 318 for (unsigned int i = 0; i < nHalfAxes_s; i++) 319 halfAxes_[i].clear(); 320 321 for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) 322 delete paramCommandBuffer_[i]; 323 paramCommandBuffer_.clear(); 324 } 325 326 void KeyBinder::resetJoyStickAxes() 327 { 328 for (unsigned int i = 8; i < nHalfAxes_s; i++) 329 { 330 halfAxes_[i].absVal_ = 0.0f; 331 halfAxes_[i].relVal_ = 0.0f; 332 } 333 } 334 335 void KeyBinder::tickInput(float dt, const HandlerState& state) 336 { 337 // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event. 338 unsigned int iBegin = 8; 339 unsigned int iEnd = 8; 340 if (state.joyStick) 341 iEnd = nHalfAxes_s; 342 if (state.mouse) 343 iBegin = 0; 344 for (unsigned int i = iBegin; i < iEnd; i++) 345 { 346 if (halfAxes_[i].hasChanged_) 347 { 348 if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_) 349 { 350 halfAxes_[i].wasDown_ = true; 351 if (halfAxes_[i].nCommands_[KeybindMode::OnPress]) 352 halfAxes_[i].execute(KeybindMode::OnPress); 353 } 354 else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_) 355 { 356 halfAxes_[i].wasDown_ = false; 357 if (halfAxes_[i].nCommands_[KeybindMode::OnRelease]) 358 halfAxes_[i].execute(KeybindMode::OnRelease); 359 } 360 halfAxes_[i].hasChanged_ = false; 361 } 362 363 if (halfAxes_[i].wasDown_) 364 { 365 if (halfAxes_[i].nCommands_[KeybindMode::OnHold]) 366 halfAxes_[i].execute(KeybindMode::OnHold); 367 } 368 369 // these are the actually useful axis bindings for analog input AND output 370 if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) 371 { 372 //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl; 373 halfAxes_[i].execute(); 374 } 375 } 376 377 if (bDeriveMouseInput_ && state.mouse) 378 { 379 if (deriveTime_ > derivePeriod_) 380 { 381 //CCOUT(3) << "mouse abs: "; 382 for (int i = 0; i < 2; i++) 383 { 384 if (mouseRelative_[i] > 0) 385 { 386 halfAxes_[2*i + 0].absVal_ 387 = mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_; 388 halfAxes_[2*i + 1].absVal_ = 0.0f; 389 } 390 else if (mouseRelative_[i] < 0) 391 { 392 halfAxes_[2*i + 0].absVal_ = 0.0f; 393 halfAxes_[2*i + 1].absVal_ 394 = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_; 395 } 396 else 397 { 398 halfAxes_[2*i + 0].absVal_ = 0.0f; 399 halfAxes_[2*i + 1].absVal_ = 0.0f; 400 } 401 //COUT(3) << mouseRelative_[i] << " | "; 402 mouseRelative_[i] = 0; 403 halfAxes_[2*i + 0].hasChanged_ = true; 404 halfAxes_[2*i + 1].hasChanged_ = true; 405 } 406 deriveTime_ = 0.0f; 407 //COUT(3) << std::endl; 408 } 409 else 410 deriveTime_ += dt; 411 } 412 413 // execute all buffered bindings (addional parameter) 414 for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) 415 paramCommandBuffer_[i]->execute(); 416 417 // always reset the relative movement of the mouse 418 if (state.mouse) 419 for (unsigned int i = 0; i < 8; i++) 420 halfAxes_[i].relVal_ = 0.0f; 421 } 422 423 void KeyBinder::keyPressed (const KeyEvent& evt) 424 { keys_[evt.key].execute(KeybindMode::OnPress); } 425 426 void KeyBinder::keyReleased(const KeyEvent& evt) 427 { keys_[evt.key].execute(KeybindMode::OnRelease); } 428 429 void KeyBinder::keyHeld (const KeyEvent& evt) 430 { keys_[evt.key].execute(KeybindMode::OnHold); } 431 432 433 void KeyBinder::mouseButtonPressed (MouseButton::Enum id) 434 { mouseButtons_[id].execute(KeybindMode::OnPress); } 435 436 void KeyBinder::mouseButtonReleased(MouseButton::Enum id) 437 { mouseButtons_[id].execute(KeybindMode::OnRelease); } 438 439 void KeyBinder::mouseButtonHeld (MouseButton::Enum id) 440 { mouseButtons_[id].execute(KeybindMode::OnHold); } 441 442 443 void KeyBinder::joyStickButtonPressed (int joyStickID, int button) 444 { joyStickButtons_[button].execute(KeybindMode::OnPress); } 445 446 void KeyBinder::joyStickButtonReleased(int joyStickID, int button) 447 { joyStickButtons_[button].execute(KeybindMode::OnRelease); } 448 449 void KeyBinder::joyStickButtonHeld (int joyStickID, int button) 450 { joyStickButtons_[button].execute(KeybindMode::OnHold); } 451 452 /** 453 @brief 454 Event handler for the mouseMoved Event. 455 @param e 456 Mouse state information 457 */ 458 void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize) 459 { 460 // y axis of mouse input is inverted 461 int rel[] = { rel_.x, -rel_.y }; 462 463 if (!bDeriveMouseInput_) 464 { 465 for (int i = 0; i < 2; i++) 466 { 467 if (rel[i]) 468 { 469 // absolute 470 halfAxes_[2*i + 0].hasChanged_ = true; 471 halfAxes_[2*i + 1].hasChanged_ = true; 472 mousePosition_[i] += rel[i]; 473 474 if (bClipMouse_) 475 { 476 if (mousePosition_[i] > 1024) 477 mousePosition_[i] = 1024; 478 if (mousePosition_[i] < -1024) 479 mousePosition_[i] = -1024; 480 } 481 482 if (mousePosition_[i] >= 0) 483 { 484 halfAxes_[2*i + 0].absVal_ = mousePosition_[i]/1024.0f * mouseSensitivity_; 485 halfAxes_[2*i + 1].absVal_ = 0.0f; 486 } 487 else 488 { 489 halfAxes_[2*i + 0].absVal_ = 0.0f; 490 halfAxes_[2*i + 1].absVal_ = -mousePosition_[i]/1024.0f * mouseSensitivity_; 491 } 492 } 493 } 494 } 495 else 496 { 497 mouseRelative_[0] += rel[0]; 498 mouseRelative_[1] += rel[1]; 499 } 500 501 // relative 365 502 for (int i = 0; i < 2; i++) 366 503 { 367 if (mouseRelative_[i] > 0) 368 { 369 halfAxes_[2*i + 0].absVal_ = mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_; 370 halfAxes_[2*i + 1].absVal_ = 0.0f; 371 } 372 else if (mouseRelative_[i] < 0) 373 { 374 halfAxes_[2*i + 0].absVal_ = 0.0f; 375 halfAxes_[2*i + 1].absVal_ = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_; 376 } 377 else 378 { 379 halfAxes_[2*i + 0].absVal_ = 0.0f; 380 halfAxes_[2*i + 1].absVal_ = 0.0f; 381 } 382 //COUT(3) << mouseRelative_[i] << " | "; 383 mouseRelative_[i] = 0; 384 halfAxes_[2*i + 0].hasChanged_ = true; 385 halfAxes_[2*i + 1].hasChanged_ = true; 386 } 387 deriveTime_ = 0.0f; 388 //COUT(3) << std::endl; 389 } 390 else 391 deriveTime_ += dt; 392 } 393 394 // execute all buffered bindings (addional parameter) 395 for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) 396 paramCommandBuffer_[i]->execute(); 397 398 // always reset the relative movement of the mouse 399 if (state.mouse) 400 for (unsigned int i = 0; i < 8; i++) 401 halfAxes_[i].relVal_ = 0.0f; 402 } 403 404 void KeyBinder::keyPressed (const KeyEvent& evt) 405 { keys_[evt.key].execute(KeybindMode::OnPress); } 406 407 void KeyBinder::keyReleased(const KeyEvent& evt) 408 { keys_[evt.key].execute(KeybindMode::OnRelease); } 409 410 void KeyBinder::keyHeld (const KeyEvent& evt) 411 { keys_[evt.key].execute(KeybindMode::OnHold); } 412 413 414 void KeyBinder::mouseButtonPressed (MouseButton::Enum id) 415 { mouseButtons_[id].execute(KeybindMode::OnPress); } 416 417 void KeyBinder::mouseButtonReleased(MouseButton::Enum id) 418 { mouseButtons_[id].execute(KeybindMode::OnRelease); } 419 420 void KeyBinder::mouseButtonHeld (MouseButton::Enum id) 421 { mouseButtons_[id].execute(KeybindMode::OnHold); } 422 423 424 void KeyBinder::joyStickButtonPressed (int joyStickID, int button) 425 { joyStickButtons_[button].execute(KeybindMode::OnPress); } 426 427 void KeyBinder::joyStickButtonReleased(int joyStickID, int button) 428 { joyStickButtons_[button].execute(KeybindMode::OnRelease); } 429 430 void KeyBinder::joyStickButtonHeld (int joyStickID, int button) 431 { joyStickButtons_[button].execute(KeybindMode::OnHold); } 432 433 /** 434 @brief Event handler for the mouseMoved Event. 435 @param e Mouse state information 436 */ 437 void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize) 438 { 439 // y axis of mouse input is inverted 440 int rel[] = { rel_.x, -rel_.y }; 441 442 if (!bDeriveMouseInput_) 443 { 444 for (int i = 0; i < 2; i++) 445 { 446 if (rel[i]) 447 { 448 // absolute 449 halfAxes_[2*i + 0].hasChanged_ = true; 450 halfAxes_[2*i + 1].hasChanged_ = true; 451 mousePosition_[i] += rel[i]; 452 453 if (bClipMouse_) 454 { 455 if (mousePosition_[i] > 1024) 456 mousePosition_[i] = 1024; 457 if (mousePosition_[i] < -1024) 458 mousePosition_[i] = -1024; 459 } 460 461 if (mousePosition_[i] >= 0) 462 { 463 halfAxes_[2*i + 0].absVal_ = mousePosition_[i]/1024.0f * mouseSensitivity_; 464 halfAxes_[2*i + 1].absVal_ = 0.0f; 465 } 466 else 467 { 468 halfAxes_[2*i + 0].absVal_ = 0.0f; 469 halfAxes_[2*i + 1].absVal_ = -mousePosition_[i]/1024.0f * mouseSensitivity_; 470 } 471 } 472 } 473 } 474 else 475 { 476 mouseRelative_[0] += rel[0]; 477 mouseRelative_[1] += rel[1]; 478 } 479 480 // relative 481 for (int i = 0; i < 2; i++) 482 { 483 if (rel[i] > 0) 484 halfAxes_[0 + 2*i].relVal_ = ((float)rel[i])/1024 * mouseSensitivity_; 485 else 486 halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_; 487 } 488 } 489 490 /** 504 if (rel[i] > 0) 505 halfAxes_[0 + 2*i].relVal_ = ((float)rel[i])/1024 * mouseSensitivity_; 506 else 507 halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_; 508 } 509 } 510 511 /** 491 512 @brief Event handler for the mouseScrolled Event. 492 513 @param e Mouse state information 493 */494 void KeyBinder::mouseScrolled(int abs, int rel)495 {496 //COUT(3) << mouseButtons_[8].name_ << " " << abs << " | " << rel << std::endl;497 498 if (rel > 0)499 for (int i = 0; i < rel/120; i++)500 mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);501 else502 for (int i = 0; i < -rel/120; i++)503 mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);504 }505 506 void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, float value)507 {508 // TODO: Use proper calibration values instead of generally 16-bit integer509 int i = 8 + axis * 2;510 if (value >= 0)511 {512 //if (value > 10000)513 //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; }514 515 halfAxes_[i].absVal_ = value;516 halfAxes_[i].relVal_ = value;517 halfAxes_[i].hasChanged_ = true;518 if (halfAxes_[i + 1].absVal_ > 0.0f)519 {520 halfAxes_[i + 1].absVal_ = -0.0f;521 halfAxes_[i + 1].relVal_ = -0.0f;522 halfAxes_[i + 1].hasChanged_ = true;523 }524 }525 else526 {527 //if (value < -10000)528 //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; }529 530 halfAxes_[i + 1].absVal_ = -value;531 halfAxes_[i + 1].relVal_ = -value;532 halfAxes_[i + 1].hasChanged_ = true;533 if (halfAxes_[i].absVal_ > 0.0f)534 {535 halfAxes_[i].absVal_ = -0.0f;536 halfAxes_[i].relVal_ = -0.0f;537 halfAxes_[i].hasChanged_ = true;538 }539 }540 }514 */ 515 void KeyBinder::mouseScrolled(int abs, int rel) 516 { 517 //COUT(3) << mouseButtons_[8].name_ << " " << abs << " | " << rel << std::endl; 518 519 if (rel > 0) 520 for (int i = 0; i < rel/120; i++) 521 mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f); 522 else 523 for (int i = 0; i < -rel/120; i++) 524 mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f); 525 } 526 527 void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, float value) 528 { 529 // TODO: Use proper calibration values instead of generally 16-bit integer 530 int i = 8 + axis * 2; 531 if (value >= 0) 532 { 533 //if (value > 10000) 534 //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; } 535 536 halfAxes_[i].absVal_ = value; 537 halfAxes_[i].relVal_ = value; 538 halfAxes_[i].hasChanged_ = true; 539 if (halfAxes_[i + 1].absVal_ > 0.0f) 540 { 541 halfAxes_[i + 1].absVal_ = -0.0f; 542 halfAxes_[i + 1].relVal_ = -0.0f; 543 halfAxes_[i + 1].hasChanged_ = true; 544 } 545 } 546 else 547 { 548 //if (value < -10000) 549 //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; } 550 551 halfAxes_[i + 1].absVal_ = -value; 552 halfAxes_[i + 1].relVal_ = -value; 553 halfAxes_[i + 1].hasChanged_ = true; 554 if (halfAxes_[i].absVal_ > 0.0f) 555 { 556 halfAxes_[i].absVal_ = -0.0f; 557 halfAxes_[i].relVal_ = -0.0f; 558 halfAxes_[i].hasChanged_ = true; 559 } 560 } 561 } 541 562 } -
code/branches/input/src/core/input/KeyBinder.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _KeyBinder_H__ … … 46 47 namespace orxonox 47 48 { 48 /** 49 @brief Handles mouse, keyboard and joy stick input while in the actual game mode. 50 Manages the key bindings. 51 */ 52 class _CoreExport KeyBinder : public KeyHandler, public MouseHandler, public JoyStickHandler, public OrxonoxClass 53 { 54 public: 55 KeyBinder (); 56 virtual ~KeyBinder(); 49 /** 50 @brief 51 Handles mouse, keyboard and joy stick input while in the actual game mode. 52 Manages the key bindings. 53 */ 54 class _CoreExport KeyBinder : public KeyHandler, public MouseHandler, public JoyStickHandler, public OrxonoxClass 55 { 56 public: 57 KeyBinder (); 58 virtual ~KeyBinder(); 57 59 58 void loadBindings();59 void clearBindings();60 void setConfigValues();61 void resetJoyStickAxes();60 void loadBindings(); 61 void clearBindings(); 62 void setConfigValues(); 63 void resetJoyStickAxes(); 62 64 63 protected: // functions64 void tickInput(float dt, const HandlerState& state);65 protected: // functions 66 void tickInput(float dt, const HandlerState& state); 65 67 66 virtual void readTrigger(Button& button);68 virtual void readTrigger(Button& button); 67 69 68 void keyPressed (const KeyEvent& evt);69 void keyReleased(const KeyEvent& evt);70 void keyHeld (const KeyEvent& evt);70 void keyPressed (const KeyEvent& evt); 71 void keyReleased(const KeyEvent& evt); 72 void keyHeld (const KeyEvent& evt); 71 73 72 void mouseButtonPressed (MouseButton::Enum id);73 void mouseButtonReleased(MouseButton::Enum id);74 void mouseButtonHeld (MouseButton::Enum id);75 void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);76 void mouseScrolled (int abs, int rel);74 void mouseButtonPressed (MouseButton::Enum id); 75 void mouseButtonReleased(MouseButton::Enum id); 76 void mouseButtonHeld (MouseButton::Enum id); 77 void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); 78 void mouseScrolled (int abs, int rel); 77 79 78 void joyStickButtonPressed (int joyStickID, int button);79 void joyStickButtonReleased(int joyStickID, int button);80 void joyStickButtonHeld (int joyStickID, int button);81 void joyStickAxisMoved (int joyStickID, int axis, float value);80 void joyStickButtonPressed (int joyStickID, int button); 81 void joyStickButtonReleased(int joyStickID, int button); 82 void joyStickButtonHeld (int joyStickID, int button); 83 void joyStickAxisMoved (int joyStickID, int axis, float value); 82 84 83 protected: // variables84 //! denotes the number of different keys there are in OIS.85 static const unsigned int nKeys_s = 0xEE;86 //! Actual key bindings as bundle for Press, Hold and Release87 Button keys_ [nKeys_s];85 protected: // variables 86 //! denotes the number of different keys there are in OIS. 87 static const unsigned int nKeys_s = 0xEE; 88 //! Actual key bindings as bundle for Press, Hold and Release 89 Button keys_ [nKeys_s]; 88 90 89 //! denotes the number of different mouse buttons there are in OIS.90 static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels91 //! Actual key bindings as bundle for Press, Hold and Release92 Button mouseButtons_ [nMouseButtons_s];91 //! denotes the number of different mouse buttons there are in OIS. 92 static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels 93 //! Actual key bindings as bundle for Press, Hold and Release 94 Button mouseButtons_ [nMouseButtons_s]; 93 95 94 //! denotes the number of different joy stick buttons there are in OIS.95 static const unsigned int nJoyStickButtons_s = 32 + 4 * 4; // 32 buttons and 4 POVs with 4 buttons96 //! Actual key bindings as bundle for Press, Hold and Release97 Button joyStickButtons_ [nJoyStickButtons_s];96 //! denotes the number of different joy stick buttons there are in OIS. 97 static const unsigned int nJoyStickButtons_s = 32 + 4 * 4; // 32 buttons and 4 POVs with 4 buttons 98 //! Actual key bindings as bundle for Press, Hold and Release 99 Button joyStickButtons_ [nJoyStickButtons_s]; 98 100 99 //! denotes the number of half axes (every axis twice) there can be.100 static const unsigned int nHalfAxes_s = 56;101 /**102 * Array with all the half axes for mouse and joy sticks.103 * Keep in mind that the positions are fixed and that the first entry is the104 * positive one and the second is negative.105 * Sequence is as follows:106 * 0 - 3: Mouse x and y107 * 4 - 7: empty108 * 8 - 23: joy stick slider axes 1 to 8109 * 24 - 55: joy stick axes 1 - 16110 */111 HalfAxis halfAxes_[nHalfAxes_s];101 //! denotes the number of half axes (every axis twice) there can be. 102 static const unsigned int nHalfAxes_s = 56; 103 /** 104 * Array with all the half axes for mouse and joy sticks. 105 * Keep in mind that the positions are fixed and that the first entry is the 106 * positive one and the second is negative. 107 * Sequence is as follows: 108 * 0 - 3: Mouse x and y 109 * 4 - 7: empty 110 * 8 - 23: joy stick slider axes 1 to 8 111 * 24 - 55: joy stick axes 1 - 16 112 */ 113 HalfAxis halfAxes_[nHalfAxes_s]; 112 114 113 /** 114 * Commands that have additional parameters (axes) are executed at the end of 115 * the tick() so that all values can be buffered for single execution. 116 */ 117 std::vector<BufferedParamCommand*> paramCommandBuffer_; 115 /** 116 @brief 117 Commands that have additional parameters (axes) are executed at the end of 118 the tick() so that all values can be buffered for single execution. 119 */ 120 std::vector<BufferedParamCommand*> paramCommandBuffer_; 118 121 119 //! Keeps track of the absolute mouse value (incl. scroll wheel)120 int mousePosition_[2];121 //! Used to derive mouse input if requested122 int mouseRelative_[2];123 float deriveTime_;122 //! Keeps track of the absolute mouse value (incl. scroll wheel) 123 int mousePosition_[2]; 124 //! Used to derive mouse input if requested 125 int mouseRelative_[2]; 126 float deriveTime_; 124 127 125 //##### ConfigValues ##### 126 //! Threshold for analog triggers until which the state is 0. 127 float analogThreshold_; 128 //! Threshold for analog triggers until which the button is not pressed. 129 float buttonThreshold_; 130 //! Derive mouse input for absolute values? 131 bool bDeriveMouseInput_; 132 //! Accuracy of the mouse input deriver. The higher the more precise, but laggier. 133 float derivePeriod_; 134 //! mouse sensitivity 135 float mouseSensitivity_; 136 //! mouse sensitivity if mouse input is derived 137 float mouseSensitivityDerived_; 138 //! Whether or not to clip abslute mouse values to 1024 139 bool bClipMouse_; 140 }; 128 129 //##### ConfigValues ##### 130 131 //! Threshold for analog triggers until which the state is 0. 132 float analogThreshold_; 133 //! Threshold for analog triggers until which the button is not pressed. 134 float buttonThreshold_; 135 //! Derive mouse input for absolute values? 136 bool bDeriveMouseInput_; 137 //! Accuracy of the mouse input deriver. The higher the more precise, but laggier. 138 float derivePeriod_; 139 //! mouse sensitivity 140 float mouseSensitivity_; 141 //! mouse sensitivity if mouse input is derived 142 float mouseSensitivityDerived_; 143 //! Whether or not to clip abslute mouse values to 1024 144 bool bClipMouse_; 145 }; 141 146 } 142 147 -
code/branches/input/src/core/input/KeyDetector.cc
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Implementation of the different input handlers. 32 */ 30 @file 31 @brief 32 Implementation of the different input handlers. 33 */ 33 34 34 35 #include "KeyDetector.h" … … 42 43 namespace orxonox 43 44 { 44 /** 45 @brief Constructor 46 */ 47 KeyDetector::KeyDetector() 48 { 49 RegisterObject(KeyDetector); 50 } 45 /** 46 @brief 47 Constructor 48 */ 49 KeyDetector::KeyDetector() 50 { 51 RegisterObject(KeyDetector); 52 } 51 53 52 /** 53 @brief Destructor 54 */ 55 KeyDetector::~KeyDetector() 56 { 57 } 54 /** 55 @brief 56 Destructor 57 */ 58 KeyDetector::~KeyDetector() 59 { 60 } 58 61 59 /** 60 @brief Loads the key and button bindings. 61 @return True if loading succeeded. 62 */ 63 void KeyDetector::loadBindings() 64 { 65 clearBindings(); 66 setConfigValues(); 67 } 62 /** 63 @brief 64 Loads the key and button bindings. 65 @return 66 True if loading succeeded. 67 */ 68 void KeyDetector::loadBindings() 69 { 70 clearBindings(); 71 setConfigValues(); 72 } 68 73 69 void KeyDetector::readTrigger(Button& button)70 {71 SimpleCommand* cmd = new SimpleCommand();72 cmd->evaluation_ = CommandExecutor::evaluate("storeKeyStroke " + button.name_);73 button.commands_[KeybindMode::OnPress] = new BaseCommand*[1];74 button.commands_[KeybindMode::OnPress][0] = cmd;75 button.nCommands_[KeybindMode::OnPress] = 1;76 }74 void KeyDetector::readTrigger(Button& button) 75 { 76 SimpleCommand* cmd = new SimpleCommand(); 77 cmd->evaluation_ = CommandExecutor::evaluate("storeKeyStroke " + button.name_); 78 button.commands_[KeybindMode::OnPress] = new BaseCommand*[1]; 79 button.commands_[KeybindMode::OnPress][0] = cmd; 80 button.nCommands_[KeybindMode::OnPress] = 1; 81 } 77 82 } -
code/branches/input/src/core/input/KeyDetector.h
r1535 r1630 28 28 29 29 /** 30 @file 31 @brief Different definitions of input processing. 32 */ 30 @file 31 @brief 32 Different definitions of input processing. 33 */ 33 34 34 35 #ifndef _KeyDetector_H__ … … 41 42 namespace orxonox 42 43 { 43 class _CoreExport KeyDetector : public KeyBinder44 {45 public:46 KeyDetector();47 ~KeyDetector();48 void loadBindings();44 class _CoreExport KeyDetector : public KeyBinder 45 { 46 public: 47 KeyDetector(); 48 ~KeyDetector(); 49 void loadBindings(); 49 50 50 protected:51 void readTrigger(Button& button);52 };51 protected: 52 void readTrigger(Button& button); 53 }; 53 54 } 54 55 -
code/branches/input/src/orxonox/OrxonoxStableHeaders.h
r1629 r1630 37 37 #include "util/OrxonoxPlatform.h" 38 38 39 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC && 039 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC 40 40 41 41 // including std headers here is useless since they're already precompiled
Note: See TracChangeset
for help on using the changeset viewer.