Spinning Topp Logo BlackTopp Studios
inc
controller.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 _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 
104  {
105  for( ConstMetaCodeIterator MCIt = Sequence.begin() ; MCIt != Sequence.end() ; ++MCIt )
106  {
107  if( !MCIt->IsControllerEvent() )
108  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Non-Controller MetaCode detected when attempting to insert an Input Sequence into Controller input device.") }
109  }
110  }
111 
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 
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(ExceptionBase::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
virtual const Input::ButtonState & GetButtonState(const UInt16 Button) const
Gets the state of the requested button.
Definition: controller.cpp:175
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
Int16 GetAxis(const UInt16 Axis) const
This Gets the value of the given joystick axis.
Definition: controller.cpp:142
UInt16 GetNumTrackballs() const
Gets the number of Trackballs on this device.
Definition: controller.cpp:133
The Last Controller axis event, all joystick axis event will be lower or equal to this...
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
const Input::HatState & GetHatState(const UInt16 Hat) const
Gets the current state of the requested hat.
Definition: controller.cpp:154
The Last Controller button event, all Controller button event will be lower or equal to this...
String DeviceName
The name of the controller device.
Definition: controller.h:68
SequenceContainer Sequences
A container for storing and detecting input sequences for an input device.
Definition: device.h:60
This is the lowest Controller button value, all joystickbutton values will be larger of equal to this...
This implements the exception hiearchy for Mezzanine.
virtual ~Controller()
Class destructor.
Definition: controller.cpp:65
Int32 GetMetaValue() const
This Returns the MetaValue.
Definition: metacode.cpp:250
UInt16 GetNumAxes() const
Gets the number of Axes on this device.
Definition: controller.cpp:130
std::vector< Input::HatState > Hats
Container storing the states for each hat on the controller.
Definition: controller.h:65
UInt16 GetNumHats() const
Gets the number of Hats on this device.
Definition: controller.cpp:136
Real GetTrackballDeltaX(const UInt16 Trackball) const
Gets the delta movement on the X axis on the requested trackball.
Definition: controller.cpp:148
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
UInt16 GetDeviceIndex() const
Gets the device index of this controller.
Definition: controller.cpp:124
Real X
Coordinate on the X vector.
Definition: vector2.h:67
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
std::vector< Whole > TransitioningIndexes
A container of indexes being tracked due to state transitions.
Definition: buttondevice.h:58
Controller(void *InternalControl, int Count)
Class constructor.
Definition: controller.cpp:53
Input::InputCode GetCode() const
This Returns the Inputcode.
Definition: metacode.cpp:244
This is the lowest Controller axis value, all jpystick values will be larger of equal to this...
int16_t Int16
An 16-bit integer.
Definition: datatypes.h:120
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
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
UInt16 Index
The index of the controller device.
Definition: controller.h:71
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Boole IsHatPushedInDirection(const UInt16 Hat, const Input::HatState &WhichWay) const
Gets whether a specific hat is pressed in a specific direction.
Definition: controller.cpp:139
ButtonState
An Optional listing of value that can be used in a metacode to represent the information of a button ...
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Vector2 GetTrackballDelta(const UInt16 Trackball) const
Gets the delta movement on the requested trackball.
Definition: controller.cpp:145
void UpdateImpl(const MetaCodeContainer &DeltaCodes, MetaCodeContainer &GeneratedCodes)
Internal implementation of the device update.
Definition: controller.cpp:69
const String & GetDeviceName() const
Gets the name of this device.
Definition: controller.cpp:127
Real GetTrackballDeltaY(const UInt16 Trackball) const
Gets the delta movement on the Y axis on the requested trackball.
Definition: controller.cpp:151
void Update(const MetaCodeContainer &NormalCodes, MetaCodeContainer &SequenceCodes)
Adds provided codes to the cache if necessary and checks for sequences that have been met...
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
std::vector< Int16 > Axes
Container storing the states for each axis on the controller.
Definition: controller.h:59
std::vector< Input::ButtonState > Buttons
A container of states for each button on the input device.
Definition: buttondevice.h:61
virtual void VerifySequenceImpl(const MetaCodeContainer &Sequence) const
Definition: controller.cpp:103
HatState
An optional listing of the possible states a hat on a controller can take.
virtual void AddPressedButtons(MetaCodeContainer &GeneratedCodes) const
Definition: controller.cpp:112
std::vector< Vector2 > Trackballs
Container storing the states for each trackball on the controller.
Definition: controller.h:62