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