Jump to content

How to detect part menu is open?


Recommended Posts

I have a PartModule that adds labels (KSPFields) to a part menu. Since computing the info to be displayed on that label is expensive, I would like to detect if the menu is actually open so I can skip computation. How do I detect if part menu is open?

 

Link to comment
Share on other sites

On 12/10/2017 at 1:16 AM, agrock said:

I have a PartModule that adds labels (KSPFields) to a part menu. Since computing the info to be displayed on that label is expensive, I would like to detect if the menu is actually open so I can skip computation. How do I detect if part menu is open?

 

bool windowOpen = UIPartActionController.Instance.ItemListContains(part, false));

Should be able to be called from within your PartModule code, likely just prior to updating your UI fields in Update/etc.

Should return true if the part-action window for the chosen part is open/active/being updated.  Untested personally, but will likely be implementing it in some of my part-modules as well (to reduce GC from string manipulation during UI field updates).

Has a full method signature of:  public bool ItemListContains(Part part, bool includeSymmetryCounterparts)

Link to comment
Share on other sites

You can do that, or, if you really want to avoid unnecessary checks in the Update method you can attach a little listener script to the UI window prefab. The script then fires an event whenever it is created alongside the part action window, then your Part Module can subscribe to that event to turn on whatever it needs to update the field.

Script: https://github.com/CobaltWolf/Surface-Experiment-Pack/blob/master/Source/SEPScience/SEP_UIWindow.cs

Code to attach the script (just run it sometime after the flight scene has started): https://github.com/CobaltWolf/Surface-Experiment-Pack/blob/master/Source/SEPScience/SEP_Utilities.cs#L131-L142

Part Module subscribing to the window creation event (just a simple Game Event defined at the top of the previously linked file) with the methods to check if the window belongs to its part, and the code in LateUpdate to fiddle with the window (adding an EC counter to a part clicked on while on EVA): https://github.com/CobaltWolf/Surface-Experiment-Pack/blob/master/Source/SEPScience/ModuleSEPECViewer.cs

Link to comment
Share on other sites

8 hours ago, Shadowmage said:

(to reduce GC from string manipulation during UI field updates).

On side note, I think the GC trashing is overrated problem due to fact that .NET uses a generational GC. Short lived objects go away easily and cheaply. .NET was designed with such high object churn in mind. 

Using StringBuilder also minimizes the problem.

Link to comment
Share on other sites

13 hours ago, agrock said:
21 hours ago, Shadowmage said:

(to reduce GC from string manipulation during UI field updates).

On side note, I think the GC trashing is overrated problem due to fact that .NET uses a generational GC. Short lived objects go away easily and cheaply. .NET was designed with such high object churn in mind. 

Unity runs on a really old mono, not MS .NET. It does not have a generational GC. It has a plain mark and sweep.  

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...