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" #include "JoyAxisHandler.h"
JoyAxisHandler::JoyAxisHandler(Uint8 axis, Sint16 min, Sint16 max) JoyAxisHandler::JoyAxisHandler(SDL_JoystickID joyid, Uint8 axis, Sint16 min, Sint16 max)
: axis_(axis) : joyid_(joyid)
, axis_(axis)
, min_(min) , min_(min)
, max_(max) , max_(max)
, pressed_(false) , pressed_(false)
@@ -15,7 +16,7 @@ void JoyAxisHandler::reset()
bool JoyAxisHandler::update(SDL_Event &e) 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_); pressed_ = (min_ <= e.jaxis.value && e.jaxis.value <= max_);
return true; return true;

View File

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

View File

@@ -1,7 +1,8 @@
#include "JoyButtonHandler.h" #include "JoyButtonHandler.h"
JoyButtonHandler::JoyButtonHandler(Uint8 button) JoyButtonHandler::JoyButtonHandler(SDL_JoystickID joynum, Uint8 button)
: button_(button) : joynum_(joynum)
, button_(button)
, pressed_(false) , 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.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; pressed_ = (e.type == SDL_JOYBUTTONDOWN) ? true : false;
return true; return true;

View File

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

View File

@@ -1,7 +1,9 @@
#include "JoyHatHandler.h" #include "JoyHatHandler.h"
JoyHatHandler::JoyHatHandler(Uint8 direction) JoyHatHandler::JoyHatHandler(SDL_JoystickID joynum, Uint8 hatnum, Uint8 direction)
: direction_(direction) : joynum_(joynum)
, hatnum_(hatnum)
, direction_(direction)
, pressed_(false) , pressed_(false)
{ {
} }
@@ -13,7 +15,7 @@ void JoyHatHandler::reset()
bool JoyHatHandler::update(SDL_Event &e) 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_); pressed_ = (e.jhat.value == direction_);
return true; return true;

View File

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

View File

@@ -128,13 +128,20 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
else if(description.find("joy") == 0) else if(description.find("joy") == 0)
{ {
std::string joydesc = Utils::replace(Utils::toLower(description), "joy", ""); 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) if(joydesc.find("button") == 0)
{ {
unsigned int button; unsigned int button;
std::stringstream ss; std::stringstream ss;
ss << Utils::replace(joydesc, "button", ""); ss << Utils::replace(joydesc, "button", "");
ss >> 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() ); Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad button " + ss.str() );
return true; return true;
} }
@@ -143,6 +150,12 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
Uint8 hat; Uint8 hat;
joydesc = Utils::replace(joydesc, "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; if(joydesc == "leftup") hat = SDL_HAT_LEFTUP;
else if(joydesc == "left") hat = SDL_HAT_LEFT; else if(joydesc == "left") hat = SDL_HAT_LEFT;
else if(joydesc == "leftdown") hat = SDL_HAT_LEFTDOWN; 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 == "right") hat = SDL_HAT_RIGHT;
else if(joydesc == "rightdown") hat = SDL_HAT_RIGHTDOWN; 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 ); Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad hat " + joydesc );
return true; return true;
} }
@@ -184,7 +197,7 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
ss << joydesc; ss << joydesc;
ss >> axis; ss >> axis;
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad axis " + ss.str() ); 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; return true;
} }
} }