]> git.mxchange.org Git - flightgear.git/blob - src/Controls/controls.hxx
54405b7619e26a415afd2b09b5d51beef2cd48d9
[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     bool throttle_idle;
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
85     // Update functions
86     inline void set_aileron( double pos ) {
87         aileron = pos;
88         CLAMP( &aileron, -1.0, 1.0 );
89                         
90         // check for autocoordination
91         if ( current_options.get_auto_coordination() == 
92              fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
93         {
94             set_rudder( aileron / 2.0 );
95         }
96     }
97     inline void move_aileron( double amt ) {
98         aileron += amt;
99         CLAMP( &aileron, -1.0, 1.0 );
100                         
101         // check for autocoordination
102         if ( current_options.get_auto_coordination() == 
103              fgOPTIONS::FG_AUTO_COORD_ENABLED ) 
104         {
105             set_rudder( aileron / 2.0 );
106         }
107     }
108     inline void set_elevator( double pos ) {
109         elevator = pos;
110         CLAMP( &elevator, -1.0, 1.0 );
111     }
112     inline void move_elevator( double amt ) {
113         elevator += amt;
114         CLAMP( &elevator, -1.0, 1.0 );
115     }
116     inline void set_elevator_trim( double pos ) {
117         elevator_trim = pos;
118         CLAMP( &elevator_trim, -1.0, 1.0 );
119     }
120     inline void move_elevator_trim( double amt ) {
121         elevator_trim += amt;
122         CLAMP( &elevator_trim, -1.0, 1.0 );
123     }
124     inline void set_rudder( double pos ) {
125         rudder = pos;
126         CLAMP( &rudder, -1.0, 1.0 );
127     }
128     inline void move_rudder( double amt ) {
129         rudder += amt;
130         CLAMP( &rudder, -1.0, 1.0 );
131     }
132     inline void set_flaps( double pos ) {
133         flaps = pos;
134         CLAMP( &flaps, 0.0, 1.0 );
135     }
136     inline void move_flaps( double amt ) {
137         flaps += amt;
138         CLAMP( &flaps, 0.0, 1.0 );
139     }
140     inline void set_throttle( int engine, double pos ) {
141         if ( engine == ALL_ENGINES ) {
142             for ( int i = 0; i < MAX_ENGINES; i++ ) {
143                 throttle[i] = pos;
144                 CLAMP( &throttle[i], 0.0, 1.0 );
145             }
146         } else {
147             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
148                 throttle[engine] = pos;
149                 CLAMP( &throttle[engine], 0.0, 1.0 );
150             }
151         }
152     }
153     inline void move_throttle( int engine, double amt ) {
154         if ( engine == ALL_ENGINES ) {
155             for ( int i = 0; i < MAX_ENGINES; i++ ) {
156                 throttle[i] += amt;
157                 CLAMP( &throttle[i], 0.0, 1.0 );
158             }
159         } else {
160             if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
161                 throttle[engine] += amt;
162                 CLAMP( &throttle[engine], 0.0, 1.0 );
163             }
164         }
165     }
166     inline void set_brake( int wheel, double pos ) {
167         if ( wheel == ALL_WHEELS ) {
168             for ( int i = 0; i < MAX_WHEELS; i++ ) {
169                 brake[i] = pos;
170                 CLAMP( &brake[i], 0.0, 1.0 );
171             }
172         } else {
173             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
174                 brake[wheel] = pos;
175                 CLAMP( &brake[wheel], 0.0, 1.0 );
176             }
177         }
178     }
179     inline void move_brake( int wheel, double amt ) {
180         if ( wheel == ALL_WHEELS ) {
181             for ( int i = 0; i < MAX_WHEELS; i++ ) {
182                 brake[i] += amt;
183                 CLAMP( &brake[i], 0.0, 1.0 );
184             }
185         } else {
186             if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
187                 brake[wheel] += amt;
188                 CLAMP( &brake[wheel], 0.0, 1.0 );
189             }
190         }
191     }
192 };
193
194
195 extern FGControls controls;
196
197
198 #endif // _CONTROLS_HXX
199
200