// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine;
namespace FlaxEditor.Gizmo
{
///
/// Draws a grid to feel better world origin position and the world units.
///
///
[HideInEditor]
public class GridGizmo : GizmoBase
{
private bool _enabled = true;
///
/// Gets or sets a value indicating whether this is enabled.
///
public bool Enabled
{
get => _enabled;
set
{
if (_enabled != value)
{
_enabled = value;
EnabledChanged?.Invoke(this);
}
}
}
///
/// Occurs when enabled state gets changed.
///
public event Action EnabledChanged;
///
/// Initializes a new instance of the class.
///
/// The gizmos owner.
public GridGizmo(IGizmoOwner owner)
: base(owner)
{
}
///
public override void Draw(ref RenderContext renderContext)
{
if (!Enabled)
return;
var viewPos = Owner.ViewPosition;
var plane = new Plane(Vector3.Zero, Vector3.UnitY);
float dst = CollisionsHelper.DistancePlanePoint(ref plane, ref viewPos);
float space, size;
if (dst <= 500.0f)
{
space = 50;
size = 8000;
}
else if (dst <= 2000.0f)
{
space = 100;
size = 8000;
}
else
{
space = 1000;
size = 100000;
}
Color color = Color.Gray * 0.7f;
int count = (int)(size / space);
Vector3 start = new Vector3(0, 0, size * -0.5f);
Vector3 end = new Vector3(0, 0, size * 0.5f);
for (int i = 0; i <= count; i++)
{
start.X = end.X = i * space + start.Z;
DebugDraw.DrawLine(start, end, color);
}
start = new Vector3(size * -0.5f, 0, 0);
end = new Vector3(size * 0.5f, 0, 0);
for (int i = 0; i <= count; i++)
{
start.Z = end.Z = i * space + start.X;
DebugDraw.DrawLine(start, end, color);
}
}
}
}