mirror of
https://github.com/GrahamKracker/UnityExplorer.git
synced 2025-07-10 14:03:30 +08:00
Implement DisplayManager, ability to use other monitors
This commit is contained in:
59
src/UI/DisplayManager.cs
Normal file
59
src/UI/DisplayManager.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityExplorer.Config;
|
||||
using UniverseLib.Input;
|
||||
|
||||
namespace UnityExplorer.UI
|
||||
{
|
||||
public static class DisplayManager
|
||||
{
|
||||
public static int ActiveDisplayIndex { get; private set; }
|
||||
public static Display ActiveDisplay => Display.displays[ActiveDisplayIndex];
|
||||
|
||||
private static Camera canvasCamera;
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
SetDisplay(ConfigManager.Target_Display.Value);
|
||||
ConfigManager.Target_Display.OnValueChanged += SetDisplay;
|
||||
}
|
||||
|
||||
public static Vector3 MousePosition => Display.RelativeMouseAt(InputManager.MousePosition);
|
||||
|
||||
public static void SetDisplay(int display)
|
||||
{
|
||||
if (ActiveDisplayIndex == display)
|
||||
return;
|
||||
|
||||
if (Display.displays.Length <= display)
|
||||
{
|
||||
ExplorerCore.LogWarning($"Cannot set display index to {display} as there are not enough monitors connected!");
|
||||
|
||||
if (ConfigManager.Target_Display.Value == display)
|
||||
ConfigManager.Target_Display.Value = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveDisplayIndex = display;
|
||||
ActiveDisplay.Activate();
|
||||
|
||||
UIManager.UICanvas.targetDisplay = display;
|
||||
|
||||
// ensure a camera is targeting the display
|
||||
if (!Camera.main || Camera.main.targetDisplay != display)
|
||||
{
|
||||
if (!canvasCamera)
|
||||
{
|
||||
canvasCamera = new GameObject("UnityExplorer_CanvasCamera").AddComponent<Camera>();
|
||||
GameObject.DontDestroyOnLoad(canvasCamera.gameObject);
|
||||
canvasCamera.hideFlags = HideFlags.HideAndDontSave;
|
||||
}
|
||||
canvasCamera.targetDisplay = display;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ namespace UnityExplorer.UI
|
||||
_currentNotification = message;
|
||||
_timeOfLastNotification = Time.realtimeSinceStartup;
|
||||
|
||||
popupLabel.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(InputManager.MousePosition) + (Vector3.up * 25);
|
||||
popupLabel.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(DisplayManager.MousePosition) + (Vector3.up * 25);
|
||||
}
|
||||
|
||||
public static void Update()
|
||||
|
@ -78,7 +78,7 @@ namespace UnityExplorer.UI.Panels
|
||||
else
|
||||
state = MouseState.NotPressed;
|
||||
|
||||
var mousePos = InputManager.MousePosition;
|
||||
var mousePos = DisplayManager.MousePosition;
|
||||
|
||||
handledInstanceThisFrame = false;
|
||||
foreach (var instance in Instances)
|
||||
@ -234,12 +234,12 @@ namespace UnityExplorer.UI.Panels
|
||||
{
|
||||
wasAnyDragging = true;
|
||||
WasDragging = true;
|
||||
lastDragPosition = InputManager.MousePosition;
|
||||
lastDragPosition = DisplayManager.MousePosition;
|
||||
}
|
||||
|
||||
public void OnDrag()
|
||||
{
|
||||
var mousePos = InputManager.MousePosition;
|
||||
var mousePos = DisplayManager.MousePosition;
|
||||
|
||||
Vector2 diff = (Vector2)mousePos - lastDragPosition;
|
||||
lastDragPosition = mousePos;
|
||||
@ -388,7 +388,7 @@ namespace UnityExplorer.UI.Panels
|
||||
// update the resize icon position to be above the mouse
|
||||
private void UpdateHoverImagePos()
|
||||
{
|
||||
resizeCursorObj.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(InputManager.MousePosition);
|
||||
resizeCursorObj.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(DisplayManager.MousePosition);
|
||||
}
|
||||
|
||||
public void OnHoverResizeEnd()
|
||||
@ -400,14 +400,14 @@ namespace UnityExplorer.UI.Panels
|
||||
public void OnBeginResize(ResizeTypes resizeType)
|
||||
{
|
||||
currentResizeType = resizeType;
|
||||
lastResizePos = InputManager.MousePosition;
|
||||
lastResizePos = DisplayManager.MousePosition;
|
||||
WasResizing = true;
|
||||
Resizing = true;
|
||||
}
|
||||
|
||||
public void OnResize()
|
||||
{
|
||||
Vector3 mousePos = InputManager.MousePosition;
|
||||
Vector3 mousePos = DisplayManager.MousePosition;
|
||||
Vector2 diff = lastResizePos - (Vector2)mousePos;
|
||||
|
||||
if ((Vector2)mousePos == lastResizePos)
|
||||
|
@ -35,7 +35,7 @@ namespace UnityExplorer.UI.Panels
|
||||
if (InputManager.GetMouseButtonDown(0) || InputManager.GetMouseButtonDown(1))
|
||||
{
|
||||
int count = UIManager.PanelHolder.transform.childCount;
|
||||
var mousePos = InputManager.MousePosition;
|
||||
var mousePos = DisplayManager.MousePosition;
|
||||
bool clickedInAny = false;
|
||||
|
||||
for (int i = count - 1; i >= 0; i--)
|
||||
|
@ -1,19 +1,10 @@
|
||||
using HarmonyLib;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.Config;
|
||||
using UnityExplorer.CSConsole;
|
||||
using UnityExplorer.Inspectors;
|
||||
using UnityExplorer.UI.Panels;
|
||||
using UnityExplorer.UI.Widgets;
|
||||
using UnityExplorer.UI.Widgets.AutoComplete;
|
||||
using UniverseLib;
|
||||
using UniverseLib.Input;
|
||||
@ -48,10 +39,10 @@ namespace UnityExplorer.UI
|
||||
|
||||
public static bool Initializing { get; internal set; } = true;
|
||||
|
||||
private static UIBase uiBase;
|
||||
public static GameObject UIRoot => uiBase?.RootObject;
|
||||
public static RectTransform UIRootRect => _uiRootRect ??= UIRoot.GetComponent<RectTransform>();
|
||||
private static RectTransform _uiRootRect;
|
||||
internal static UIBase UiBase { get; private set; }
|
||||
public static GameObject UIRoot => UiBase?.RootObject;
|
||||
public static RectTransform UIRootRect { get; private set; }
|
||||
public static Canvas UICanvas { get; private set; }
|
||||
|
||||
internal static GameObject PanelHolder { get; private set; }
|
||||
private static readonly Dictionary<Panels, UIPanel> UIPanels = new();
|
||||
@ -71,10 +62,10 @@ namespace UnityExplorer.UI
|
||||
|
||||
public static bool ShowMenu
|
||||
{
|
||||
get => uiBase != null && uiBase.Enabled;
|
||||
get => UiBase != null && UiBase.Enabled;
|
||||
set
|
||||
{
|
||||
if (uiBase == null || !UIRoot || uiBase.Enabled == value)
|
||||
if (UiBase == null || !UIRoot || UiBase.Enabled == value)
|
||||
return;
|
||||
|
||||
UniversalUI.SetUIActive(ExplorerCore.GUID, value);
|
||||
@ -85,11 +76,16 @@ namespace UnityExplorer.UI
|
||||
|
||||
internal static void InitUI()
|
||||
{
|
||||
uiBase = UniversalUI.RegisterUI(ExplorerCore.GUID, Update);
|
||||
UiBase = UniversalUI.RegisterUI(ExplorerCore.GUID, Update);
|
||||
|
||||
lastScreenWidth = Screen.width;
|
||||
lastScreenHeight = Screen.height;
|
||||
UIRootRect = UIRoot.GetComponent<RectTransform>();
|
||||
UICanvas = UIRoot.GetComponent<Canvas>();
|
||||
|
||||
DisplayManager.Init();
|
||||
|
||||
var display = DisplayManager.ActiveDisplay;
|
||||
lastScreenWidth = display.renderingWidth;
|
||||
lastScreenHeight = display.renderingHeight;
|
||||
|
||||
// Create UI.
|
||||
CreatePanelHolder();
|
||||
@ -169,7 +165,8 @@ namespace UnityExplorer.UI
|
||||
}
|
||||
|
||||
// check screen dimension change
|
||||
if (Screen.width != lastScreenWidth || Screen.height != lastScreenHeight)
|
||||
var display = DisplayManager.ActiveDisplay;
|
||||
if (display.renderingWidth != lastScreenWidth || display.renderingHeight != lastScreenHeight)
|
||||
OnScreenDimensionsChanged();
|
||||
}
|
||||
|
||||
@ -233,8 +230,9 @@ namespace UnityExplorer.UI
|
||||
|
||||
private static void OnScreenDimensionsChanged()
|
||||
{
|
||||
lastScreenWidth = Screen.width;
|
||||
lastScreenHeight = Screen.height;
|
||||
var display = DisplayManager.ActiveDisplay;
|
||||
lastScreenWidth = display.renderingWidth;
|
||||
lastScreenHeight = display.renderingHeight;
|
||||
|
||||
foreach (var panel in UIPanels)
|
||||
{
|
||||
@ -254,6 +252,8 @@ namespace UnityExplorer.UI
|
||||
closeBtn.ButtonText.text = val.ToString();
|
||||
}
|
||||
|
||||
// Time controls
|
||||
|
||||
private static void OnTimeInputEndEdit(string val)
|
||||
{
|
||||
if (pauseButtonPausing)
|
||||
|
Reference in New Issue
Block a user