update text gizmo

This commit is contained in:
Mikhail 2025-12-01 17:39:16 +08:00
parent c583d28079
commit e23fcbc74b
6 changed files with 80 additions and 37 deletions

View File

@ -1,6 +1,7 @@
#if DISABLE_DEBUG
#undef DEBUG
#endif
using System;
using UnityEngine;
namespace DCFApixels
@ -8,80 +9,72 @@ namespace DCFApixels
/// <summary>
/// All additional settings for text rendering are stored here.
/// </summary>
public readonly struct DebugXTextSettings
public struct DebugXTextSettings
{
public const TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.MiddleLeft;
public const int DEFAULT_FONT_SIZE = 16;
public const float SCREEN_SPACE_SCALE_BLEND = 0f;
public const float WORLD_SPACE_SCALE_BLEND = 1f;
public const float SCREEN_SPACE_SCALE_FACTOR = 0f;
public const float WORLD_SPACE_SCALE_FACTOR = 1f;
public static readonly DebugXTextSettings ScreenSpace = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, Color.clear, 0);
public static readonly DebugXTextSettings WorldSpace = ScreenSpace.InWorldSpace();
public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0);
public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor();
/// <summary>
/// Font size. Default is <see cref="DEFAULT_FONT_SIZE" />.
/// </summary>
public readonly int FontSize;
/// <summary>
/// Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary>
public readonly TextAnchor TextAnchor;
public readonly Color BackgroundColor;
public readonly float WorldSpaceScaleFactor;
/// <summary> Font size. Default is <see cref="DEFAULT_FONT_SIZE" />. </summary>
public int FontSize;
/// <summary> Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />. </summary>
public TextAnchor TextAnchor;
public Color BackgroundColor;
public float WorldSpaceBlendMultiplier;
// ReSharper disable once UnusedMember.Global
public bool IsHasBackground => BackgroundColor.a > 0;
public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor, float worldSpaceScaleFactor)
public bool IsHasBackground { get { return BackgroundColor.a > 0; } }
public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor, float worldSpaceBlendMultiplier)
{
FontSize = fontSize;
TextAnchor = textAnchor;
BackgroundColor = backgroundColor;
WorldSpaceScaleFactor = worldSpaceScaleFactor;
WorldSpaceBlendMultiplier = worldSpaceBlendMultiplier;
}
/// <summary>
/// Set font size. Default is <see cref="DEFAULT_FONT_SIZE" />.
/// </summary>
public DebugXTextSettings SetSize(int fontSize)
public DebugXTextSettings Size(int fontSize)
{
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor);
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
/// <summary>
/// Sets text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary>
public DebugXTextSettings SetAnchor(TextAnchor textAnchor)
public DebugXTextSettings Anchor(TextAnchor textAnchor)
{
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor);
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
/// <summary>
/// Sets background image color behind text. Ignored if transparent.
/// </summary>
public DebugXTextSettings SetBackground(Color backgroundColor)
public DebugXTextSettings Background(Color backgroundColor)
{
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor);
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceBlendMultiplier);
}
/// <summary>
/// Synchronizes the text scale in screen space. The text will remain the same size on the screen.
/// </summary>
// ReSharper disable once UnusedMember.Global
public DebugXTextSettings SetScreenSpaceScaleFactor()
public DebugXTextSettings InScreenSpace()
{
return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_FACTOR);
return ScreenWorldSpaceBlend(SCREEN_SPACE_SCALE_BLEND);
}
/// <summary>
/// Synchronizes the text scale in world space. The text will remain the same size on the scene.
/// </summary>
public DebugXTextSettings SetWorldSpaceScaleFactor()
public DebugXTextSettings InWorldSpace()
{
return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_FACTOR);
return ScreenWorldSpaceBlend(WORLD_SPACE_SCALE_BLEND);
}
/// <summary>
@ -93,9 +86,59 @@ namespace DCFApixels
/// 1 - world space<br />
/// Values in between [0.00 - 1.00] blend these spaces together.
/// </param>
public DebugXTextSettings ScreenWorldSpaceBlend(float t)
{
return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, t);
}
#region Obsolete
[Obsolete("Use " + nameof(WorldSpaceBlendMultiplier))]
public float WorldSpaceScaleFactor
{
get => WorldSpaceBlendMultiplier;
set => WorldSpaceBlendMultiplier = value;
}
[Obsolete("Use " + nameof(ScreenSpace))]
public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0);
[Obsolete("Use " + nameof(WorldSpace))]
public static readonly DebugXTextSettings WorldSpaceScale = Default.InWorldSpace();
[Obsolete("Use " + nameof(SCREEN_SPACE_SCALE_BLEND))]
public const float SCREEN_SPACE_SCALE_FACTOR = 0f;
[Obsolete("Use " + nameof(WORLD_SPACE_SCALE_BLEND))]
public const float WORLD_SPACE_SCALE_FACTOR = 1f;
[Obsolete("Use " + nameof(Size))]
public DebugXTextSettings SetSize(int fontSize)
{
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(Anchor))]
public DebugXTextSettings SetAnchor(TextAnchor textAnchor)
{
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(Background))]
public DebugXTextSettings SetBackground(Color backgroundColor)
{
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(InScreenSpace))]
public DebugXTextSettings SetScreenSpaceScaleFactor()
{
return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_BLEND);
}
[Obsolete("Use " + nameof(InWorldSpace))]
public DebugXTextSettings SetWorldSpaceScaleFactor()
{
return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_BLEND);
}
[Obsolete("Use " + nameof(ScreenWorldSpaceBlend))]
public DebugXTextSettings SetCustomSpaceScaleFactor(float factor)
{
return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, factor);
}
#endregion
}
}

