Skip to content

Commit

Permalink
Fixes #2568 - Unity's IMGUI works better without KSP locking.
Browse files Browse the repository at this point in the history
KSP's control locking as a way to disable keypresses from
doing things to the game isn't needed when the window is a
Unity IMGUI window that handles keyboard focus itself
anyway, so this gives an option for it to disable that part of
KOSManagedWindow.
  • Loading branch information
Dunbaratu committed Jul 28, 2019
1 parent dd28b61 commit 62afd61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/kOS/Screen/GUIWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public bool IsForShared(SharedObjects s)

public void Awake()
{

// Transparent - leave the widget inside it to draw background if it wants to.
style = new GUIStyle(HighLogic.Skin.window);
style.normal.background = null;
Expand All @@ -66,6 +67,10 @@ public void Awake()

GameEvents.onHideUI.Add (OnHideUI);
GameEvents.onShowUI.Add (OnShowUI);

// Fixes #2568 - Unity IMGUI does its own individual input locking per field that needs it,
// so don't use KSP's more high-level control locking:
OptOutOfControlLocking = true;
}

public void OnDestroy()
Expand Down
16 changes: 16 additions & 0 deletions src/kOS/Screen/KOSManagedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public abstract class KOSManagedWindow : MonoBehaviour

private string lockIdName;

private bool optOutOfControlLocking;
/// <summary>
/// Fixes #2568 - If the window is one where Unity can handle doing the keyboard focus
/// properly itself, like a Unity IMGUI window, then it should set this to true so it
/// will avoid using KSP's more high level control locking scheme whcih is a bit flaky at times:
/// </summary>
public bool OptOutOfControlLocking
{
get { return optOutOfControlLocking; }
set { optOutOfControlLocking = value; if (value) InputLockManager.RemoveControlLock(lockIdName); }
}

protected KOSManagedWindow(string lockIdName = "")
{
// multiply by 50 so there's a range for future expansion for other GUI objects inside the window:
Expand Down Expand Up @@ -144,6 +156,8 @@ protected Vector2 MouseButtonDownPosRelative
/// </summary>
public virtual void GetFocus()
{
if (OptOutOfControlLocking)
return;
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneHasPlanetarium)
InputLockManager.SetControlLock(ControlTypes.ALLBUTCAMERAS, lockIdName);
}
Expand All @@ -156,6 +170,8 @@ public virtual void GetFocus()
/// </summary>
public virtual void LoseFocus()
{
if (OptOutOfControlLocking)
return;
InputLockManager.RemoveControlLock(lockIdName);
}

Expand Down

0 comments on commit 62afd61

Please sign in to comment.