MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventuserinput.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 #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  // ©ode 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  /// @internal
171  /// @brief Adds a MetaCode created from a RawEvent.
172  /// @param RawEvent_ The RawEvent which will be translated into exactly One MetaCode.
173  /// @details This will add MetaCode to this event which will be create from a RawEvent which can produce Exactly one MetaCode. This is used by engine internals, it is
174  /// recommended to not use this in game code.
175  /// @warning Do not use this without reading and fully understanding the warnings on MetaCode::MetaCode(const RawEvent &RawEvent_) . This function has all the same
176  /// Restrictions. If game code is using RawEvents at all, the game logic should be scrutinized carefully, there is probably something wrong, but if it must it should use
177  /// EventUserInput::AddCodesFromRawEvent instead, as it can make the needed determinations automatically and in a platform agnostic way.
178  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
179  Input::MetaCode AddCode(const RawEvent& RawEvent_);
180 
181  /// @brief Adds a MetaCode.
182  /// @param Code_ The User Input MetaCode tobe added.
183  /// @details This adds an existing metacode to this event.
184  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
185  Input::MetaCode AddCode(const Input::MetaCode& Code_);
186 
187  /// @brief Adds a MetaCode Created From Raw Values.
188  /// @param MetaValue_ The MetaValue that will be in the MetaCode.
189  /// @param Code_ The InputCode that will be in the MetaCode.
190  /// @details This creates metacode a metacode and adds it to this event.
191  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
192  Input::MetaCode AddCode(const int& MetaValue_, const Input::InputCode& Code_);
193 
194  /// @brief Adds a MetaCode Created From Raw Values.
195  /// @param MetaValue_ The MetaValue that will be in the MetaCode.
196  /// @param Code_ The InputCode that will be in the MetaCode.
197  /// @param DeviceIndex_ The index of the device the added code applies to.
198  /// @details This creates metacode a metacode and adds it to this event.
199  /// @return This returns a const reference to the MetaCode that was Added. This reference is valid for the lifetime of this EventUserInput.
200  Input::MetaCode AddCode(const int& MetaValue_, const Input::InputCode& Code_, const UInt16& DeviceIndex_);
201 
202  /// @brief Add Several MetaCodes from a vector.
203  /// @param Codes A vector of MetaCodes to be added to this event.
204  /// @details This adds several existing metacodes to this event.
205  void AddCodes(const std::vector<Input::MetaCode>& Codes);
206 
207  /// @brief Adds all possible MetaCodes that can be created from the given RawEvent.
208  /// @param RawEvent_ The RawEvent which will be translated into a group of metacodes and added to this.
209  /// @details This will add MetaCode to this event which will be create from a RawEvent which can produce Exactly one MetaCode. This is used by engine internals, it is
210  /// recommended to not use this in game code.
211  /// @warning If game code is using RawEvents at all, the game logic should be scrutinized carefully, there is probably something wrong, but if it must them this is the correct
212  /// function to use. This will work same on a all platforms. However, the binary format of the Rawevent could chnage meaning you would have to recompile the game code to work
213  /// with new version of the engine.
214  /// \n This Function is currently incomplete, and does not yet process all events such as joysticks events and some mouse events.
215  /// @return this returns a complete set of all the MetaCodes added.
216  std::vector<Input::MetaCode> AddCodesFromRawEvent(const RawEvent& RawEvent_);
217 
218  /// @brief Removes a specific code from storage.
219  /// @param Code_ This will search for all matching copies of this.
220  /// @details All MetaCodes that are equal to Code_ will simply be erased.
221  void EraseCode(const Input::MetaCode& Code_);
222 
223  /// @brief Removes a specific code from storage.
224  /// @param Index This is the location to removed from.
225  /// @details The MetaCode at and only at the given Index will be deleted.
226  void EraseCode(const unsigned int& Index);
227 
228  /// @brief Returns the type of this event.
229  /// @return Returns EventType::UserInput.
230  virtual EventType GetType() const;
231  };
232 }// /Mezz
233 
234 ///////////////////////////////////////////////////////////////////////////////
235 // Class External << Operators for streaming or assignment
236 /// @brief Serializes the passed Mezzanine::EventUserInput to XML.
237 /// @param stream The ostream to send the xml to.
238 /// @param Ev the Mezzanine::EventUserInput to be serialized.
239 /// @return this returns the ostream, now with the serialized data.
240 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::EventUserInput& Ev);
241 
242 /// @brief Deserialize a Mezzanine::EventUserInput.
243 /// @param stream The istream to get the xml from to (re)make the Mezzanine::EventUserInput.
244 /// @param Ev the Mezzanine::EventUserInput to be deserialized.
245 /// @return this returns the ostream, advanced past the Mezzanine::EventUserInput that was recreated onto Ev.
246 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::EventUserInput& Ev);
247 
248 /// @brief Set all values of a Mezzanine::EventUserInput from parsed xml.
249 /// @param OneNode The istream to get the xml from to (re)make the Mezzanine::EventUserInput.
250 /// @param Ev the Mezzanine::EventUserInput to be reset.
251 /// @return This returns theXML::Node that was passed in.
252 void MEZZ_LIB operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::EventUserInput& Ev);
253 
254 #endif