Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit d67507e

Browse files
committed
Implement DisplayManager, ability to use other monitors
1 parent d730fbe commit d67507e

File tree

9 files changed

+120
-54
lines changed

9 files changed

+120
-54
lines changed

src/Config/ConfigManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static class ConfigManager
2121

2222
// Actual UE Settings
2323
public static ConfigElement<KeyCode> Master_Toggle;
24+
public static ConfigElement<int> Target_Display;
2425
public static ConfigElement<UIManager.VerticalAnchor> Main_Navbar_Anchor;
2526
public static ConfigElement<bool> Force_Unlock_Mouse;
2627
public static ConfigElement<KeyCode> Force_Unlock_Toggle;
@@ -79,6 +80,11 @@ private static void CreateConfigElements()
7980
"The key to enable or disable UnityExplorer's menu and features.",
8081
KeyCode.F7);
8182

83+
Target_Display = new ConfigElement<int>("Target Display",
84+
"The monitor index for UnityExplorer to use, if you have multiple. 0 is the default display, 1 is secondary, etc. " +
85+
"A restart is required to deactivate extra windows.",
86+
0);
87+
8288
Main_Navbar_Anchor = new ConfigElement<UIManager.VerticalAnchor>("Main Navbar Anchor",
8389
"The vertical anchor of the main UnityExplorer Navbar, in case you want to move it.",
8490
UIManager.VerticalAnchor.Top);

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace UnityExplorer
1616
public static class ExplorerCore
1717
{
1818
public const string NAME = "UnityExplorer";
19-
public const string VERSION = "4.5.0";
19+
public const string VERSION = "4.5.1";
2020
public const string AUTHOR = "Sinai";
2121
public const string GUID = "com.sinai.unityexplorer";
2222

src/Inspectors/InspectUnderMouse.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,18 @@ public class InspectUnderMouse : UIPanel
3131
public static bool Inspecting { get; set; }
3232
public static MouseInspectMode Mode { get; set; }
3333

34-
private static Vector3 lastMousePos;
35-
36-
public MouseInspectorBase CurrentInspector
34+
public MouseInspectorBase CurrentInspector => Mode switch
3735
{
38-
get
39-
{
40-
switch (Mode)
41-
{
42-
case MouseInspectMode.UI:
43-
return uiInspector;
44-
case MouseInspectMode.World:
45-
return worldInspector;
46-
}
47-
return null;
48-
}
49-
}
36+
MouseInspectMode.UI => uiInspector,
37+
MouseInspectMode.World => worldInspector,
38+
_ => null,
39+
};
40+
41+
private static Vector3 lastMousePos;
5042

5143
// UIPanel
44+
private UIBase inspectorUIBase;
45+
5246
public override string Name => "Inspect Under Mouse";
5347
public override UIManager.Panels PanelType => UIManager.Panels.MouseInspector;
5448
public override int MinWidth => -1;
@@ -164,7 +158,7 @@ internal void UpdatePosition(Vector2 mousePos)
164158
mousePos.y -= 10;
165159

166160
// calculate and set our UI position
167-
var inversePos = UIManager.UIRoot.transform.InverseTransformPoint(mousePos);
161+
var inversePos = inspectorUIBase.RootObject.transform.InverseTransformPoint(mousePos);
168162
UIRoot.transform.localPosition = new Vector3(inversePos.x, inversePos.y, 0);
169163
}
170164

@@ -207,6 +201,12 @@ public override void ConstructPanelContent()
207201
UIFactory.SetLayoutElement(objPathLabel.gameObject, minHeight: 75);
208202

209203
UIRoot.SetActive(false);
204+
205+
// Create a new canvas for this panel to live on.
206+
// It needs to always be shown on the main display, other panels can move displays.
207+
208+
inspectorUIBase = UniversalUI.RegisterUI($"{ExplorerCore.GUID}.MouseInspector", null);
209+
UIRoot.transform.SetParent(inspectorUIBase.RootObject.transform);
210210
}
211211
}
212212
}

