ScenePartition/Editor/ScenePartitionSceneViewEditor.cs
max 8b44c33813 Editor layout
- Cleanup the editors a bit
- Editor foldout scope
- Cell size to editor (this is user preference data, could be put in EditorPrefs later)
2023-07-09 23:01:48 +02:00

99 lines
4.1 KiB
C#

using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[InitializeOnLoad]
public class ScenePartitionSceneViewEditor : UnityEditor.Editor
{
public static int cellSize = 100;
private static ScenePartitionSO scenePartitionSO = null;
static ScenePartitionSceneViewEditor()
{
SceneView.duringSceneGui += HandleDuringSceneGui;
}
private static void HandleDuringSceneGui(SceneView sceneView)
{
if (Event.current.modifiers != EventModifiers.Control) return;
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out scenePartitionSO)) return;
Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition);
int gridId = SceneGrid.CalculateGridPosition(gridPosition, cellSize);
{ // Draw Cells.
foreach (int id in scenePartitionSO.Data.SceneGrid.Grid.Keys)
{
(int x, int y) = SceneGrid.IntToIntPair(id);
Vector3 cellOrign = new Vector3(x * cellSize, 0, y * cellSize);
Vector3[] lines = new Vector3[] {
new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top
new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top
new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom
new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top
};
Handles.DrawLines(lines);
}
}
if (!scenePartitionSO.Data.SceneGrid.Grid.ContainsKey(gridId)) return;
{ // Draw Selection.
int x = Mathf.FloorToInt(gridPosition.x / cellSize);
int z = Mathf.FloorToInt(gridPosition.z / cellSize);
Vector3 cellOrign = new Vector3(x * cellSize, 0, z * cellSize);
Vector3[] lines = new Vector3[] {
new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top
new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top
new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom
new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top
};
Color c = Handles.color;
Handles.color = Color.yellow;
Handles.DrawLines(lines);
Handles.color = c;
}
// Context menu to load.
if (Event.current.button == 1 && Event.current.type == EventType.MouseDown)
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent($"Load {gridId}"), false, Load, gridId);
menu.ShowAsContext();
Event.current.Use();
}
SceneView.RepaintAll();
}
private static Vector3 CalculateGridPosition(Vector2 mousePosition)
{
if (Event.current == null) return Vector3.zero;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.zero); // Horizontal plane at y = 0
if (!plane.Raycast(ray, out float distance)) return Vector3.zero;
Vector3 intersectionPoint = ray.GetPoint(distance);
return intersectionPoint;
}
private static void Load(object gridId)
{
if (scenePartitionSO == null) return;
if (gridId == null) return;
if (gridId is not int cellId) return;
scenePartitionSO.LoadCell(cellId);
}
}
}