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