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 | // ##### ParamCommand ##### |
---|
68 | // ############################### |
---|
69 | |
---|
70 | /** |
---|
71 | @brief |
---|
72 | Executes a parameter command. The commmand string is not directly executed, |
---|
73 | but instead stored in a buffer list so that values can be combined. |
---|
74 | @return |
---|
75 | Always true. |
---|
76 | */ |
---|
77 | bool ParamCommand::execute(float abs, float rel) |
---|
78 | { |
---|
79 | BufferedParamCommand& cmd = *paramCommand_; |
---|
80 | // command has an additional parameter |
---|
81 | if (bRelative_) |
---|
82 | { |
---|
83 | if (rel != 0.0f) |
---|
84 | { |
---|
85 | // we have to calculate a relative movement. |
---|
86 | // paramModifier_ says how much one keystroke is |
---|
87 | cmd.value_ += paramModifier_ * rel; |
---|
88 | } |
---|
89 | } |
---|
90 | else if (abs != 0.0f) |
---|
91 | { |
---|
92 | // Usually, joy sticks create 'noise' (they return values if they're in 0 position) |
---|
93 | // and normally this is caught in tickInput(), but that threshold cannot be to high |
---|
94 | // in order to preserve accuracy. Instead, we have to catch the problem here. An example: |
---|
95 | // Someone only uses buttons with an active joystick. The joy stick value could then |
---|
96 | // be 0.05 for instance and the the key value 1. Without handling the problem, the final |
---|
97 | // value would be computed to (1+0.05)/2=0.5025 which is not what the user expects. |
---|
98 | float absQ = abs * abs; |
---|
99 | float valueQ = cmd.value_ * cmd.value_; |
---|
100 | if (absQ > 50.0f * valueQ) // ease up comparison by using quadratics |
---|
101 | { |
---|
102 | cmd.value_ = abs * paramModifier_; |
---|
103 | cmd.nValuesAdded_ = 1; |
---|
104 | } |
---|
105 | else if (absQ * 50.0f < valueQ) |
---|
106 | { |
---|
107 | // abs is too small, we just don't do anything |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | // we have to calculate the absolute position of the axis. |
---|
112 | // Since there might be another axis that is affected, we have to wait and |
---|
113 | // store the result in a temporary place |
---|
114 | cmd.value_ = (cmd.value_ * cmd.nValuesAdded_ + paramModifier_ * abs) / ++cmd.nValuesAdded_; |
---|
115 | } |
---|
116 | } |
---|
117 | return true; |
---|
118 | } |
---|
119 | } |
---|