MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
metacode.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _inputmetacode_h
42 #define _inputmetacode_h
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 // This is a container for user the smallest possible unit of userinput
46 ///////////////////////////////////////
47 
48 //From SDL
49 /*
50  SDL - Simple DirectMedia Layer
51  Copyright (C) 1997-2011 Sam Lantinga
52 
53  This library is free software; you can redistribute it and/or
54  modify it under the terms of the GNU Lesser General Public
55  License as published by the Free Software Foundation; either
56  version 2.1 of the License, or (at your option) any later version.
57 
58  This library is distributed in the hope that it will be useful,
59  but WITHOUT ANY WARRANTY; without even the implied warranty of
60  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
61  Lesser General Public License for more details.
62 
63  You should have received a copy of the GNU Lesser General Public
64  License along with this library; if not, write to the Free Software
65  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
66 
67  Sam Lantinga
68  slouken@libsdl.org
69 */
70 
71 #include "datatypes.h"
72 #include "Input/inputenumerations.h"
73 #ifndef SWIG
74  #include "XML/xml.h"
75 #endif
76 
77 namespace Mezzanine
78 {
79  namespace Input
80  {
81  ///////////////////////////////////////////////////////////////////////////////
82  /// @class MetaCode
83  /// @headerfile metacode.h
84  /// @brief This Determines the kind of user input.
85  /// @details A Metacode contains the data that is passed around with an input
86  /// event. It stores one type of button press or analog representation (Mouse
87  /// move, joystick tilt, wheel spin, etc...). If it is an analog representation
88  /// it will also store how far or how it is pushed, pressed, rotated, or
89  /// whatever. Several of these can be used in combination to represent button
90  /// combinations, or complex input combinations (like portions of fighter game
91  /// moves).
92  ///////////////////////////////////////
94  {
95  protected:
96  //The values that makes a MetaCode useful.
97  Int32 MetaValue;
98  //Optional value for handling input devices such as controllers.
99  UInt16 DeviceIndex;
100  Input::InputCode Code;
101 
102  //function that the constructors can call aid initial setup
103  void Construct(const RawEvent& RawEvent_);
104  void Construct(const Int32& Value, const Input::InputCode& NewCode);
105 
106  //This assigns a value to Inputcode by doing an efficient low level copy of the correct portion of an SDL_event
107  //If mishandled this function can corrupt the MetaCode, and it can throw any error memcpy could throw.
108  static Input::InputCode GetInputCodeFromSDL_KEY(const RawEvent &RawEvent_);
109  static Input::InputCode GetInputCodeFromSDL_MOUSE(const RawEvent &RawEvent_);
110  static Input::InputCode GetInputCodeFromSDL_JOYSTICK(const RawEvent &RawEvent_);
111 
112  public:
113  /// @brief Default constructor.
114  /// @details This sets nothing on the MetaCode and leaves it completely unassigned. Accessing a data member could cause problems.
115  MetaCode();
116  /// @brief Copy constructor.
117  /// @details This sets all values in the MetaCode, leaving it in completely ready state.
118  /// @param Other The other MetaCode to copy from.
119  MetaCode(const MetaCode& Other);
120  /// @brief Descriptive Constructor.
121  /// @details This sets all values in the MetaCode, leaving it in completely ready state. This is the ideal constructor for simulating user input.
122  /// @param Value How much is something moving, tilting, rotating or whatever. For buttons a positive value is pushed, and a negative value is becoming unpressed, and 0 is unpressed.
123  /// @param NewCode Which key or which type of input was pressed. Sqeaky, thinks this has partial unicode support.
124  MetaCode(const Int32& Value, const Input::InputCode& NewCode);
125  /// @brief Descriptive Constructor.
126  /// @details This sets all values in the MetaCode, leaving it in completely ready state. This is the ideal constructor for simulating user input.
127  /// @param Value How much is something moving, tilting, rotating or whatever. For buttons a positive value is pushed, and a negative value is becoming unpressed, and 0 is unpressed.
128  /// @param NewCode Which key or which type of input was pressed. Sqeaky, thinks this has partial unicode support.
129  /// @param Index The index of the device this metacode is describing.
130  MetaCode(const Int32& Value, const Input::InputCode& NewCode, const UInt16& Index);
131  /// @brief The Heavy Lifting Constructor.
132  /// @details This contructor accepts a RawEvent from the input event subsystem internal to the engine. This converts all the required information
133  /// from the lower level format and store what is needed in the event that is created. This is used heavily by engine internals. \n
134  /// This constructor expects to receive a type of RawEvent that can be converted into exactly one kind of Metacode. Depending on the
135  /// User input subsystem, this could be all RawEvents, or even just some RawEvents.
136  /// @exception "RawEvent which creates Multiple Metacodes inserted into Metacode" - Thrown when passed a certain (system dependant) incorrect type of RawEvent.
137  /// @exception "Unknown User Input Inserted into Metacode" - Thrown when receiving either a corrupt, improperly handle, or unsupported RawEvent.
138  /// @warning We recomend against using this Constructor, because the binary format of RawEvent could change if the input event SubSystem Changes. In
139  /// that event you would have to recompile your application to get it working with a new version of Mezzanine. Using this function in Game code removes any gaurantees of Game Code
140  /// Portability.
141  MetaCode(const RawEvent& RawEvent_);
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // Gets and Sets
145 
146  /// @brief This Sets The InputCode.
147  /// @details See @ref GetCode to see exactly what the Code is. This will Set the code stored in this MetaCode. This value can be retrieved with @ref GetCode .
148  /// @param NewCode The value you want the stored code to become.
149  void SetCode(const Input::InputCode& NewCode);
150  /// @brief This Sets The InputCode using an Int32.
151  /// @warning This will cast an Int32 into an InputCode. Be careful, it is possible to put impossible or ridiculous values, in with
152  /// this. For example Accidentally stuffing in the result of MOUSEBUTTON + 22 looks like it would give you MOUSEBUTTON_22. But that
153  /// Doesn't exist, at the time of this writing you would get MOUSEABSOLUTEVERTICAL. Be careful, or skip this alltogether and use one of
154  /// the provided functions that do the math for you like.
155  /// @param NewCode The value you want the stored code to become.
156  void SetCode(const Int32& NewCode);
157  /// @brief This Returns the Inputcode.
158  /// @details This Value can be use to determine what keyboard button has been pressed, or what specific kind of Joystick or mouse event has occurred.
159  /// This value can be set with @ref SetCode .
160  /// @return This returns the input code for this MetaCode.
161  Input::InputCode GetCode() const;
162  /// @brief This Sets The MetaValue.
163  /// @details See @ref GetMetaValue to see exactly what the MetaValue is. This will set the MetaValue stored in this MetaCode. This value can be retrieved with @ref GetMetaValue .
164  /// @param Value The value you want the stored MetaValue to become. No bounds checking will be done. You can supply a completely invalid value if you choose to.
165  void SetMetaValue(const Int32& Value);
166  /// @brief This Returns the MetaValue.
167  /// @details The MetaValue can be use to determine how far something is tilted, pushed, rotated, or other analog value.
168  /// This value can be set with @ref SetMetaValue .
169  /// @return This returns the input code for this MetaCode. This could return any number inside a range (depending on hardware and configuration)
170  /// to represent how tilted a joystick or how much a mouse moved.
171  Int32 GetMetaValue() const;
172  /// @brief Sets the device index if applicable.
173  /// @param Index The index of the device this metacode applies to.
174  void SetDeviceIndex(const UInt16& Index);
175  /// @brief Gets the currently set device index.
176  /// @remarks If no device is set or applicable, this will return the max value for a UInt16 (-1).
177  /// @return Returns a UInt16 that is the for the device this metacode applies to.
178  UInt16 GetDeviceIndex() const;
179  /// @brief Sets all the values of this MetaCode to Null values.
180  void SetNullValues();
181 
182  ///////////////////////////////////////////////////////////////////////////////
183  // Conversion and Casting Functions
184 
185  /// @brief Get the MetaValue as a Input::ButtonState.
186  /// @return This returns the appropriate button state or throws an Mezzanine::Exception if an invalid button state is stored in the MetaValue.
187  /// @throw This throws a Mezzanine::Exception if the MetaValue is less than BUTTON_LIFTING or greater than BUTTON_DOWN.
188  Input::ButtonState GetMetaValueAsButtonState() const;
189  /// @brief Get the MetaValue as a Input::DirectionalMotionState.
190  /// @return This returns the appropriate MouseWheel state or throws an Mezzanine::Exception if an invalid state is stored in the MetaValue
191  /// @throw This throws a Mezzanine::Exception if the MetaValue is less than MOUSEWHEEL_DOWN or greater than MOUSEWHEEL_UP.
192  Input::DirectionalMotionState GetMetaValueAsDirectionalMotionState() const;
193 
194  /// @brief Accepts a int and returns the InputCode for the Corresponding Mouse button.
195  /// @param ButtonerNumber The number of the button you want the code for.
196  /// @return When passed 0 this returns Input::MOUSEBUTTON, otherwise this returns Input::MOUSEBUTTON_X where X is the number that was passed in.
197  static Input::InputCode GetMouseButtonCode(const UInt16 ButtonNumber);
198  /// @brief Accepts a int and returns the InputCode for the Corresponding Controller button.
199  /// @param ButtonerNumber The number of the button you want the code for.
200  /// @return When passed 0 this returns Input::JOYSTICKBUTTON, otherwise this returns Input::CONTROLLERBUTTON_X where X is the number that was passed in.
201  static Input::InputCode GetControllerButtonCode(const UInt16 ButtonNumber);
202  /// @brief Accepts a int and returns the InputCode for the Corresponding Controller Axis.
203  /// @param AxisNumber The number of the button you want the code for.
204  /// @return When passed 0 this returns Input::JOYSTICKAXIS, otherwise this returns Input::CONTROLLERAXIS_X where X is the number that was passed in.
205  static Input::InputCode GetControllerAxisCode(const UInt16 AxisNumber);
206 
207  ///////////////////////////////////////////////////////////////////////////////
208  // Utility Checks
209 
210  /// @brief Does this MetaCode Represent a state of a keyboard key.
211  /// @return This returns a bool which will be true if this is a keyboard event.
212  bool IsKeyboardButton() const;
213  /// @brief Does this MetaCode Represent a state of a Mouse button.
214  /// @return This returns a bool which will be true if this is a MOUSEBUTTON_X event.
215  bool IsMouseButton() const;
216  /// @brief Does this MetaCode Represent a state of a Controller button.
217  /// @return This returns a bool which will be true if this is a CONTROLLERBUTTON_X event.
218  bool IsControllerButton() const;
219  /// @brief Does this MetaCode Represent a state of any button on an input device.
220  /// @return This returns a bool which will be true if this is event pertains to any button on any input device.
221  bool IsDeviceButton() const;
222 
223  /// @brief Does this MetaCode Represent a state of a keyboard Event.
224  /// @return This returns a bool which will be true if this is between KEY_FIRST and KEY_LAST.
225  bool IsKeyboardEvent() const;
226  /// @brief Does this MetaCode Represent a state of a mouse Event.
227  /// @return This returns a bool which will be true if this is between MOUSE_FIRST and MOUSE_LAST.
228  bool IsMouseEvent() const;
229  /// @brief Does this MetaCode Represent movement of the mouse or mouse wheel.
230  /// @return This returns a bool which will be true if this is between MOUSEMOTION_FIRST and MOUSEMOTION_LAST.
231  bool IsMouseMotionEvent() const;
232  /// @brief Does this MetaCode Represent multiple clicks of a mouse button.
233  /// @return This returns a bool which will be true if this is between the first mouse multiclick input code and the last mouse multiclick input code.
234  bool IsMouseMultiClickEvent() const;
235  /// @brief Does this MetaCode Represent a state of a multitouch device.
236  /// @return This returns a bool which will be true if this is between the MULTITOUCH_FIRST and MULTITOUCH_LAST.
237  bool IsMultitouchEvent() const;
238  /// @brief Does this MetaCode Represent a state of a Controller Event
239  /// @return This returns a bool which will be true if this is between CONTROLLER_FIRST and CONTROLLER_LAST.
240  bool IsControllerEvent() const;
241  /// @brief Does this MetaCode Represent an axis position on a controller.
242  /// @return This returns a bool which will be true if this is between CONTROLLERAXIS_FIRST and CONTROLLERAXIS_LAST.
243  bool IsControllerAxisEvent() const;
244  /// @brief Does this MetaCode Represent a hat position on a controller.
245  /// @return This returns a bool which will be true if this is between CONTROLLERHAT_FIRST and CONTROLLERHAT_LAST.
246  bool IsControllerHatEvent() const;
247  /// @brief Does this MetaCode Represent some other (non-keyboard and non-mouse button).
248  /// @return This returns a bool which will be true if this is between INPUTEVENT_FIRST and INPUTEVENT_LAST.
249  bool IsInputEvent() const;
250 
251  /// @brief Is this a left or right Alt key.
252  /// @return This returns a bool which will be true if this is a left or right Alt key.
253  bool IsAltKey() const;
254  /// @brief Is this a left or right Ctrl key.
255  /// @return This returns a bool which will be true if this is a left or right Ctrl key.
256  bool IsCtrlKey() const;
257  /// @brief Is this a left or right Shift key.
258  /// @return This returns a bool which will be true if this is a left or right Shift key.
259  bool IsShiftKey() const;
260  /// @brief Is this a left or right Super key (Windows logo key, Apple logo key, etc...).
261  /// @return This returns a bool which will be true if this is a left or right Super key.
262  bool IsSuperKey() const;
263 
264  /// @brief Is this metacode a pollable event.
265  /// @return if this metacode stores a device button then this is a pollable event.
266  bool IsPollable() const;
267 
268  /// @brief Gets the device-type this MetaCode is representing.
269  /// @return Returns an Input::InputDevice value for the device this MetaCode is representing.
270  Input::InputDevice GetDeviceType() const;
271 
272  ///////////////////////////////////////////////////////////////////////////////
273  // Operators
274 
275  /// @brief Assignment operator.
276  /// @param Other The other MetaCode to assign to this.
277  /// @return Returns a reference to this.
278  MetaCode& operator=(const MetaCode& Other);
279  /// @brief Compares two MetaCode's for equality.
280  /// @param Other The other MetaCode to compare with.
281  /// @return Returns true if the two MetaCode's are equal, false otherwise.
282  bool operator==(const MetaCode& Other) const;
283  /// @brief Compares two MetaCode's for inequality.
284  /// @param Other The other MetaCode to compare with.
285  /// @return Returns true if the two MetaCode's are not equal, false otherwise.
286  bool operator!=(const MetaCode& Other) const;
287  /// @brief Compares two MetaCode's to see if this is less.
288  /// @note This only compares the InputCode contained in both MetaCode's.
289  /// @param Other The other MetaCode to compare with.
290  /// @return Returns true if this MetaCode is less then the MetaCode being compared, false otherwise.
291  bool operator<(const MetaCode& Other) const;
292  /// @brief Compares two MetaCode's to see if this is greater.
293  /// @note This only compares the InputCode contained in both MetaCode's.
294  /// @param Other The other MetaCode to compare with.
295  /// @return Returns true if this MetaCode is greater then the MetaCode being compared, false otherwise.
296  bool operator>(const MetaCode& Other) const;
297 
298  ///////////////////////////////////////////////////////////////////////////////
299  // Serialization
300 
301  // Serializable
302  /// @brief Convert this class to an XML::Node ready for serialization.
303  /// @param CurrentRoot The point in the XML hierarchy that all this vector3 should be appended to.
304  void ProtoSerialize(XML::Node& CurrentRoot) const;
305  // DeSerializable
306  /// @brief Take the data stored in an XML and overwrite this instance of this object with it.
307  /// @param OneNode and XML::Node containing the data.
308  void ProtoDeSerialize(const XML::Node& OneNode);
309  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
310  /// @return A string containing "Vector3".
311  String SerializableName() const;
312  };//MetaCode
313  }//Input
314  // Commonly used data types
315  typedef std::vector< Input::MetaCode > MetaCodeContainer;
316  typedef MetaCodeContainer::iterator MetaCodeIterator;
317  typedef MetaCodeContainer::const_iterator ConstMetaCodeIterator;
318 }//Mezzanine
319 
320 /// @brief Allows for streaming of MetaCodes
321 /// @details If it can be streamed, then it can be logged Holds true for the MetaCode.
322 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Input::MetaCode& x);
323 
324 /// @brief Used to de-serialize an Mezzanine::MetaCode from a stream
325 /// @details This reads in the xml and sets the target MetaCode according to values from the stream.
326 /// @param x The Mezzanine::MetaCode that will accept the values from the xml
327 /// @param stream The place to get the characters from, that define the Mezzanine::MetaCode.
328 /// @return Get an std::ostream that was read from, this allow chaining of the >> operators.
329 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
330 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Input::MetaCode& x);
331 
332 /// @brief Converts an XML node into a Mezzanine::MetaCode
333 /// @details This will convert an XML::Node will a valid serialized Mezzanine::MetaCode into a Mezzanine::MetaCode
334 /// @param OneNode An XML Node containing the the text of a MetaCode
335 /// @param x the Mezzanine::MetaCode to store the deserialized MetaCode
336 /// @return This returns a reference to the XML::Node for operator chaining or whatever.
337 /// @throw Can throw any exception that any function in the Mezzanine::xml namespace could throw in addition to a Mezzanine::Exception if the serialization version doesn't match.
339 
340 #endif