1 // controls.hxx -- defines a standard interface to all flight sim controls
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/misc/props.hxx>
29 #include <Sound/soundmgr.hxx>
30 #include <Main/fgfs.hxx>
31 #include <Main/globals.hxx>
34 # error This library requires C++
38 // Define a structure containing the control parameters
40 class FGControls : public FGSubsystem
64 double throttle[MAX_ENGINES];
65 double mixture[MAX_ENGINES];
66 double prop_advance[MAX_ENGINES];
67 double brake[MAX_WHEELS];
70 SGPropertyNode * auto_coordination;
72 inline void CLAMP(double *x, double min, double max ) {
73 if ( *x < min ) { *x = min; }
74 if ( *x > max ) { *x = max; }
82 // Implementation of FGSubsystem.
92 inline double get_aileron() const { return aileron; }
93 inline double get_elevator() const { return elevator; }
94 inline double get_elevator_trim() const { return elevator_trim; }
95 inline double get_rudder() const { return rudder; }
96 inline double get_flaps() const { return flaps; }
97 inline double get_throttle(int engine) const { return throttle[engine]; }
98 inline double get_mixture(int engine) const { return mixture[engine]; }
99 inline double get_prop_advance(int engine) const {
100 return prop_advance[engine];
102 inline double get_brake(int wheel) const { return brake[wheel]; }
105 inline void set_aileron( double pos ) {
107 CLAMP( &aileron, -1.0, 1.0 );
109 // check for autocoordination
110 if ( auto_coordination->getBoolValue() )
112 set_rudder( aileron / 2.0 );
115 inline void move_aileron( double amt ) {
117 CLAMP( &aileron, -1.0, 1.0 );
119 // check for autocoordination
120 if ( auto_coordination->getBoolValue() )
122 set_rudder( aileron / 2.0 );
125 inline void set_elevator( double pos ) {
127 CLAMP( &elevator, -1.0, 1.0 );
129 inline void move_elevator( double amt ) {
131 CLAMP( &elevator, -1.0, 1.0 );
133 inline void set_elevator_trim( double pos ) {
135 CLAMP( &elevator_trim, -1.0, 1.0 );
137 inline void move_elevator_trim( double amt ) {
138 elevator_trim += amt;
139 CLAMP( &elevator_trim, -1.0, 1.0 );
141 inline void set_rudder( double pos ) {
143 CLAMP( &rudder, -1.0, 1.0 );
145 inline void move_rudder( double amt ) {
147 CLAMP( &rudder, -1.0, 1.0 );
149 inline void set_flaps( double pos ) {
150 if ( flaps != pos ) {
151 globals->get_soundmgr()->play_once( "flaps" );
154 CLAMP( &flaps, 0.0, 1.0 );
156 inline void move_flaps( double amt ) {
157 if ( fabs(amt) > 0.0 ) {
158 globals->get_soundmgr()->play_once( "flaps" );
161 CLAMP( &flaps, 0.0, 1.0 );
163 inline void set_throttle( int engine, double pos ) {
164 if ( engine == ALL_ENGINES ) {
165 for ( int i = 0; i < MAX_ENGINES; i++ ) {
167 CLAMP( &throttle[i], 0.0, 1.0 );
170 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
171 throttle[engine] = pos;
172 CLAMP( &throttle[engine], 0.0, 1.0 );
176 inline void move_throttle( int engine, double amt ) {
177 if ( engine == ALL_ENGINES ) {
178 for ( int i = 0; i < MAX_ENGINES; i++ ) {
180 CLAMP( &throttle[i], 0.0, 1.0 );
183 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
184 throttle[engine] += amt;
185 CLAMP( &throttle[engine], 0.0, 1.0 );
189 inline void set_mixture( int engine, double pos ) {
190 if ( engine == ALL_ENGINES ) {
191 for ( int i = 0; i < MAX_ENGINES; i++ ) {
193 CLAMP( &mixture[i], 0.0, 1.0 );
196 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
197 mixture[engine] = pos;
198 CLAMP( &mixture[engine], 0.0, 1.0 );
202 inline void move_mixture( int engine, double amt ) {
203 if ( engine == ALL_ENGINES ) {
204 for ( int i = 0; i < MAX_ENGINES; i++ ) {
206 CLAMP( &mixture[i], 0.0, 1.0 );
209 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
210 mixture[engine] += amt;
211 CLAMP( &mixture[engine], 0.0, 1.0 );
215 inline void set_prop_advance( int engine, double pos ) {
216 if ( engine == ALL_ENGINES ) {
217 for ( int i = 0; i < MAX_ENGINES; i++ ) {
218 prop_advance[i] = pos;
219 CLAMP( &prop_advance[i], 0.0, 1.0 );
222 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
223 prop_advance[engine] = pos;
224 CLAMP( &prop_advance[engine], 0.0, 1.0 );
228 inline void move_prop_advance( int engine, double amt ) {
229 if ( engine == ALL_ENGINES ) {
230 for ( int i = 0; i < MAX_ENGINES; i++ ) {
231 prop_advance[i] += amt;
232 CLAMP( &prop_advance[i], 0.0, 1.0 );
235 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
236 prop_advance[engine] += amt;
237 CLAMP( &prop_advance[engine], 0.0, 1.0 );
241 inline void set_brake( int wheel, double pos ) {
242 if ( wheel == ALL_WHEELS ) {
243 for ( int i = 0; i < MAX_WHEELS; i++ ) {
245 CLAMP( &brake[i], 0.0, 1.0 );
248 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
250 CLAMP( &brake[wheel], 0.0, 1.0 );
254 inline void move_brake( int wheel, double amt ) {
255 if ( wheel == ALL_WHEELS ) {
256 for ( int i = 0; i < MAX_WHEELS; i++ ) {
258 CLAMP( &brake[i], 0.0, 1.0 );
261 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
263 CLAMP( &brake[wheel], 0.0, 1.0 );
270 #endif // _CONTROLS_HXX