]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.hxx
15757faeeeb357c214c0dd3c5d0a06abe74d5a46
[simgear.git] / simgear / structure / SGBinding.hxx
1 /**
2  * \file commands.hxx
3  * Interface definition for encapsulated commands.
4  * Started Spring 2001 by David Megginson, david@megginson.com
5  * This code is released into the Public Domain.
6  *
7  * $Id$
8  */
9
10 #ifndef __SGBINDING_HXX
11 #define __SGBINDING_HXX
12
13
14 #include <simgear/compiler.h>
15
16 #include <string>
17 #include <map>
18 #include <vector>
19
20 #include <simgear/props/props.hxx>
21 #include <simgear/props/condition.hxx>
22
23 #include "commands.hxx"
24
25 /**
26  * An input binding of some sort.
27  *
28  * <p>This class represents a binding that can be assigned to a
29  * keyboard key, a joystick button or axis, or even a panel
30  * instrument.</p>
31  */
32 class SGBinding : public SGConditional
33 {
34 public:
35
36   /**
37    * Default constructor.
38    */
39   SGBinding ();
40
41     /**
42      * Convenience constructor.
43      *
44      * @param node The binding will be built from this node.
45      */
46     SGBinding(const std::string& commandName);
47
48   /**
49    * Convenience constructor.
50    *
51    * @param node The binding will be built from this node.
52    */
53   SGBinding (const SGPropertyNode * node, SGPropertyNode* root);
54
55
56   /**
57    * Destructor.
58    */
59   virtual ~SGBinding ();
60
61   
62   /**
63    * clear internal state of the binding back to empty. This is useful
64    * if you don't want the 'remove on delete' behaviour of the 
65    * destructor.
66    */
67   void clear();
68
69
70   /**
71    * Get the command name.
72    *
73    * @return The string name of the command for this binding.
74    */
75   const std::string &getCommandName () const { return _command_name; }
76
77
78   /**
79    * Get the command itself.
80    *
81    * @return The command associated with this binding, or 0 if none
82    * is present.
83    */
84   SGCommandMgr::Command* getCommand () const { return _command; }
85
86
87   /**
88    * Get the argument that will be passed to the command.
89    *
90    * @return A property node that will be passed to the command as its
91    * argument, or 0 if none was supplied.
92    */
93   const SGPropertyNode * getArg () { return _arg; }
94   
95
96   /**
97    * Read a binding from a property node.
98    *
99    * @param node The property node containing the binding.
100    */
101   void read (const SGPropertyNode * node, SGPropertyNode* root);
102
103
104   /**
105    * Fire a binding.
106    */
107   void fire () const;
108
109
110   /**
111    * Fire a binding with a scaled movement (rather than absolute position).
112    */
113   void fire (double offset, double max) const;
114
115
116   /**
117    * Fire a binding with a setting (i.e. joystick axis).
118    *
119    * A double 'setting' property will be added to the arguments.
120    *
121    * @param setting The input setting, usually between -1.0 and 1.0.
122    */
123   void fire (double setting) const;
124
125   /**
126    * Fire a binding with a number of additional parameters
127    * 
128    * The children of params will be merged with the fixed arguments.
129    */
130   void fire (SGPropertyNode* params) const;
131   
132 private:
133   void innerFire() const;
134                                 // just to be safe.
135   SGBinding (const SGBinding &binding);
136
137   std::string _command_name;
138   mutable SGCommandMgr::Command* _command;
139   mutable SGPropertyNode_ptr _arg;
140   mutable SGPropertyNode_ptr _setting;
141 };
142
143 typedef SGSharedPtr<SGBinding> SGBinding_ptr;
144
145 typedef std::vector<SGBinding_ptr > SGBindingList;
146 typedef std::map<unsigned,SGBindingList> SGBindingMap;
147
148 /**
149  * fire every binding in a list, in sequence
150  
151  */
152 void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params = NULL);
153
154 /**
155  * fire every binding in a list with a setting value
156  
157  */
158 void fireBindingListWithOffset(const SGBindingList& aBindings, double offset, double max);
159
160 /**
161  * read multiple bindings from property-list format
162  */
163 SGBindingList readBindingList(const simgear::PropertyList& aNodes, SGPropertyNode* aRoot);
164
165 /**
166  * call clear() on every binding in a list
167  */
168 void clearBindingList(const SGBindingList& aBindings);
169
170 #endif