Skip to content

Commit

Permalink
Fixed performance issue in the editor when FocusFollowsMouse was active
Browse files Browse the repository at this point in the history
  Two problems:
   GetComponent was being called on every part
   EditorActionGroups.Instance.ClearSelection was being called every time through Update()
 Added global value for FocusFollowsClick or FocusFollowsMouse for all new saves
  • Loading branch information
linuxgurugamer committed Oct 9, 2020
1 parent bf690f9 commit 86141a6
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 50 deletions.
7 changes: 7 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
ChangeLog

0.1.10.14
Fixed performance issue in the editor when FocusFollowsMouse was active
Two problems:
GetComponent was being called on every part
EditorActionGroups.Instance.ClearSelection was being called every time through Update()
Added global value for FocusFollowsClick or FocusFollowsMouse for all new saves

0.1.10.13
Fixed inputlocks not being cleared when canceling the window
Added mode toggle on new button to allow toggling between different modes during the game
Expand Down
2 changes: 1 addition & 1 deletion ClickThroughBlocker.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"MAJOR": 0,
"MINOR": 1,
"PATCH": 10,
"BUILD": 13
"BUILD": 14
},
"KSP_VERSION_MIN": {
"MAJOR": 1,
Expand Down
2 changes: 1 addition & 1 deletion ClickThroughBlocker/AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

using System.Reflection;

[assembly: AssemblyVersion("0.1.10.12")]
[assembly: AssemblyVersion("0.1.10.14")]
106 changes: 76 additions & 30 deletions ClickThroughBlocker/CBTMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,113 @@
using UnityEngine;
using System.Collections.Generic;
using KSP.UI.Screens;


#if !DUMMY
namespace ClickThroughFix
{
internal class PartEAPS
{

internal uint persistentId;
internal Part p;
internal EditorActionPartSelector eaps;

internal PartEAPS(Part p)
{
this.persistentId = p.persistentId;
this.p = p;
this.eaps = p.GetComponent<EditorActionPartSelector>();
}
}

[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
class CBTMonitor : MonoBehaviour
{
static internal Dictionary<uint, PartEAPS> partEAPSDict;

void Start()
{
DontDestroyOnLoad(this);
GameEvents.onGameSceneLoadRequested.Add(onGameSceneLoadRequested);
GameEvents.onGameSceneLoadRequested.Add(onGameSceneLoadRequested);
GameEvents.onEditorShipModified.Add(_onEditorShipModified);
GameEvents.onLevelWasLoadedGUIReady.Add(onLevelWasLoadedGUIReady);

ClickThruBlocker.CTBWin.activeBlockerCnt = 0;
partEAPSDict = new Dictionary<uint, PartEAPS>();
}

void onGameSceneLoadRequested(GameScenes gs)
{
ClickThruBlocker.CTBWin.activeBlockerCnt = 0;
partEAPSDict.Clear();
}

void ScanParts(List<Part> parts)
{
for (int i = parts.Count - 1; i >= 0; i--)
{
if (!partEAPSDict.ContainsKey(parts[i].persistentId))
{
partEAPSDict.Add(parts[i].persistentId, new PartEAPS(parts[i]));
}
}
}
void _onEditorShipModified(ShipConstruct sc)
{
if (sc != null && sc.parts != null)
{
ScanParts(sc.parts);
}
}

void onLevelWasLoadedGUIReady(GameScenes gs)
{
if (HighLogic.LoadedSceneIsEditor)
{
if (EditorLogic.fetch != null && EditorLogic.fetch.ship != null && EditorLogic.fetch.ship.Parts != null)
ScanParts(EditorLogic.fetch.ship.Parts);
}
}

// this whole mess below is to work around a stock bug.
// The bug is that the editor ignores the lock when the Action Group pane is show.
// The bug is that the editor ignores the lock when the Action Group pane is shown.
// So we have to each time, clear all locks and then reset those which were active when
// the mouse moved over a protected window
void Update()
{
if (HighLogic.LoadedSceneIsEditor && ClearInputLocks.focusFollowsclick) // ||
//(!HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick && !HighLogic.LoadedSceneIsEditor))
if (HighLogic.LoadedSceneIsEditor && ClearInputLocks.focusFollowsclick)
return;

if (ClickThruBlocker.CTBWin.activeBlockerCnt > 0)
{
if (ClickThruBlocker.CTBWin.activeBlockerCnt > 0)
{
//Log.Info("Setting Mouse.HoveredPart to null & deselecting all parts");
Mouse.HoveredPart = null;
//Log.Info("Setting Mouse.HoveredPart to null & deselecting all parts");
Mouse.HoveredPart = null;

if (EditorLogic.fetch == null)
{
return;
}
if (EditorLogic.fetch == null)
{
return;
}
for (int i = EditorLogic.fetch.ship.Parts.Count - 1; i >= 0; i--)
{
EditorActionPartSelector selector = partEAPSDict[EditorLogic.fetch.ship.Parts[i].persistentId].eaps; // EditorLogic.fetch.ship.Parts[i].GetComponent<EditorActionPartSelector>();
if (selector != null)
selector.Deselect();
}

for (int i = EditorLogic.fetch.ship.Parts.Count - 1; i >= 0; i--)
//for (int i = 0; i < EditorLogic.fetch.ship.Parts.Count; i++)
if (EditorActionGroups.Instance != null)
{
if (EditorActionGroups.Instance.HasSelectedParts())
EditorActionGroups.Instance.ClearSelection(true);
for (int i = ClickThruBlocker.CTBWin.selectedParts.Count - 1; i >= 0; i--)
//for (int i = 0; i < ClickThruBlocker.CTBWin.selectedParts.Count; i++)
{
EditorActionPartSelector selector = EditorLogic.fetch.ship.Parts[i].GetComponent<EditorActionPartSelector>();
EditorActionPartSelector selector = partEAPSDict[ClickThruBlocker.CTBWin.selectedParts[i].persistentId].eaps; // ClickThruBlocker.CTBWin.selectedParts[i].GetComponent<EditorActionPartSelector>();
if (selector != null)
selector.Deselect();
}

if (EditorActionGroups.Instance != null)
{
EditorActionGroups.Instance.ClearSelection(true);
for (int i = ClickThruBlocker.CTBWin.selectedParts.Count - 1; i >= 0; i--)
//for (int i = 0; i < ClickThruBlocker.CTBWin.selectedParts.Count; i++)
{
EditorActionPartSelector selector = ClickThruBlocker.CTBWin.selectedParts[i].GetComponent<EditorActionPartSelector>();
if (selector != null)
EditorActionGroups.Instance.AddToSelection(selector);
}
EditorActionGroups.Instance.AddToSelection(selector);
}
}

}

}

//static internal long timeTics = 0;
Expand Down
3 changes: 3 additions & 0 deletions ClickThroughBlocker/ClearInputLocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ void CallModeWindow()
{
//ClickThroughFix.Log.Info("CallModeWindow, modeWindow: " + (modeWindow != null));
if (modeWindow == null)
{
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().showPopup = true;
modeWindow = gameObject.AddComponent<OneTimePopup>();
}
else
{
Destroy(modeWindow);
Expand Down
7 changes: 6 additions & 1 deletion ClickThroughBlocker/ClickThroughBlocker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@
start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT deploy.bat $(TargetDir) $(TargetFileName)




if $(ConfigurationName) == Release (



start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT buildRelease.bat $(TargetDir) $(TargetFileName)

start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT buildRelease.bat $(TargetDir) $(TargetFileName) $(TargetName)



)</PostBuildEvent>
</PropertyGroup>
Expand Down
44 changes: 38 additions & 6 deletions ClickThroughBlocker/OneTimePopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ public class OneTimePopup : MonoBehaviour
const int HEIGHT = 350;
Rect popupRect = new Rect(300, 50, WIDTH, HEIGHT);
bool visible = false;
string popUpShownCfgPath;
static string popUpShownCfgPath { get {
return Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg"); ;
} }

string cancelStr = "Cancel (window will open next startup)";
Game curGame;
public void Awake()
{
popUpShownCfgPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg");
//popUpShownCfgPath = Path.Combine(
// Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg");
if (HighLogic.CurrentGame != curGame)
{
ClearInputLocks.focusFollowsclick = HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick;
Expand Down Expand Up @@ -102,6 +106,10 @@ void PopUpWindow(int id)
if (!focusFollowsClick && !focusFollowsMouse)
GUI.enabled = false;
GUILayout.BeginHorizontal();
if (GUILayout.Button("Save as global default for all new saves"))
{
SaveGlobalDefault();
}
if (GUILayout.Button("Accept"))
{
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick = focusFollowsClick;
Expand All @@ -125,14 +133,38 @@ void PopUpWindow(int id)
GUI.DragWindow();
}


void CreatePopUpFlagFile()
static string GlobalDefaultFile
{
get
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/../Global.cfg";
}
}
void SaveGlobalDefault()
{
ConfigNode node = new ConfigNode();
node.AddValue("focusFollowsClick", focusFollowsClick);
node.Save(GlobalDefaultFile);
}
static internal bool GetGlobalDefault(ref bool b)
{
if (System.IO.File.Exists(GlobalDefaultFile))
{
ConfigNode node = ConfigNode.Load(GlobalDefaultFile);
if (node.TryGetValue("focusFollowsClick", ref b))
{
return true;
}
}
return false;
}
static internal void CreatePopUpFlagFile()
{
RemovePopUpFlagFile(); // remove first to avoid any overwriting
System.IO.File.WriteAllText(popUpShownCfgPath, "popupshown = true");
}

public void RemovePopUpFlagFile()
static public void RemovePopUpFlagFile()
{
System.IO.File.Delete(popUpShownCfgPath);
}
Expand Down
29 changes: 23 additions & 6 deletions ClickThroughBlocker/RegisterToolbar.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using ToolbarControl_NS;
using KSP.UI.Screens;
using KSP.Localization;
using System;
using System.IO;

using KSP.IO;
using UnityEngine;


Expand All @@ -17,6 +11,29 @@ void Start()
{
ToolbarControl.RegisterMod(ClearInputLocks.MODID, ClearInputLocks.MODNAME);
ToolbarControl.RegisterMod(ClearInputLocks.MODID2, ClearInputLocks.MODNAME2);
GameEvents.onGameNewStart.Add(OnGameNewStart);
GameEvents.onGameStateCreated.Add(OnGameStateCreated);
}

void OnGameNewStart()
{
bool b = false ;
if (OneTimePopup.GetGlobalDefault(ref b))
{
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick = b;
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().showPopup = false;
OneTimePopup.CreatePopUpFlagFile();
}
}
void OnGameStateCreated(Game g)
{
bool b = false;
if (OneTimePopup.GetGlobalDefault(ref b))
{
g.Parameters.CustomParams<CTB>().focusFollowsclick = b;
g.Parameters.CustomParams<CTB>().showPopup = false;
OneTimePopup.CreatePopUpFlagFile();
}
}
}
}
2 changes: 1 addition & 1 deletion ClickThroughBlocker/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override bool Interactible(MemberInfo member, GameParameters parameters)
showPopup = false;
}
if (showPopup && OneTimePopup.Instance != null)
OneTimePopup.Instance.RemovePopUpFlagFile();
OneTimePopup.RemovePopUpFlagFile();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"MAJOR": 0,
"MINOR": 1,
"PATCH": 10,
"BUILD": 12
"BUILD": 14
},
"KSP_VERSION_MIN": {
"MAJOR": 1,
Expand Down
16 changes: 15 additions & 1 deletion GameData/000_ClickThroughBlocker/changelog.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ KERBALCHANGELOG
author = Linuxgurugamer


VERSION
{
version = 0.1.10.14
CHANGE
{
change = Fixed performance issue in the editor when FocusFollowsMouse was active
change = Added global value for FocusFollowsClick or FocusFollowsMouse for all new saves

type = update
}
}

VERSION
{
version = 0.1.10.13
CHANGE
{
change = Fixed inputlogs not being cleared when canceling the window
change = Fixed inputlocks not being cleared when canceling the window
change = Added mode toggle on new button to allow toggling between different modes during the game
change = Updated settings to make it clear if popup will be shown next time in the space center
change = Added code to disable the ShowPopup flag if the focusFollowsClick is changed in the settings
change = Thanks to github user @SteveBenz for this: Make the configuration file not depend on Environment.CurrentDirectory and instead depend on the deployment location of the DLL
type = update
}
}
Expand Down
1 change: 1 addition & 0 deletions buildRelease.bat
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rem Copy files to GameData locations

copy /Y "%1%2" "%GAMEDATA%\%GAMEDIR%\Plugins"
copy /Y %VERSIONFILE% %GAMEDATA%\%GAMEDIR%
copy /Y changelog.cfg %GAMEDATA%\%GAMEDIR%

if "%LICENSE%" NEQ "" copy /y %LICENSE% %GAMEDATA%\%GAMEDIR%
if "%README%" NEQ "" copy /Y %README% %GAMEDATA%\%GAMEDIR%
Expand Down
14 changes: 13 additions & 1 deletion changelog.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ KERBALCHANGELOG
author = Linuxgurugamer



VERSION
{
version = 0.1.10.14
CHANGE
{
change = Fixed performance issue in the editor when FocusFollowsMouse was active
subchange = Two problems:
subchange = GetComponent was being called on every part
subchange = EditorActionGroups.Instance.ClearSelection was being called every time through Update()
change = Added global value for FocusFollowsClick or FocusFollowsMouse for all new saves
type = update
}
}

VERSION
{
Expand Down
Loading

0 comments on commit 86141a6

Please sign in to comment.