#include "stdafx.h" #include "JoyStickListener.h" using namespace JSParser; JoyStickListener::JoyStickListener() :m_pInputMgr(NULL) ,m_pJoystickMgr(NULL) { } JoyStickListener::~JoyStickListener() { m_pInputMgr->destroyInputObject(m_pJoystickMgr); OIS::InputManager::destroyInputSystem(m_pInputMgr); } void JoyStickListener::init(HWND hWnd) { OIS::ParamList pl; //HWND _hWnd = AfxGetMainWnd()->GetSafeHwnd(); std::ostringstream wnd; wnd << (size_t)hWnd; pl.insert(std::make_pair(std::string("WINDOW"), wnd.str())); pl.insert(std::make_pair(std::string("w32_joystick"), std::string("DISCL_BACKGROUND"))); //后台模式 pl.insert(std::make_pair(std::string("w32_joystick"), std::string("DISCL_NONEXCLUSIVE")));//非独占模式 try { m_pInputMgr = OIS::InputManager::createInputSystem( pl ); } catch (...) { std::cout << "Fail to initialize InputSystem......" << std::endl; } if (m_pInputMgr != NULL) { try { //布尔值表示(非)缓冲模式 m_pJoystickMgr = static_cast(m_pInputMgr->createInputObject( OIS::OISJoyStick, true )); } catch(...) { m_pJoystickMgr = NULL; std::cout << "Fail to initialize Joystick......" << std::endl; } if (m_pJoystickMgr != NULL) { //设置摇杆消息监听 m_pJoystickMgr->setEventCallback(this); m_pJoyStickState = m_pJoystickMgr->getJoyStickState(); m_pJoyStickState.clear(); } } } void JoyStickListener::UpdateStick(float &a,float &e) { m_pJoystickMgr->capture(); m_pJoyStickState = m_pJoystickMgr->getJoyStickState(); a = double((m_pJoyStickState.mAxes[0]).abs) / OIS::JoyStick::MAX_AXIS;//纵向 e = double((m_pJoyStickState.mAxes[1]).abs) / OIS::JoyStick::MAX_AXIS;//横向 } void JoyStickListener::UpdateButtons(std::vector &buttons) { m_pJoystickMgr->capture(); m_pJoyStickState = m_pJoystickMgr->getJoyStickState(); buttons = m_pJoyStickState.mButtons; } bool JoyStickListener::buttonPressed( const OIS::JoyStickEvent &arg, int button ) { std::cout << "Button Pressed: " << button << std::endl; return true; } bool JoyStickListener::buttonReleased( const OIS::JoyStickEvent &arg, int button ) { std::cout << "Button Released: " << button << std::endl; return true; } bool JoyStickListener::axisMoved( const OIS::JoyStickEvent &arg, int axis ) { std::cout << "Joystick Axis[" << axis << "] Moved(Abs): " << arg.state.mAxes[axis].abs << std::endl; return true; } bool JoyStickListener::povMoved( const OIS::JoyStickEvent &arg, int pov ) { std::cout << "Pov[" << pov << "] Moved Direction: " << arg.state.mPOV[pov].direction << std::endl; std::cout << "Pov Moved to "; if(arg.state.mPOV[pov].direction & OIS::Pov::Centered) std::cout << "Centered"; else if(arg.state.mPOV[pov].direction & OIS::Pov::East) std::cout << "East"; else if(arg.state.mPOV[pov].direction & OIS::Pov::South) std::cout << "South"; else if(arg.state.mPOV[pov].direction & OIS::Pov::West) std::cout << "West"; else if(arg.state.mPOV[pov].direction & OIS::Pov::North) std::cout << "North"; else if(arg.state.mPOV[pov].direction & OIS::Pov::NorthEast) std::cout << "NorthEast"; else if(arg.state.mPOV[pov].direction & OIS::Pov::NorthWest) std::cout << "NorthWest"; else if(arg.state.mPOV[pov].direction & OIS::Pov::SouthEast) std::cout << "SouthEast"; else if(arg.state.mPOV[pov].direction & OIS::Pov::SouthWest) std::cout << "SouthWest"; std::cout << std::endl; return true; }