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