|
|
|
@@ -0,0 +1,167 @@
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[CustomEditor(typeof(MapCoordinateSystem))]
|
|
|
|
|
public sealed class MapCoordinateSystemEditor : Editor
|
|
|
|
|
{
|
|
|
|
|
public override void OnInspectorGUI()
|
|
|
|
|
{
|
|
|
|
|
DrawDefaultInspector();
|
|
|
|
|
EditorGUILayout.HelpBox(
|
|
|
|
|
"Select MapCoordinates to display its grid and live crosshair coordinates in the Scene view. " +
|
|
|
|
|
"This visualization is not rendered in the Game view or in builds.",
|
|
|
|
|
MessageType.Info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSceneGUI()
|
|
|
|
|
{
|
|
|
|
|
MapCoordinateSystem coordinateSystem = (MapCoordinateSystem)target;
|
|
|
|
|
if (!coordinateSystem.ShowSceneVisualization || !coordinateSystem.HasValidCorners())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DrawGrid(coordinateSystem);
|
|
|
|
|
DrawCorners(coordinateSystem);
|
|
|
|
|
DrawCrosshairs(coordinateSystem);
|
|
|
|
|
DrawTargets(coordinateSystem);
|
|
|
|
|
DrawPlayers(coordinateSystem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawGrid(MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
int divisions = coordinateSystem.GridDivisions;
|
|
|
|
|
GUIStyle labelStyle = CreateLabelStyle(coordinateSystem.BoundaryColor);
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index <= divisions; index++)
|
|
|
|
|
{
|
|
|
|
|
float normalized = (float)index / divisions;
|
|
|
|
|
|
|
|
|
|
Vector3 verticalStart = Vector3.Lerp(
|
|
|
|
|
coordinateSystem.BottomLeft.position,
|
|
|
|
|
coordinateSystem.BottomRight.position,
|
|
|
|
|
normalized);
|
|
|
|
|
Vector3 verticalEnd = Vector3.Lerp(
|
|
|
|
|
coordinateSystem.TopLeft.position,
|
|
|
|
|
coordinateSystem.TopRight.position,
|
|
|
|
|
normalized);
|
|
|
|
|
Vector3 horizontalStart = Vector3.Lerp(
|
|
|
|
|
coordinateSystem.BottomLeft.position,
|
|
|
|
|
coordinateSystem.TopLeft.position,
|
|
|
|
|
normalized);
|
|
|
|
|
Vector3 horizontalEnd = Vector3.Lerp(
|
|
|
|
|
coordinateSystem.BottomRight.position,
|
|
|
|
|
coordinateSystem.TopRight.position,
|
|
|
|
|
normalized);
|
|
|
|
|
|
|
|
|
|
bool isBoundary = index == 0 || index == divisions;
|
|
|
|
|
Handles.color = isBoundary ? coordinateSystem.BoundaryColor : coordinateSystem.GridColor;
|
|
|
|
|
Handles.DrawLine(verticalStart, verticalEnd);
|
|
|
|
|
Handles.DrawLine(horizontalStart, horizontalEnd);
|
|
|
|
|
|
|
|
|
|
if (!coordinateSystem.ShowCoordinateLabels)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
float horizontalCoordinate = coordinateSystem.MaximumCoordinates.x * normalized;
|
|
|
|
|
float verticalCoordinate = coordinateSystem.MaximumCoordinates.y * normalized;
|
|
|
|
|
Handles.Label(verticalStart, horizontalCoordinate.ToString("0.##"), labelStyle);
|
|
|
|
|
Handles.Label(horizontalStart, verticalCoordinate.ToString("0.##"), labelStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawCorners(MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
Handles.color = coordinateSystem.BoundaryColor;
|
|
|
|
|
DrawCorner(coordinateSystem.BottomLeft, "(0, 0)", coordinateSystem);
|
|
|
|
|
DrawCorner(
|
|
|
|
|
coordinateSystem.BottomRight,
|
|
|
|
|
$"({coordinateSystem.MaximumCoordinates.x:0.##}, 0)",
|
|
|
|
|
coordinateSystem);
|
|
|
|
|
DrawCorner(
|
|
|
|
|
coordinateSystem.TopLeft,
|
|
|
|
|
$"(0, {coordinateSystem.MaximumCoordinates.y:0.##})",
|
|
|
|
|
coordinateSystem);
|
|
|
|
|
DrawCorner(
|
|
|
|
|
coordinateSystem.TopRight,
|
|
|
|
|
$"({coordinateSystem.MaximumCoordinates.x:0.##}, " +
|
|
|
|
|
$"{coordinateSystem.MaximumCoordinates.y:0.##})",
|
|
|
|
|
coordinateSystem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawCorner(
|
|
|
|
|
Transform corner,
|
|
|
|
|
string label,
|
|
|
|
|
MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
float size = coordinateSystem.MarkerSize * HandleUtility.GetHandleSize(corner.position);
|
|
|
|
|
Handles.SphereHandleCap(0, corner.position, Quaternion.identity, size, EventType.Repaint);
|
|
|
|
|
|
|
|
|
|
if (coordinateSystem.ShowCoordinateLabels)
|
|
|
|
|
Handles.Label(corner.position, label, CreateLabelStyle(coordinateSystem.BoundaryColor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawCrosshairs(MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
MapController[] controllers =
|
|
|
|
|
Object.FindObjectsByType<MapController>(FindObjectsSortMode.None);
|
|
|
|
|
|
|
|
|
|
foreach (MapController controller in controllers)
|
|
|
|
|
{
|
|
|
|
|
if (controller.CoordinateSystem != coordinateSystem || controller.CrosshairVisual == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Vector3 position = controller.CrosshairVisual.position;
|
|
|
|
|
Vector2 coordinates = coordinateSystem.WorldPositionToCoordinates(position);
|
|
|
|
|
float size = coordinateSystem.MarkerSize * 1.35f * HandleUtility.GetHandleSize(position);
|
|
|
|
|
|
|
|
|
|
Handles.color = Color.yellow;
|
|
|
|
|
Handles.SphereHandleCap(0, position, Quaternion.identity, size, EventType.Repaint);
|
|
|
|
|
Handles.Label(
|
|
|
|
|
position,
|
|
|
|
|
$"Crosshair ({coordinates.x:0.00}, {coordinates.y:0.00})",
|
|
|
|
|
CreateLabelStyle(Color.yellow));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawTargets(MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
if (!coordinateSystem.ShowCoordinateLabels)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
MapTarget[] targets = Object.FindObjectsByType<MapTarget>(FindObjectsSortMode.None);
|
|
|
|
|
foreach (MapTarget mapTarget in targets)
|
|
|
|
|
{
|
|
|
|
|
if (mapTarget.CoordinateSystem != null && mapTarget.CoordinateSystem != coordinateSystem)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Vector3 position = mapTarget.transform.position;
|
|
|
|
|
Vector2 coordinates = coordinateSystem.WorldPositionToCoordinates(position);
|
|
|
|
|
Handles.Label(
|
|
|
|
|
position,
|
|
|
|
|
$"{mapTarget.name} ({coordinates.x:0.00}, {coordinates.y:0.00})",
|
|
|
|
|
CreateLabelStyle(Color.red));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DrawPlayers(MapCoordinateSystem coordinateSystem)
|
|
|
|
|
{
|
|
|
|
|
MapPlayer[] players = Object.FindObjectsByType<MapPlayer>(FindObjectsSortMode.None);
|
|
|
|
|
foreach (MapPlayer mapPlayer in players)
|
|
|
|
|
{
|
|
|
|
|
if (mapPlayer.CoordinateSystem != null && mapPlayer.CoordinateSystem != coordinateSystem)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Vector3 position = mapPlayer.transform.position;
|
|
|
|
|
Vector2 coordinates = coordinateSystem.WorldPositionToCoordinates(position);
|
|
|
|
|
Handles.Label(
|
|
|
|
|
position,
|
|
|
|
|
$"MapPlayer ({coordinates.x:0.00}, {coordinates.y:0.00})",
|
|
|
|
|
CreateLabelStyle(Color.green));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static GUIStyle CreateLabelStyle(Color color)
|
|
|
|
|
{
|
|
|
|
|
GUIStyle style = new(EditorStyles.boldLabel);
|
|
|
|
|
style.normal.textColor = color;
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
}
|