Skip to content

Commit 86141a6

Browse files
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
1 parent bf690f9 commit 86141a6

File tree

14 files changed

+191
-50
lines changed

14 files changed

+191
-50
lines changed

Changelog.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
ChangeLog
22

3+
0.1.10.14
4+
Fixed performance issue in the editor when FocusFollowsMouse was active
5+
Two problems:
6+
GetComponent was being called on every part
7+
EditorActionGroups.Instance.ClearSelection was being called every time through Update()
8+
Added global value for FocusFollowsClick or FocusFollowsMouse for all new saves
9+
310
0.1.10.13
411
Fixed inputlocks not being cleared when canceling the window
512
Added mode toggle on new button to allow toggling between different modes during the game

ClickThroughBlocker.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"MAJOR": 0,
1111
"MINOR": 1,
1212
"PATCH": 10,
13-
"BUILD": 13
13+
"BUILD": 14
1414
},
1515
"KSP_VERSION_MIN": {
1616
"MAJOR": 1,

ClickThroughBlocker/AssemblyVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
using System.Reflection;
77

8-
[assembly: AssemblyVersion("0.1.10.12")]
8+
[assembly: AssemblyVersion("0.1.10.14")]

ClickThroughBlocker/CBTMonitor.cs

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,113 @@
11
using UnityEngine;
2+
using System.Collections.Generic;
23
using KSP.UI.Screens;
34

5+
46
#if !DUMMY
57
namespace ClickThroughFix
68
{
9+
internal class PartEAPS
10+
{
11+
12+
internal uint persistentId;
13+
internal Part p;
14+
internal EditorActionPartSelector eaps;
15+
16+
internal PartEAPS(Part p)
17+
{
18+
this.persistentId = p.persistentId;
19+
this.p = p;
20+
this.eaps = p.GetComponent<EditorActionPartSelector>();
21+
}
22+
}
23+
724
[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
825
class CBTMonitor : MonoBehaviour
926
{
27+
static internal Dictionary<uint, PartEAPS> partEAPSDict;
28+
1029
void Start()
1130
{
1231
DontDestroyOnLoad(this);
13-
GameEvents.onGameSceneLoadRequested.Add(onGameSceneLoadRequested);
32+
GameEvents.onGameSceneLoadRequested.Add(onGameSceneLoadRequested);
33+
GameEvents.onEditorShipModified.Add(_onEditorShipModified);
34+
GameEvents.onLevelWasLoadedGUIReady.Add(onLevelWasLoadedGUIReady);
35+
1436
ClickThruBlocker.CTBWin.activeBlockerCnt = 0;
37+
partEAPSDict = new Dictionary<uint, PartEAPS>();
1538
}
1639

1740
void onGameSceneLoadRequested(GameScenes gs)
1841
{
1942
ClickThruBlocker.CTBWin.activeBlockerCnt = 0;
43+
partEAPSDict.Clear();
44+
}
45+
46+
void ScanParts(List<Part> parts)
47+
{
48+
for (int i = parts.Count - 1; i >= 0; i--)
49+
{
50+
if (!partEAPSDict.ContainsKey(parts[i].persistentId))
51+
{
52+
partEAPSDict.Add(parts[i].persistentId, new PartEAPS(parts[i]));
53+
}
54+
}
55+
}
56+
void _onEditorShipModified(ShipConstruct sc)
57+
{
58+
if (sc != null && sc.parts != null)
59+
{
60+
ScanParts(sc.parts);
61+
}
62+
}
63+
64+
void onLevelWasLoadedGUIReady(GameScenes gs)
65+
{
66+
if (HighLogic.LoadedSceneIsEditor)
67+
{
68+
if (EditorLogic.fetch != null && EditorLogic.fetch.ship != null && EditorLogic.fetch.ship.Parts != null)
69+
ScanParts(EditorLogic.fetch.ship.Parts);
70+
}
2071
}
2172

2273
// this whole mess below is to work around a stock bug.
23-
// The bug is that the editor ignores the lock when the Action Group pane is show.
74+
// The bug is that the editor ignores the lock when the Action Group pane is shown.
2475
// So we have to each time, clear all locks and then reset those which were active when
2576
// the mouse moved over a protected window
2677
void Update()
2778
{
28-
if (HighLogic.LoadedSceneIsEditor && ClearInputLocks.focusFollowsclick) // ||
29-
//(!HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick && !HighLogic.LoadedSceneIsEditor))
79+
if (HighLogic.LoadedSceneIsEditor && ClearInputLocks.focusFollowsclick)
3080
return;
31-
81+
if (ClickThruBlocker.CTBWin.activeBlockerCnt > 0)
3282
{
33-
if (ClickThruBlocker.CTBWin.activeBlockerCnt > 0)
34-
{
35-
//Log.Info("Setting Mouse.HoveredPart to null & deselecting all parts");
36-
Mouse.HoveredPart = null;
83+
//Log.Info("Setting Mouse.HoveredPart to null & deselecting all parts");
84+
Mouse.HoveredPart = null;
3785

38-
if (EditorLogic.fetch == null)
39-
{
40-
return;
41-
}
86+
if (EditorLogic.fetch == null)
87+
{
88+
return;
89+
}
90+
for (int i = EditorLogic.fetch.ship.Parts.Count - 1; i >= 0; i--)
91+
{
92+
EditorActionPartSelector selector = partEAPSDict[EditorLogic.fetch.ship.Parts[i].persistentId].eaps; // EditorLogic.fetch.ship.Parts[i].GetComponent<EditorActionPartSelector>();
93+
if (selector != null)
94+
selector.Deselect();
95+
}
4296

43-
for (int i = EditorLogic.fetch.ship.Parts.Count - 1; i >= 0; i--)
44-
//for (int i = 0; i < EditorLogic.fetch.ship.Parts.Count; i++)
97+
if (EditorActionGroups.Instance != null)
98+
{
99+
if (EditorActionGroups.Instance.HasSelectedParts())
100+
EditorActionGroups.Instance.ClearSelection(true);
101+
for (int i = ClickThruBlocker.CTBWin.selectedParts.Count - 1; i >= 0; i--)
102+
//for (int i = 0; i < ClickThruBlocker.CTBWin.selectedParts.Count; i++)
45103
{
46-
EditorActionPartSelector selector = EditorLogic.fetch.ship.Parts[i].GetComponent<EditorActionPartSelector>();
104+
EditorActionPartSelector selector = partEAPSDict[ClickThruBlocker.CTBWin.selectedParts[i].persistentId].eaps; // ClickThruBlocker.CTBWin.selectedParts[i].GetComponent<EditorActionPartSelector>();
47105
if (selector != null)
48-
selector.Deselect();
49-
}
50-
51-
if (EditorActionGroups.Instance != null)
52-
{
53-
EditorActionGroups.Instance.ClearSelection(true);
54-
for (int i = ClickThruBlocker.CTBWin.selectedParts.Count - 1; i >= 0; i--)
55-
//for (int i = 0; i < ClickThruBlocker.CTBWin.selectedParts.Count; i++)
56-
{
57-
EditorActionPartSelector selector = ClickThruBlocker.CTBWin.selectedParts[i].GetComponent<EditorActionPartSelector>();
58-
if (selector != null)
59-
EditorActionGroups.Instance.AddToSelection(selector);
60-
}
106+
EditorActionGroups.Instance.AddToSelection(selector);
61107
}
62108
}
63-
64109
}
110+
65111
}
66112

67113
//static internal long timeTics = 0;

ClickThroughBlocker/ClearInputLocks.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ void CallModeWindow()
100100
{
101101
//ClickThroughFix.Log.Info("CallModeWindow, modeWindow: " + (modeWindow != null));
102102
if (modeWindow == null)
103+
{
104+
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().showPopup = true;
103105
modeWindow = gameObject.AddComponent<OneTimePopup>();
106+
}
104107
else
105108
{
106109
Destroy(modeWindow);

ClickThroughBlocker/ClickThroughBlocker.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@
9393
start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT deploy.bat $(TargetDir) $(TargetFileName)
9494

9595

96+
97+
9698
if $(ConfigurationName) == Release (
9799

98100

99101

100-
start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT buildRelease.bat $(TargetDir) $(TargetFileName)
102+
103+
start /D D:\Users\jbb\github\ClickThroughBlocker /WAIT buildRelease.bat $(TargetDir) $(TargetFileName) $(TargetName)
104+
105+
101106

102107
)</PostBuildEvent>
103108
</PropertyGroup>

ClickThroughBlocker/OneTimePopup.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ public class OneTimePopup : MonoBehaviour
1616
const int HEIGHT = 350;
1717
Rect popupRect = new Rect(300, 50, WIDTH, HEIGHT);
1818
bool visible = false;
19-
string popUpShownCfgPath;
19+
static string popUpShownCfgPath { get {
20+
return Path.Combine(
21+
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg"); ;
22+
} }
23+
2024
string cancelStr = "Cancel (window will open next startup)";
2125
Game curGame;
2226
public void Awake()
2327
{
24-
popUpShownCfgPath = Path.Combine(
25-
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg");
28+
//popUpShownCfgPath = Path.Combine(
29+
// Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../PluginData/PopUpShown.cfg");
2630
if (HighLogic.CurrentGame != curGame)
2731
{
2832
ClearInputLocks.focusFollowsclick = HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick;
@@ -102,6 +106,10 @@ void PopUpWindow(int id)
102106
if (!focusFollowsClick && !focusFollowsMouse)
103107
GUI.enabled = false;
104108
GUILayout.BeginHorizontal();
109+
if (GUILayout.Button("Save as global default for all new saves"))
110+
{
111+
SaveGlobalDefault();
112+
}
105113
if (GUILayout.Button("Accept"))
106114
{
107115
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick = focusFollowsClick;
@@ -125,14 +133,38 @@ void PopUpWindow(int id)
125133
GUI.DragWindow();
126134
}
127135

128-
129-
void CreatePopUpFlagFile()
136+
static string GlobalDefaultFile
137+
{
138+
get
139+
{
140+
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/../Global.cfg";
141+
}
142+
}
143+
void SaveGlobalDefault()
144+
{
145+
ConfigNode node = new ConfigNode();
146+
node.AddValue("focusFollowsClick", focusFollowsClick);
147+
node.Save(GlobalDefaultFile);
148+
}
149+
static internal bool GetGlobalDefault(ref bool b)
150+
{
151+
if (System.IO.File.Exists(GlobalDefaultFile))
152+
{
153+
ConfigNode node = ConfigNode.Load(GlobalDefaultFile);
154+
if (node.TryGetValue("focusFollowsClick", ref b))
155+
{
156+
return true;
157+
}
158+
}
159+
return false;
160+
}
161+
static internal void CreatePopUpFlagFile()
130162
{
131163
RemovePopUpFlagFile(); // remove first to avoid any overwriting
132164
System.IO.File.WriteAllText(popUpShownCfgPath, "popupshown = true");
133165
}
134166

135-
public void RemovePopUpFlagFile()
167+
static public void RemovePopUpFlagFile()
136168
{
137169
System.IO.File.Delete(popUpShownCfgPath);
138170
}
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
using ToolbarControl_NS;
2-
using KSP.UI.Screens;
3-
using KSP.Localization;
4-
using System;
5-
using System.IO;
6-
7-
using KSP.IO;
82
using UnityEngine;
93

104

@@ -17,6 +11,29 @@ void Start()
1711
{
1812
ToolbarControl.RegisterMod(ClearInputLocks.MODID, ClearInputLocks.MODNAME);
1913
ToolbarControl.RegisterMod(ClearInputLocks.MODID2, ClearInputLocks.MODNAME2);
14+
GameEvents.onGameNewStart.Add(OnGameNewStart);
15+
GameEvents.onGameStateCreated.Add(OnGameStateCreated);
16+
}
17+
18+
void OnGameNewStart()
19+
{
20+
bool b = false ;
21+
if (OneTimePopup.GetGlobalDefault(ref b))
22+
{
23+
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().focusFollowsclick = b;
24+
HighLogic.CurrentGame.Parameters.CustomParams<CTB>().showPopup = false;
25+
OneTimePopup.CreatePopUpFlagFile();
26+
}
27+
}
28+
void OnGameStateCreated(Game g)
29+
{
30+
bool b = false;
31+
if (OneTimePopup.GetGlobalDefault(ref b))
32+
{
33+
g.Parameters.CustomParams<CTB>().focusFollowsclick = b;
34+
g.Parameters.CustomParams<CTB>().showPopup = false;
35+
OneTimePopup.CreatePopUpFlagFile();
36+
}
2037
}
2138
}
2239
}

ClickThroughBlocker/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override bool Interactible(MemberInfo member, GameParameters parameters)
5959
showPopup = false;
6060
}
6161
if (showPopup && OneTimePopup.Instance != null)
62-
OneTimePopup.Instance.RemovePopUpFlagFile();
62+
OneTimePopup.RemovePopUpFlagFile();
6363
return true;
6464
}
6565

GameData/000_ClickThroughBlocker/ClickThroughBlocker.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"MAJOR": 0,
1111
"MINOR": 1,
1212
"PATCH": 10,
13-
"BUILD": 12
13+
"BUILD": 14
1414
},
1515
"KSP_VERSION_MIN": {
1616
"MAJOR": 1,

0 commit comments

Comments
 (0)