]> git.mxchange.org Git - flightgear.git/blob - src/Controls/controls.hxx
Updates from the Jon and Tony show.
[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/options.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 trimmed_throttle[MAX_ENGINES];
61     double brake[MAX_WHEELS];
62
63     inline void CLAMP(double *x, double min, double max ) {
64         if ( *x < min ) { *x = min; }
65         if ( *x > max ) { *x = max; }
66     }
67                 
68 public:
69
70     FGControls();
71     ~FGControls();
72
73     // Reset function
74     void reset_all(void);
75         
76     // Query functions
77     inline double get_aileron() const { return aileron; }
78     inline double get_elevator() const { return elevator; }
79     inline double get_elevator_trim() const { return elevator_trim; }
80     inline double get_rudder() const { return rudder; }
81     inline double get_flaps() const { return flaps; }
82     inline double get_throttle(int engine) const { return throttle[engine]; }
83     inline double get_brake(int wheel) const { return brake[wheel]; }
84     inline double get_trimmed_throttle(int engine) const {
85         return trimmed_throttle[engine];
86     }
87
88     // Update functions
89     inline void set_aileron( double pos ) {
90         aileron = pos;
91         CLAMP( &aileron, -1.0, 1.0 );
92                         
93         // check for autocoordination
94         if ( current_options.get_auto_coordination() == 
95              fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
96         {
97             set_rudder( aileron / 2.0 );
98         }
99     }
100     inline void move_aileron( double amt ) {
101         aileron += amt;
102         CLAMP( &aileron, -1.0, 1.0 );
103                         
104         // check for autocoordination
105         if ( current_options.get_auto_coordination() == 
106              fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
107         {
108             set_rudder( aileron / 2.0 );
109         }
110     }
111     inline void set_elevator( double pos ) {
112         elevator = pos;
113         CLAMP( &elevator, -1.0, 1.0 );
114     }
115     inline void move_elevator( double amt ) {
116         elevator += amt;
117         CLAMP( &elevator, -1.0, 1.0 );
118     }
119     inline void set_elevator_trim( double pos ) {
120         elevator_trim = pos;
121         CLAMP( &elevator_trim, -1.0, 1.0 );
122     }
123     inline void move_elevator_trim( double amt ) {
124         elevator_trim += amt;
125         CLAMP( &elevator_trim, -1.0, 1.0 );
126     }
127     inline void set_rudder( double pos ) {
128         rudder = pos;
129         CLAMP( &rudder, -1.0, 1.0 );
130     }
131     inline void move_rudder( double amt ) {
132         rudder += amt;
133         CLAMP( &rudder, -1.0, 1.0 );
134     }
135     inline void set_flaps( double pos ) {
136         flaps = pos;
137         CLAMP( &flaps, 0.0, 1.0 );
138     }
139     inline void move_flaps( double amt ) {
140         flaps += amt;
141         CLAMP( &flaps, 0.0, 1.0 );
142     }
143     inline void set_throttle( int engine, double pos ) {
144         if ( engine == ALL_ENGINES ) {
145             for ( int i = 0; i < MAX_ENGINES; i++ ) {
146                 throttle[i] = pos;
147                 CLAMP( &throttle[i], 0.0, 1.0 );
148             }
149         } else {
150             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
151                 throttle[engine] = pos;
152                 CLAMP( &throttle[engine], 0.0, 1.0 );
153             }
154         }
155     }
156     inline void set_trimmed_throttle( int engine, double pos ) {
157         if ( engine == ALL_ENGINES ) {
158             for ( int i = 0; i < MAX_ENGINES; i++ ) {
159                 trimmed_throttle[i] = pos;
160                 CLAMP( &trimmed_throttle[i], 0.0, 1.0 );
161             }
162         } else {
163             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
164                 trimmed_throttle[engine] = pos;
165                 CLAMP( &trimmed_throttle[engine], 0.0, 1.0 );
166             }
167         }
168     }
169     inline void move_throttle( int engine, double amt ) {
170         if ( engine == ALL_ENGINES ) {
171             for ( int i = 0; i < MAX_ENGINES; i++ ) {
172                 throttle[i] += amt;
173                 CLAMP( &throttle[i], 0.0, 1.0 );
174             }
175         } else {
176             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
177                 throttle[engine] += amt;
178                 CLAMP( &throttle[engine], 0.0, 1.0 );
179             }
180         }
181     }
182     inline void set_brake( int wheel, double pos ) {
183         if ( wheel == ALL_WHEELS ) {
184             for ( int i = 0; i < MAX_WHEELS; i++ ) {
185                 brake[i] = pos;
186                 CLAMP( &brake[i], 0.0, 1.0 );
187             }
188         } else {
189             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
190                 brake[wheel] = pos;
191                 CLAMP( &brake[wheel], 0.0, 1.0 );
192             }
193         }
194     }
195     inline void move_brake( int wheel, double amt ) {
196         if ( wheel == ALL_WHEELS ) {
197             for ( int i = 0; i < MAX_WHEELS; i++ ) {
198                 brake[i] += amt;
199                 CLAMP( &brake[i], 0.0, 1.0 );
200             }
201         } else {
202             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
203                 brake[wheel] += amt;
204                 CLAMP( &brake[wheel], 0.0, 1.0 );
205             }
206         }
207     }
208 };
209
210
211 extern FGControls controls;
212
213
214 #endif // _CONTROLS_HXX
215
216