using System;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting.APIUpdating;
using Object = UnityEngine.Object;
namespace UnityEditor.Tilemaps
{
/// Stores the selection made on a GridLayout.
[MovedFrom(true, "UnityEditor", "UnityEditor")]
[HelpURL("https://docs.unity3d.com/Manual/TilemapPainting-SelectionTool.html#GridSelect")]
[Serializable]
public class GridSelection : ScriptableObject
{
private static string kUpdateGridSelection = L10n.Tr("Update Grid Selection");
/// Callback for when the active GridSelection has changed.
public static event Action gridSelectionChanged;
[SerializeField]
private BoundsInt m_Position;
[SerializeField]
private GameObject m_Target;
[SerializeField]
private Object m_PreviousSelection;
[SerializeField]
private Scene m_Scene;
[SerializeField]
private GameObject m_OriginalPalette;
/// 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)
{
RegisterUndo();
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)
{
var newSelection = CreateInstance();
newSelection.m_PreviousSelection = Selection.activeObject;
newSelection.m_Target = target as GameObject;
newSelection.m_Position = bounds;
newSelection.m_OriginalPalette = null;
Undo.RegisterCreatedObjectUndo(newSelection, kUpdateGridSelection);
var currentGroup = Undo.GetCurrentGroup();
Selection.activeObject = newSelection;
Undo.CollapseUndoOperations(currentGroup);
if (gridSelectionChanged != null)
gridSelectionChanged();
}
/// Clears the active GridSelection.
public static void Clear()
{
if (active)
{
RegisterUndo();
selection.m_Position = new BoundsInt();
if (selection.m_Scene.IsValid())
{
DestroyImmediate(selection.m_Target);
selection.m_Target = null;
selection.m_OriginalPalette = null;
EditorSceneManager.ClosePreviewScene(selection.m_Scene);
}
Selection.activeObject = selection.m_PreviousSelection;
if (gridSelectionChanged != null)
gridSelectionChanged();
}
}
internal static void SaveStandalone()
{
if (!selection.m_Scene.IsValid()
|| selection.m_OriginalPalette == null
|| selection.m_Target == null)
return;
TilePaletteSaveUtility.SaveTilePalette(selection.m_OriginalPalette, selection.m_Target.transform.root.gameObject);
}
internal static void TransferToStandalone(GameObject palette)
{
if (!active)
return;
if (!selection.m_Scene.IsValid())
{
selection.m_Scene = EditorSceneManager.NewPreviewScene();
if (!selection.m_Scene.IsValid())
throw new InvalidOperationException("Preview scene could not be created");
}
SceneManager.MoveGameObjectToScene(selection.m_Target.transform.root.gameObject, selection.m_Scene);
selection.m_OriginalPalette = palette;
}
internal static void RegisterUndo()
{
if (selection != null)
Undo.RegisterCompleteObjectUndo(selection, kUpdateGridSelection);
}
private void OnDisable()
{
Clear();
}
}
}