diff --git a/apps/OpenSpace/main.cpp b/apps/OpenSpace/main.cpp index 2683289563..5d88b905cc 100644 --- a/apps/OpenSpace/main.cpp +++ b/apps/OpenSpace/main.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -240,7 +241,7 @@ void checkJoystickStatus() { for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) { ZoneScopedN("Joystick state"); - JoystickInputState& state = global::joystickInputStates->at(i); + JoystickInputState& state = global::interactionHandler->joystickInputStates().at(i); const int present = glfwJoystickPresent(i); if (present == GLFW_FALSE) { diff --git a/include/openspace/engine/globals.h b/include/openspace/engine/globals.h index c90907cc11..066e2924f2 100644 --- a/include/openspace/engine/globals.h +++ b/include/openspace/engine/globals.h @@ -33,7 +33,6 @@ namespace ghoul::fontrendering { class FontManager; } namespace openspace { namespace interaction { - struct JoystickInputStates; class ActionManager; class InteractionHandler; class KeybindingManager; @@ -91,7 +90,6 @@ inline WindowDelegate* windowDelegate; inline Configuration* configuration; inline interaction::ActionManager* actionManager; inline interaction::InteractionHandler* interactionHandler; -inline interaction::JoystickInputStates* joystickInputStates; inline interaction::KeybindingManager* keybindingManager; inline interaction::KeyframeRecordingHandler* keyframeRecording; inline interaction::NavigationHandler* navigationHandler; diff --git a/include/openspace/interaction/interactionhandler.h b/include/openspace/interaction/interactionhandler.h index 2ba5b86105..2f13adda27 100644 --- a/include/openspace/interaction/interactionhandler.h +++ b/include/openspace/interaction/interactionhandler.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -56,6 +57,9 @@ public: WebsocketInputStates& websocketInputStates(); const WebsocketInputStates& websocketInputStates() const; + JoystickInputStates& joystickInputStates(); + const JoystickInputStates& joystickInputStates() const; + bool disabledKeybindings() const; bool disabledMouse() const; bool disabledJoystick() const; @@ -74,12 +78,13 @@ public: */ void markInteraction(); - void clearGlobalJoystickStates(); + void clearJoystickStates(); private: MouseInputState _mouseInputState; KeyboardInputState _keyboardInputState; WebsocketInputStates _websocketInputStates; + JoystickInputStates _joystickInputStates; // TODO: add joystick and websocket input state diff --git a/modules/imgui/src/guijoystickcomponent.cpp b/modules/imgui/src/guijoystickcomponent.cpp index d4505e8434..96ad639c0d 100644 --- a/modules/imgui/src/guijoystickcomponent.cpp +++ b/modules/imgui/src/guijoystickcomponent.cpp @@ -26,6 +26,7 @@ #include #include +#include #include namespace { @@ -50,8 +51,12 @@ void GuiJoystickComponent::render() { _isEnabled = v; _isCollapsed = ImGui::IsWindowCollapsed(); - for (size_t i = 0; i < global::joystickInputStates->size(); i++) { - const JoystickInputState& state = global::joystickInputStates->at(i); + JoystickInputStates joystickStates = + global::interactionHandler->joystickInputStates(); + + for (size_t i = 0; i < joystickStates.size(); i++) { + const JoystickInputState& state = joystickStates.at(i); + if (!state.isConnected) { continue; } @@ -86,8 +91,8 @@ void GuiJoystickComponent::render() { ImGui::Text("%s", "Summed contributions"); ImGui::Text("%s", "Axes"); - for (int i = 0; i < global::joystickInputStates->numAxes(); i++) { - float f = global::joystickInputStates->axis("", i); + for (int i = 0; i < joystickStates.numAxes(); i++) { + float f = joystickStates.axis("", i); const std::string id = std::to_string(i) + "##" + "TotalAxis"; ImGui::SliderFloat( id.c_str(), @@ -97,12 +102,12 @@ void GuiJoystickComponent::render() { ); } ImGui::Text("%s", "Buttons"); - for (int i = 0; i < global::joystickInputStates->numButtons(); i++) { + for (int i = 0; i < joystickStates.numButtons(); i++) { const std::string id = std::to_string(i) + "##" + "TotalButton"; ImGui::RadioButton( id.c_str(), - global::joystickInputStates->button("", i, JoystickAction::Press) || - global::joystickInputStates->button("", i, JoystickAction::Repeat) + joystickStates.button("", i, JoystickAction::Press) || + joystickStates.button("", i, JoystickAction::Repeat) ); } diff --git a/src/engine/globals.cpp b/src/engine/globals.cpp index 5463b6826a..5dc4dec06a 100644 --- a/src/engine/globals.cpp +++ b/src/engine/globals.cpp @@ -36,7 +36,6 @@ #include #include #include -#include #include #include #include @@ -94,7 +93,6 @@ namespace { sizeof(Configuration) + sizeof(interaction::ActionManager) + sizeof(interaction::InteractionHandler) + - sizeof(interaction::JoystickInputStates) + sizeof(interaction::KeybindingManager) + sizeof(interaction::KeyframeRecordingHandler) + sizeof(interaction::NavigationHandler) + @@ -300,14 +298,6 @@ void create() { interactionHandler = new interaction::InteractionHandler; #endif // WIN32 -#ifdef WIN32 - joystickInputStates = new (currentPos) interaction::JoystickInputStates; - ghoul_assert(joystickInputStates, "No joystickInputStates"); - currentPos += sizeof(interaction::JoystickInputStates); -#else // ^^^ WIN32 / !WIN32 vvv - joystickInputStates = new interaction::JoystickInputStates; -#endif // WIN32 - #ifdef WIN32 keybindingManager = new (currentPos) interaction::KeybindingManager; ghoul_assert(keybindingManager, "No keybindingManager"); @@ -484,13 +474,6 @@ void destroy() { delete keybindingManager; #endif // WIN32 - LDEBUGC("Globals", "Destroying 'JoystickInputStates'"); -#ifdef WIN32 - joystickInputStates->~JoystickInputStates(); -#else // ^^^ WIN32 / !WIN32 vvv - delete joystickInputStates; -#endif // WIN32 - LDEBUGC("Globals", "Destroying 'InteractionHandler'"); #ifdef WIN32 interactionHandler->~InteractionHandler(); diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 7813a78ad5..01fcca7ae6 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -102,7 +102,7 @@ InteractionHandler::InteractionHandler() _disableJoystickInputs.onChange([this]() { if (_disableJoystickInputs) { - clearGlobalJoystickStates(); + clearJoystickStates(); } }); @@ -144,6 +144,14 @@ const WebsocketInputStates& InteractionHandler::websocketInputStates() const { return _websocketInputStates; } +JoystickInputStates& InteractionHandler::joystickInputStates() { + return _joystickInputStates; +} + +const JoystickInputStates& InteractionHandler::joystickInputStates() const { + return _joystickInputStates; +} + bool InteractionHandler::disabledKeybindings() const { return _disableKeybindings; } @@ -230,10 +238,10 @@ void InteractionHandler::markInteraction() { _interactionMonitor.markInteraction(); } -void InteractionHandler::clearGlobalJoystickStates() { +void InteractionHandler::clearJoystickStates() { std::fill( - global::joystickInputStates->begin(), - global::joystickInputStates->end(), + _joystickInputStates.begin(), + _joystickInputStates.end(), JoystickInputState() ); } diff --git a/src/navigation/navigationhandler_lua.inl b/src/navigation/navigationhandler_lua.inl index 34fb14606b..5614585b5a 100644 --- a/src/navigation/navigationhandler_lua.inl +++ b/src/navigation/navigationhandler_lua.inl @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -643,10 +644,13 @@ struct [[codegen::Dictionary(JoystickAxis)]] JoystickAxis { */ [[codegen::luawrap]] std::vector listAllJoysticks() { using namespace openspace; - std::vector result; - result.reserve(global::joystickInputStates->size()); - for (const interaction::JoystickInputState& state : *global::joystickInputStates) { + interaction::JoystickInputStates joysticks = + global::interactionHandler->joystickInputStates(); + + std::vector result; + result.reserve(joysticks.size()); + for (const interaction::JoystickInputState& state : joysticks) { if (!state.name.empty()) { result.push_back(state.name); } diff --git a/src/navigation/orbitalnavigator/joystickcamerastates.cpp b/src/navigation/orbitalnavigator/joystickcamerastates.cpp index d23e97906e..ff40edb25b 100644 --- a/src/navigation/orbitalnavigator/joystickcamerastates.cpp +++ b/src/navigation/orbitalnavigator/joystickcamerastates.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -182,7 +183,7 @@ void JoystickCameraStates::updateStateFromInput( for (int i = 0; i < nButtons; i++) { auto itRange = joystick->buttonMapping.equal_range(i); for (auto it = itRange.first; it != itRange.second; it++) { - const bool active = global::joystickInputStates->button( + const bool active = global::interactionHandler->joystickInputStates().button( joystickInputState.name, i, it->second.action @@ -267,7 +268,7 @@ void JoystickCameraStates::setAxisMapping(const std::string& joystickName, joystickMapping->axisMapping[axis].sensitivity = sensitivity; joystickMapping->prevAxisValues[axis] = - global::joystickInputStates->axis(joystickName, axis); + global::interactionHandler->joystickInputStates().axis(joystickName, axis); } void JoystickCameraStates::setAxisMappingProperty(const std::string& joystickName, @@ -296,7 +297,7 @@ void JoystickCameraStates::setAxisMappingProperty(const std::string& joystickNam joystickMapping->axisMapping[axis].isRemote = isRemote; joystickMapping->prevAxisValues[axis] = - global::joystickInputStates->axis(joystickName, axis); + global::interactionHandler->joystickInputStates().axis(joystickName, axis); } JoystickCameraStates::AxisInformation JoystickCameraStates::axisMapping( @@ -320,18 +321,18 @@ JoystickCameraStates::AxisInformation JoystickCameraStates::axisMapping( void JoystickCameraStates::setDeadzone(const std::string& joystickName, int axis, float deadzone) { - Joystick* joystickMapping = findOrAddJoystickMapping(joystickName); - if (!joystickMapping) { + Joystick* joystick = findOrAddJoystickMapping(joystickName); + if (!joystick) { return; } // If the axis index is too big for the vector then resize it to have room - if (axis >= static_cast(joystickMapping->axisMapping.size())) { - joystickMapping->axisMapping.resize(axis + 1); - joystickMapping->prevAxisValues.resize(axis + 1); + if (axis >= static_cast(joystick->axisMapping.size())) { + joystick->axisMapping.resize(axis + 1); + joystick->prevAxisValues.resize(axis + 1); } - joystickMapping->axisMapping[axis].deadzone = deadzone; + joystick->axisMapping[axis].deadzone = deadzone; } float JoystickCameraStates::deadzone(const std::string& joystickName, int axis) const { diff --git a/src/navigation/orbitalnavigator/orbitalinputhandler.cpp b/src/navigation/orbitalnavigator/orbitalinputhandler.cpp index 4c0578a878..c26d24716a 100644 --- a/src/navigation/orbitalnavigator/orbitalinputhandler.cpp +++ b/src/navigation/orbitalnavigator/orbitalinputhandler.cpp @@ -184,8 +184,17 @@ void OrbitalInputHandler::updateStatesFromInput(double deltaTime) { global::interactionHandler->keyboardInputState(), deltaTime ); - _joystickStates.updateStateFromInput(*global::joystickInputStates, deltaTime); - _websocketStates.updateStateFromInput(global::interactionHandler->websocketInputStates(), deltaTime); + + _joystickStates.updateStateFromInput( + global::interactionHandler->joystickInputStates(), + deltaTime + ); + + _websocketStates.updateStateFromInput( + global::interactionHandler->websocketInputStates(), + deltaTime + ); + _scriptStates.updateStateFromInput(deltaTime); }