Skip to content

Commit

Permalink
✉️ MSG_APP_REINIT_INPUT_REQUESTED
Browse files Browse the repository at this point in the history
Changing "input grab mode" in GameSettings UI no longer needs a game restart.
  • Loading branch information
ohlidalp committed Aug 29, 2024
1 parent 56c0f4f commit 012c38a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/angelscript/Script2Game/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ enum MsgType
MSG_APP_MODCACHE_PURGE_REQUESTED, //!< Request cleanup and full rebuild of mod cache.
MSG_APP_LOAD_SCRIPT_REQUESTED, //!< Request loading a script from resource(file) or memory; Params 'filename' (string)/'buffer'(string - has precedence over filename), 'category' (ScriptCategory), 'associated_actor' (int - only for SCRIPT_CATEGORY_ACTOR)
MSG_APP_UNLOAD_SCRIPT_REQUESTED, //!< Request unloading a script; Param 'id' (int - the ID of the script unit, see 'Script Monitor' tab in console UI.)
MSG_APP_REINIT_INPUT_REQUESTED, //!< Request restarting the entire input subsystem (mouse, keyboard, controllers) including reloading input mappings. Use with caution.
// Networking
MSG_NET_CONNECT_REQUESTED, //!< Request connection to multiplayer server specified by cvars 'mp_server_host, mp_server_port, mp_server_password'. No params.
MSG_NET_CONNECT_STARTED, //!< Networking notification, DO NOT PUSH MANUALLY.
Expand Down
7 changes: 7 additions & 0 deletions source/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ void DestroyOverlayWrapper()
g_overlay_wrapper = nullptr;
}

void DestroyInputEngine()
{
delete g_input_engine;
g_input_engine = nullptr;
}

} // namespace App

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -562,6 +568,7 @@ const char* MsgTypeToString(MsgType type)
case MSG_APP_LOAD_SCRIPT_REQUESTED : return "MSG_APP_LOAD_SCRIPT_REQUESTED";
case MSG_APP_UNLOAD_SCRIPT_REQUESTED : return "MSG_APP_UNLOAD_SCRIPT_REQUESTED";
case MSG_APP_SCRIPT_THREAD_STATUS : return "MSG_APP_SCRIPT_THREAD_STATUS";
case MSG_APP_REINIT_INPUT_REQUESTED : return "MSG_APP_REINIT_INPUT_REQUESTED";

case MSG_NET_CONNECT_REQUESTED : return "MSG_NET_CONNECT_REQUESTED";
case MSG_NET_CONNECT_STARTED : return "MSG_NET_CONNECT_STARTED";
Expand Down
5 changes: 2 additions & 3 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ enum MsgType
MSG_APP_LOAD_SCRIPT_REQUESTED, //!< Payload = RoR::LoadScriptRequest* (owner)
MSG_APP_UNLOAD_SCRIPT_REQUESTED, //!< Payload = RoR::ScriptUnitId_t* (owner)
MSG_APP_SCRIPT_THREAD_STATUS, //!< Payload = RoR::ScriptEventArgs* (owner)
MSG_APP_REINIT_INPUT_REQUESTED,
// Networking
MSG_NET_CONNECT_REQUESTED,
MSG_NET_CONNECT_STARTED,
Expand Down Expand Up @@ -533,11 +534,9 @@ void CreateGfxScene();
void CreateSoundScriptManager();
void CreateScriptEngine();

// Setters
void SetCacheSystem (CacheSystem* obj);

// Cleanups
void DestroyOverlayWrapper();
void DestroyInputEngine();

} // namespace App

Expand Down
12 changes: 10 additions & 2 deletions source/main/gui/panels/GUI_GameSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,16 @@ void GameSettings::DrawControlSettings()
{
ImGui::TextDisabled("%s", _LC("GameSettings", "Controller options"));

DrawGCombo(App::io_input_grab_mode, _LC("GameSettings", "Input grab mode"),
m_combo_items_input_grab.c_str());
IoInputGrabMode io_input_grab_mode_old = App::io_input_grab_mode->getEnum<IoInputGrabMode>();
DrawGCombo(App::io_input_grab_mode, _LC("GameSettings", "Input grab mode"), m_combo_items_input_grab.c_str());
if (io_input_grab_mode_old != App::io_input_grab_mode->getEnum<IoInputGrabMode>())
{
App::GetGameContext()->PushMessage(Message(MSG_APP_REINIT_INPUT_REQUESTED));
// This may take a second - display a 'please wait' box
App::GetGuiManager()->LoadingWindow.SetProgress(
App::GetGuiManager()->LoadingWindow.PERC_HIDE_PROGRESSBAR,
_LC("GameSettings", "Restarting input subsystem, please wait..."), /*render_frame:*/false);
}

DrawGFloatSlider(App::io_analog_smoothing, _LC("GameSettings", "Analog Input Smoothing"), 0.5f, 2.0f);
DrawGFloatSlider(App::io_analog_sensitivity, _LC("GameSettings", "Analog Input Sensitivity"), 0.5f, 2.0f);
Expand Down
17 changes: 17 additions & 0 deletions source/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,23 @@ int main(int argc, char *argv[])
break;
}

case MSG_APP_REINIT_INPUT_REQUESTED:
{
try
{
LOG(fmt::format("[RoR] !! Reinitializing input engine !!"));
App::DestroyInputEngine();
App::GetAppContext()->SetUpInput();
LOG(fmt::format("[RoR] DONE Reinitializing input engine."));
App::GetGuiManager()->LoadingWindow.SetVisible(false); // Shown by `GUI::GameSettings` when changing 'grab mode'
}
catch (...)
{
HandleMsgQueueException(m.type);
}
break;
}

// -- Network events --

case MSG_NET_CONNECT_REQUESTED:
Expand Down
1 change: 1 addition & 0 deletions source/main/scripting/bindings/MsgQueueAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void RoR::RegisterMessageQueue(asIScriptEngine* engine)
result = engine->RegisterEnumValue("MsgType", "MSG_APP_LOAD_SCRIPT_REQUESTED", MSG_APP_LOAD_SCRIPT_REQUESTED); ROR_ASSERT(result >= 0);
result = engine->RegisterEnumValue("MsgType", "MSG_APP_UNLOAD_SCRIPT_REQUESTED", MSG_APP_UNLOAD_SCRIPT_REQUESTED); ROR_ASSERT(result >= 0);
result = engine->RegisterEnumValue("MsgType", "MSG_APP_SCRIPT_THREAD_STATUS", MSG_APP_SCRIPT_THREAD_STATUS); ROR_ASSERT(result >= 0);
result = engine->RegisterEnumValue("MsgType", "MSG_APP_REINIT_INPUT_REQUESTED", MSG_APP_REINIT_INPUT_REQUESTED); ROR_ASSERT(result >= 0);
// Networking
result = engine->RegisterEnumValue("MsgType", "MSG_NET_CONNECT_REQUESTED", MSG_NET_CONNECT_REQUESTED); ROR_ASSERT(result >= 0);
result = engine->RegisterEnumValue("MsgType", "MSG_NET_CONNECT_STARTED", MSG_NET_CONNECT_STARTED); ROR_ASSERT(result >= 0);
Expand Down
4 changes: 4 additions & 0 deletions source/main/utils/InputEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ void InputEngine::setup()
{
ShowCursor(FALSE);
}
else
{
ShowCursor(TRUE); // To make `MSG_APP_REINIT_INPUT_REQUESTED` work correctly
}
#endif

mInputManager = OIS::InputManager::createInputSystem(pl);
Expand Down

0 comments on commit 012c38a

Please sign in to comment.