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