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