using System;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
using Object = UnityEngine.Object;
namespace UnityEditor.Tilemaps
{
/// Stores the selection made on a GridLayout.
[MovedFrom(true, "UnityEditor", "UnityEditor")]
public class GridSelection : ScriptableObject
{
/// Callback for when the active GridSelection has changed.
public static event Action gridSelectionChanged;
private BoundsInt m_Position;
private GameObject m_Target;
[SerializeField] private Object m_PreviousSelection;
/// Whether there is an active GridSelection made on a GridLayout.
public static bool active { get { return Selection.activeObject is GridSelection && selection.m_Target != null; } }
private static GridSelection selection { get { return Selection.activeObject as GridSelection; } }
/// The cell coordinates of the active GridSelection made on the GridLayout.
public static BoundsInt position
{
get { return selection != null ? selection.m_Position : new BoundsInt(); }
set
{
if (selection != null && selection.m_Position != value)
{
selection.m_Position = value;
if (gridSelectionChanged != null)
gridSelectionChanged();
}
}
}
/// The GameObject of the GridLayout where the active GridSelection was made.
public static GameObject target { get { return selection != null ? selection.m_Target : null; } }
/// The Grid of the target of the active GridSelection.
public static Grid grid { get { return selection != null && selection.m_Target != null ? selection.m_Target.GetComponentInParent() : null; } }
/// Creates a new GridSelection and sets it as the active GridSelection.
/// The target GameObject for the GridSelection.
/// The cell coordinates of selection made.
public static void Select(Object target, BoundsInt bounds)
{
GridSelection newSelection = CreateInstance();
newSelection.m_PreviousSelection = Selection.activeObject;
newSelection.m_Target = target as GameObject;
newSelection.m_Position = bounds;
Selection.activeObject = newSelection;
if (gridSelectionChanged != null)
gridSelectionChanged();
}
/// Clears the active GridSelection.
public static void Clear()
{
if (active)
{
selection.m_Position = new BoundsInt();
Selection.activeObject = selection.m_PreviousSelection;
if (gridSelectionChanged != null)
gridSelectionChanged();
}
}
}
}