| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file | 
|---|
| 31 | @brief | 
|---|
| 32 |     Implementation of the different input handlers. | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include "InputCommands.h" | 
|---|
| 36 | #include "core/CommandExecutor.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     // ############################### | 
|---|
| 41 |     // ###  BufferedParamCommand   ### | 
|---|
| 42 |     // ############################### | 
|---|
| 43 |  | 
|---|
| 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() | 
|---|
| 52 |     { | 
|---|
| 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; | 
|---|
| 64 |     } | 
|---|
| 65 |  | 
|---|
| 66 |     // ############################### | 
|---|
| 67 |     // #####    SimpleCommand    ##### | 
|---|
| 68 |     // ############################### | 
|---|
| 69 |  | 
|---|
| 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 |     } | 
|---|
| 80 |  | 
|---|
| 81 |     // ############################### | 
|---|
| 82 |     // #####    ParamCommand     ##### | 
|---|
| 83 |     // ############################### | 
|---|
| 84 |  | 
|---|
| 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) | 
|---|
| 93 |     { | 
|---|
| 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; | 
|---|
| 133 |     } | 
|---|
| 134 | } | 
|---|