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