From 32e3e4a504183a06a46a848710b042543dc66e05 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 21 Apr 2020 19:40:30 +0800 Subject: [PATCH] Make gamepad visualization work with SDL2 --- src/input/input_manager.cpp | 18 ++++++++++-- src/input/input_manager.hpp | 2 ++ src/main.cpp | 58 +++++++------------------------------ 3 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index 912847f52..8f0c53232 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -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. */ diff --git a/src/input/input_manager.hpp b/src/input/input_manager.hpp index 69302423b..4771aac3c 100644 --- a/src/input/input_manager.hpp +++ b/src/input/input_manager.hpp @@ -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, diff --git a/src/main.cpp b/src/main.cpp index 92f791b64..829f492c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -274,58 +274,18 @@ void runUnitTests(); void gamepadVisualisation() { +#ifndef SERVER_ONLY - core::array 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) return true; - - Gamepad& g = m_gamepads[evt.Joystick]; - for (int i=0; igetDevice()->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; ngetGamepadCount(); 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; aendScene(); } +#endif } // gamepadVisualisation // ============================================================================