UnityExplorer/src/Inspectors/MouseInspector.cs

236 lines
7.9 KiB
C#
Raw Normal View History

using UnityEngine;
2021-05-15 06:20:56 +10:00
using UnityEngine.UI;
using UnityExplorer.Config;
using UnityExplorer.Inspectors.MouseInspectors;
2021-05-15 06:20:56 +10:00
using UnityExplorer.UI;
using UnityExplorer.UI.Panels;
using UniverseLib.Input;
2021-12-02 18:35:46 +11:00
using UniverseLib.UI;
2022-04-14 01:25:59 +10:00
using UniverseLib.UI.Panels;
2022-01-31 21:24:01 +11:00
using UniverseLib.Utility;
2021-05-15 06:20:56 +10:00
namespace UnityExplorer.Inspectors
2021-05-15 06:20:56 +10:00
{
public enum MouseInspectMode
{
World,
UI
}
2022-04-14 01:25:59 +10:00
public class MouseInspector : UEPanel
2021-05-15 06:20:56 +10:00
{
public static MouseInspector Instance { get; private set; }
2021-05-15 06:20:56 +10:00
private readonly WorldInspector worldInspector;
private readonly UiInspector uiInspector;
2021-05-15 06:20:56 +10:00
public static bool Inspecting { get; set; }
public static MouseInspectMode Mode { get; set; }
public MouseInspectorBase CurrentInspector => Mode switch
2021-05-15 06:20:56 +10:00
{
MouseInspectMode.UI => uiInspector,
MouseInspectMode.World => worldInspector,
_ => null,
};
private static Vector3 lastMousePos;
2021-05-15 06:20:56 +10:00
// UIPanel
internal static readonly string UIBaseGUID = $"{ExplorerCore.GUID}.MouseInspector";
private UIBase inspectorUIBase;
2021-05-15 06:20:56 +10:00
public override string Name => "Inspect Under Mouse";
public override UIManager.Panels PanelType => UIManager.Panels.MouseInspector;
public override int MinWidth => -1;
public override int MinHeight => -1;
2022-04-14 01:25:59 +10:00
public override Vector2 DefaultAnchorMin => Vector2.zero;
public override Vector2 DefaultAnchorMax => Vector2.zero;
2021-05-15 06:20:56 +10:00
public override bool CanDragAndResize => false;
public override bool NavButtonWanted => false;
public override bool ShouldSaveActiveState => false;
public override bool ShowByDefault => false;
internal Text objNameLabel;
internal Text objPathLabel;
internal Text mousePosLabel;
2021-05-15 06:20:56 +10:00
2022-04-14 01:25:59 +10:00
public MouseInspector(UIBase owner) : base(owner)
{
Instance = this;
worldInspector = new WorldInspector();
uiInspector = new UiInspector();
}
2021-05-15 06:20:56 +10:00
public static void OnDropdownSelect(int index)
2021-05-15 06:20:56 +10:00
{
switch (index)
2021-06-11 17:36:17 +10:00
{
case 0: return;
case 1: Instance.StartInspect(MouseInspectMode.World); break;
case 2: Instance.StartInspect(MouseInspectMode.UI); break;
2021-06-11 17:36:17 +10:00
}
InspectorPanel.Instance.MouseInspectDropdown.value = 0;
}
2021-06-05 19:36:09 +10:00
public void StartInspect(MouseInspectMode mode)
{
2021-05-15 06:20:56 +10:00
Mode = mode;
Inspecting = true;
CurrentInspector.OnBeginMouseInspect();
2022-04-14 01:25:59 +10:00
PanelManager.ForceEndResize();
2021-05-15 06:20:56 +10:00
UIManager.NavBarRect.gameObject.SetActive(false);
2022-04-14 01:25:59 +10:00
UIManager.UiBase.Panels.PanelHolder.SetActive(false);
UIManager.UiBase.SetOnTop();
2021-05-15 06:20:56 +10:00
2022-04-14 01:25:59 +10:00
SetActive(true);
2021-05-15 06:20:56 +10:00
}
internal void ClearHitData()
{
CurrentInspector.ClearHitData();
2021-05-15 06:20:56 +10:00
objNameLabel.text = "No hits...";
objPathLabel.text = "";
}
public void StopInspect()
{
CurrentInspector.OnEndInspect();
ClearHitData();
2021-05-15 06:20:56 +10:00
Inspecting = false;
2021-05-15 06:20:56 +10:00
UIManager.NavBarRect.gameObject.SetActive(true);
2022-04-14 01:25:59 +10:00
UIManager.UiBase.Panels.PanelHolder.SetActive(true);
2021-06-05 19:36:09 +10:00
Dropdown drop = InspectorPanel.Instance.MouseInspectDropdown;
if (drop.transform.Find("Dropdown List") is Transform list)
drop.DestroyDropdownList(list.gameObject);
2021-05-15 06:20:56 +10:00
UIRoot.SetActive(false);
}
private static float timeOfLastRaycast;
public bool TryUpdate()
{
if (ConfigManager.World_MouseInspect_Keybind.Value != KeyCode.None)
{
if (InputManager.GetKeyDown(ConfigManager.World_MouseInspect_Keybind.Value))
Instance.StartInspect(MouseInspectMode.World);
}
if (ConfigManager.World_MouseInspect_Keybind.Value != KeyCode.None)
{
if (InputManager.GetKeyDown(ConfigManager.World_MouseInspect_Keybind.Value))
Instance.StartInspect(MouseInspectMode.World);
}
if (Inspecting)
UpdateInspect();
return Inspecting;
}
2021-05-15 06:20:56 +10:00
public void UpdateInspect()
{
if (InputManager.GetKeyDown(KeyCode.Escape))
{
StopInspect();
return;
}
if (InputManager.GetMouseButtonDown(0))
2021-05-15 06:20:56 +10:00
{
CurrentInspector.OnSelectMouseInspect();
2021-05-15 06:20:56 +10:00
StopInspect();
return;
}
Vector3 mousePos = InputManager.MousePosition;
2021-05-15 06:20:56 +10:00
if (mousePos != lastMousePos)
UpdatePosition(mousePos);
if (!timeOfLastRaycast.OccuredEarlierThan(0.1f))
return;
timeOfLastRaycast = Time.realtimeSinceStartup;
CurrentInspector.UpdateMouseInspect(mousePos);
2021-05-15 06:20:56 +10:00
}
internal void UpdatePosition(Vector2 mousePos)
{
lastMousePos = mousePos;
// use the raw mouse pos for the label
mousePosLabel.text = $"<color=grey>Mouse Position:</color> {mousePos.ToString()}";
// constrain the mouse pos we use within certain bounds
if (mousePos.x < 350)
mousePos.x = 350;
if (mousePos.x > Screen.width - 350)
mousePos.x = Screen.width - 350;
if (mousePos.y < Rect.rect.height)
mousePos.y += Rect.rect.height + 10;
2021-05-15 06:20:56 +10:00
else
mousePos.y -= 10;
// calculate and set our UI position
Vector3 inversePos = inspectorUIBase.RootObject.transform.InverseTransformPoint(mousePos);
2021-05-15 06:20:56 +10:00
UIRoot.transform.localPosition = new Vector3(inversePos.x, inversePos.y, 0);
}
// UI Construction
2022-04-14 01:25:59 +10:00
public override void SetDefaultSizeAndPosition()
2021-05-15 06:20:56 +10:00
{
2022-04-14 01:25:59 +10:00
base.SetDefaultSizeAndPosition();
Rect.anchorMin = Vector2.zero;
Rect.anchorMax = Vector2.zero;
Rect.pivot = new Vector2(0.5f, 1);
Rect.sizeDelta = new Vector2(700, 150);
2021-05-15 06:20:56 +10:00
}
2022-04-14 01:25:59 +10:00
protected override void ConstructPanelContent()
2021-05-15 06:20:56 +10:00
{
// hide title bar
this.TitleBar.SetActive(false);
2021-12-02 18:35:46 +11:00
this.UIRoot.transform.SetParent(UIManager.UIRoot.transform, false);
2021-05-15 06:20:56 +10:00
2022-04-14 01:25:59 +10:00
GameObject inspectContent = UIFactory.CreateVerticalGroup(this.ContentRoot, "InspectContent", true, true, true, true, 3, new Vector4(2, 2, 2, 2));
2021-05-15 06:20:56 +10:00
UIFactory.SetLayoutElement(inspectContent, flexibleWidth: 9999, flexibleHeight: 9999);
// Title text
Text title = UIFactory.CreateLabel(inspectContent,
"InspectLabel",
"<b>Mouse Inspector</b> (press <b>ESC</b> to cancel)",
TextAnchor.MiddleCenter);
2021-05-15 06:20:56 +10:00
UIFactory.SetLayoutElement(title.gameObject, flexibleWidth: 9999);
mousePosLabel = UIFactory.CreateLabel(inspectContent, "MousePosLabel", "Mouse Position:", TextAnchor.MiddleCenter);
objNameLabel = UIFactory.CreateLabel(inspectContent, "HitLabelObj", "No hits...", TextAnchor.MiddleLeft);
objNameLabel.horizontalOverflow = HorizontalWrapMode.Overflow;
objPathLabel = UIFactory.CreateLabel(inspectContent, "PathLabel", "", TextAnchor.MiddleLeft);
objPathLabel.fontStyle = FontStyle.Italic;
objPathLabel.horizontalOverflow = HorizontalWrapMode.Wrap;
UIFactory.SetLayoutElement(objPathLabel.gameObject, minHeight: 75);
UIRoot.SetActive(false);
// Create a new canvas for this panel to live on.
// It needs to always be shown on the main display, other panels can move displays.
inspectorUIBase = UniversalUI.RegisterUI(UIBaseGUID, null);
UIRoot.transform.SetParent(inspectorUIBase.RootObject.transform);
2021-05-15 06:20:56 +10:00
}
}
}