Spinning Topp Logo BlackTopp Studios
inc
metacode.h
1 // © Copyright 2010 - 2016 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  public:
96  /// @brief Convenience datatype for storage of MetaCodes.
97  typedef std::vector< Input::MetaCode > MetaCodeContainer;
98  /// @brief Iterator type for convenient MetaCode storage.
99  typedef MetaCodeContainer::iterator MetaCodeIterator;
100  /// @brief Const Iterator type for convenient MetaCode storage.
101  typedef MetaCodeContainer::const_iterator ConstMetaCodeIterator;
102  protected:
103  /// @internal
104  /// @brief The relevant value for the type of input this is, if applicable.
106  /// @internal
107  /// @brief The device index if this is input from a controller. For keyboards and mice this is the max value if a Int32.
109  /// @internal
110  /// @brief The code indicating what type of input this is.
112 
113  /// @internal
114  /// @brief Converts an internal keyboard input event into a Mezzanine keyboard InputCode.
115  /// @param Raw The internal event to be converted into a keyboard InputCode.
116  /// @return Returns the converted Mezzanine InputCode.
117  static Input::InputCode GetInputCodeFromSDL_KEY(const RawEvent& Raw);
118  /// @internal
119  /// @brief Converts an internal mouse input event into a Mezzanine mouse InputCode.
120  /// @param Raw The internal event to be converted into a mouse InputCode.
121  /// @return Returns the converted Mezzanine InputCode.
122  static Input::InputCode GetInputCodeFromSDL_MOUSE(const RawEvent& Raw);
123  /// @internal
124  /// @brief Converts an internal controller input event into a Mezzanine controller InputCode.
125  /// @param Raw The internal event to be converted into a controller InputCode.
126  /// @return Returns the converted Mezzanine InputCode.
127  static Input::InputCode GetInputCodeFromSDL_JOYSTICK(const RawEvent& Raw);
128  public:
129  ///////////////////////////////////////////////////////////////////////////////
130  // Construction and Deconstruction
131 
132  /// @brief Default constructor.
133  /// @remarks This sets nothing on the MetaCode and leaves it completely unassigned. Accessing a data member could cause problems.
134  MetaCode();
135  /// @brief Copy constructor.
136  /// @param Other The other MetaCode to copy from.
137  MetaCode(const MetaCode& Other);
138  /// @brief Descriptive Constructor.
139  /// @param Value How much is something moving, tilting, rotating or whatever.
140  /// @param NewCode Which key or which type of input was pressed.
141  MetaCode(const Int32 Value, const Input::InputCode NewCode);
142  /// @brief Descriptive Constructor.
143  /// @param Value How much is something moving, tilting, rotating or whatever.
144  /// @param NewCode Which key or which type of input was pressed.
145  /// @param Index The index of the device this metacode is describing.
146  MetaCode(const Int32 Value, const Input::InputCode NewCode, const Int32 Index);
147  /// @brief The Heavy Lifting Constructor.
148  /// @details This contructor accepts a RawEvent from the input event subsystem internal to the engine. This converts all the required information
149  /// from the lower level format and store what is needed in the event that is created. This is used heavily by engine internals. \n
150  /// This constructor expects to receive a type of RawEvent that can be converted into exactly one kind of Metacode. Depending on the
151  /// User input subsystem, this could be all RawEvents, or even just some RawEvents.
152  /// @exception "RawEvent which creates Multiple Metacodes inserted into Metacode" - Thrown when passed a certain (system dependant) incorrect type of RawEvent.
153  /// @exception "Unknown User Input Inserted into Metacode" - Thrown when receiving either a corrupt, improperly handle, or unsupported RawEvent.
154  /// @warning We recomend against using this Constructor, because the binary format of RawEvent could change if the input event SubSystem Changes. In
155  /// 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
156  /// Portability.
157  MetaCode(const RawEvent& Raw);
158  /// @brief Class destructor.
159  ~MetaCode();
160 
161  /// @internal
162  /// @brief Internal creation method.
163  /// @remarks This method accepts a RawEvent from the input event subsystem internal to the engine. This converts all the required information
164  /// from the lower level format and store what is needed in the event that is created. This is used heavily by engine internals. @n
165  /// This constructor expects to receive a type of RawEvent that can be converted into one or more kinds of Metacode.
166  /// @warning We recomend against using this Constructor, because the binary format of RawEvent could change if the input event SubSystem Changes. In
167  /// 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
168  /// Portability.
169  /// @exception "RawEvent which creates Multiple Metacodes inserted into Metacode" - Thrown when passed a certain (system dependant) incorrect type of RawEvent.
170  /// @exception "Unknown User Input Inserted into Metacode" - Thrown when receiving either a corrupt, improperly handle, or unsupported RawEvent.
171  /// @param Raw The internal event to be converted into one or more MetaCodes.
172  /// @return Returns a container of the created MetaCodes.
173  static MetaCodeContainer CreateMetaCodes(const RawEvent& Raw);
174 
175  ///////////////////////////////////////////////////////////////////////////////
176  // Gets and Sets
177 
178  /// @brief This Sets The InputCode.
179  /// @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 .
180  /// @param NewCode The value you want the stored code to become.
181  void SetCode(const Input::InputCode NewCode);
182  /// @brief This Sets The InputCode using an Int32.
183  /// @warning This will cast an Int32 into an InputCode. Be careful, it is possible to put impossible or ridiculous values, in with
184  /// this. For example Accidentally stuffing in the result of MOUSEBUTTON + 22 looks like it would give you MOUSEBUTTON_22. But that
185  /// Doesn't exist, at the time of this writing you would get MOUSEABSOLUTEVERTICAL. Be careful, or skip this alltogether and use one of
186  /// the provided functions that do the math for you like.
187  /// @param NewCode The value you want the stored code to become.
188  void SetCode(const Int32 NewCode);
189  /// @brief This Returns the Inputcode.
190  /// @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.
191  /// This value can be set with @ref SetCode .
192  /// @return This returns the input code for this MetaCode.
193  Input::InputCode GetCode() const;
194  /// @brief This Sets The MetaValue.
195  /// @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 .
196  /// @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.
197  void SetMetaValue(const Int32 Value);
198  /// @brief This Returns the MetaValue.
199  /// @details The MetaValue can be use to determine how far something is tilted, pushed, rotated, or other analog value.
200  /// This value can be set with @ref SetMetaValue .
201  /// @return This returns the input code for this MetaCode. This could return any number inside a range (depending on hardware and configuration)
202  /// to represent how tilted a joystick or how much a mouse moved.
203  Int32 GetMetaValue() const;
204  /// @brief Sets the device index if applicable.
205  /// @param Index The index of the device this metacode applies to.
206  void SetDeviceIndex(const Int32 Index);
207  /// @brief Gets the currently set device index.
208  /// @remarks If no device is set or applicable, this will return the max value for a Int32 (-1).
209  /// @return Returns a Int32 that is the for the device this metacode applies to.
210  Int32 GetDeviceIndex() const;
211  /// @brief Sets all the values of this MetaCode to Null values.
212  void SetNullValues();
213 
214  ///////////////////////////////////////////////////////////////////////////////
215  // Conversion and Casting Functions
216 
217  /// @brief Get the MetaValue as a Input::ButtonState.
218  /// @return This returns the appropriate button state or throws an Mezzanine::Exception if an invalid button state is stored in the MetaValue.
219  /// @throw This throws a Mezzanine::Exception if the MetaValue is less than BUTTON_LIFTING or greater than BUTTON_DOWN.
220  Input::ButtonState GetMetaValueAsButtonState() const;
221  /// @brief Get the MetaValue as a Input::DirectionalMotionState.
222  /// @return This returns the appropriate MouseWheel state or throws an Mezzanine::Exception if an invalid state is stored in the MetaValue
223  /// @throw This throws a Mezzanine::Exception if the MetaValue is less than MOUSEWHEEL_DOWN or greater than MOUSEWHEEL_UP.
224  Input::DirectionalMotionState GetMetaValueAsDirectionalMotionState() const;
225 
226  /// @brief Accepts a int and returns the InputCode for the Corresponding Mouse button.
227  /// @param ButtonerNumber The number of the button you want the code for.
228  /// @return When passed 0 this returns Input::MOUSEBUTTON, otherwise this returns Input::MOUSEBUTTON_X where X is the number that was passed in.
229  static Input::InputCode GetMouseButtonCode(const UInt16 ButtonNumber);
230  /// @brief Accepts a int and returns the InputCode for the Corresponding Controller button.
231  /// @param ButtonerNumber The number of the button you want the code for.
232  /// @return When passed 0 this returns Input::CONTROLLERBUTTON, otherwise this returns Input::CONTROLLERBUTTON_X where X is the number that was passed in.
233  static Input::InputCode GetControllerButtonCode(const UInt16 ButtonNumber);
234  /// @brief Accepts a int and returns the InputCode for the Corresponding Controller Axis.
235  /// @param AxisNumber The number of the axis you want the code for.
236  /// @return When passed 0 this returns Input::CONTROLLERAXIS, otherwise this returns Input::CONTROLLERAXIS_X where X is the number that was passed in.
237  static Input::InputCode GetControllerAxisCode(const UInt16 AxisNumber);
238  /// @brief Accepts a int and returns the InputCode for the Corresponding Controller Hat.
239  /// @param HatNumber The index of the hat you want the code for.
240  /// @return When passed 0 this returns Input::CONTROLLERHAT, otherwise this returns Input::CONTROLLERHAT_X where X is the number that was passed in.
241  static Input::InputCode GetControllerHatCode(const UInt16 HatNumber);
242 
243  ///////////////////////////////////////////////////////////////////////////////
244  // Utility Checks
245 
246  /// @brief Does this MetaCode Represent a state of a keyboard key.
247  /// @return This returns a Boole which will be true if this is a keyboard event.
248  Boole IsKeyboardButton() const;
249  /// @brief Does this MetaCode Represent a state of a Mouse button.
250  /// @return This returns a Boole which will be true if this is a MOUSEBUTTON_X event.
251  Boole IsMouseButton() const;
252  /// @brief Does this MetaCode Represent a state of a Controller button.
253  /// @return This returns a Boole which will be true if this is a CONTROLLERBUTTON_X event.
254  Boole IsControllerButton() const;
255  /// @brief Does this MetaCode Represent a state of any button on an input device.
256  /// @return This returns a Boole which will be true if this is event pertains to any button on any input device.
257  Boole IsDeviceButton() const;
258 
259  /// @brief Does this MetaCode Represent a state of a keyboard Event.
260  /// @return This returns a Boole which will be true if this is between KEY_FIRST and KEY_LAST.
261  Boole IsKeyboardEvent() const;
262  /// @brief Does this MetaCode Represent a state of a mouse Event.
263  /// @return This returns a Boole which will be true if this is between MOUSE_FIRST and MOUSE_LAST.
264  Boole IsMouseEvent() const;
265  /// @brief Does this MetaCode Represent movement of the mouse or mouse wheel.
266  /// @return This returns a Boole which will be true if this is between MOUSEMOTION_FIRST and MOUSEMOTION_LAST.
267  Boole IsMouseMotionEvent() const;
268  /// @brief Does this MetaCode Represent multiple clicks of a mouse button.
269  /// @return This returns a Boole which will be true if this is between the first mouse multiclick input code and the last mouse multiclick input code.
270  Boole IsMouseMultiClickEvent() const;
271  /// @brief Does this MetaCode Represent a state of a multitouch device.
272  /// @return This returns a Boole which will be true if this is between the MULTITOUCH_FIRST and MULTITOUCH_LAST.
273  Boole IsMultitouchEvent() const;
274  /// @brief Does this MetaCode Represent a state of a Controller Event
275  /// @return This returns a Boole which will be true if this is between CONTROLLER_FIRST and CONTROLLER_LAST.
276  Boole IsControllerEvent() const;
277  /// @brief Does this MetaCode Represent an axis position on a controller.
278  /// @return This returns a Boole which will be true if this is between CONTROLLERAXIS_FIRST and CONTROLLERAXIS_LAST.
279  Boole IsControllerAxisEvent() const;
280  /// @brief Does this MetaCode Represent a hat position on a controller.
281  /// @return This returns a Boole which will be true if this is between CONTROLLERHAT_FIRST and CONTROLLERHAT_LAST.
282  Boole IsControllerHatEvent() const;
283  /// @brief Does this MetaCode Represent some other (non-keyboard and non-mouse button).
284  /// @return This returns a Boole which will be true if this is between INPUTEVENT_FIRST and INPUTEVENT_LAST.
285  Boole IsInputEvent() const;
286 
287  /// @brief Is this a left or right Alt key.
288  /// @return This returns a Boole which will be true if this is a left or right Alt key.
289  Boole IsAltKey() const;
290  /// @brief Is this a left or right Ctrl key.
291  /// @return This returns a Boole which will be true if this is a left or right Ctrl key.
292  Boole IsCtrlKey() const;
293  /// @brief Is this a left or right Shift key.
294  /// @return This returns a Boole which will be true if this is a left or right Shift key.
295  Boole IsShiftKey() const;
296  /// @brief Is this a left or right Super key (Windows logo key, Apple logo key, etc...).
297  /// @return This returns a Boole which will be true if this is a left or right Super key.
298  Boole IsSuperKey() const;
299 
300  /// @brief Is this metacode a pollable event.
301  /// @return if this metacode stores a device button then this is a pollable event.
302  Boole IsPollable() const;
303 
304  /// @brief Gets the device-type this MetaCode is representing.
305  /// @return Returns an Input::InputDevice value for the device this MetaCode is representing.
306  Input::InputDevice GetDeviceType() const;
307 
308  ///////////////////////////////////////////////////////////////////////////////
309  // Operators
310 
311  /// @brief Assignment operator.
312  /// @param Other The other MetaCode to assign to this.
313  /// @return Returns a reference to this.
314  MetaCode& operator=(const MetaCode& Other);
315  /// @brief Compares two MetaCode's for equality.
316  /// @param Other The other MetaCode to compare with.
317  /// @return Returns true if the two MetaCode's are equal, false otherwise.
318  Boole operator==(const MetaCode& Other) const;
319  /// @brief Compares two MetaCode's for inequality.
320  /// @param Other The other MetaCode to compare with.
321  /// @return Returns true if the two MetaCode's are not equal, false otherwise.
322  Boole operator!=(const MetaCode& Other) const;
323  /// @brief Compares two MetaCode's to see if this is less.
324  /// @note This only compares the InputCode contained in both MetaCode's.
325  /// @param Other The other MetaCode to compare with.
326  /// @return Returns true if this MetaCode is less then the MetaCode being compared, false otherwise.
327  Boole operator<(const MetaCode& Other) const;
328  /// @brief Compares two MetaCode's to see if this is greater.
329  /// @note This only compares the InputCode contained in both MetaCode's.
330  /// @param Other The other MetaCode to compare with.
331  /// @return Returns true if this MetaCode is greater then the MetaCode being compared, false otherwise.
332  Boole operator>(const MetaCode& Other) const;
333 
334  ///////////////////////////////////////////////////////////////////////////////
335  // Serialization
336 
337  /// @brief Convert this class to an XML::Node ready for serialization.
338  /// @param CurrentRoot The point in the XML hierarchy that all this vector3 should be appended to.
339  void ProtoSerialize(XML::Node& CurrentRoot) const;
340  /// @brief Take the data stored in an XML and overwrite this instance of this object with it.
341  /// @param OneNode and XML::Node containing the data.
342  void ProtoDeSerialize(const XML::Node& OneNode);
343 
344  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
345  /// @return A string containing "Vector3".
346  String GetSerializableName() const;
347  };//MetaCode
348  }//Input
349 
350  /// @brief Convenience datatype for storage of MetaCodes.
352  /// @brief Iterator type for convenient MetaCode storage.
354  /// @brief Const Iterator type for convenient MetaCode storage.
356 }//Mezzanine
357 
358 /// @brief Allows for streaming of MetaCodes
359 /// @details If it can be streamed, then it can be logged Holds true for the MetaCode.
360 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Input::MetaCode& x);
361 
362 /// @brief Used to de-serialize an Mezzanine::MetaCode from a stream
363 /// @details This reads in the xml and sets the target MetaCode according to values from the stream.
364 /// @param x The Mezzanine::MetaCode that will accept the values from the xml
365 /// @param stream The place to get the characters from, that define the Mezzanine::MetaCode.
366 /// @return Get an std::ostream that was read from, this allow chaining of the >> operators.
367 /// @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.
368 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Input::MetaCode& x);
369 
370 /// @brief Converts an XML node into a Mezzanine::MetaCode
371 /// @details This will convert an XML::Node will a valid serialized Mezzanine::MetaCode into a Mezzanine::MetaCode
372 /// @param OneNode An XML Node containing the the text of a MetaCode
373 /// @param x the Mezzanine::MetaCode to store the deserialized MetaCode
374 /// @return This returns a reference to the XML::Node for operator chaining or whatever.
375 /// @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.
377 
378 #endif
DirectionalMotionState
An Optional listing of values that can be used in a metacode Indicate spin, digital or binary travel ...
std::ostream & operator<<(std::ostream &stream, const Mezzanine::LinearInterpolator< T > &Lint)
Used to Serialize an Mezzanine::LinearInterpolator to a human readable stream.
Definition: interpolator.h:433
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
MetaCodeContainer::iterator MetaCodeIterator
Iterator type for convenient MetaCode storage.
Definition: metacode.h:99
InputCode
The InputCode enum defines all the posible types of inputs.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Input::InputCode Code
The code indicating what type of input this is.
Definition: metacode.h:111
Int32 DeviceIndex
The device index if this is input from a controller. For keyboards and mice this is the max value if ...
Definition: metacode.h:108
All the definitions for datatypes as well as some basic conversion functions are defined here...
Int32 MetaValue
The relevant value for the type of input this is, if applicable.
Definition: metacode.h:105
SDL_Event RawEvent
This is an internal datatype use to communicate with the User input Subsystem.
Definition: datatypes.h:219
Input::MetaCode::MetaCodeIterator MetaCodeIterator
Iterator type for convenient MetaCode storage.
Definition: metacode.h:353
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
InputDevice
An enum listing containing the different kind of input devices, useful for some query functions...
Input::MetaCode::ConstMetaCodeIterator ConstMetaCodeIterator
Const Iterator type for convenient MetaCode storage.
Definition: metacode.h:355
This Determines the kind of user input.
Definition: metacode.h:93
Input::MetaCode::MetaCodeContainer MetaCodeContainer
Convenience datatype for storage of MetaCodes.
Definition: metacode.h:351
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
std::vector< Input::MetaCode > MetaCodeContainer
Convenience datatype for storage of MetaCodes.
Definition: metacode.h:97
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
ButtonState
An Optional listing of value that can be used in a metacode to represent the information of a button ...
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
MetaCodeContainer::const_iterator ConstMetaCodeIterator
Const Iterator type for convenient MetaCode storage.
Definition: metacode.h:101
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159