src/UI/DisplayManager.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
using UnityExplorer.Config;
7+
using UniverseLib.Input;
8+
9+
namespace UnityExplorer.UI
10+
{
11+
public static class DisplayManager
12+
{
13+
public static int ActiveDisplayIndex { get; private set; }
14+
public static Display ActiveDisplay => Display.displays[ActiveDisplayIndex];
15+
16+
private static Camera canvasCamera;
17+
18+
internal static void Init()
19+
{
20+
SetDisplay(ConfigManager.Target_Display.Value);
21+
ConfigManager.Target_Display.OnValueChanged += SetDisplay;
22+
}
23+
24+
public static Vector3 MousePosition => Display.RelativeMouseAt(InputManager.MousePosition);
25+
26+
public static void SetDisplay(int display)
27+
{
28+
if (ActiveDisplayIndex == display)
29+
return;
30+
31+
if (Display.displays.Length <= display)
32+
{
33+
ExplorerCore.LogWarning($"Cannot set display index to {display} as there are not enough monitors connected!");
34+
35+
if (ConfigManager.Target_Display.Value == display)
36+
ConfigManager.Target_Display.Value = 0;
37+
38+
return;
39+
}
40+
41+
ActiveDisplayIndex = display;
42+
ActiveDisplay.Activate();
43+
44+
UIManager.UICanvas.targetDisplay = display;
45+
46+
// ensure a camera is targeting the display
47+
if (!Camera.main || Camera.main.targetDisplay != display)
48+
{
49+
if (!canvasCamera)
50+
{
51+
canvasCamera = new GameObject("UnityExplorer_CanvasCamera").AddComponent<Camera>();
52+
GameObject.DontDestroyOnLoad(canvasCamera.gameObject);
53+
canvasCamera.hideFlags = HideFlags.HideAndDontSave;
54+
}
55+
canvasCamera.targetDisplay = display;
56+
}
57+
}
58+
}
59+
}

src/UI/Notification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void ShowMessage(string message)
2727
_currentNotification = message;
2828
_timeOfLastNotification = Time.realtimeSinceStartup;
2929

30-
popupLabel.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(InputManager.MousePosition) + (Vector3.up * 25);
30+
popupLabel.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(DisplayManager.MousePosition) + (Vector3.up * 25);
3131
}
3232

3333
public static void Update()

src/UI/Panels/PanelDragger.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void UpdateInstances()
7878
else
7979
state = MouseState.NotPressed;
8080

81-
var mousePos = InputManager.MousePosition;
81+
var mousePos = DisplayManager.MousePosition;
8282

8383
handledInstanceThisFrame = false;
8484
foreach (var instance in Instances)
@@ -234,12 +234,12 @@ public void OnBeginDrag()
234234
{
235235
wasAnyDragging = true;
236236
WasDragging = true;
237-
lastDragPosition = InputManager.MousePosition;
237+
lastDragPosition = DisplayManager.MousePosition;
238238
}
239239

