Axis support at last!

This commit is contained in:
Aloshi 2012-07-23 12:27:38 -05:00
parent 646cda75a6
commit 59ca4f301a
5 changed files with 70 additions and 14 deletions

View file

@ -7,7 +7,8 @@
std::vector<GuiComponent*> InputManager::inputVector;
SDL_Event* InputManager::lastEvent = NULL;
std::map<int, InputManager::InputButton> InputManager::joystickButtonMap, InputManager::joystickAxisMap;
std::map<int, InputManager::InputButton> InputManager::joystickButtonMap, InputManager::joystickAxisPosMap, InputManager::joystickAxisNegMap;
std::map<int, int> InputManager::axisState;
int InputManager::deadzone = 32000;
@ -95,6 +96,38 @@ void InputManager::processEvent(SDL_Event* event)
button = LEFT;
if(event->jhat.value & SDL_HAT_RIGHT)
button = RIGHT;
}else{
if(event->type == SDL_JOYAXISMOTION)
{
int axis = event->jaxis.axis;
int value = event->jaxis.value;
//if this axis was previously not centered, it can only keyUp
if(axisState[axis] != 0)
{
if(abs(value) < deadzone) //if it has indeed centered
{
if(axisState[axis] > 0)
button = joystickAxisPosMap[axis];
else
button = joystickAxisNegMap[axis];
axisState[axis] = 0;
}
}else{
if(value > deadzone)
{
//axisPos keyDown
axisState[axis] = 1;
keyDown = true;
button = joystickAxisPosMap[axis];
}else if(value < -deadzone)
{
axisState[axis] = -1;
keyDown = true;
button = joystickAxisNegMap[axis];
}
}
}
}
}
}
@ -109,7 +142,8 @@ void InputManager::loadConfig(std::string path)
{
//clear any old config
joystickButtonMap.clear();
joystickAxisMap.clear();
joystickAxisPosMap.clear();
joystickAxisNegMap.clear();
std::ifstream file(path.c_str());
@ -143,9 +177,12 @@ void InputManager::loadConfig(std::string path)
if(token[0] == "BUTTON")
{
joystickButtonMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str());
}else if(token[0] == "AXIS")
}else if(token[0] == "AXISPOS")
{
joystickAxisMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str());
joystickAxisPosMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str());
}else if(token[0] == "AXISNEG")
{
joystickAxisNegMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str());
}else{
std::cerr << "Invalid input type - " << token[0] << "\n";
return;

View file

@ -15,7 +15,7 @@ namespace InputManager {
void loadConfig(std::string path);
//enum for identifying input, regardless of configuration
enum InputButton { UP, DOWN, LEFT, RIGHT, BUTTON1, BUTTON2, UNKNOWN};
enum InputButton { UNKNOWN, UP, DOWN, LEFT, RIGHT, BUTTON1, BUTTON2 };
void processEvent(SDL_Event* event);
@ -24,7 +24,8 @@ namespace InputManager {
extern int deadzone;
extern std::map<int, InputButton> joystickButtonMap;
extern std::map<int, InputButton> joystickAxisMap;
extern std::map<int, InputButton> joystickAxisPosMap, joystickAxisNegMap;
extern std::map<int, int> axisState;
}
#endif

View file

@ -4,13 +4,14 @@
#include <fstream>
std::string GuiInputConfig::sConfigPath = "./input.cfg";
std::string GuiInputConfig::sInputs[] = { "UP", "DOWN", "LEFT", "RIGHT", "BUTTON1", "BUTTON2" };
std::string GuiInputConfig::sInputs[] = { "UNKNOWN", "UP", "DOWN", "LEFT", "RIGHT", "BUTTON1", "BUTTON2" }; //must be same order as InputManager::InputButton enum
int GuiInputConfig::sInputCount = 6;
GuiInputConfig::GuiInputConfig()
{
mInputNum = 0;
mInputNum = 1;
mDone = false;
mLastAxis = -1;
Renderer::registerComponent(this);
InputManager::registerComponent(this);
@ -71,16 +72,26 @@ void GuiInputConfig::onInput(InputManager::InputButton button, bool keyDown)
if(event->type == SDL_JOYAXISMOTION)
{
std::cout << "motion on axis " << event->jaxis.axis << " to value " << event->jaxis.value << "\n";
//std::cout << "motion on axis " << event->jaxis.axis << " to value " << event->jaxis.value << "\n";
if(event->jaxis.axis == mLastAxis)
{
if(event->jaxis.value < InputManager::deadzone && event->jaxis.value > -InputManager::deadzone)
mLastAxis = -1;
return;
}
if(event->jaxis.value > InputManager::deadzone)
{
mAxisMap[event->jaxis.axis] = (InputManager::InputButton)mInputNum;
mAxisPosMap[event->jaxis.axis] = (InputManager::InputButton)mInputNum;
mInputNum++;
mLastAxis = event->jaxis.axis;
std::cout << " Mapping " << sInputs[mInputNum] << " to axis+ " << mLastAxis << "\n";
}else if(event->jaxis.value < -InputManager::deadzone)
{
mAxisMap[-event->jaxis.axis] = (InputManager::InputButton)mInputNum;
mAxisNegMap[event->jaxis.axis] = (InputManager::InputButton)mInputNum;
mInputNum++;
mLastAxis = event->jaxis.axis;
std::cout << " Mapping " << sInputs[mInputNum] << " to axis- " << mLastAxis << "\n";
}
}
@ -101,9 +112,13 @@ void GuiInputConfig::writeConfig(std::string path)
file << "BUTTON " << iter->first << " " << iter->second << "\n";
}
for(it_type iter = mAxisMap.begin(); iter != mAxisMap.end(); iter++)
for(it_type iter = mAxisPosMap.begin(); iter != mAxisPosMap.end(); iter++)
{
file << "AXIS " << iter->first << " " << iter->second << "\n";
file << "AXISPOS " << iter->first << " " << iter->second << "\n";
}
for(it_type iter = mAxisNegMap.begin(); iter != mAxisNegMap.end(); iter++)
{
file << "AXISNEG " << iter->first << " " << iter->second << "\n";
}
}

View file

@ -16,13 +16,15 @@ public:
private:
bool mDone;
int mInputNum;
int mLastAxis;
SDL_Joystick* mJoystick;
static std::string sInputs[];
static int sInputCount;
static std::string sConfigPath;
std::map<int, InputManager::InputButton> mButtonMap;
std::map<int, InputManager::InputButton> mAxisMap;
std::map<int, InputManager::InputButton> mAxisPosMap;
std::map<int, InputManager::InputButton> mAxisNegMap;
void writeConfig(std::string path);
};

View file

@ -52,6 +52,7 @@ int main()
{
switch(event.type)
{
case SDL_JOYAXISMOTION:
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
case SDL_KEYDOWN: