Skip to content

Commit

Permalink
Disable game input when scrolling in EditWindows
Browse files Browse the repository at this point in the history
  • Loading branch information
Falki-git committed Feb 21, 2024
1 parent 9e4b2cd commit f94807e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/MicroEngineer/UI/EditWindowsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ private System.Collections.IEnumerator StartInitialization()
CloseButton = Root.Q<Button>("close-button");
CloseButton.RegisterCallback<PointerUpEvent>(_ => CloseWindow());
AvailableScrollView = Root.Q<ScrollView>("available-scrollview");
AvailableScrollView.StopMouseEventsToGameInputPropagation();
InstalledScrollView = Root.Q<ScrollView>("installed-scrollview");
InstalledScrollView.StopMouseEventsToGameInputPropagation();
CategoryDropdown = Root.Q<DropdownField>("category__dropdown");
SelectedWindow = Root.Q<TextField>("selected-window");
SelectedWindow.RegisterValueChangedCallback(RenameWindow);
Expand Down
85 changes: 85 additions & 0 deletions src/MicroEngineer/Utilities/UiToolkitExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using KSP.Game;
using UnityEngine.UIElements;

namespace MicroEngineer.Utilities;

/// <summary>
/// Taken from Science Arkive: https://github.com/Kerbalight/ScienceArkive/blob/main/src/ScienceArkive/API/Extensions/UIToolkitExtensions.cs#L19
/// </summary>
public static class UIToolkitExtensions
{
public static GameInstance Game => GameManager.Instance.Game;

/// <summary>
/// Stop the mouse events (scroll and click) from propagating to the game (e.g. zoom).
/// The only place where the Click still doesn't get stopped is in the MapView, neither the Focus or the Orbit mouse events.
/// </summary>
/// <param name="element"></param>
public static void StopMouseEventsToGameInputPropagation(this VisualElement element)
{
element.RegisterCallback<PointerEnterEvent>(OnVisualElementPointerEnter);
element.RegisterCallback<PointerLeaveEvent>(OnVisualElementPointerLeave);
}

private static void OnVisualElementPointerEnter(PointerEnterEvent evt)
{
Game.Input.Flight.CameraZoom.Disable();
Game.Input.Flight.mouseDoubleTap.Disable();
Game.Input.Flight.mouseSecondaryTap.Disable();

Game.Input.MapView.cameraZoom.Disable();
Game.Input.MapView.Focus.Disable();
Game.Input.MapView.mousePrimary.Disable();
Game.Input.MapView.mouseSecondary.Disable();
Game.Input.MapView.mouseTertiary.Disable();
Game.Input.MapView.mousePosition.Disable();

Game.Input.VAB.cameraZoom.Disable();
Game.Input.VAB.mousePrimary.Disable();
Game.Input.VAB.mouseSecondary.Disable();
}

private static void OnVisualElementPointerLeave(PointerLeaveEvent evt)
{
Game.Input.Flight.CameraZoom.Enable();
Game.Input.Flight.mouseDoubleTap.Enable();
Game.Input.Flight.mouseSecondaryTap.Enable();

Game.Input.MapView.cameraZoom.Enable();
Game.Input.MapView.Focus.Enable();
Game.Input.MapView.mousePrimary.Enable();
Game.Input.MapView.mouseSecondary.Enable();
Game.Input.MapView.mouseTertiary.Enable();
Game.Input.MapView.mousePosition.Enable();

Game.Input.VAB.cameraZoom.Enable();
Game.Input.VAB.mousePrimary.Enable();
Game.Input.VAB.mouseSecondary.Enable();
}

/// <summary>
/// Checks if the visual element that is a container lost focus to an element outside of it
/// </summary>
public static bool HasContainerLostFocus(this VisualElement container, VisualElement targetOfFocusOutEvent)
{
if (targetOfFocusOutEvent == null)
return true;

if (targetOfFocusOutEvent == container)
return false;

if (targetOfFocusOutEvent.parent == null)
return true;

if (targetOfFocusOutEvent.parent == container)
return false;

if (targetOfFocusOutEvent.parent.parent == null)
return true;

if (targetOfFocusOutEvent.parent.parent == container)
return false;

return true;
}
}

0 comments on commit f94807e

Please sign in to comment.