Skip to content

Commit

Permalink
Now actually stops drawing when the window is closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhistleWiz committed Sep 9, 2023
1 parent a507ee7 commit b916f99
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions MapifyEditor/Tools/TrackToolsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public static void ShowWindow()
window.Show();
window.titleContent = new GUIContent("Track Tools");
window.autoRepaintOnSceneChange = true;
window._isOpen = true;
window._updateCounter = 0;
window.RegisterEvents();
}

#region FIELDS
Expand Down Expand Up @@ -112,6 +115,7 @@ public static void ShowWindow()

// Window.
private float _scrollMain = 0;
private bool _isOpen = false;

// Drawing.
private Vector3[] _forwardPoints = new Vector3[0];
Expand Down Expand Up @@ -163,34 +167,33 @@ private void OnGUI()

private void OnInspectorUpdate()
{
_updateCounter = (_updateCounter + 1) % 10;

if ((!_performanceMode || _updateCounter % 10 == 0) &&
_selectionType != SelectionType.None)
if (!_isOpen)
{
RemakeAndRepaint();
return;
}
}

private void OnFocus()
{
SceneView.duringSceneGui += DrawHandles;
Selection.selectionChanged += PrepareSelection;
RemakeAndRepaint();
}
_updateCounter = (_updateCounter + 1) % 10;

private void OnLostFocus()
{
// Was supposed to stop drawing after losing focus but in this case it also
// stops when changing selections. Actual visible functions only exist in
// later versions of unity.
//SceneView.duringSceneGui -= DrawHandles;
if (!_performanceMode || _updateCounter % 10 == 0)
{
// Only check if the window is closed if the last state is open.
if (_isOpen && !HasOpenInstances<TrackToolsWindow>())
{
_isOpen = false;
UnregisterEvents();
return;
}

if (_selectionType != SelectionType.None)
{
RemakeAndRepaint();
}
}
}

private void OnDestroy()
{
SceneView.duringSceneGui -= DrawHandles;
Selection.selectionChanged -= PrepareSelection;
UnregisterEvents();
}

private void OnSelectionChange()
Expand All @@ -208,6 +211,24 @@ private void DoNullCheck()
}
}

private void RegisterEvents(bool redraw = true)
{
SceneView.duringSceneGui += DrawHandles;
Selection.selectionChanged += PrepareSelection;

if (redraw)
{
RemakeAndRepaint();
}
}

private void UnregisterEvents()
{
SceneView.duringSceneGui -= DrawHandles;
Selection.selectionChanged -= PrepareSelection;
RemakeAndRepaint();
}

// Called when editor selection changes.
private void PrepareSelection()
{
Expand Down

0 comments on commit b916f99

Please sign in to comment.