You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB
C++

#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<OIS::JoyStick*>(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<bool> &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;
}