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];
71 SGPropertyNode * auto_coordination;
73 inline void CLAMP(double *x, double min, double max ) {
74 if ( *x < min ) { *x = min; }
75 if ( *x > max ) { *x = max; }
83 // Implementation of FGSubsystem.
93 inline double get_aileron() const { return aileron; }
94 inline double get_elevator() const { return elevator; }
95 inline double get_elevator_trim() const { return elevator_trim; }
96 inline double get_rudder() const { return rudder; }
97 inline double get_flaps() const { return flaps; }
98 inline double get_throttle(int engine) const { return throttle[engine]; }
99 inline double get_mixture(int engine) const { return mixture[engine]; }
100 inline double get_prop_advance(int engine) const {
101 return prop_advance[engine];
103 inline double get_brake(int wheel) const { return brake[wheel]; }
104 inline bool get_gear_down() const { return gear_down; }
107 inline void set_aileron( double pos ) {
109 CLAMP( &aileron, -1.0, 1.0 );
111 // check for autocoordination
112 if ( auto_coordination->getBoolValue() )
114 set_rudder( aileron / 2.0 );
117 inline void move_aileron( double amt ) {
119 CLAMP( &aileron, -1.0, 1.0 );
121 // check for autocoordination
122 if ( auto_coordination->getBoolValue() )
124 set_rudder( aileron / 2.0 );
127 inline void set_elevator( double pos ) {
129 CLAMP( &elevator, -1.0, 1.0 );
131 inline void move_elevator( double amt ) {
133 CLAMP( &elevator, -1.0, 1.0 );
135 inline void set_elevator_trim( double pos ) {
137 CLAMP( &elevator_trim, -1.0, 1.0 );
139 inline void move_elevator_trim( double amt ) {
140 elevator_trim += amt;
141 CLAMP( &elevator_trim, -1.0, 1.0 );
143 inline void set_rudder( double pos ) {
145 CLAMP( &rudder, -1.0, 1.0 );
147 inline void move_rudder( double amt ) {
149 CLAMP( &rudder, -1.0, 1.0 );
151 inline void set_flaps( double pos ) {
152 if ( flaps != pos ) {
153 globals->get_soundmgr()->play_once( "flaps" );
156 CLAMP( &flaps, 0.0, 1.0 );
158 inline void move_flaps( double amt ) {
159 if ( fabs(amt) > 0.0 ) {
160 globals->get_soundmgr()->play_once( "flaps" );
163 CLAMP( &flaps, 0.0, 1.0 );
165 inline void set_throttle( int engine, double pos ) {
166 if ( engine == ALL_ENGINES ) {
167 for ( int i = 0; i < MAX_ENGINES; i++ ) {
169 CLAMP( &throttle[i], 0.0, 1.0 );
172 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
173 throttle[engine] = pos;
174 CLAMP( &throttle[engine], 0.0, 1.0 );
178 inline void move_throttle( int engine, double amt ) {
179 if ( engine == ALL_ENGINES ) {
180 for ( int i = 0; i < MAX_ENGINES; i++ ) {
182 CLAMP( &throttle[i], 0.0, 1.0 );
185 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
186 throttle[engine] += amt;
187 CLAMP( &throttle[engine], 0.0, 1.0 );
191 inline void set_mixture( int engine, double pos ) {
192 if ( engine == ALL_ENGINES ) {
193 for ( int i = 0; i < MAX_ENGINES; i++ ) {
195 CLAMP( &mixture[i], 0.0, 1.0 );
198 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
199 mixture[engine] = pos;
200 CLAMP( &mixture[engine], 0.0, 1.0 );
204 inline void move_mixture( int engine, double amt ) {
205 if ( engine == ALL_ENGINES ) {
206 for ( int i = 0; i < MAX_ENGINES; i++ ) {
208 CLAMP( &mixture[i], 0.0, 1.0 );
211 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
212 mixture[engine] += amt;
213 CLAMP( &mixture[engine], 0.0, 1.0 );
217 inline void set_prop_advance( int engine, double pos ) {
218 if ( engine == ALL_ENGINES ) {
219 for ( int i = 0; i < MAX_ENGINES; i++ ) {
220 prop_advance[i] = pos;
221 CLAMP( &prop_advance[i], 0.0, 1.0 );
224 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
225 prop_advance[engine] = pos;
226 CLAMP( &prop_advance[engine], 0.0, 1.0 );
230 inline void move_prop_advance( int engine, double amt ) {
231 if ( engine == ALL_ENGINES ) {
232 for ( int i = 0; i < MAX_ENGINES; i++ ) {
233 prop_advance[i] += amt;
234 CLAMP( &prop_advance[i], 0.0, 1.0 );
237 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
238 prop_advance[engine] += amt;
239 CLAMP( &prop_advance[engine], 0.0, 1.0 );
243 inline void set_brake( int wheel, double pos ) {
244 if ( wheel == ALL_WHEELS ) {
245 for ( int i = 0; i < MAX_WHEELS; i++ ) {
247 CLAMP( &brake[i], 0.0, 1.0 );
250 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
252 CLAMP( &brake[wheel], 0.0, 1.0 );
256 inline void move_brake( int wheel, double amt ) {
257 if ( wheel == ALL_WHEELS ) {
258 for ( int i = 0; i < MAX_WHEELS; i++ ) {
260 CLAMP( &brake[i], 0.0, 1.0 );
263 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
265 CLAMP( &brake[wheel], 0.0, 1.0 );
269 inline void set_gear_down( bool gear ) { gear_down = gear; }
273 #endif // _CONTROLS_HXX