UnityExplorer/src/UI/UIManager.cs

271 lines
9.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Config;
using UnityExplorer.CSConsole;
using UnityExplorer.Inspectors;
using UnityExplorer.UI.Panels;
using UnityExplorer.UI.Widgets;
2021-04-23 21:50:58 +10:00
using UnityExplorer.UI.Widgets.AutoComplete;
2021-12-02 18:35:46 +11:00
using UniverseLib;
using UniverseLib.Input;
using UniverseLib.UI;
2022-01-31 21:24:01 +11:00
using UniverseLib.UI.Models;
2022-04-14 01:25:59 +10:00
using UniverseLib.UI.Panels;
2022-01-31 21:24:01 +11:00
using UniverseLib.UI.Widgets.ScrollView;
using UniverseLib.Utility;
namespace UnityExplorer.UI
{
public static class UIManager
{
2021-04-23 21:50:58 +10:00
public enum Panels
{
ObjectExplorer,
Inspector,
CSConsole,
Options,
ConsoleLog,
AutoCompleter,
UIInspectorResults,
2021-09-06 23:04:40 +10:00
HookManager,
2022-04-18 21:39:17 +10:00
Clipboard,
Freecam
2021-04-23 21:50:58 +10:00
}
public enum VerticalAnchor
{
Top,
Bottom
}
public static VerticalAnchor NavbarAnchor = VerticalAnchor.Top;
2021-12-02 18:35:46 +11:00
public static bool Initializing { get; internal set; } = true;
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; }
2022-04-14 01:25:59 +10:00
internal static readonly Dictionary<Panels, UEPanel> UIPanels = new();
public static RectTransform NavBarRect;
public static GameObject NavbarTabButtonHolder;
private static readonly Vector2 NAVBAR_DIMENSIONS = new(1020f, 35f);
private static ButtonRef closeBtn;
private static TimeScaleWidget timeScaleWidget;
private static int lastScreenWidth;
private static int lastScreenHeight;
2021-03-11 17:57:58 +11:00
public static bool ShowMenu
{
get => UiBase != null && UiBase.Enabled;
set
2021-03-11 17:57:58 +11:00
{
if (UiBase == null || !UIRoot || UiBase.Enabled == value)
return;
2021-12-02 18:35:46 +11:00
UniversalUI.SetUIActive(ExplorerCore.GUID, value);
UniversalUI.SetUIActive(MouseInspector.UIBaseGUID, value);
2021-03-11 17:57:58 +11:00
}
}
// Initialization
internal static void InitUI()
{
2022-04-14 01:25:59 +10:00
UiBase = UniversalUI.RegisterUI<ExplorerUIBase>(ExplorerCore.GUID, Update);
UIRootRect = UIRoot.GetComponent<RectTransform>();
UICanvas = UIRoot.GetComponent<Canvas>();
DisplayManager.Init();
2022-03-23 18:04:27 +11:00
Display display = DisplayManager.ActiveDisplay;
lastScreenWidth = display.renderingWidth;
lastScreenHeight = display.renderingHeight;
// Create UI.
CreateTopNavBar();
// This could be automated with Assembly.GetTypes(),
// but the order is important and I'd have to write something to handle the order.
2022-04-14 01:25:59 +10:00
UIPanels.Add(Panels.AutoCompleter, new AutoCompleteModal(UiBase));
UIPanels.Add(Panels.ObjectExplorer, new ObjectExplorerPanel(UiBase));
UIPanels.Add(Panels.Inspector, new InspectorPanel(UiBase));
UIPanels.Add(Panels.CSConsole, new CSConsolePanel(UiBase));
UIPanels.Add(Panels.HookManager, new HookManagerPanel(UiBase));
2022-04-18 21:39:17 +10:00
UIPanels.Add(Panels.Freecam, new FreeCamPanel(UiBase));
2022-04-14 01:25:59 +10:00
UIPanels.Add(Panels.Clipboard, new ClipboardPanel(UiBase));
UIPanels.Add(Panels.ConsoleLog, new LogPanel(UiBase));
UIPanels.Add(Panels.Options, new OptionsPanel(UiBase));
UIPanels.Add(Panels.UIInspectorResults, new MouseInspectorResultsPanel(UiBase));
2022-04-18 21:39:17 +10:00
MouseInspector.inspectorUIBase = UniversalUI.RegisterUI(MouseInspector.UIBaseGUID, null);
new MouseInspector(MouseInspector.inspectorUIBase);
// Call some initialize methods
Notification.Init();
ConsoleController.Init();
// Failsafe fix, in some games all dropdowns displayed values are blank on startup for some reason.
foreach (Dropdown dropdown in UIRoot.GetComponentsInChildren<Dropdown>(true))
dropdown.RefreshShownValue();
2021-12-02 18:35:46 +11:00
Initializing = false;
2022-04-25 19:37:29 +10:00
if (ConfigManager.Hide_On_Startup.Value)
ShowMenu = false;
}
// Main UI Update loop
public static void Update()
{
2021-12-02 18:35:46 +11:00
if (!UIRoot)
return;
// If we are doing a Mouse Inspect, we don't need to update anything else.
if (MouseInspector.Instance.TryUpdate())
return;
// Update Notification modal
Notification.Update();
// Check forceUnlockMouse toggle
if (InputManager.GetKeyDown(ConfigManager.Force_Unlock_Toggle.Value))
2021-12-02 18:35:46 +11:00
UniverseLib.Config.ConfigManager.Force_Unlock_Mouse = !UniverseLib.Config.ConfigManager.Force_Unlock_Mouse;
2021-05-28 18:48:55 +10:00
// update the timescale value
timeScaleWidget.Update();
2021-05-28 18:48:55 +10:00
// check screen dimension change
Display display = DisplayManager.ActiveDisplay;
if (display.renderingWidth != lastScreenWidth || display.renderingHeight != lastScreenHeight)
OnScreenDimensionsChanged();
}
2021-05-13 23:03:30 +10:00
// Panels
2021-03-11 17:57:58 +11:00
2022-04-14 01:25:59 +10:00
public static UEPanel GetPanel(Panels panel) => UIPanels[panel];
2022-04-14 01:25:59 +10:00
public static T GetPanel<T>(Panels panel) where T : UEPanel => (T)UIPanels[panel];
public static void TogglePanel(Panels panel)
{
2022-04-14 01:25:59 +10:00
UEPanel uiPanel = GetPanel(panel);
SetPanelActive(panel, !uiPanel.Enabled);
}
public static void SetPanelActive(Panels panelType, bool active)
{
2022-04-14 01:25:59 +10:00
GetPanel(panelType).SetActive(active);
2021-04-28 23:58:13 +10:00
}
2022-04-14 01:25:59 +10:00
public static void SetPanelActive(UEPanel panel, bool active)
2021-04-28 23:58:13 +10:00
{
panel.SetActive(active);
}
// navbar
public static void SetNavBarAnchor()
2021-05-13 23:03:30 +10:00
{
switch (NavbarAnchor)
{
case VerticalAnchor.Top:
NavBarRect.anchorMin = new Vector2(0.5f, 1f);
NavBarRect.anchorMax = new Vector2(0.5f, 1f);
NavBarRect.anchoredPosition = new Vector2(NavBarRect.anchoredPosition.x, 0);
NavBarRect.sizeDelta = NAVBAR_DIMENSIONS;
break;
2021-05-13 23:03:30 +10:00
case VerticalAnchor.Bottom:
NavBarRect.anchorMin = new Vector2(0.5f, 0f);
NavBarRect.anchorMax = new Vector2(0.5f, 0f);
NavBarRect.anchoredPosition = new Vector2(NavBarRect.anchoredPosition.x, 35);
NavBarRect.sizeDelta = NAVBAR_DIMENSIONS;
break;
}
}
2021-05-13 23:03:30 +10:00
// listeners
2021-05-13 23:03:30 +10:00
private static void OnScreenDimensionsChanged()
{
Display display = DisplayManager.ActiveDisplay;
lastScreenWidth = display.renderingWidth;
lastScreenHeight = display.renderingHeight;
2022-04-14 01:25:59 +10:00
foreach (KeyValuePair<Panels, UEPanel> panel in UIPanels)
{
panel.Value.EnsureValidSize();
2022-04-14 01:25:59 +10:00
panel.Value.EnsureValidPosition();
panel.Value.Dragger.OnEndResize();
}
2021-05-13 23:03:30 +10:00
}
private static void OnCloseButtonClicked()
{
ShowMenu = false;
}
private static void Master_Toggle_OnValueChanged(KeyCode val)
{
closeBtn.ButtonText.text = val.ToString();
}
// UI Construction
2021-04-28 23:58:13 +10:00
private static void CreateTopNavBar()
2021-04-23 21:50:58 +10:00
{
GameObject navbarPanel = UIFactory.CreateUIObject("MainNavbar", UIRoot);
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(navbarPanel, false, false, true, true, 5, 4, 4, 4, 4, TextAnchor.MiddleCenter);
2021-04-28 23:58:13 +10:00
navbarPanel.AddComponent<Image>().color = new Color(0.1f, 0.1f, 0.1f);
NavBarRect = navbarPanel.GetComponent<RectTransform>();
NavBarRect.pivot = new Vector2(0.5f, 1f);
NavbarAnchor = ConfigManager.Main_Navbar_Anchor.Value;
SetNavBarAnchor();
2021-06-05 19:36:09 +10:00
ConfigManager.Main_Navbar_Anchor.OnValueChanged += (VerticalAnchor val) =>
{
NavbarAnchor = val;
SetNavBarAnchor();
};
2021-04-23 21:50:58 +10:00
2021-04-28 23:58:13 +10:00
// UnityExplorer title
2022-04-18 21:39:17 +10:00
string titleTxt = $"UE <i><color=grey>{ExplorerCore.VERSION}</color></i>";
Text title = UIFactory.CreateLabel(navbarPanel, "Title", titleTxt, TextAnchor.MiddleCenter, default, true, 14);
UIFactory.SetLayoutElement(title.gameObject, minWidth: 75, flexibleWidth: 0);
// panel tabs
2021-04-23 21:50:58 +10:00
NavbarTabButtonHolder = UIFactory.CreateUIObject("NavTabButtonHolder", navbarPanel);
UIFactory.SetLayoutElement(NavbarTabButtonHolder, minHeight: 25, flexibleHeight: 999, flexibleWidth: 999);
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(NavbarTabButtonHolder, false, true, true, true, 4, 2, 2, 2, 2);
// Time scale widget
timeScaleWidget = new(navbarPanel);
//spacer
GameObject spacer = UIFactory.CreateUIObject("Spacer", navbarPanel);
UIFactory.SetLayoutElement(spacer, minWidth: 15);
2021-04-28 23:58:13 +10:00
// Hide menu button
2021-04-23 21:50:58 +10:00
closeBtn = UIFactory.CreateButton(navbarPanel, "CloseButton", ConfigManager.Master_Toggle.Value.ToString());
UIFactory.SetLayoutElement(closeBtn.Component.gameObject, minHeight: 25, minWidth: 60, flexibleWidth: 0);
2022-01-31 21:24:01 +11:00
RuntimeHelper.SetColorBlock(closeBtn.Component, new Color(0.63f, 0.32f, 0.31f),
2021-04-23 21:50:58 +10:00
new Color(0.81f, 0.25f, 0.2f), new Color(0.6f, 0.18f, 0.16f));
ConfigManager.Master_Toggle.OnValueChanged += Master_Toggle_OnValueChanged;
closeBtn.OnClick += OnCloseButtonClicked;
}
}
}