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