diff --git a/src/Inspectors/MouseInspector.cs b/src/Inspectors/MouseInspector.cs index 850903bd..72e444b5 100644 --- a/src/Inspectors/MouseInspector.cs +++ b/src/Inspectors/MouseInspector.cs @@ -30,7 +30,7 @@ public class MouseInspector : PanelBase MouseInspectMode.World => worldInspector, _ => null, }; - + private static Vector3 lastMousePos; // UIPanel @@ -44,10 +44,11 @@ public class MouseInspector : PanelBase public override Vector2 DefaultAnchorMax => Vector2.zero; public override bool CanDragAndResize => false; - - internal Text objNameLabel; - internal Text objPathLabel; - internal Text mousePosLabel; + + private Text inspectorLabelTitle; + private Text objNameLabel; + private Text objPathLabel; + private Text mousePosLabel; public MouseInspector(UIBase owner) : base(owner) { @@ -121,6 +122,45 @@ public bool TryUpdate() return Inspecting; } + + /// + /// Updates the title text in the inspector UI, if the inspector title label is assigned. + /// + /// The new title text to display in the inspector. + internal void UpdateInspectorTitle(string title) + { + // Unity null check - if inspectorLabelTitle is assigned, update its text. + if (inspectorLabelTitle) + { + inspectorLabelTitle.text = title; + } + } + + /// + /// Updates the object name label in the inspector UI, if the label is assigned. + /// + /// The new object name to display. + internal void UpdateObjectNameLabel(string name) + { + // Unity null check - if objNameLabel is assigned, update its text. + if (objNameLabel) + { + objNameLabel.text = name; + } + } + + /// + /// Updates the object path label in the inspector UI, if the label is assigned. + /// + /// The new object path to display. + internal void UpdateObjectPathLabel(string path) + { + // Unity null check - if objPathLabel is assigned, update its text. + if (objPathLabel) + { + objPathLabel.text = path; + } + } public void UpdateInspect() { @@ -181,7 +221,6 @@ public override void SetDefaultSizeAndPosition() Rect.pivot = new Vector2(0.5f, 1); Rect.sizeDelta = new Vector2(700, 150); } - protected override void ConstructPanelContent() { // hide title bar @@ -192,12 +231,11 @@ protected override void ConstructPanelContent() UIFactory.SetLayoutElement(inspectContent, flexibleWidth: 9999, flexibleHeight: 9999); // Title text - - Text title = UIFactory.CreateLabel(inspectContent, + inspectorLabelTitle = UIFactory.CreateLabel(inspectContent, "InspectLabel", - "Mouse Inspector (press ESC to cancel)", + "", TextAnchor.MiddleCenter); - UIFactory.SetLayoutElement(title.gameObject, flexibleWidth: 9999); + UIFactory.SetLayoutElement(inspectorLabelTitle.gameObject, flexibleWidth: 9999); mousePosLabel = UIFactory.CreateLabel(inspectContent, "MousePosLabel", "Mouse Position:", TextAnchor.MiddleCenter); @@ -218,4 +256,4 @@ protected override void ConstructPanelContent() //UIRoot.transform.SetParent(inspectorUIBase.RootObject.transform); } } -} +} \ No newline at end of file diff --git a/src/Inspectors/MouseInspectors/UiInspector.cs b/src/Inspectors/MouseInspectors/UiInspector.cs index 7dcccfbf..bdd648bb 100644 --- a/src/Inspectors/MouseInspectors/UiInspector.cs +++ b/src/Inspectors/MouseInspectors/UiInspector.cs @@ -17,10 +17,13 @@ public class UiInspector : MouseInspectorBase private static readonly List wasDisabledCanvasGroups = new(); private static readonly List objectsAddedCastersTo = new(); + private const string DEFAULT_INSPECTOR_TITLE = "UI Inspector (press ESC to cancel)"; + public override void OnBeginMouseInspect() { SetupUIRaycast(); - MouseInspector.Instance.objPathLabel.text = ""; + MouseInspector.Instance.UpdateInspectorTitle(DEFAULT_INSPECTOR_TITLE); + MouseInspector.Instance.UpdateObjectPathLabel(""); } public override void ClearHitData() @@ -70,9 +73,9 @@ public override void UpdateMouseInspect(Vector2 mousePos) } if (currentHitObjects.Any()) - MouseInspector.Instance.objNameLabel.text = $"Click to view UI Objects under mouse: {currentHitObjects.Count}"; + MouseInspector.Instance.UpdateObjectNameLabel($"Click to view UI Objects under mouse: {currentHitObjects.Count}"); else - MouseInspector.Instance.objNameLabel.text = $"No UI objects under mouse."; + MouseInspector.Instance.UpdateObjectNameLabel("No UI objects under mouse."); } private static void SetupUIRaycast() @@ -140,4 +143,4 @@ public override void OnEndInspect() wasDisabledGraphics.Clear(); } } -} +} \ No newline at end of file diff --git a/src/Inspectors/MouseInspectors/WorldInspector.cs b/src/Inspectors/MouseInspectors/WorldInspector.cs index aa518f4e..dc035155 100644 --- a/src/Inspectors/MouseInspectors/WorldInspector.cs +++ b/src/Inspectors/MouseInspectors/WorldInspector.cs @@ -7,15 +7,31 @@ public class WorldInspector : MouseInspectorBase public override void OnBeginMouseInspect() { - MainCamera = Camera.main; - - if (!MainCamera) + if (!EnsureMainCamera()) { - ExplorerCore.LogWarning("No MainCamera found! Cannot inspect world!"); - return; + ExplorerCore.LogWarning("No valid cameras found! Cannot inspect world!"); } } + /// + /// Checks if the given camera is valid. If it is, assigns it as the MainCamera, + /// updates the inspector title, logs its usage, and returns true. + /// Returns false if the camera is invalid. + /// + /// The camera to validate and possibly assign. + /// True if the camera is valid and assigned as MainCamera; false otherwise. + private static bool IsValidCam(Camera cam) + { + if (!cam) return false; + + MainCamera = cam; + MouseInspector.Instance.UpdateInspectorTitle( + $"World Inspector ({MainCamera.name}) (press ESC to cancel)" + ); + ExplorerCore.Log($"Using '{MainCamera.transform.GetTransformPath(true)}'"); + return true; + } + public override void ClearHitData() { lastHitObject = null; @@ -25,12 +41,40 @@ public override void OnSelectMouseInspect() { InspectorManager.Inspect(lastHitObject); } + + /// + /// Attempts to ensure that MainCamera is assigned. Tries Camera.main, then looks for a camera + /// named "Main Camera" or "MainCamera", and finally falls back to the first available camera. + /// If no cameras are available, logs a warning and returns null. + /// + private static Camera EnsureMainCamera() + { + if (MainCamera) + return MainCamera; + + if (IsValidCam(Camera.main)) + return MainCamera; + + ExplorerCore.LogWarning("No Camera.main found, trying to find a camera named 'Main Camera' or 'MainCamera'..."); + var namedCam = Camera.allCameras.FirstOrDefault(c => c.name is "Main Camera" or "MainCamera"); + if (IsValidCam(namedCam)) + return MainCamera; + + ExplorerCore.LogWarning("No camera named 'Main Camera' or 'MainCamera' found, using the first camera created..."); + var fallbackCam = Camera.allCameras.FirstOrDefault(); + if (IsValidCam(fallbackCam)) + return MainCamera; + + // If we reach here, no cameras were found at all. + ExplorerCore.LogWarning("No valid cameras found!"); + return null; + } public override void UpdateMouseInspect(Vector2 mousePos) { - if (!MainCamera) - MainCamera = Camera.main; - if (!MainCamera) + // Attempt to ensure camera each time UpdateMouseInspect is called + // in case something changed or wasn't set initially. + if (!EnsureMainCamera()) { ExplorerCore.LogWarning("No Main Camera was found, unable to inspect world!"); MouseInspector.Instance.StopInspect(); @@ -51,8 +95,8 @@ internal void OnHitGameObject(GameObject obj) if (obj != lastHitObject) { lastHitObject = obj; - MouseInspector.Instance.objNameLabel.text = $"Click to Inspect: {obj.name}"; - MouseInspector.Instance.objPathLabel.text = $"Path: {obj.transform.GetTransformPath(true)}"; + MouseInspector.Instance.UpdateObjectNameLabel($"Click to Inspect: {obj.name}"); + MouseInspector.Instance.UpdateObjectPathLabel($"Path: {obj.transform.GetTransformPath(true)}"); } } @@ -61,4 +105,4 @@ public override void OnEndInspect() // not needed } } -} +} \ No newline at end of file