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 Implementation of the different input handlers. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "InputCommands.h" |
---|
35 | #include "core/CommandExecutor.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | // ############################### |
---|
40 | // ### BufferedParamCommand ### |
---|
41 | // ############################### |
---|
42 | |
---|
43 | /** |
---|
44 | * Executes a buffered command. This is used for commands with additional |
---|
45 | * parameters. |
---|
46 | * @return True if command execution was successful or value was zero. |
---|
47 | */ |
---|
48 | bool BufferedParamCommand::execute() |
---|
49 | { |
---|
50 | if (nValuesAdded_) |
---|
51 | { |
---|
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(); |
---|
58 | } |
---|
59 | else |
---|
60 | return true; |
---|
61 | } |
---|
62 | |
---|
63 | // ############################### |
---|
64 | // ##### SimpleCommand ##### |
---|
65 | // ############################### |
---|
66 | |
---|
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 | } |
---|
75 | |
---|
76 | // ############################### |
---|
77 | // ##### ParamCommand ##### |
---|
78 | // ############################### |
---|
79 | |
---|
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_) |
---|
90 | { |
---|
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 | } |
---|
97 | } |
---|
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 high |
---|
102 | // 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 then |
---|
104 | // be 0.05 for instance and the the key value 1. Without handling the problem, the final |
---|
105 | // 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 quadratics |
---|
109 | { |
---|
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 anything |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
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 and |
---|
121 | // store the result in a temporary place |
---|
122 | cmd.value_ = (cmd.value_ * cmd.nValuesAdded_ + paramModifier_ * abs) / ++cmd.nValuesAdded_; |
---|
123 | } |
---|
124 | } |
---|
125 | return true; |
---|
126 | } |
---|
127 | } |
---|