Skip to content

Commit 9694d18

Browse files
authored
Merge pull request #16 from Falki-git/disable-control-when-editing-textfield
Fix: disable game input when editing window names
2 parents a4b945c + a0cb964 commit 9694d18

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

MicroEngineerProject/MicroEngineer/MicroEngineerMod.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class MicroEngineerMod : BaseSpaceWarpPlugin
4343
// Index of the stage for which user wants to select a different CelestialBody for different TWR calculations. -1 -> no stage is selected
4444
private int _celestialBodySelectionStageIndex = -1;
4545

46+
// If game input is enabled or disabled (used for locking controls when user is editing a text field
47+
private bool _gameInputState = true;
48+
4649
public override void OnInitialized()
4750
{
4851
MicroStyles.InitializeStyles();
@@ -192,6 +195,8 @@ private void OnGUI()
192195
#region Flight scene UI and logic
193196
private void OnGUI_Flight()
194197
{
198+
_gameInputState = MicroUtility.ToggleGameInputOnControlInFocus(_gameInputState, _showGuiFlight);
199+
195200
if (!_showGuiFlight || MicroUtility.ActiveVessel == null) return;
196201

197202
MicroWindow mainGui = MicroWindows.Find(window => window.MainWindow == MainWindow.MainGui);
@@ -559,8 +564,10 @@ private void DrawEditWindow(int windowIndex)
559564
{
560565
selectedWindowId = selectedWindowId > 0 ? selectedWindowId - 1 : editableWindows.Count - 1;
561566
}
567+
GUI.SetNextControlName(MicroUtility.InputDisableWindowAbbreviation);
562568
editableWindows[selectedWindowId].Abbreviation = GUILayout.TextField(editableWindows[selectedWindowId].Abbreviation, MicroStyles.WindowSelectionAbbrevitionTextFieldStyle);
563569
editableWindows[selectedWindowId].Abbreviation = MicroUtility.ValidateAbbreviation(editableWindows[selectedWindowId].Abbreviation);
570+
GUI.SetNextControlName(MicroUtility.InputDisableWindowName);
564571
editableWindows[selectedWindowId].Name = GUILayout.TextField(editableWindows[selectedWindowId].Name, MicroStyles.WindowSelectionTextFieldStyle);
565572
if (GUILayout.Button(">", MicroStyles.OneCharacterBtnStyle))
566573
{

MicroEngineerProject/MicroEngineer/MicroUtility.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public static class MicroUtility
2222
public static GameStateConfiguration GameState;
2323
public static MessageCenter MessageCenter;
2424
public static VesselDeltaVComponent VesselDeltaVComponentOAB;
25+
public static string InputDisableWindowAbbreviation = "WindowAbbreviation";
26+
public static string InputDisableWindowName = "WindowName";
2527

2628
/// <summary>
2729
/// Refreshes the ActiveVessel and CurrentManeuver
@@ -225,7 +227,37 @@ public static Vector2 ClampToScreen(Vector2 position, Vector2 size)
225227
float y = Mathf.Clamp(position.y, 0, Screen.height - size.y);
226228
return new Vector2(x, y);
227229
}
228-
}
230+
231+
/// <summary>
232+
/// Check if focus is on an editable text field. If it is, disable input controls. If it's not, reenable controls.
233+
/// </summary>
234+
/// <param name="gameInputState">If input is currently enabled or disabled</param>
235+
/// <param name="showGuiFlight">If MainGui window is opened</param>
236+
/// <returns>True = input is enabled. False = input is disabled</returns>
237+
internal static bool ToggleGameInputOnControlInFocus(bool gameInputState, bool showGuiFlight)
238+
{
239+
if (gameInputState)
240+
{
241+
if (MicroUtility.InputDisableWindowAbbreviation == GUI.GetNameOfFocusedControl() || MicroUtility.InputDisableWindowName == GUI.GetNameOfFocusedControl())
242+
{
243+
GameManager.Instance.Game.Input.Disable();
244+
return false;
245+
}
246+
247+
return true;
248+
}
249+
else
250+
{
251+
if ((MicroUtility.InputDisableWindowAbbreviation != GUI.GetNameOfFocusedControl() && MicroUtility.InputDisableWindowName != GUI.GetNameOfFocusedControl()) || !showGuiFlight)
252+
{
253+
GameManager.Instance.Game.Input.Enable();
254+
return true;
255+
}
256+
257+
return false;
258+
}
259+
}
260+
}
229261

230262
public static class AeroForces
231263
{

0 commit comments

Comments
 (0)