Jump to content

[Fixed] Stopping click through a GUI window. (Part menu in flight and Editor)


Recommended Posts

Many modders know of the very annoying issue where a user clicks on a part underneath their mod's windows, and end up activating something. Personal worst event was a decouple, ending in my carrier releasing its main engine, while on intercept with Moho...

The following have been tested to work in 0.23.5


Flight click through: (Part menu)

Put this snippet at the end of your OnGUI()


// Hide part rightclick menu.
if (!GUIUtility.hotControl.IsNull())
{
if (windowPosition.Contains(Input.mousePosition) && GUIUtility.hotControl == 0)
{
foreach (var window in GameObject.FindObjectsOfType(typeof(UIPartActionWindow))
.OfType<UIPartActionWindow>().Where(p => p.Display == UIPartActionWindow.DisplayType.Selected))
{
window.enabled = false;
window.displayDirty = true;
}
}
}

The way it works is by first determining if the mouse' current position is within the bounds of the window and that the mouse is not currently clicking. If true, it will cycle through all active UIPartActionWindows and set each to disabled and refresh it.

Notes:

If you don't destroy your windows when closing them, you will need an if statement before this, to return. ie: if(windowIsHidden) return;

There can seem to be some flickering on the parts window, however it is only cosmetic. This happens because the part's window is rendered before the mod's, therefor it will open, and then the mod will tell it to close again.


if (windowPosition.Contains(Input.mousePosition) && GUIUtility.hotControl == 0 && Input.GetMouseButton(0))
{
...
}

If you dont like the flickering, you can change the initial if statement with the code above to change the behaviour of the code. Instead of hiding the window, it will simply not allow the user to left click on any part window buttons. However, they will still open.


Editor Click through:

Put this snippet in your OnGUI()


// Lock editor while mouse is over the window.
if (windowPosition.Contains(Input.mousePosition))
{
EditorTooltip.Instance.HideToolTip();
EditorLogic.fetch.Lock(false, false, false, "UniqueLock");
}
else if (!windowPosition.Contains(Input.mousePosition))
{
EditorLogic.fetch.Unlock("UniqueLock");
}

This basically locks the editor when the mouse moves over the window, and unlocks it again when the cursor leaves the window. However, this can at times be carried over into the Flight view, and so you must use the code below to make sure that parts are unlocked when the window is destroyed.

Put this snippet in your OnDestroy()


EditorLogic.fetch.Unlock("UniqueLock");

Finally, put this snippet in your Awake()


InputLockManager.RemoveControlLock("UniqueLock");

Important!: Change the string "UniqueLock" to something unique of your mod. This is to prevent other mods from messing with yours. If they happen to use the same approach.


Any comments, bugs or alternative scripts are welcome.

Edited by Yilmas
Link to comment
Share on other sites

Important!: Change the string "UniqueLock" to something unique of your mod. This is to prevent other mods from messing with yours. If they happen to use the same approach.

Also, it helps to identify stray input locks in the debug window (Alt+F12.)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...