]> git.mxchange.org Git - flightgear.git/blob - src/Controls/controls.hxx
Patches from Dave Luffto pass the magneto and starter control movements
[flightgear.git] / src / Controls / controls.hxx
1 // controls.hxx -- defines a standard interface to all flight sim controls
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifndef _CONTROLS_HXX
25 #define _CONTROLS_HXX
26
27 #include <simgear/misc/props.hxx>
28
29 #include <Sound/soundmgr.hxx>
30 #include <Main/fgfs.hxx>
31 #include <Main/globals.hxx>
32
33 #ifndef __cplusplus                                                          
34 # error This library requires C++
35 #endif                                   
36
37
38 // Define a structure containing the control parameters
39
40 class FGControls : public FGSubsystem
41 {
42
43 public:
44
45     enum
46     {
47         ALL_ENGINES = -1,
48         MAX_ENGINES = 10
49     };
50
51     enum
52     {
53         ALL_WHEELS = -1,
54         MAX_WHEELS = 3
55     };
56
57 private:
58
59     double aileron;
60     double elevator;
61     double elevator_trim;
62     double rudder;
63     double flaps;
64     double throttle[MAX_ENGINES];
65     double mixture[MAX_ENGINES];
66     double prop_advance[MAX_ENGINES];
67     double brake[MAX_WHEELS];
68     int magnetos[MAX_ENGINES];
69     bool throttle_idle;
70     bool starter[MAX_ENGINES];
71     bool gear_down;
72
73     SGPropertyNode * auto_coordination;
74
75     inline void CLAMP(double *x, double min, double max ) {
76         if ( *x < min ) { *x = min; }
77         if ( *x > max ) { *x = max; }
78     }
79
80     inline void CLAMP(int *i, int min, int max ) {
81         if ( *i < min ) { *i = min; }
82         if ( *i > max ) { *i = max; }
83     }
84     
85 public:
86
87     FGControls();
88     ~FGControls();
89
90     // Implementation of FGSubsystem.
91     void init ();
92     void bind ();
93     void unbind ();
94     void update ();
95
96     // Reset function
97     void reset_all(void);
98         
99     // Query functions
100     inline double get_aileron() const { return aileron; }
101     inline double get_elevator() const { return elevator; }
102     inline double get_elevator_trim() const { return elevator_trim; }
103     inline double get_rudder() const { return rudder; }
104     inline double get_flaps() const { return flaps; }
105     inline double get_throttle(int engine) const { return throttle[engine]; }
106     inline double get_mixture(int engine) const { return mixture[engine]; }
107     inline double get_prop_advance(int engine) const {
108         return prop_advance[engine];
109     }
110     inline double get_brake(int wheel) const { return brake[wheel]; }
111     inline int get_magnetos(int engine) const { return magnetos[engine]; }
112     inline bool get_starter(int engine) const { return starter[engine]; }
113     inline bool get_gear_down() const { return gear_down; }
114
115     // Update functions
116     inline void set_aileron( double pos ) {
117         aileron = pos;
118         CLAMP( &aileron, -1.0, 1.0 );
119                         
120         // check for autocoordination
121         if ( auto_coordination->getBoolValue() ) 
122         {
123             set_rudder( aileron / 2.0 );
124         }
125     }
126     inline void move_aileron( double amt ) {
127         aileron += amt;
128         CLAMP( &aileron, -1.0, 1.0 );
129                         
130         // check for autocoordination
131         if ( auto_coordination->getBoolValue() ) 
132         {
133             set_rudder( aileron / 2.0 );
134         }
135     }
136     inline void set_elevator( double pos ) {
137         elevator = pos;
138         CLAMP( &elevator, -1.0, 1.0 );
139     }
140     inline void move_elevator( double amt ) {
141         elevator += amt;
142         CLAMP( &elevator, -1.0, 1.0 );
143     }
144     inline void set_elevator_trim( double pos ) {
145         elevator_trim = pos;
146         CLAMP( &elevator_trim, -1.0, 1.0 );
147     }
148     inline void move_elevator_trim( double amt ) {
149         elevator_trim += amt;
150         CLAMP( &elevator_trim, -1.0, 1.0 );
151     }
152     inline void set_rudder( double pos ) {
153         rudder = pos;
154         CLAMP( &rudder, -1.0, 1.0 );
155     }
156     inline void move_rudder( double amt ) {
157         rudder += amt;
158         CLAMP( &rudder, -1.0, 1.0 );
159     }
160     inline void set_flaps( double pos ) {
161         if ( flaps != pos ) {
162             globals->get_soundmgr()->play_once( "flaps" );
163         }
164         flaps = pos;
165         CLAMP( &flaps, 0.0, 1.0 );
166     }
167     inline void move_flaps( double amt ) {
168         if ( fabs(amt) > 0.0 ) {
169             globals->get_soundmgr()->play_once( "flaps" );
170         }
171         flaps += amt;
172         CLAMP( &flaps, 0.0, 1.0 );
173     }
174     inline void set_throttle( int engine, double pos ) {
175         if ( engine == ALL_ENGINES ) {
176             for ( int i = 0; i < MAX_ENGINES; i++ ) {
177                 throttle[i] = pos;
178                 CLAMP( &throttle[i], 0.0, 1.0 );
179             }
180         } else {
181             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
182                 throttle[engine] = pos;
183                 CLAMP( &throttle[engine], 0.0, 1.0 );
184             }
185         }
186     }
187     inline void move_throttle( int engine, double amt ) {
188         if ( engine == ALL_ENGINES ) {
189             for ( int i = 0; i < MAX_ENGINES; i++ ) {
190                 throttle[i] += amt;
191                 CLAMP( &throttle[i], 0.0, 1.0 );
192             }
193         } else {
194             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
195                 throttle[engine] += amt;
196                 CLAMP( &throttle[engine], 0.0, 1.0 );
197             }
198         }
199     }
200     inline void set_mixture( int engine, double pos ) {
201         if ( engine == ALL_ENGINES ) {
202             for ( int i = 0; i < MAX_ENGINES; i++ ) {
203                 mixture[i] = pos;
204                 CLAMP( &mixture[i], 0.0, 1.0 );
205             }
206         } else {
207             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
208                 mixture[engine] = pos;
209                 CLAMP( &mixture[engine], 0.0, 1.0 );
210             }
211         }
212     }
213     inline void move_mixture( int engine, double amt ) {
214         if ( engine == ALL_ENGINES ) {
215             for ( int i = 0; i < MAX_ENGINES; i++ ) {
216                 mixture[i] += amt;
217                 CLAMP( &mixture[i], 0.0, 1.0 );
218             }
219         } else {
220             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
221                 mixture[engine] += amt;
222                 CLAMP( &mixture[engine], 0.0, 1.0 );
223             }
224         }
225     }
226     inline void set_prop_advance( int engine, double pos ) {
227         if ( engine == ALL_ENGINES ) {
228             for ( int i = 0; i < MAX_ENGINES; i++ ) {
229                 prop_advance[i] = pos;
230                 CLAMP( &prop_advance[i], 0.0, 1.0 );
231             }
232         } else {
233             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
234                 prop_advance[engine] = pos;
235                 CLAMP( &prop_advance[engine], 0.0, 1.0 );
236             }
237         }
238     }
239     inline void move_prop_advance( int engine, double amt ) {
240         if ( engine == ALL_ENGINES ) {
241             for ( int i = 0; i < MAX_ENGINES; i++ ) {
242                 prop_advance[i] += amt;
243                 CLAMP( &prop_advance[i], 0.0, 1.0 );
244             }
245         } else {
246             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
247                 prop_advance[engine] += amt;
248                 CLAMP( &prop_advance[engine], 0.0, 1.0 );
249             }
250         }
251     }
252     inline void set_magnetos( int engine, int pos ) {
253         if ( engine == ALL_ENGINES ) {
254             for ( int i = 0; i < MAX_ENGINES; i++ ) {
255                 magnetos[i] = pos;
256                 CLAMP( &magnetos[i], 0, 3 );
257             }
258         } else {
259             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
260                 magnetos[engine] = pos;
261                 CLAMP( &magnetos[engine], 0, 3 );
262             }
263         }
264     }
265     inline void move_magnetos( int engine, int amt ) {
266         if ( engine == ALL_ENGINES ) {
267             for ( int i = 0; i < MAX_ENGINES; i++ ) {
268                 magnetos[i] += amt;
269                 CLAMP( &magnetos[i], 0, 3 );
270             }
271         } else {
272             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
273                 magnetos[engine] += amt;
274                 CLAMP( &magnetos[engine], 0, 3 );
275             }
276         }
277     }
278     inline void set_starter( int engine, bool flag ) {
279         if ( engine == ALL_ENGINES ) {
280             for ( int i = 0; i < MAX_ENGINES; i++ ) {
281                 starter[i] = flag;
282             }
283         } else {
284             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
285                 starter[engine] = flag;
286             }
287         }
288     }
289     inline void set_brake( int wheel, double pos ) {
290         if ( wheel == ALL_WHEELS ) {
291             for ( int i = 0; i < MAX_WHEELS; i++ ) {
292                 brake[i] = pos;
293                 CLAMP( &brake[i], 0.0, 1.0 );
294             }
295         } else {
296             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
297                 brake[wheel] = pos;
298                 CLAMP( &brake[wheel], 0.0, 1.0 );
299             }
300         }
301     }
302     inline void move_brake( int wheel, double amt ) {
303         if ( wheel == ALL_WHEELS ) {
304             for ( int i = 0; i < MAX_WHEELS; i++ ) {
305                 brake[i] += amt;
306                 CLAMP( &brake[i], 0.0, 1.0 );
307             }
308         } else {
309             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
310                 brake[wheel] += amt;
311                 CLAMP( &brake[wheel], 0.0, 1.0 );
312             }
313         }
314     }
315     inline void set_gear_down( bool gear ) { gear_down = gear; }
316 };
317
318
319 #endif // _CONTROLS_HXX
320
321