]> git.mxchange.org Git - flightgear.git/blob - src/Controls/controls.hxx
c90bcb304a144c33f3ef38ea2779db29459f5290
[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     bool throttle_idle;
69     bool gear_down;
70
71     SGPropertyNode * auto_coordination;
72
73     inline void CLAMP(double *x, double min, double max ) {
74         if ( *x < min ) { *x = min; }
75         if ( *x > max ) { *x = max; }
76     }
77                 
78 public:
79
80     FGControls();
81     ~FGControls();
82
83     // Implementation of FGSubsystem.
84     void init ();
85     void bind ();
86     void unbind ();
87     void update ();
88
89     // Reset function
90     void reset_all(void);
91         
92     // Query functions
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];
102     }
103     inline double get_brake(int wheel) const { return brake[wheel]; }
104     inline bool get_gear_down() const { return gear_down; }
105
106     // Update functions
107     inline void set_aileron( double pos ) {
108         aileron = pos;
109         CLAMP( &aileron, -1.0, 1.0 );
110                         
111         // check for autocoordination
112         if ( auto_coordination->getBoolValue() ) 
113         {
114             set_rudder( aileron / 2.0 );
115         }
116     }
117     inline void move_aileron( double amt ) {
118         aileron += amt;
119         CLAMP( &aileron, -1.0, 1.0 );
120                         
121         // check for autocoordination
122         if ( auto_coordination->getBoolValue() ) 
123         {
124             set_rudder( aileron / 2.0 );
125         }
126     }
127     inline void set_elevator( double pos ) {
128         elevator = pos;
129         CLAMP( &elevator, -1.0, 1.0 );
130     }
131     inline void move_elevator( double amt ) {
132         elevator += amt;
133         CLAMP( &elevator, -1.0, 1.0 );
134     }
135     inline void set_elevator_trim( double pos ) {
136         elevator_trim = pos;
137         CLAMP( &elevator_trim, -1.0, 1.0 );
138     }
139     inline void move_elevator_trim( double amt ) {
140         elevator_trim += amt;
141         CLAMP( &elevator_trim, -1.0, 1.0 );
142     }
143     inline void set_rudder( double pos ) {
144         rudder = pos;
145         CLAMP( &rudder, -1.0, 1.0 );
146     }
147     inline void move_rudder( double amt ) {
148         rudder += amt;
149         CLAMP( &rudder, -1.0, 1.0 );
150     }
151     inline void set_flaps( double pos ) {
152         if ( flaps != pos ) {
153             globals->get_soundmgr()->play_once( "flaps" );
154         }
155         flaps = pos;
156         CLAMP( &flaps, 0.0, 1.0 );
157     }
158     inline void move_flaps( double amt ) {
159         if ( fabs(amt) > 0.0 ) {
160             globals->get_soundmgr()->play_once( "flaps" );
161         }
162         flaps += amt;
163         CLAMP( &flaps, 0.0, 1.0 );
164     }
165     inline void set_throttle( int engine, double pos ) {
166         if ( engine == ALL_ENGINES ) {
167             for ( int i = 0; i < MAX_ENGINES; i++ ) {
168                 throttle[i] = pos;
169                 CLAMP( &throttle[i], 0.0, 1.0 );
170             }
171         } else {
172             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
173                 throttle[engine] = pos;
174                 CLAMP( &throttle[engine], 0.0, 1.0 );
175             }
176         }
177     }
178     inline void move_throttle( int engine, double amt ) {
179         if ( engine == ALL_ENGINES ) {
180             for ( int i = 0; i < MAX_ENGINES; i++ ) {
181                 throttle[i] += amt;
182                 CLAMP( &throttle[i], 0.0, 1.0 );
183             }
184         } else {
185             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
186                 throttle[engine] += amt;
187                 CLAMP( &throttle[engine], 0.0, 1.0 );
188             }
189         }
190     }
191     inline void set_mixture( int engine, double pos ) {
192         if ( engine == ALL_ENGINES ) {
193             for ( int i = 0; i < MAX_ENGINES; i++ ) {
194                 mixture[i] = pos;
195                 CLAMP( &mixture[i], 0.0, 1.0 );
196             }
197         } else {
198             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
199                 mixture[engine] = pos;
200                 CLAMP( &mixture[engine], 0.0, 1.0 );
201             }
202         }
203     }
204     inline void move_mixture( int engine, double amt ) {
205         if ( engine == ALL_ENGINES ) {
206             for ( int i = 0; i < MAX_ENGINES; i++ ) {
207                 mixture[i] += amt;
208                 CLAMP( &mixture[i], 0.0, 1.0 );
209             }
210         } else {
211             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
212                 mixture[engine] += amt;
213                 CLAMP( &mixture[engine], 0.0, 1.0 );
214             }
215         }
216     }
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 );
222             }
223         } else {
224             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
225                 prop_advance[engine] = pos;
226                 CLAMP( &prop_advance[engine], 0.0, 1.0 );
227             }
228         }
229     }
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 );
235             }
236         } else {
237             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
238                 prop_advance[engine] += amt;
239                 CLAMP( &prop_advance[engine], 0.0, 1.0 );
240             }
241         }
242     }
243     inline void set_brake( int wheel, double pos ) {
244         if ( wheel == ALL_WHEELS ) {
245             for ( int i = 0; i < MAX_WHEELS; i++ ) {
246                 brake[i] = pos;
247                 CLAMP( &brake[i], 0.0, 1.0 );
248             }
249         } else {
250             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
251                 brake[wheel] = pos;
252                 CLAMP( &brake[wheel], 0.0, 1.0 );
253             }
254         }
255     }
256     inline void move_brake( int wheel, double amt ) {
257         if ( wheel == ALL_WHEELS ) {
258             for ( int i = 0; i < MAX_WHEELS; i++ ) {
259                 brake[i] += amt;
260                 CLAMP( &brake[i], 0.0, 1.0 );
261             }
262         } else {
263             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
264                 brake[wheel] += amt;
265                 CLAMP( &brake[wheel], 0.0, 1.0 );
266             }
267         }
268     }
269     inline void set_gear_down( bool gear ) { gear_down = gear; }
270 };
271
272
273 #endif // _CONTROLS_HXX
274
275