Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PartModule-level OnGUI calls to improve performance #21

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions Source/ROLib/Modules/ModuleROTank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,6 @@ private ROLModelModule<ModuleROTank> GetUpperFairingModelModule()

#region GUI

private void OnGUI()
{
foreach (var window in AbstractWindow.Windows.Values)
window.Draw();
}

private void HideAllWindows()
{
if (dimWindow != null)
Expand All @@ -975,7 +969,8 @@ public void EditDimensions()
}
else
{
dimWindow = new DimensionWindow(this);
dimWindow = gameObject.AddComponent<DimensionWindow>();
dimWindow.InitForModule(this);
dimWindow.Show();
}
}
Expand All @@ -989,7 +984,8 @@ public void SelectModelWindow(ROLModelModule<ModuleROTank> m, ModelDefinitionLay
modWindow = null;
if (!openedDifferentWindow) return;
}
modWindow = new ModelWindow(this, m, defs, name);
modWindow = gameObject.AddComponent<ModelWindow>();
modWindow.InitForModule(this, m, defs, name);
modWindow.Show();
}

Expand Down
92 changes: 56 additions & 36 deletions Source/ROLib/UI/AbstractWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,61 @@

namespace ROLib
{
public abstract class AbstractWindow
public abstract class AbstractWindow : MonoBehaviour
{
private const string InputLockID = "ROLWindowLock";

public abstract Rect InitialPosition { get; }
public abstract string Title { get; }

public Guid Guid { get; private set; }
public string Tooltip { get; private set; }

public Rect Position;
public string Title { get; set; }
public string Tooltip { get; set; }
public bool Enabled = false;
public static GUIStyle Frame = new GUIStyle(HighLogic.Skin.window);
private readonly Guid mGuid;
public static Dictionary<Guid, AbstractWindow> Windows = new Dictionary<Guid, AbstractWindow>();
public static GUIStyle headingStyle, boldBtnStyle, boldLblStyle, pressedButton;

// Initial width and height of the window
public float mInitialWidth;
public float mInitialHeight;
protected static GUIStyle Frame;
protected static GUIStyle headingStyle, boldBtnStyle, boldLblStyle, pressedButton;

// Callback trigger for the change in the position
public Action onPositionChanged = delegate { };
public Rect backupPosition;
protected Rect backupPosition;

protected void Awake()
{
if (Guid == default) Guid = Guid.NewGuid();

InitGUI();

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

protected void OnGUI()
{
Draw();
}

protected void OnDestroy()
{
GameEvents.onHideUI.Remove(OnHideUI);
GameEvents.onShowUI.Remove(OnShowUI);

if (Enabled) Hide();
}

protected virtual void InitGUI()
{
if (Frame == null) InitStaticGUI();

Position = InitialPosition;
backupPosition = Position;
}

static AbstractWindow()
protected virtual void InitStaticGUI()
{
Frame = new GUIStyle(HighLogic.Skin.window);
Frame.padding = new RectOffset(5, 5, 5, 5);
headingStyle = new GUIStyle(HighLogic.Skin.label)
{
Expand All @@ -47,31 +81,18 @@ static AbstractWindow()
};
}

public AbstractWindow(Guid id, string title, Rect position)
{
mGuid = id;
Title = title;
Position = position;
backupPosition = position;
mInitialHeight = position.height + 15;
mInitialWidth = position.width + 15;

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

public Rect RequestPosition() => Position;

public virtual void Show()
{
if (Enabled)
return;

if (Windows.ContainsKey(mGuid))
if (Windows.ContainsKey(Guid))
{
Windows[mGuid].Hide();
Windows[Guid].Hide();
}
Windows[mGuid] = this;
Windows[Guid] = this;
Enabled = true;
}

Expand All @@ -80,21 +101,20 @@ public virtual void Show()

public virtual void Hide()
{
Windows.Remove(mGuid);
Windows.Remove(Guid);
Enabled = false;
GameEvents.onHideUI.Remove(OnHideUI);
GameEvents.onShowUI.Remove(OnShowUI);
InputLockManager.RemoveControlLock("ROLWindowLock");
InputLockManager.RemoveControlLock(InputLockID);
Destroy(this);
}

private void WindowPre(int uid)
{
try
{
InputLockManager.RemoveControlLock("ROLWindowLock");
InputLockManager.RemoveControlLock(InputLockID);
/* Block clicks through window onto ship or other editor UI */
if (this.backupPosition.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
InputLockManager.SetControlLock(ControlTypes.EDITOR_LOCK, "ROLWindowLock");
InputLockManager.SetControlLock(ControlTypes.EDITOR_LOCK, InputLockID);

GUI.skin = HighLogic.Skin;

Expand Down Expand Up @@ -134,7 +154,7 @@ public virtual void Draw()
Position.height = 0;
}

Position = GUILayout.Window(mGuid.GetHashCode(), Position, WindowPre, new GUIContent(), Frame);
Position = GUILayout.Window(Guid.GetHashCode(), Position, WindowPre, new GUIContent(), Frame);

if (Event.current.type == EventType.Repaint)
{
Expand All @@ -160,7 +180,7 @@ public virtual void Draw()
/// <summary>
/// Toggle the window
/// </summary>
public void toggleWindow()
public void ToggleWindow()
{
if (Enabled) Hide();
else Show();
Expand All @@ -185,7 +205,7 @@ public void RenderGrid(int columns, IEnumerable<Action> items)
}

public T RenderRadioSelectors<T>(T selected, Dictionary<T, string> options, params GUILayoutOption[] toggleOptions)
where T : Enum
where T : Enum
{
using (new GUILayout.HorizontalScope())
{
Expand Down
10 changes: 7 additions & 3 deletions Source/ROLib/UI/DimensionWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ private string TrimmedDecimal(double num, int maxDecimalPlaces = 1) =>
Unit.Inch => diameterInches,
_ => throw new Exception(),
};

public override Rect InitialPosition => new Rect(300, 300, 400, 600);

public override string Title => "ROTanks Diameter Selection";

const int ApplicationPrecision = 3;
string diameterBuf;
private void ResetDiameterBuf() => diameterBuf = TrimmedDecimal(diameterInputUnit, ApplicationPrecision);
Expand All @@ -68,16 +73,14 @@ private string TrimmedDecimal(double num, int maxDecimalPlaces = 1) =>
{ "3x", 3d },
};

public DimensionWindow(ModuleROTank mod) :
base(Guid.NewGuid(), "ROTanks Diameter Selection", new Rect(300, 300, 400, 600))
public void InitForModule(ModuleROTank mod)
{
module = mod;
diameter = mod.currentDiameter;
ResetDiameterBuf();
LoadPresetsFromConfigs();
}


private void LoadPresetsFromConfigs()
{
presets.Clear();
Expand All @@ -91,6 +94,7 @@ private void LoadPresetsFromConfigs()
}
presets.Sort((a, b) => a.Diameter.CompareTo(b.Diameter));
}

private string PresetDescription(string name, double diam) => showPresetNameOnly
? name
: $"{name} [{diam:N1}m / {ConvertUnit(Unit.Meter, Unit.Foot, diam):N1}ft]";
Expand Down
9 changes: 5 additions & 4 deletions Source/ROLib/UI/ModelSelectionGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ public class ModelSelectionGUI : AbstractWindow
private ModuleROTank ROTank;
private static ModuleTab currentTab;

public override Rect InitialPosition => new Rect(300, 300, 800, 600);

public override string Title => "ROTanks Model Selection";

private enum ModuleTab
{
Core,
Nose,
Mount
};

public ModelSelectionGUI (ModuleROTank m) :
base(new Guid(), $"ROTanks Model Selection", new Rect(300, 300, 800, 600))
public void InitForModule(ModuleROTank m)
{
ROTank = m;
diameterStr = m.currentDiameter.ToString("N3");
Expand Down Expand Up @@ -94,7 +97,6 @@ private static void SwitchTab(ModuleTab newTab)

public override void Window(int id)
{
//ROLLog.debug("ModelWindow: DrawWindow()");
GUI.skin = HighLogic.Skin;
try
{
Expand All @@ -115,7 +117,6 @@ public override void Window(int id)
GUILayout.EndHorizontal();
GUI.DragWindow();
base.Window(id);
//ROLLog.debug("ModelWindow: End DrawWindow()");
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions Source/ROLib/UI/ModelWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ namespace ROLib
{
class ModelWindow : AbstractWindow
{
Vector2 selectModelScroll;
private readonly ModuleROTank pm;
public readonly ROLModelModule<ModuleROTank> module;
private readonly ModelDefinitionLayoutOptions[] def;
private Vector2 selectModelScroll;
private ModuleROTank pm;
public ROLModelModule<ModuleROTank> module;
private ModelDefinitionLayoutOptions[] def;
private string modelName;
private string oldModel;

public ModelWindow(ModuleROTank m, ROLModelModule<ModuleROTank> mod, ModelDefinitionLayoutOptions[] d, string name) :
base(Guid.NewGuid(), $"ROTanks {name} Selection", new Rect(800, 350, 250, 600))
public override Rect InitialPosition => new Rect(800, 350, 250, 600);

private string _title;
public override string Title => _title;

public void InitForModule(ModuleROTank m, ROLModelModule<ModuleROTank> mod, ModelDefinitionLayoutOptions[] d, string name)
{
selectModelScroll = new Vector2();
pm = m;
module = mod;
def = d;
_title = $"ROTanks {name} Selection";
}

private void UpdateModelSelections()
{
foreach (ModelDefinitionLayoutOptions options in def)
{

if (RenderToggleButton($"{options.definition.title}", options.definition.name == module.modelName))
{
modelName = options.definition.name;
Expand Down Expand Up @@ -71,7 +75,6 @@ private void SelectCurrentModel(string model)
fld.uiControlEditor.onFieldChanged.Invoke(fld, oldModel);
}
}
//MonoUtilities.RefreshContextWindows(pm.part);
}

public void SelectModel()
Expand Down