View File

@ -16,7 +16,7 @@ namespace DCFApixels
private static bool _singleWarningToggle = true;
#endif
[IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.Default);
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.ScreenSpace);
[IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings)
{

View File

@ -77,7 +77,7 @@ namespace DCFApixels
GUIStyle style = _labelStyle;
var zoom = GetCameraZoom(camera, item.Value.Position);
float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor);
float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceBlendMultiplier);
style.fontSize = Mathf.Max(1, Mathf.FloorToInt(fontSize));
style.alignment = item.Value.Settings.TextAnchor;

View File

@ -30,7 +30,7 @@ namespace DCFApixels.DebugXCore.Samples
i++; DebugX.Draw(GetColor(Points[i])).BillboardCross(Points[i].position, Points[i].localScale.x);
i++; DebugX.Draw(GetColor(Points[i])).BillboardCircle(Points[i].position, Points[i].localScale.x * RADIUS_M);
i++; DebugX.Draw(GetColor(Points[i])).WireMesh<SphereMesh>(Points[i].position, Points[i].rotation, Points[i].localScale * RADIUS_M);
i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.WorldSpaceScale.SetSize(26).SetBackground(TextBackgroundColor));
i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.WorldSpace.SetSize(26).SetBackground(TextBackgroundColor));
i++; DebugX.Draw(GetColor(Points[i])).Dot(Points[i].position);
i++; DebugX.Draw(GetColor(Points[i])).WireDot(Points[i].position);

View File

@ -56,7 +56,7 @@ namespace DCFApixels.DebugXCore.Samples
hit = Physics2D.CapsuleCast(ray.origin, point.localScale, CapsuleDirection2D.Vertical, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
DebugX.Draw(GetColor(point)).CapsuleCast2D(ray, point.eulerAngles.z, point.localScale, CapsuleDirection2D.Vertical, hit);
#else
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpace.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
#endif
if (Application.isPlaying && RotatedTransform)

View File

@ -58,7 +58,7 @@ namespace DCFApixels.DebugXCore.Samples
Physics.CapsuleCast(point1, point2, point.localScale.x * RADIUS_M, ray.direction, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).CapsuleCast(point1, point2, ray.direction, point.localScale.x * RADIUS_M, hit);
#else
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS3D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS3D\" define", DebugXTextSettings.WorldSpace.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
#endif
if (Application.isPlaying && RotatedTransform)