Spinning Topp Logo BlackTopp Studios
inc
eventuserinput.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 #ifndef _EVENTUSERINPUT_H
41 #define _EVENTUSERINPUT_H
42 ///////////////////////////////////////////////////////////////////////////////
43 // This will expose all keyboard and mouse, joystick and other userinput events
44 // to developers, we are using the SDL keymap to get us started, large portions
45 // are directly copy/pasta'd, so we included their license too
46 ///////////////////////////////////////
47 
48 //From SDL
49 /*
50  SDL - Simple DirectMedia Layer
51  Copyright (C) 1997-2009 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 "Input/metacode.h"
72 #include "eventbase.h"
73 #ifndef SWIG
74  #include "XML/xml.h"
75 #endif
76 
77 namespace Mezzanine
78 {
79 
80  ///////////////////////////////////////////////////////////////////////////////
81  /// @class EventUserInput
82  /// @headerfile eventuserinput.h
83  /// @brief This is a container for MetaCodes that is used in the EventManager
84  /// @details The EventUserInput is the container for information about how a
85  /// user enters data and commands into a program. By Default one is inserted into
86  /// event manager the with all the user input from each run of the main loop.
87  /// These can be manually inserted into the EventManager to simulate input from
88  /// other sources. If setup properly this can allow computer controlled
89  /// characters to use the same interface players, allowing for more realistic
90  /// response from them. \n \n
91  /// This is implemented by inheriting from std::vector. Any feature of std::vector
92  /// can be used to interact with the metacodes stored within. Using Iterators is
93  /// great way to quickly and easily get what you need from this. For the most part
94  /// any extra functions on this are for seamless interaction with the EventManager,
95  /// or to convert incoming data into a more usable form.
96  ///////////////////////////////////////////////////////////////////////////////
97  class MEZZ_LIB EventUserInput : public EventBase, public std::vector<Input::MetaCode>
98  {
99  protected:
100  //These private methods all accept a specific kind of RawEvent from SDL and will behave non-deterministically if
101  //passed any other kind of data.
102 
103  /// @internal
104  /// @brief Gets the MetaCode from RawInput Data.
105  /// @param RawEvent_ The event that contains only Mouse Motion data.
106  /// @return a vector of metacode that represents button presses.
107  std::vector<Input::MetaCode> AddCodesFromSDLMouseMotion(const RawEvent& RawEvent_);
108 
109  /// @internal
110  /// @brief Gets the MetaCode from RawInput Data.
111  /// @param RawEvent_ The event that contains only Joystick Motion data.
112  /// @return a vector of metacode that represents button presses.
113  std::vector<Input::MetaCode> AddCodesFromSDLJoyStickMotion(const RawEvent& RawEvent_);
114 
115  /// @internal
116  /// @brief Gets the MetaCode from RawInput Data.
117  /// @param RawEvent_ The event that contains only Joystick Hat data.
118  /// @return a vector of metacode that represents hat presses.
119  std::vector<Input::MetaCode> AddCodeFromSDLJoyStickHat(const RawEvent& RawEvent_);
120 
121  /// @internal
122  /// @brief Gets the MetaCode from RawInput Data.
123  /// @param RawEvent_ The event that contains only Joystick Ball data.
124  /// @return a vector of metacode that represents ball motions.
125  std::vector<Input::MetaCode> AddCodeFromSDLJoyStickBall(const RawEvent& RawEvent_);
126 
127  /// @internal
128  /// @brief Gets the MetaCode from RawInput Data.
129  /// @param RawEvent_ The event that contains only Mouse button data.
130  /// @return a metacode that represents a button press.
131  Input::MetaCode AddCodeFromSDLMouseButton(const RawEvent &RawEvent_);
132 
133  /*/// @internal
134  /// @brief Gets the MetaCode from RawInput Data.
135  /// @param RawEvent_ The event that contains only Joystick button data.
136  /// @return a metacode that represents a button press.
137  MetaCode AddCodeFromSDLJoyStickButton(const RawEvent &RawEvent_);// */
138 
139  public:
140 
141  /// @brief Default constructor.
142  /// @details This creates a perfectly functional, but empty EventUserInput.
143  EventUserInput();
144 
145  /// @brief Single Data Point constructor.
146  /// @param Code_ This MetaCode will be added to the EventUserInput during creation.
147  /// @details This creates a functional EventUserInput which already contains one metacode.
148  EventUserInput(const Input::MetaCode& Code_);
149 
150  /// @brief Multi Data Point constructor.
151  /// @param Codes_ The MetaCodes in this vecotor will be added to the EventUserInput during creation.
152  /// @details This creates a ready to use EventUserInput which already contains all the metacodes included.
153  EventUserInput(const std::vector<Input::MetaCode>& Codes_);
154 
155  /// @brief Default destructor.
156  virtual ~EventUserInput();
157 
158  // Code managment functions
159  /// @brief Single Data Point constructor.
160  /// @return Index The requested MetaCode to return.
161  /// @details This function simply retrieves the requested MetaCode. It can throw standard Out of bounds exceptions if attemped to reference a negative item or an item with Index higher than what exists
162  /// \n This is useful for accessing each MetaCode stored in this UserInputEvent.
163  const Input::MetaCode& GetMetaCode(const unsigned int& Index);
164 
165  /// @brief Retrieves a count of the stored Metacodes.
166  /// @return The amount of codes stored in this EventUserInput.
167  /// @details Retrieves a count of the stored Metacodes. Synonym for vector::size();
168  size_t GetMetaCodeCount();
169 
170  /// @brief Adds a MetaCode.
171  /// @param Code_ The User Input MetaCode tobe added.
172  /// @details This adds an existing metacode to this event.
173  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
174  Input::MetaCode AddCode(const Input::MetaCode& Code_);
175 
176  /// @brief Adds a MetaCode Created From Raw Values.
177  /// @param MetaValue_ The MetaValue that will be in the MetaCode.
178  /// @param Code_ The InputCode that will be in the MetaCode.
179  /// @details This creates metacode a metacode and adds it to this event.
180  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
181  Input::MetaCode AddCode(const int& MetaValue_, const Input::InputCode& Code_);
182 
183  /// @brief Adds a MetaCode Created From Raw Values.
184  /// @param MetaValue_ The MetaValue that will be in the MetaCode.
185  /// @param Code_ The InputCode that will be in the MetaCode.
186  /// @param DeviceIndex_ The index of the device the added code applies to.
187  /// @details This creates metacode a metacode and adds it to this event.
188  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
189  Input::MetaCode AddCode(const int& MetaValue_, const Input::InputCode& Code_, const UInt16& DeviceIndex_);
190 
191  /// @brief Add Several MetaCodes from a vector.
192  /// @param Codes A vector of MetaCodes to be added to this event.
193  /// @details This adds several existing metacodes to this event.
194  void AddCodes(const std::vector<Input::MetaCode>& Codes);
195 
196  /// @brief Removes a specific code from storage.
197  /// @param Code_ This will search for all matching copies of this.
198  /// @details All MetaCodes that are equal to Code_ will simply be erased.
199  void EraseCode(const Input::MetaCode& Code_);
200 
201  /// @brief Removes a specific code from storage.
202  /// @param Index This is the location to removed from.
203  /// @details The MetaCode at and only at the given Index will be deleted.
204  void EraseCode(const unsigned int& Index);
205 
206  /// @brief Returns the type of this event.
207  /// @return Returns EventType::UserInput.
208  virtual EventType GetType() const;
209  };
210 }// /Mezz
211 
212 ///////////////////////////////////////////////////////////////////////////////
213 // Class External << Operators for streaming or assignment
214 /// @brief Serializes the passed Mezzanine::EventUserInput to XML.
215 /// @param stream The ostream to send the xml to.
216 /// @param Ev the Mezzanine::EventUserInput to be serialized.
217 /// @return this returns the ostream, now with the serialized data.
218 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::EventUserInput& Ev);
219 
220 /// @brief Deserialize a Mezzanine::EventUserInput.
221 /// @param stream The istream to get the xml from to (re)make the Mezzanine::EventUserInput.
222 /// @param Ev the Mezzanine::EventUserInput to be deserialized.
223 /// @return this returns the ostream, advanced past the Mezzanine::EventUserInput that was recreated onto Ev.
224 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::EventUserInput& Ev);
225 
226 /// @brief Set all values of a Mezzanine::EventUserInput from parsed xml.
227 /// @param OneNode The istream to get the xml from to (re)make the Mezzanine::EventUserInput.
228 /// @param Ev the Mezzanine::EventUserInput to be reset.
229 /// @return This returns theXML::Node that was passed in.
231 
232 #endif
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
InputCode
The InputCode enum defines all the posible types of inputs.
This is a container for MetaCodes that is used in the EventManager.
SDL_Event RawEvent
This is an internal datatype use to communicate with the User input Subsystem.
Definition: datatypes.h:219
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
EventType
A listing of values that can be used to identify Events.
Definition: eventbase.h:66
The base class for all events.
Definition: eventbase.h:61
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
This Determines the kind of user input.
Definition: metacode.h:93
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448