Spinning Topp Logo BlackTopp Studios
inc
eventuserinput.cpp
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_CPP
41 #define _EVENTUSERINPUT_CPP
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 
49 ///////////////////////////////////////////////////////////////////////////////
50 // Includes
51 ///////////////////////////////////////
52 #include "eventuserinput.h"
53 //#include "eventbase.h"
54 #include "exception.h"
55 #include "stringtool.h"
56 
57 #include <memory>
58 #include <vector>
59 
60 #include "SDL.h"
61 
62 using namespace std;
63 
64 
65 namespace Mezzanine
66 {
67  ///////////////////////////////////////////////////////////////////////////////
68  // EventUserInput
69  ///////////////////////////////////////
70  EventUserInput::EventUserInput()
71  {}
72 
73  EventUserInput::EventUserInput(const Input::MetaCode& Code_)
74  { this->push_back(Code_); }
75 
76  EventUserInput::EventUserInput(const vector<Input::MetaCode>& Code_)
77  { AddCodes(Code_); }
78 
79  EventUserInput::~EventUserInput()
80  {}
81 
82  const Input::MetaCode& EventUserInput::GetMetaCode(const unsigned int& Index)
83  { return this->at(Index); }
84 
85  size_t EventUserInput::GetMetaCodeCount()
86  { return this->size(); }
87 
88  Input::MetaCode EventUserInput::AddCode(const Input::MetaCode& Code_)
89  {
90  this->push_back(Code_);
91  return Code_;
92  }
93 
94  Input::MetaCode EventUserInput::AddCode(const int& MetaValue_, const Input::InputCode& Code_)
95  {
96  Input::MetaCode CurrentMetaCode( MetaValue_, Code_ );
97  return this->AddCode(CurrentMetaCode);
98  }
99 
100  Input::MetaCode EventUserInput::AddCode(const int& MetaValue_, const Input::InputCode& Code_, const UInt16& DeviceIndex_)
101  {
102  Input::MetaCode CurrentMetaCode( MetaValue_, Code_, DeviceIndex_ );
103  return this->AddCode(CurrentMetaCode);
104  }
105 
106  void EventUserInput::AddCodes(const vector<Input::MetaCode>& Codes)
107  {
108  for(unsigned int c=0; Codes.size()>c ; c++)
109  { this->push_back(Codes.at(c)); }
110  }
111 
112  void EventUserInput::EraseCode(const Input::MetaCode& Code_)
113  {
114  vector<Input::MetaCode>::iterator iter;
115 
116  for(iter=this->begin(); this->end()!=iter ; iter++)
117  {
118  if(*iter == Code_)
119  {
120  this->erase(iter);
121  }
122  }
123  }
124 
125  void EventUserInput::EraseCode(const unsigned int& Index)
126  { this->erase(this->begin()+Index); }
127 
128  EventBase::EventType EventUserInput::GetType() const
129  { return UserInput; }
130 
131  ///////////////////////////////////////////////////////////////////////////////
132  // EventUserInput Private Methods
133  ///////////////////////////////////////
134 
135  vector<Input::MetaCode> EventUserInput::AddCodesFromSDLMouseMotion(const RawEvent& RawEvent_)
136  {
137  vector<Input::MetaCode> Results;
138 
139  Results.push_back(this->AddCode(RawEvent_.motion.x, Input::MOUSEABSOLUTEHORIZONTAL));
140  Results.push_back(this->AddCode(RawEvent_.motion.y, Input::MOUSEABSOLUTEVERTICAL));
141 
142  if(0 != RawEvent_.motion.xrel)
143  { Results.push_back(this->AddCode(RawEvent_.motion.xrel, Input::MOUSEHORIZONTAL));}
144 
145  if(0 != RawEvent_.motion.yrel)
146  { Results.push_back(this->AddCode(RawEvent_.motion.yrel, Input::MOUSEVERTICAL));}
147  return Results;
148  }
149 
150  vector<Input::MetaCode> EventUserInput::AddCodesFromSDLJoyStickMotion(const RawEvent& RawEvent_)
151  {
152  vector<Input::MetaCode> Results;
153 
154  Results.push_back(this->AddCode(RawEvent_.jaxis.value, Input::MetaCode::GetControllerAxisCode(RawEvent_.jaxis.axis+1), RawEvent_.jaxis.which));
155 
156  return Results;
157  }
158 
159  vector<Input::MetaCode> EventUserInput::AddCodeFromSDLJoyStickHat(const RawEvent& RawEvent_)
160  {
161  vector<Input::MetaCode> Results;
162  Input::InputCode Hat = (Input::InputCode)(RawEvent_.jhat.hat + Input::CONTROLLERHAT_FIRST);
163 
164  if( Input::CONTROLLERHAT_FIRST > Hat || Input::CONTROLLERHAT_LAST < Hat )
165  {
166  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Unsupported Controller Hat Event");
167  }
168 
169  Results.push_back(this->AddCode( RawEvent_.jhat.value, Hat, RawEvent_.jhat.which ));
170  return Results;
171  }
172 
173  vector<Input::MetaCode> EventUserInput::AddCodeFromSDLJoyStickBall(const RawEvent& RawEvent_)
174  {
175  vector<Input::MetaCode> Results;
176 
177  if( 0 == RawEvent_.jball.ball )
178  {
179  if( RawEvent_.jball.yrel != 0 )
180  { Results.push_back(this->AddCode(RawEvent_.jball.yrel, Input::CONTROLLERBALL_1_VERTICAL, RawEvent_.jball.which)); }
181  if( RawEvent_.jball.xrel != 0 )
182  { Results.push_back(this->AddCode(RawEvent_.jball.xrel, Input::CONTROLLERBALL_1_HORIZONTAL, RawEvent_.jball.which)); }
183  }else if( 1 == RawEvent_.jball.ball ){
184  if( RawEvent_.jball.yrel != 0 )
185  { Results.push_back(this->AddCode(RawEvent_.jball.yrel, Input::CONTROLLERBALL_2_VERTICAL, RawEvent_.jball.which)); }
186  if( RawEvent_.jball.xrel != 0 )
187  { Results.push_back(this->AddCode(RawEvent_.jball.xrel, Input::CONTROLLERBALL_2_HORIZONTAL, RawEvent_.jball.which)); }
188  }else{
189  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"More then 2 trackballs is currently not supported. Perhaps we should expand our enum.");
190  }
191 
192  return Results;
193  }
194 
195 } // /Mezz
196 
197 ///////////////////////////////////////////////////////////////////////////////
198 // Class External << Operators for streaming or assignment
199 std::ostream& operator << (std::ostream& stream, const Mezzanine::EventUserInput& Ev)
200 {
201  stream << "<EventUserInput Version=\"1\">";
202  for (vector<Mezzanine::Input::MetaCode>::const_iterator Iter = Ev.begin(); Iter!=Ev.end(); ++Iter)
203  {
204  stream << *Iter;
205  }
206  stream << "</EventUserInput>";
207 
208  return stream;
209 }
210 
211 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::EventUserInput& Ev)
212 {
215 
216  Doc->GetFirstChild() >> Ev;
217 
218  return stream;
219 }
220 
222 {
223  if ( Mezzanine::String(OneNode.Name())==Mezzanine::String("EventUserInput") )
224  {
225  if(OneNode.GetAttribute("Version").AsInt() == 1)
226  { Ev.clear();
227 
228  //Ev.Impulse=OneNode.GetAttribute("Impulse").AsReal();
229  Mezzanine::XML::Node Child = OneNode.GetFirstChild();
231  while(Child)
232  {
233  Child >> ACode;
234  Ev.AddCode(ACode);
235  Child = Child.GetNextSibling();
236  }
237 
238  }else{
239  MEZZ_EXCEPTION(Mezzanine::ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for EventUserInput: Not Version 1.");
240  }
241  }else{
242  MEZZ_EXCEPTION(Mezzanine::ExceptionBase::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a EventUserInput, found a " + Mezzanine::String(OneNode.Name()));
243  }
244 }
245 
246 
247 #endif
String GetOneTag(std::istream &stream)
Gets the first tag out of the Stream and returns it as a String.
Definition: xmlstring.cpp:72
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.
Document * PreParseClassFromSingleTag(const String &NameSpace, const String &ClassName, const String &OneTag)
Perform a basic series of checks for extracting meaning from a single xml tag.
Definition: xmlstring.cpp:116
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
A simple reference counting pointer.
Definition: countedptr.h:70
STL namespace.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
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
This implements the exception hiearchy for Mezzanine.
Node GetNextSibling() const
Attempt to retrieve the next sibling of this Node.
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
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Input::MetaCode AddCode(const Input::MetaCode &Code_)
Adds a MetaCode.
This Determines the kind of user input.
Definition: metacode.h:93
Thrown when the identity string wasn't valid at all.
Definition: exception.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
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.