Make gamepad visualization work with SDL2

This commit is contained in:
Benau 2020-04-21 19:40:30 +08:00
parent 6a875e04b6
commit 32e3e4a504
3 changed files with 28 additions and 50 deletions

View File

@ -137,14 +137,16 @@ void InputManager::update(float dt)
auto& controller = m_sdl_controller.at(event.jaxis.which);
if (m_mode == INPUT_SENSE_GAMEPAD)
controller->handleAxisInputSense(event);
if (controller->handleAxis(event))
if (controller->handleAxis(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
case SDL_JOYHATMOTION:
{
auto& controller = m_sdl_controller.at(event.jhat.which);
if (controller->handleHat(event))
if (controller->handleHat(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
@ -152,7 +154,8 @@ void InputManager::update(float dt)
case SDL_JOYBUTTONDOWN:
{
auto& controller = m_sdl_controller.at(event.jbutton.which);
if (controller->handleButton(event))
if (controller->handleButton(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
@ -174,6 +177,15 @@ void InputManager::update(float dt)
}
}
//-----------------------------------------------------------------------------
#ifndef SERVER_ONLY
const irr::SEvent& InputManager::getEventForGamePad(unsigned i) const
{
auto it = m_sdl_controller.begin();
return std::next(it, i)->second->getEvent();
}
#endif
//-----------------------------------------------------------------------------
/** Destructor. Frees all data structures.
*/

View File

@ -113,6 +113,8 @@ public:
* disconnected gamepad will not be removed from device manager to allow
* re-plugging later with the same ID. */
size_t getGamepadCount() const { return m_sdl_controller.size(); }
/** Returns irrlicht joystick for gamepad visualization. */
const irr::SEvent& getEventForGamePad(unsigned i) const;
#endif
void dispatchInput(Input::InputType, int deviceID, int btnID,

View File

@ -274,58 +274,18 @@ void runUnitTests();
void gamepadVisualisation()
{
#ifndef SERVER_ONLY
core::array<SJoystickInfo> irrlicht_gamepads;
irr_driver->getDevice()->activateJoysticks(irrlicht_gamepads);
struct Gamepad
{
s16 m_axis[SEvent::SJoystickEvent::NUMBER_OF_AXES];
bool m_button_state[SEvent::SJoystickEvent::NUMBER_OF_BUTTONS];
};
input_manager = new InputManager();
#define GAMEPAD_COUNT 8 // const won't work
class EventReceiver : public IEventReceiver
{
public:
Gamepad m_gamepads[GAMEPAD_COUNT];
EventReceiver()
{
for (int n=0; n<GAMEPAD_COUNT; n++)
{
Gamepad& g = m_gamepads[n];
for (int i=0; i<SEvent::SJoystickEvent::NUMBER_OF_AXES; i++)
g.m_axis[i] = 0;
for (int i=0; i<SEvent::SJoystickEvent::NUMBER_OF_BUTTONS; i++)
g.m_button_state[i] = false;
}
}
virtual bool OnEvent (const irr::SEvent &event)
{
switch (event.EventType)
{
case EET_JOYSTICK_INPUT_EVENT :
{
const SEvent::SJoystickEvent& evt = event.JoystickEvent;
if (evt.Joystick >= GAMEPAD_COUNT) return true;
Gamepad& g = m_gamepads[evt.Joystick];
for (int i=0; i<SEvent::SJoystickEvent::NUMBER_OF_AXES;i++)
{
g.m_axis[i] = evt.Axis[i];
}
for (int i=0; i<SEvent::SJoystickEvent::NUMBER_OF_BUTTONS;
i++)
{
g.m_button_state[i] = evt.IsButtonPressed(i);
}
break;
}
case EET_KEY_INPUT_EVENT:
{
const SEvent::SKeyInput& evt = event.KeyInput;
@ -357,14 +317,17 @@ void gamepadVisualisation()
{
if (!irr_driver->getDevice()->run()) break;
input_manager->update(0);
video::IVideoDriver* driver = irr_driver->getVideoDriver();
const core::dimension2du size = driver ->getCurrentRenderTargetSize();
driver->beginScene(true, true, video::SColor(255,0,0,0));
for (int n=0; n<GAMEPAD_COUNT; n++)
for (unsigned n = 0; n < input_manager->getGamepadCount(); n++)
{
Gamepad& g = events->m_gamepads[n];
if (n >= GAMEPAD_COUNT)
break;
const irr::SEvent& g = input_manager->getEventForGamePad(n);
const int MARGIN = 10;
const int x = (n & 1 ? size.Width/2 + MARGIN : MARGIN );
@ -384,7 +347,7 @@ void gamepadVisualisation()
core::position2di pos(btn_x + b*BTN_SIZE, btn_y);
core::dimension2di size(BTN_SIZE, BTN_SIZE);
if (g.m_button_state[b])
if (g.JoystickEvent.IsButtonPressed(b))
{
driver->draw2DRectangle (video::SColor(255,255,0,0),
core::recti(pos, size));
@ -401,13 +364,13 @@ void gamepadVisualisation()
for (int a=0; a<SEvent::SJoystickEvent::NUMBER_OF_AXES; a++)
{
const float rate = g.m_axis[a] / 32767.0f;
const float rate = g.JoystickEvent.Axis[a] / 32767.0f;
core::position2di pos(axis_x, axis_y + a*axis_h);
core::dimension2di size(axis_w, axis_h);
// Assume a default deadzone value of 4096
const bool deadzone = (abs(g.m_axis[a]) < 4096);
const bool deadzone = (abs(g.JoystickEvent.Axis[a]) < 4096);
core::recti fillbar(core::position2di(axis_x + axis_w/2,
axis_y + a*axis_h),
@ -423,6 +386,7 @@ void gamepadVisualisation()
driver->endScene();
}
#endif
} // gamepadVisualisation
// ============================================================================