From 59ca4f301ad46636a8d8238673e2771abdca3f68 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Mon, 23 Jul 2012 12:27:38 -0500 Subject: [PATCH] Axis support at last! --- src/InputManager.cpp | 45 ++++++++++++++++++++++++++++--- src/InputManager.h | 5 ++-- src/components/GuiInputConfig.cpp | 29 +++++++++++++++----- src/components/GuiInputConfig.h | 4 ++- src/main.cpp | 1 + 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/InputManager.cpp b/src/InputManager.cpp index e2c58e92a..b7637ddf9 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -7,7 +7,8 @@ std::vector InputManager::inputVector; SDL_Event* InputManager::lastEvent = NULL; -std::map InputManager::joystickButtonMap, InputManager::joystickAxisMap; +std::map InputManager::joystickButtonMap, InputManager::joystickAxisPosMap, InputManager::joystickAxisNegMap; +std::map 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; diff --git a/src/InputManager.h b/src/InputManager.h index 9e1be2be3..a39a4ecbf 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -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 joystickButtonMap; - extern std::map joystickAxisMap; + extern std::map joystickAxisPosMap, joystickAxisNegMap; + extern std::map axisState; } #endif diff --git a/src/components/GuiInputConfig.cpp b/src/components/GuiInputConfig.cpp index fadd36980..17e6dd823 100644 --- a/src/components/GuiInputConfig.cpp +++ b/src/components/GuiInputConfig.cpp @@ -4,13 +4,14 @@ #include 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"; + } } diff --git a/src/components/GuiInputConfig.h b/src/components/GuiInputConfig.h index 0e0624614..7900750f6 100644 --- a/src/components/GuiInputConfig.h +++ b/src/components/GuiInputConfig.h @@ -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 mButtonMap; - std::map mAxisMap; + std::map mAxisPosMap; + std::map mAxisNegMap; void writeConfig(std::string path); }; diff --git a/src/main.cpp b/src/main.cpp index 7e1c460f6..7a3e2f13f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,6 +52,7 @@ int main() { switch(event.type) { + case SDL_JOYAXISMOTION: case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: case SDL_KEYDOWN: