MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
controller.cpp
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 _inputcontroller_cpp
41 #define _inputcontroller_cpp
42 
43 #include "Input/controller.h"
44 
45 #include "exception.h"
46 
47 #include "SDL.h"
48 
49 namespace Mezzanine
50 {
51  namespace Input
52  {
53  Controller::Controller(void* InternalControl, int Count)
54  {
55  SDL_Joystick* InternalController = (SDL_Joystick*)InternalControl;
56  this->Axes.resize( SDL_JoystickNumAxes(InternalController), 0 );
57  this->Trackballs.resize( SDL_JoystickNumBalls(InternalController), Vector2(0,0) );
58  this->Buttons.resize( SDL_JoystickNumButtons(InternalController), Input::BUTTON_UP );
59  this->Hats.resize( SDL_JoystickNumHats(InternalController), Input::CONTROLLERHAT_CENTERED );
60  this->Index = Count; //SDL_JoystickIndex(InternalController);
61  this->DeviceName = SDL_JoystickName(InternalController);
62  //DeviceName = "DefaultName";
63  }
64 
66  {
67  }
68 
69  void Controller::UpdateImpl(const MetaCodeContainer& DeltaCodes, MetaCodeContainer& GeneratedCodes)
70  {
71  for( Whole X = 0 ; X < DeltaCodes.size() ; ++X )
72  {
73  const MetaCode& CurrCode = DeltaCodes[X];
75  {
76  this->Axes.at( CurrCode.GetCode() - Input::CONTROLLERAXIS_FIRST ) = CurrCode.GetMetaValue();
77  }
78  else if(CurrCode.GetCode() >= Input::CONTROLLERBALL_FIRST && CurrCode.GetCode() <= Input::CONTROLLERBALL_LAST)
79  {
80  if( Input::CONTROLLERBALL_1_HORIZONTAL == CurrCode.GetCode() ){
81  this->Trackballs.at(0).X = CurrCode.GetMetaValue();
82  }else if( Input::CONTROLLERBALL_1_VERTICAL == CurrCode.GetCode() ){
83  this->Trackballs.at(0).Y = CurrCode.GetMetaValue();
84  }else if( Input::CONTROLLERBALL_2_HORIZONTAL == CurrCode.GetCode() ){
85  this->Trackballs.at(1).X = CurrCode.GetMetaValue();
86  }else if( Input::CONTROLLERBALL_2_VERTICAL == CurrCode.GetCode() ){
87  this->Trackballs.at(1).Y = CurrCode.GetMetaValue();
88  }
89  }
90  else if(CurrCode.GetCode() >= Input::CONTROLLERBUTTON_FIRST && CurrCode.GetCode() <= Input::CONTROLLERBUTTON_LAST)
91  {
92  this->TransitioningIndexes.push_back( CurrCode.GetCode() - Input::CONTROLLERBUTTON_FIRST );
93  this->Buttons.at( CurrCode.GetCode() - Input::CONTROLLERBUTTON_FIRST ) = static_cast<Input::ButtonState>(CurrCode.GetMetaValue());
94  }
95  else if(CurrCode.GetCode() >= Input::CONTROLLERHAT_FIRST && CurrCode.GetCode() <= Input::CONTROLLERHAT_LAST)
96  {
97  this->Hats.at( CurrCode.GetCode() - Input::CONTROLLERHAT_FIRST ) = static_cast<Input::HatState>(CurrCode.GetMetaValue());
98  }
99  }
100  this->Sequences.Update(DeltaCodes,GeneratedCodes);
101  }
102 
103  void Controller::VerifySequenceImpl(const MetaCodeContainer& Sequence) const
104  {
105  for( ConstMetaCodeIterator MCIt = Sequence.begin() ; MCIt != Sequence.end() ; ++MCIt )
106  {
107  if( !MCIt->IsControllerEvent() )
108  { MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Non-Controller MetaCode detected when attempting to insert an Input Sequence into Controller input device.") }
109  }
110  }
111 
112  void Controller::AddPressedButtons(MetaCodeContainer& GeneratedCodes) const
113  {
114  for( UInt32 Index = 0 ; Index < this->Buttons.size() ; ++Index )
115  {
116  if( this->Buttons.at(Index) == Input::BUTTON_DOWN )
117  GeneratedCodes.push_back( MetaCode(Input::BUTTON_DOWN,static_cast<Input::InputCode>(Input::CONTROLLERBUTTON_FIRST + Index),this->GetDeviceIndex()) );
118  }
119  }
120 
121  ///////////////////////////////////////////////////////////////////////////////
122  // Query Methods
123 
125  { return this->Index; }
126 
128  { return this->DeviceName; }
129 
131  { return this->Axes.size(); }
132 
134  { return this->Trackballs.size(); }
135 
137  { return this->Hats.size(); }
138 
139  bool Controller::IsHatPushedInDirection(const UInt16 Hat, const Input::HatState& WhichWay) const
140  { return this->GetHatState(Hat) == WhichWay; }
141 
142  Int16 Controller::GetAxis(const UInt16 Axis) const
143  { return this->Axes.at( Axis - 1 ); }
144 
146  { return this->Trackballs.at( Trackball - 1 ); }
147 
149  { return this->GetTrackballDelta(Trackball).X; }
150 
152  { return this->GetTrackballDelta(Trackball).Y; }
153 
155  { return this->Hats.at( Hat - 1 ); }
156 
158  { return this->Axes.at( Axis - Input::CONTROLLERAXIS_FIRST ); }
159 
161  {
162  if( Input::CONTROLLERBALL_1_HORIZONTAL == Trackball ){
163  return this->Trackballs.at(0).X;
164  }else if( Input::CONTROLLERBALL_1_VERTICAL == Trackball ){
165  return this->Trackballs.at(0).Y;
166  }else if( Input::CONTROLLERBALL_2_HORIZONTAL == Trackball ){
167  return this->Trackballs.at(1).X;
168  }else if( Input::CONTROLLERBALL_2_VERTICAL == Trackball ){
169  return this->Trackballs.at(1).Y;
170  }else{
171  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Invalid Trackball parameter.");
172  }
173  }
174 
176  { return this->Buttons.at( Button - 1 ); }
177 
179  { return this->Buttons.at( Button - Input::CONTROLLERBUTTON_FIRST ); }
180 
182  { return this->Hats.at( Hat - Input::CONTROLLERHAT_FIRST ); }
183 
184  ///////////////////////////////////////////////////////////////////////////////
185  // Configuration Methods
186 
187  ///////////////////////////////////////////////////////////////////////////////
188  // Utility Methods
189  }//Input
190 }//Mezzanine
191 
192 #endif