]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGEventInput.hxx
Add aubmodels to AI objects
[flightgear.git] / src / Input / FGEventInput.hxx
1 // FGEventInput.hxx -- handle event driven input devices
2 //
3 // Written by Torsten Dreyer, started July 2009
4 //
5 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifndef __FGEVENTINPUT_HXX
24 #define __FGEVENTINPUT_HXX
25
26 #include "FGCommonInput.hxx"
27 #include "FGButton.hxx"
28 #include "FGDeviceConfigurationMap.hxx"
29 #include <simgear/structure/subsystem_mgr.hxx>
30
31 /*
32  * A base structure for event data. 
33  * To be extended for O/S specific implementation data
34  */
35 struct FGEventData {
36   FGEventData( double aValue, double aDt, int aModifiers ) : modifiers(aModifiers), value(aValue), dt(aDt) {}
37   int modifiers;
38   double value;
39   double dt;
40 };
41
42 class FGEventSetting : public SGReferenced {
43 public:
44   FGEventSetting( SGPropertyNode_ptr base );
45
46   bool Test();
47
48   /* 
49    * access for the value property
50    */
51   double GetValue();
52
53 protected:
54   double value;
55   SGPropertyNode_ptr valueNode;
56   SGSharedPtr<const SGCondition> condition;
57 };
58
59 typedef SGSharedPtr<FGEventSetting> FGEventSetting_ptr;
60 typedef vector<FGEventSetting_ptr> setting_list_t;
61
62 /*
63  * A wrapper class for a configured event. 
64  * 
65  * <event>
66  *   <desc>Change the view pitch</desc>
67  *   <name>rel-x-rotate</name>
68  *   <binding>
69  *     <command>property-adjust</command>
70  *     <property>sim/current-view/pitch-offset-deg</property>
71  *     <factor type="double">0.01</factor>
72  *     <min type="double">-90.0</min>
73  *     <max type="double">90.0</max>
74  *     <wrap type="bool">false</wrap>
75  *   </binding>
76  *   <mod-xyz>
77  *    <binding>
78  *      ...
79  *    </binding>
80  *   </mod-xyz>
81  * </event>
82  */
83 class FGInputDevice;
84 class FGInputEvent : public SGReferenced,FGCommonInput {
85 public:
86
87   /*
88    * Constructor for the class. The arg node shall point
89    * to the property corresponding to the <event>  node
90    */
91   FGInputEvent( FGInputDevice * device, SGPropertyNode_ptr node );
92   virtual ~FGInputEvent();
93
94   /*
95    * dispatch the event value through all bindings 
96    */
97   virtual void fire( FGEventData & eventData );
98
99   /*
100    * access for the name property
101    */
102   string GetName() const { return name; }
103
104   /*
105    * access for the description property
106    */
107   string GetDescription() const { return desc; }
108
109   virtual void update( double dt );
110
111   static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node );
112
113 protected:
114   virtual void fire( SGBinding * binding, FGEventData & eventData );
115   /* A more or less meaningfull description of the event */
116   string desc;
117
118   /* One of the predefined names of the event */
119   string name;
120
121   /* A list of SGBinding objects */
122   binding_list_t bindings[KEYMOD_MAX];
123
124   /* A list of FGEventSetting objects */
125   setting_list_t settings;
126
127   /* A pointer to the associated device */
128   FGInputDevice * device;
129
130   double lastDt;
131   double intervalSec;
132   double lastSettingValue;
133 };
134
135 class FGButtonEvent : public FGInputEvent {
136 public:
137   FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node );
138   virtual void fire( FGEventData & eventData );
139
140 protected:
141   bool repeatable;
142   bool lastState;
143 };
144
145 class FGAxisEvent : public FGInputEvent {
146 public:
147   FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
148   void SetMaxRange( double value ) { maxRange = value; }
149   void SetMinRange( double value ) { minRange = value; }
150   void SetRange( double min, double max ) { minRange = min; maxRange = max; }
151 protected:
152   virtual void fire( FGEventData & eventData );
153   double tolerance;
154   double minRange;
155   double maxRange;
156   double center;
157   double deadband;
158   double lowThreshold;
159   double highThreshold;
160   double lastValue;
161 };
162
163 class FGRelAxisEvent : public FGAxisEvent {
164 public:
165   FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
166 protected:
167   virtual void fire( SGBinding * binding, FGEventData & eventData );
168 };
169
170 class FGAbsAxisEvent : public FGAxisEvent {
171 public:
172   FGAbsAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) : FGAxisEvent( device, node ) {}
173 protected:
174   virtual void fire( SGBinding * binding, FGEventData & eventData );
175 };
176
177 typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr;
178
179 /*
180  * A abstract class implementing basic functionality of input devices for
181  * all operating systems. This is the base class for the O/S-specific
182  * implementation of input device handlers
183  */
184 class FGInputDevice : public SGReferenced {
185 public:
186   FGInputDevice() : debugEvents(false), grab(false) {}
187   FGInputDevice( string aName ) : name(aName) {}
188     
189   virtual ~FGInputDevice();
190
191   virtual void Open() = 0;
192   virtual void Close() = 0;
193
194   virtual void Send( const char * eventName, double value ) = 0;
195
196   inline void Send( const string & eventName, double value ) {
197     Send( eventName.c_str(), value );
198   }
199
200   virtual const char * TranslateEventName( FGEventData & eventData ) = 0;
201
202
203   void SetName( string name );
204   string & GetName() { return name; }
205
206   void HandleEvent( FGEventData & eventData );
207
208   void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
209     if( handledEvents.count( handledEvent->GetName() ) == 0 )
210       handledEvents[handledEvent->GetName()] = handledEvent;
211   }
212
213   virtual void Configure( SGPropertyNode_ptr deviceNode );
214
215   virtual void update( double dt );
216
217   bool GetDebugEvents () const { return debugEvents; }
218
219   bool GetGrab() const { return grab; }
220
221   const string & GetNasalModule() const { return nasalModule; }
222
223 private:
224   // A map of events, this device handles
225   map<string,FGInputEvent_ptr> handledEvents;
226
227   // the device has a name to be recognized
228   string name;
229
230   // print out events comming in from the device
231   // if true
232   bool   debugEvents;
233
234   // grab the device exclusively, if O/S supports this
235   // so events are not sent to other applications
236   bool   grab;
237
238   SGPropertyNode_ptr deviceNode;
239   string nasalModule;
240 };
241
242 typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr;
243
244
245 /*
246  * The Subsystem for the event input device 
247  */
248 class FGEventInput : public SGSubsystem,FGCommonInput {
249 public:
250   FGEventInput();
251   virtual ~FGEventInput();
252   virtual void init();
253   virtual void postinit();
254   virtual void update( double dt );
255
256   const static unsigned MAX_DEVICES = 1000;
257   const static unsigned INVALID_DEVICE_INDEX = MAX_DEVICES + 1;
258 protected:
259   static const char * PROPERTY_ROOT;
260
261   unsigned AddDevice( FGInputDevice * inputDevice );
262   void RemoveDevice( unsigned index );
263
264   map<int,FGInputDevice*> input_devices;
265   FGDeviceConfigurationMap configMap;
266
267   SGPropertyNode_ptr nasalClose;
268 };
269
270 #endif