Skip to content

Commit

Permalink
Merge pull request #35 from p1xel8ted/master
Browse files Browse the repository at this point in the history
UI/World Inspector Update
  • Loading branch information
yukieiji authored Dec 12, 2024
2 parents aa3030a + 9052e4e commit aae4fb1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 26 deletions.
60 changes: 49 additions & 11 deletions src/Inspectors/MouseInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MouseInspector : PanelBase
MouseInspectMode.World => worldInspector,
_ => null,
};

private static Vector3 lastMousePos;

// UIPanel
Expand All @@ -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)
{
Expand Down Expand Up @@ -121,6 +122,45 @@ public bool TryUpdate()

return Inspecting;
}

/// <summary>
/// Updates the title text in the inspector UI, if the inspector title label is assigned.
/// </summary>
/// <param name="title">The new title text to display in the inspector.</param>
internal void UpdateInspectorTitle(string title)
{
// Unity null check - if inspectorLabelTitle is assigned, update its text.
if (inspectorLabelTitle)
{
inspectorLabelTitle.text = title;
}
}

/// <summary>
/// Updates the object name label in the inspector UI, if the label is assigned.
/// </summary>
/// <param name="name">The new object name to display.</param>
internal void UpdateObjectNameLabel(string name)
{
// Unity null check - if objNameLabel is assigned, update its text.
if (objNameLabel)
{
objNameLabel.text = name;
}
}

/// <summary>
/// Updates the object path label in the inspector UI, if the label is assigned.
/// </summary>
/// <param name="path">The new object path to display.</param>
internal void UpdateObjectPathLabel(string path)
{
// Unity null check - if objPathLabel is assigned, update its text.
if (objPathLabel)
{
objPathLabel.text = path;
}
}

public void UpdateInspect()
{
Expand Down Expand Up @@ -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
Expand All @@ -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",
"<b>Mouse Inspector</b> (press <b>ESC</b> 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);

Expand All @@ -218,4 +256,4 @@ protected override void ConstructPanelContent()
//UIRoot.transform.SetParent(inspectorUIBase.RootObject.transform);
}
}
}
}
11 changes: 7 additions & 4 deletions src/Inspectors/MouseInspectors/UiInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public class UiInspector : MouseInspectorBase
private static readonly List<CanvasGroup> wasDisabledCanvasGroups = new();
private static readonly List<GameObject> objectsAddedCastersTo = new();

private const string DEFAULT_INSPECTOR_TITLE = "<b>UI Inspector</b> (press <b>ESC</b> to cancel)";

public override void OnBeginMouseInspect()
{
SetupUIRaycast();
MouseInspector.Instance.objPathLabel.text = "";
MouseInspector.Instance.UpdateInspectorTitle(DEFAULT_INSPECTOR_TITLE);
MouseInspector.Instance.UpdateObjectPathLabel("");
}

public override void ClearHitData()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -140,4 +143,4 @@ public override void OnEndInspect()
wasDisabledGraphics.Clear();
}
}
}
}
66 changes: 55 additions & 11 deletions src/Inspectors/MouseInspectors/WorldInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}

/// <summary>
/// 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.
/// </summary>
/// <param name="cam">The camera to validate and possibly assign.</param>
/// <returns>True if the camera is valid and assigned as MainCamera; false otherwise.</returns>
private static bool IsValidCam(Camera cam)
{
if (!cam) return false;

MainCamera = cam;
MouseInspector.Instance.UpdateInspectorTitle(
$"<b>World Inspector ({MainCamera.name})</b> (press <b>ESC</b> to cancel)"
);
ExplorerCore.Log($"Using '{MainCamera.transform.GetTransformPath(true)}'");
return true;
}

public override void ClearHitData()
{
lastHitObject = null;
Expand All @@ -25,12 +41,40 @@ public override void OnSelectMouseInspect()
{
InspectorManager.Inspect(lastHitObject);
}

/// <summary>
/// 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.
/// </summary>
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();
Expand All @@ -51,8 +95,8 @@ internal void OnHitGameObject(GameObject obj)
if (obj != lastHitObject)
{
lastHitObject = obj;
MouseInspector.Instance.objNameLabel.text = $"<b>Click to Inspect:</b> <color=cyan>{obj.name}</color>";
MouseInspector.Instance.objPathLabel.text = $"Path: {obj.transform.GetTransformPath(true)}";
MouseInspector.Instance.UpdateObjectNameLabel($"<b>Click to Inspect:</b> <color=cyan>{obj.name}</color>");
MouseInspector.Instance.UpdateObjectPathLabel($"Path: {obj.transform.GetTransformPath(true)}");
}
}

Expand All @@ -61,4 +105,4 @@ public override void OnEndInspect()
// not needed
}
}
}
}

0 comments on commit aae4fb1

Please sign in to comment.