Added support for multiple sticks and hats on a gamepad

This commit is contained in:
Don Honerbrink 2015-07-24 17:01:01 -05:00
parent b9850ce1a8
commit fa439c831b
7 changed files with 36 additions and 15 deletions

View File

@ -1,7 +1,8 @@
#include "JoyAxisHandler.h"
JoyAxisHandler::JoyAxisHandler(Uint8 axis, Sint16 min, Sint16 max)
: axis_(axis)
JoyAxisHandler::JoyAxisHandler(SDL_JoystickID joyid, Uint8 axis, Sint16 min, Sint16 max)
: joyid_(joyid)
, axis_(axis)
, min_(min)
, max_(max)
, pressed_(false)
@ -15,7 +16,7 @@ void JoyAxisHandler::reset()
bool JoyAxisHandler::update(SDL_Event &e)
{
if(e.type != SDL_JOYAXISMOTION || e.jaxis.axis != axis_) return false;
if(e.type != SDL_JOYAXISMOTION || e.jaxis.which != joyid_ || e.jaxis.axis != axis_) return false;
pressed_ = (min_ <= e.jaxis.value && e.jaxis.value <= max_);
return true;

View File

@ -5,12 +5,13 @@
class JoyAxisHandler : public InputHandler
{
public:
JoyAxisHandler(Uint8 axis, Sint16 min, Sint16 max);
JoyAxisHandler(SDL_JoystickID joyid, Uint8 axis, Sint16 min, Sint16 max);
bool update(SDL_Event &e);
bool pressed();
void reset();
private:
SDL_JoystickID joyid_;
Uint8 axis_;
Sint16 min_;
Sint16 max_;

View File

@ -1,7 +1,8 @@
#include "JoyButtonHandler.h"
JoyButtonHandler::JoyButtonHandler(Uint8 button)
: button_(button)
JoyButtonHandler::JoyButtonHandler(SDL_JoystickID joynum, Uint8 button)
: joynum_(joynum)
, button_(button)
, pressed_(false)
{
}
@ -15,7 +16,7 @@ bool JoyButtonHandler::update(SDL_Event &e)
{
if(e.type != SDL_JOYBUTTONUP && e.type != SDL_JOYBUTTONDOWN) return false;
if(e.jbutton.button == button_)
if(e.jbutton.which == joynum_ && e.jbutton.button == button_)
{
pressed_ = (e.type == SDL_JOYBUTTONDOWN) ? true : false;
return true;

View File

@ -5,12 +5,13 @@
class JoyButtonHandler : public InputHandler
{
public:
JoyButtonHandler(Uint8 button);
JoyButtonHandler(SDL_JoystickID joynum, Uint8 button);
bool update(SDL_Event &e);
bool pressed();
void reset();
private:
SDL_JoystickID joynum_;
Uint8 button_;
bool pressed_;
};

View File

@ -1,7 +1,9 @@
#include "JoyHatHandler.h"
JoyHatHandler::JoyHatHandler(Uint8 direction)
: direction_(direction)
JoyHatHandler::JoyHatHandler(SDL_JoystickID joynum, Uint8 hatnum, Uint8 direction)
: joynum_(joynum)
, hatnum_(hatnum)
, direction_(direction)
, pressed_(false)
{
}
@ -13,7 +15,7 @@ void JoyHatHandler::reset()
bool JoyHatHandler::update(SDL_Event &e)
{
if(e.type != SDL_JOYHATMOTION) return false;
if(e.type != SDL_JOYHATMOTION || e.jhat.which != joynum_ || e.jhat.hat != hatnum_) return false;
pressed_ = (e.jhat.value == direction_);
return true;

View File

@ -5,12 +5,14 @@
class JoyHatHandler : public InputHandler
{
public:
JoyHatHandler(Uint8 direction);
JoyHatHandler(SDL_JoystickID joynum, Uint8 hatnum, Uint8 direction);
bool update(SDL_Event &e);
bool pressed();
void reset();
private:
SDL_JoystickID joynum_;
Uint8 hatnum_;
Uint8 direction_;
bool pressed_;
};

View File

@ -128,13 +128,20 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
else if(description.find("joy") == 0)
{
std::string joydesc = Utils::replace(Utils::toLower(description), "joy", "");
std::stringstream ssjoy;
ssjoy << joydesc.at(0);
int joynum;
ssjoy >> joynum;
joydesc = joydesc.erase(0, 1);
if(joydesc.find("button") == 0)
{
unsigned int button;
std::stringstream ss;
ss << Utils::replace(joydesc, "button", "");
ss >> button;
keyHandlers_[key] = new JoyButtonHandler(button);
keyHandlers_[key] = new JoyButtonHandler(joynum, button);
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad button " + ss.str() );
return true;
}
@ -143,6 +150,12 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
Uint8 hat;
joydesc = Utils::replace(joydesc, "hat", "");
std::stringstream sshat;
sshat << joydesc.at(0);
int hatnum;
ssjoy >> hatnum;
joydesc = joydesc.erase(0, 1);
if(joydesc == "leftup") hat = SDL_HAT_LEFTUP;
else if(joydesc == "left") hat = SDL_HAT_LEFT;
else if(joydesc == "leftdown") hat = SDL_HAT_LEFTDOWN;
@ -153,7 +166,7 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
else if(joydesc == "right") hat = SDL_HAT_RIGHT;
else if(joydesc == "rightdown") hat = SDL_HAT_RIGHTDOWN;
keyHandlers_[key] = new JoyHatHandler(hat);
keyHandlers_[key] = new JoyHatHandler(joynum, hatnum, hat);
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad hat " + joydesc );
return true;
}
@ -184,7 +197,7 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
ss << joydesc;
ss >> axis;
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad axis " + ss.str() );
keyHandlers_[key] = new JoyAxisHandler(axis, min, max);
keyHandlers_[key] = new JoyAxisHandler(joynum, axis, min, max);
return true;
}
}