Skip to content

Commit

Permalink
Lock game input when "Lock" is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
Falki-git committed Sep 3, 2023
1 parent 1e1fb0e commit 7254eff
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
80 changes: 80 additions & 0 deletions src/UI/Widgets/TimeScaleWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,85 @@ void OnPauseButtonClicked()
lockBtn.ButtonText.text = locked ? "Unlock" : "Lock";
}

// Enable or disable game input
public void LockInput()
{
// to enable game input => KSP.Game.GameManager.Instance.Game.Input.Enable();
// to disable game input => KSP.Game.GameManager.Instance.Game.Input.Disable();
// is game input enabled => KSP.Game.GameManager.Instance.Game.Input.m_Global.enabled

// Get the currently executing assembly (your code).
Assembly currentAssembly = Assembly.GetExecutingAssembly();

// Find the "Assembly-CSharp" assembly among the loaded assemblies.
Assembly targetAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(assembly => assembly.GetName().Name == "Assembly-CSharp");

Type gameManagerType = targetAssembly.GetType("KSP.Game.GameManager");


if (gameManagerType != null)
{
// Get the "Instance" property from KSP.Game.GameManager
PropertyInfo instanceProperty = gameManagerType.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static);

if (instanceProperty != null)
{
// Get the value of the "Instance" property (an instance of KSP.Game.GameManager).
object gameManagerInstance = instanceProperty.GetValue(null, null);

// Get the "Game" property from the GameManager instance.
PropertyInfo gameProperty = gameManagerType.GetProperty("Game");

if (gameProperty != null)
{
// Get the value of the "Game" property (an instance of whatever type "Game" is).
object gameInstance = gameProperty.GetValue(gameManagerInstance, null);

// Get the "Input" property.
PropertyInfo inputProperty = gameInstance.GetType().GetProperty("Input");

if (inputProperty != null)
{
// Get the value of the "Input" property (an instance of whatever type "Input" is).
object inputInstance = inputProperty.GetValue(gameInstance, null);

//PropertyInfo globalProperty = inputInstance.GetType().GetProperty("m_Global", BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo globalField = inputInstance.GetType().GetField("m_Global", BindingFlags.NonPublic | BindingFlags.Instance);

if (globalField != null)
{
object globalInstance = globalField.GetValue(inputInstance);

PropertyInfo enabledProperty = globalInstance.GetType().GetProperty("enabled");

if (enabledProperty != null)
{
bool enabled = (bool)enabledProperty.GetValue(globalInstance, null);

// Get the Enable and Disable methods
MethodInfo enableMethod = inputInstance.GetType().GetMethod("Enable");
MethodInfo disableMethod = inputInstance.GetType().GetMethod("Disable");

/// If it's needed to lock gameinput depending on current value =>
//if (enabled)
// disableMethod?.Invoke(inputInstance, null);
//else
// enableMethod?.Invoke(inputInstance, null);

if (locked)
disableMethod?.Invoke(inputInstance, null);
else
enableMethod?.Invoke(inputInstance, null);

}
}
}
}
}
}
}

// UI Construction

void ConstructUI(GameObject parent)
Expand All @@ -85,6 +164,7 @@ void ConstructUI(GameObject parent)
lockBtn = UIFactory.CreateButton(parent, "PauseButton", "Lock", new Color(0.2f, 0.2f, 0.2f));
UIFactory.SetLayoutElement(lockBtn.Component.gameObject, minHeight: 25, minWidth: 50);
lockBtn.OnClick += OnPauseButtonClicked;
lockBtn.OnClick += LockInput;
}

// Only allow Time.timeScale to be set if the user hasn't "locked" it or if we are setting the value internally.
Expand Down
6 changes: 3 additions & 3 deletions src/UnityExplorer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnityExplorer", "UnityExplo
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release_BIE_Cpp|Any CPU = Release_BIE_Cpp|Any CPU
Release_BIE_CoreCLR|Any CPU = Release_BIE_CoreCLR|Any CPU
Release_BIE_Cpp|Any CPU = Release_BIE_Cpp|Any CPU
Release_BIE5_Mono|Any CPU = Release_BIE5_Mono|Any CPU
Release_BIE6_Mono|Any CPU = Release_BIE6_Mono|Any CPU
Release_ML_Cpp_net472|Any CPU = Release_ML_Cpp_net472|Any CPU
Expand All @@ -18,10 +18,10 @@ Global
Release_STANDALONE_Mono|Any CPU = Release_STANDALONE_Mono|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.ActiveCfg = BIE5_Mono|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.Build.0 = BIE5_Mono|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_Cpp|Any CPU.ActiveCfg = BIE_Cpp|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_Cpp|Any CPU.Build.0 = BIE_Cpp|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.ActiveCfg = BIE_Cpp_CoreCLR|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.Build.0 = BIE_Cpp_CoreCLR|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE5_Mono|Any CPU.ActiveCfg = BIE5_Mono|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE5_Mono|Any CPU.Build.0 = BIE5_Mono|Any CPU
{B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE6_Mono|Any CPU.ActiveCfg = BIE6_Mono|Any CPU
Expand Down

0 comments on commit 7254eff

Please sign in to comment.