240240
public void OnDrag()
241241
{
242-
var mousePos = InputManager.MousePosition;
242+
var mousePos = DisplayManager.MousePosition;
243243

244244
Vector2 diff = (Vector2)mousePos - lastDragPosition;
245245
lastDragPosition = mousePos;
@@ -388,7 +388,7 @@ public void OnHoverResize(ResizeTypes resizeType)
388388
// update the resize icon position to be above the mouse
389389
private void UpdateHoverImagePos()
390390
{
391-
resizeCursorObj.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(InputManager.MousePosition);
391+
resizeCursorObj.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(DisplayManager.MousePosition);
392392
}
393393

394394
public void OnHoverResizeEnd()
@@ -400,14 +400,14 @@ public void OnHoverResizeEnd()
400400
public void OnBeginResize(ResizeTypes resizeType)
401401
{
402402
currentResizeType = resizeType;
403-
lastResizePos = InputManager.MousePosition;
403+
lastResizePos = DisplayManager.MousePosition;
404404
WasResizing = true;
405405
Resizing = true;
406406
}
407407

408408
public void OnResize()
409409
{
410-
Vector3 mousePos = InputManager.MousePosition;
410+
Vector3 mousePos = DisplayManager.MousePosition;
411411
Vector2 diff = lastResizePos - (Vector2)mousePos;
412412

413413
if ((Vector2)mousePos == lastResizePos)

src/UI/Panels/UIPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void UpdateFocus()
3535
if (InputManager.GetMouseButtonDown(0) || InputManager.GetMouseButtonDown(1))
3636
{
3737
int count = UIManager.PanelHolder.transform.childCount;
38-
var mousePos = InputManager.MousePosition;
38+
var mousePos = DisplayManager.MousePosition;
3939
bool clickedInAny = false;
4040

4141
for (int i = count - 1; i >= 0; i--)

src/UI/UIManager.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
using HarmonyLib;
2-
using System;
3-
using System.Collections;
4-
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Reflection;
8-
using System.Text;
1+
using System.Collections.Generic;
92
using UnityEngine;
10-
using UnityEngine.EventSystems;
113
using UnityEngine.UI;
124
using UnityExplorer.Config;
135
using UnityExplorer.CSConsole;
146
using UnityExplorer.Inspectors;
157
using UnityExplorer.UI.Panels;
16-
using UnityExplorer.UI.Widgets;
178
using UnityExplorer.UI.Widgets.AutoComplete;
189
using UniverseLib;
1910
using UniverseLib.Input;
@@ -48,10 +39,10 @@ public enum VerticalAnchor
4839

4940
public static bool Initializing { get; internal set; } = true;
5041

51-
private static UIBase uiBase;
52-
public static GameObject UIRoot => uiBase?.RootObject;
53-
public static RectTransform UIRootRect => _uiRootRect ??= UIRoot.GetComponent<RectTransform>();
54-
private static RectTransform _uiRootRect;
42+
internal static UIBase UiBase { get; private set; }
43+
public static GameObject UIRoot => UiBase?.RootObject;
44+
public static RectTransform UIRootRect { get; private set; }
45+
public static Canvas UICanvas { get; private set; }
5546

5647
internal static GameObject PanelHolder { get; private set; }
5748
private static readonly Dictionary<Panels, UIPanel> UIPanels = new();
@@ -71,10 +62,10 @@ public enum VerticalAnchor
7162

7263
public static bool ShowMenu
7364
{
74-
get => uiBase != null && uiBase.Enabled;
65+
get => UiBase != null && UiBase.Enabled;
7566
set
7667
{
77-
if (uiBase == null || !UIRoot || uiBase.Enabled == value)
68+
if (UiBase == null || !UIRoot || UiBase.Enabled == value)
7869
return;
7970

8071
UniversalUI.SetUIActive(ExplorerCore.GUID, value);
@@ -85,11 +76,16 @@ public static bool ShowMenu
8576

8677
internal static void InitUI()
8778
{
88-
uiBase = UniversalUI.RegisterUI(ExplorerCore.GUID, Update);
79+
UiBase = UniversalUI.RegisterUI(ExplorerCore.GUID, Update);
8980

90-
lastScreenWidth = Screen.width;
91-
lastScreenHeight = Screen.height;
81+
UIRootRect = UIRoot.GetComponent<RectTransform>();
82+
UICanvas = UIRoot.GetComponent<Canvas>();
9283

84+
DisplayManager.Init();
85+
86+
var display = DisplayManager.ActiveDisplay;
87+
lastScreenWidth = display.renderingWidth;
88+
lastScreenHeight = display.renderingHeight;
9389

9490
// Create UI.
9591
CreatePanelHolder();
@@ -169,7 +165,8 @@ public static void Update()
169165
}
170166

171167
// check screen dimension change
172-
if (Screen.width != lastScreenWidth || Screen.height != lastScreenHeight)
168+
var display = DisplayManager.ActiveDisplay;
169+
if (display.renderingWidth != lastScreenWidth || display.renderingHeight != lastScreenHeight)
173170
OnScreenDimensionsChanged();
174171
}
175172

@@ -233,8 +230,9 @@ public static void SetNavBarAnchor()
233230

234231
private static void OnScreenDimensionsChanged()
235232
{
236-
lastScreenWidth = Screen.width;
237-
lastScreenHeight = Screen.height;
233+
var display = DisplayManager.ActiveDisplay;
234+
lastScreenWidth = display.renderingWidth;
235+
lastScreenHeight = display.renderingHeight;
238236

239237
foreach (var panel in UIPanels)
240238
{
@@ -254,6 +252,8 @@ private static void Master_Toggle_OnValueChanged(KeyCode val)
254252
closeBtn.ButtonText.text = val.ToString();
255253
}
256254

255+
// Time controls
256+
257257
private static void OnTimeInputEndEdit(string val)
258258
{
259259
if (pauseButtonPausing)

src/UnityExplorer.csproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@
107107
<Private>False</Private>
108108
</Reference>
109109
</ItemGroup>
110-
<!-- Non-MelonLoader (it includes Tomlet) -->
111-
<ItemGroup Condition="'$(IsMelonLoader)'=='false'">
112-
<Reference Include="Tomlet, Version=3.1.3.0, Culture=neutral, processorArchitecture=MSIL">
113-
<HintPath>packages\Samboy063.Tomlet.3.1.3\lib\net35\Tomlet.dll</HintPath>
114-
<Private>False</Private>
115-
</Reference>
116-
</ItemGroup>
110+
<!-- Non-MelonLoader (it includes Tomlet) -->
111+
<ItemGroup Condition="'$(IsMelonLoader)'=='false'">
112+
<Reference Include="Tomlet, Version=3.1.3.0, Culture=neutral, processorArchitecture=MSIL">
113+
<HintPath>packages\Samboy063.Tomlet.3.1.3\lib\net35\Tomlet.dll</HintPath>
114+
<Private>False</Private>
115+
</Reference>
116+
</ItemGroup>
117117
<!-- MelonLoader refs -->
118118
<ItemGroup Condition="'$(IsMelonLoader)'=='true'">
119119
<Reference Include="MelonLoader">
@@ -259,6 +259,7 @@
259259
<Compile Include="CacheObject\Views\CacheListEntryCell.cs" />
260260
<Compile Include="CacheObject\Views\CacheMemberCell.cs" />
261261
<Compile Include="CacheObject\Views\CacheObjectCell.cs" />
262+
<Compile Include="UI\DisplayManager.cs" />
262263
<Compile Include="UI\Notification.cs" />
263264
<Compile Include="UI\Panels\ClipboardPanel.cs" />
264265
<Compile Include="UI\Widgets\AutoComplete\EnumCompleter.cs" />

0 commit comments

Comments
 (0)