12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- using System;
- using System.Collections.Generic;
- using UnityEditor.EditorTools;
- using UnityEngine;
- using UnityEngine.Tilemaps;
- using UnityEngine.UIElements;
- using Event = UnityEngine.Event;
-
- namespace UnityEditor.Tilemaps
- {
- [Serializable]
- internal class GridPaintPaletteClipboard : PaintableGrid
- {
- static class Styles
- {
- public static readonly GUIContent emptyProjectInfo = EditorGUIUtility.TrTextContent("Create a new palette in the dropdown above.");
- public static readonly GUIContent emptyPaletteInfo = EditorGUIUtility.TrTextContent("Drag Tile, Sprite or Sprite Texture assets here.");
- public static readonly GUIContent invalidPaletteInfo = EditorGUIUtility.TrTextContent("This is an invalid palette. Did you delete the palette asset?");
- public static readonly GUIContent invalidGridInfo = EditorGUIUtility.TrTextContent("The palette has an invalid Grid. Did you add a Grid to the palette asset?");
- }
-
-
-
- private static List<GridPaintPaletteClipboard> s_Instances;
- public static List<GridPaintPaletteClipboard> instances
- {
- get
- {
- if (s_Instances == null)
- s_Instances = new List<GridPaintPaletteClipboard>();
- return s_Instances;
- }
- }
-
- private bool disableOnBrushPicked;
- public event Action onBrushPicked;
-
- private static readonly string paletteSavedOutsideClipboard = L10n.Tr("Palette Asset {0} was changed outside of the Tile Palette. All changes in the Tile Palette made will be reverted.");
-
- private bool m_PaletteNeedsSave;
- private const float k_ZoomSpeed = 7f;
- private const float k_MinZoom = 10f; // How many pixels per cell at minimum
- private const float k_MaxZoom = 100f; // How many pixels per cell at maximum
- private const float k_Padding = 0.75f; // How many percentages of window size is the empty padding around the palette content
-
- private int m_KeyboardPanningID;
- private int m_MousePanningID;
- private Vector2 m_MouseZoomInitialPosition;
-
- private float k_KeyboardPanningSpeed = 3.0f;
-
- private Vector3 m_KeyboardPanning;
-
- private Rect m_GUIRect = new Rect(0, 0, 200, 200);
-
- public Rect guiRect
- {
- get => m_GUIRect;
- set
- {
- if (m_GUIRect == value)
- return;
- var oldValue = m_GUIRect;
- m_GUIRect = value;
- OnViewSizeChanged(oldValue, m_GUIRect);
- }
- }
-
- private VisualElement m_VisualElement;
-
- public bool activeDragAndDrop { get { return DragAndDrop.objectReferences.Length > 0 && guiRect.Contains(Event.current.mousePosition); } }
-
- [SerializeField] private bool m_CameraInitializedToBounds;
- [SerializeField] public bool m_CameraPositionSaved;
- [SerializeField] public Vector3 m_CameraPosition;
- [SerializeField] public float m_CameraOrthographicSize;
- [SerializeField] public GridLayout.CellSwizzle m_CameraSwizzleView;
-
- private BoundsInt? m_ActivePick;
- private Vector3Int m_ActivePivot;
- private Dictionary<Vector2Int, TileDragAndDropHoverData> m_HoverData;
- private bool m_Unlocked;
-
- private GameObject palette => GridPaintingState.palette;
- private GridBrushBase gridBrush => GridPaintingState.gridBrush;
-
- private PreviewRenderUtility m_PreviewUtility;
-
- internal Vector3 cameraPosition
- {
- get => m_PreviewUtility.camera.transform.position;
- set
- {
- m_PreviewUtility.camera.transform.position = value;
- ClampZoomAndPan();
- }
- }
-
- internal float cameraSize
- {
- get => m_PreviewUtility.camera.orthographicSize;
- set
- {
- m_PreviewUtility.camera.orthographicSize = value;
- ClampZoomAndPan();
- }
- }
-
- internal TransparencySortMode cameraTransparencySortMode
- {
- get => m_PreviewUtility.camera.transparencySortMode;
- set => m_PreviewUtility.camera.transparencySortMode = value;
- }
-
- internal Vector3 cameraTransparencySortAxis
- {
- get => m_PreviewUtility.camera.transparencySortAxis;
- set => m_PreviewUtility.camera.transparencySortAxis = value;
- }
-
- [SerializeField] private GameObject m_PaletteInstance;
-
- internal GameObject paletteInstance
- {
- get
- {
- if (m_PaletteInstance == null && palette != null && m_PreviewUtility != null)
- ResetPreviewInstance();
- return m_PaletteInstance;
- }
- }
-
- private Tilemap tilemap { get { return paletteInstance != null ? paletteInstance.GetComponentInChildren<Tilemap>() : null; } }
- private Grid grid { get { return paletteInstance != null ? paletteInstance.GetComponent<Grid>() : null; } }
- private Grid prefabGrid { get { return palette != null ? palette.GetComponent<Grid>() : null; } }
-
- private Mesh m_GridMesh;
- private int m_LastGridHash;
- private Material m_GridMaterial;
- private static readonly Color k_GridColor = Color.white.AlphaMultiplied(0.1f);
- private static readonly PrefColor tilePaletteBackgroundColor = new PrefColor("2D/Tile Palette Background"
- , 118.0f / 255.0f // Light
- , 118.0f / 255.0f
- , 118.0f / 255.0f
- , 127.0f / 255.0f
- , 31.0f / 255.0f // Dark
- , 31.0f / 255.0f
- , 31.0f / 255.0f
- , 127.0f / 255.0f);
-
- private bool m_PaletteUsed; // We mark palette used, when it has been changed in any way during being actively open.
- private Vector2? m_PreviousMousePosition;
-
- private bool m_DelayedResetPaletteInstance;
- internal void DelayedResetPreviewInstance()
- {
- m_DelayedResetPaletteInstance = true;
- }
-
- public TileBase activeTile
- {
- get
- {
- if (m_ActivePick.HasValue && m_ActivePick.Value.size == Vector3Int.one && GridPaintingState.defaultBrush != null && GridPaintingState.defaultBrush.cellCount > 0)
- return GridPaintingState.defaultBrush.cells[0].tile;
- return null;
- }
- }
-
- private RectInt bounds
- {
- get
- {
- RectInt r = default;
- if (tilemap == null || TilemapIsEmpty(tilemap))
- return r;
-
- tilemap.CompressBounds();
- var origin = tilemap.origin;
- var size = tilemap.size;
- r = new RectInt(origin.x, origin.y, size.x, size.y);
- return r;
- }
- }
-
- // Max area we are ever showing. Depends on the zoom level and content of palette.
- private Rect paddedBounds
- {
- get
- {
- var GUIAspect = m_GUIRect.width / m_GUIRect.height;
- var orthographicSize = m_PreviewUtility.camera.orthographicSize;
- var paddingW = orthographicSize * GUIAspect * k_Padding * 2f;
- var paddingH = orthographicSize * k_Padding * 2f;
-
- Bounds localBounds = grid.GetBoundsLocal(
- new Vector3(bounds.xMin, bounds.yMin, 0.0f),
- new Vector3(bounds.size.x, bounds.size.y, 0.0f));
- Rect result = new Rect(
- new Vector2(localBounds.min.x - paddingW, localBounds.min.y - paddingH),
- new Vector2(localBounds.size.x + paddingW * 2f, localBounds.size.y + paddingH * 2f));
-
- return result;
- }
- }
-
- private RectInt paddedBoundsInt
- {
- get
- {
- var min = Vector3Int.FloorToInt(paddedBounds.min);
- var max = Vector3Int.CeilToInt(paddedBounds.max);
- return new RectInt(min.x, min.y, max.x - min.x, max.y - min.y);
- }
- }
-
- private GameObject brushTarget
- {
- get
- {
- return (tilemap != null) ? tilemap.gameObject : (grid != null) ? grid.gameObject : null;
- }
- }
-
- public bool unlocked
- {
- get { return m_Unlocked; }
- set
- {
- if (value == false && m_Unlocked)
- {
- if (tilemap != null)
- tilemap.ClearAllEditorPreviewTiles();
- SavePaletteIfNecessary();
- }
- m_Unlocked = value;
- unlockedChanged?.Invoke(m_Unlocked);
- }
- }
- public event Action<bool> unlockedChanged;
-
- public bool isReceivingDragAndDrop { get { return m_HoverData != null && m_HoverData.Count > 0; } }
-
- public bool showNewEmptyClipboardInfo
- {
- get
- {
- if (paletteInstance == null)
- return false;
-
- if (tilemap == null)
- return false;
-
- if (unlocked && inEditMode)
- return false;
-
- if (!TilemapIsEmpty(tilemap))
- return false;
-
- if (tilemap.transform.childCount > 0)
- return false;
-
- if (isReceivingDragAndDrop)
- return false;
-
- // If user happens to erase the last content of used palette, we don't want to show the new palette info anymore
- if (m_PaletteUsed)
- return false;
-
- return true;
- }
- }
-
- public bool isModified { get { return m_PaletteNeedsSave; } }
-
- public VisualElement attachedVisualElement
- {
- set { m_VisualElement = value; }
- }
-
- public void OnBeforePaletteSelectionChanged()
- {
- SavePaletteIfNecessary();
- DestroyPreviewInstance();
- FlushHoverData();
- }
-
- private void FlushHoverData()
- {
- if (m_HoverData != null)
- {
- m_HoverData.Clear();
- m_HoverData = null;
- }
- }
-
- public void OnAfterPaletteSelectionChanged()
- {
- m_PaletteUsed = false;
- ResetPreviewInstance();
-
- if (palette != null)
- ResetPreviewCamera();
- }
-
- public void SetupPreviewCameraOnInit()
- {
- if (m_CameraPositionSaved)
- LoadSavedCameraPosition();
- else
- ResetPreviewCamera();
- }
-
- private void LoadSavedCameraPosition()
- {
- m_PreviewUtility.camera.transform.position = m_CameraPosition;
- m_PreviewUtility.camera.orthographicSize = m_CameraOrthographicSize;
- m_PreviewUtility.camera.nearClipPlane = 0.01f;
- m_PreviewUtility.camera.farClipPlane = 100f;
- }
-
- private Vector3 GetCameraPositionFromXYZ(Vector3 xyzPosition)
- {
- var position = Grid.Swizzle(m_CameraSwizzleView, xyzPosition);
- position = GetCameraPosition(position);
- return position;
- }
-
- private Vector3 GetCameraPosition(Vector3 xyzPosition)
- {
- var position = xyzPosition;
- switch (m_CameraSwizzleView)
- {
- case GridLayout.CellSwizzle.XZY:
- {
- position.y = 10f;
- }
- break;
- case GridLayout.CellSwizzle.YZX:
- {
- position.y = -10f;
- }
- break;
- case GridLayout.CellSwizzle.ZYX:
- {
- position.x = 10f;
- }
- break;
- case GridLayout.CellSwizzle.ZXY:
- {
- position.x = -10f;
- }
- break;
- case GridLayout.CellSwizzle.YXZ:
- {
- position.z = 10f;
- }
- break;
- case GridLayout.CellSwizzle.XYZ:
- default:
- {
- position.z = -10f;
- }
- break;
- }
- return position;
- }
-
- private void ResetPreviewCamera()
- {
- var transform = m_PreviewUtility.camera.transform;
-
- transform.position = GetCameraPositionFromXYZ(Vector3.zero);
- switch (m_CameraSwizzleView)
- {
- case GridLayout.CellSwizzle.XZY:
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(0, -1, 0), new Vector3(0, 0, 1));
- }
- break;
- case GridLayout.CellSwizzle.YZX:
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(0, 1, 0), new Vector3(1, 0, 0));
- }
- break;
- case GridLayout.CellSwizzle.ZXY:
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(1, 0, 0), new Vector3(0, 0, 1));
- }
- break;
- case GridLayout.CellSwizzle.ZYX:
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(-1, 0, 0), new Vector3(0, 1, 0));
- }
- break;
- case GridLayout.CellSwizzle.YXZ:
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(0, 0, -1), new Vector3(1, 0, 0));
- }
- break;
- case GridLayout.CellSwizzle.XYZ:
- default:
- {
- transform.rotation = Quaternion.identity;
- }
- break;
- }
-
- m_PreviewUtility.camera.nearClipPlane = 0.01f;
- m_PreviewUtility.camera.farClipPlane = 100f;
-
- FrameEntirePalette();
- }
-
- public void InitPreviewUtility()
- {
- m_PreviewUtility = new PreviewRenderUtility(true, true);
- m_PreviewUtility.camera.orthographic = true;
- m_PreviewUtility.camera.orthographicSize = 5f;
- m_PreviewUtility.camera.transform.position = new Vector3(0f, 0f, -10f);
- m_PreviewUtility.ambientColor = new Color(1f, 1f, 1f, 0);
- }
-
- public void ResetPreviewInstance()
- {
- // Store GridSelection for current Palette Instance
- Stack<int> childPositions = null;
- BoundsInt previousGridSelectionPosition = default;
- if (m_PaletteInstance != null && GridSelection.active && GridSelection.target.transform.IsChildOf(m_PaletteInstance.transform))
- {
- childPositions = new Stack<int>();
- var transform = GridSelection.target.transform;
- while (transform != null && transform != m_PaletteInstance.transform)
- {
- childPositions.Push(transform.GetSiblingIndex());
- transform = transform.parent;
- }
- previousGridSelectionPosition = GridSelection.position;
- ClearGridSelection();
- }
-
- DestroyPreviewInstance();
- if (palette != null)
- {
- m_PaletteInstance = m_PreviewUtility.InstantiatePrefabInScene(palette);
-
- // Disconnecting prefabs is no longer possible.
- // If performance of overrides on palette palette instance turns out to be a problem.
- // unpack the prefab instance here, and overwrite the prefab later instead of reconnecting.
- PrefabUtility.UnpackPrefabInstance(m_PaletteInstance, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
-
- EditorUtility.InitInstantiatedPreviewRecursive(m_PaletteInstance);
- m_PaletteInstance.transform.position = new Vector3(0, 0, 0);
- m_PaletteInstance.transform.rotation = Quaternion.identity;
- m_PaletteInstance.transform.localScale = Vector3.one;
-
- var paletteAsset = GridPaletteUtility.GetGridPaletteFromPaletteAsset(palette);
- if (paletteAsset != null)
- {
- // Handle Cell Sizing for Palette
- if (paletteAsset.cellSizing == GridPalette.CellSizing.Automatic)
- {
- var paletteGrid = m_PaletteInstance.GetComponent<Grid>();
- if (paletteGrid != null)
- {
- paletteGrid.cellSize = GridPaletteUtility.CalculateAutoCellSize(paletteGrid, paletteGrid.cellSize);
- }
- else
- {
- Debug.LogWarning("Grid component not found from: " + palette.name);
- }
- }
-
- // Handle Transparency Sort Settings
- m_PreviewUtility.camera.transparencySortMode = paletteAsset.transparencySortMode;
- m_PreviewUtility.camera.transparencySortAxis = paletteAsset.transparencySortAxis;
-
- // Handle Camera View for Grid
- m_CameraSwizzleView = GridLayout.CellSwizzle.XYZ;
- var instanceGrid = m_PaletteInstance.GetComponent<Grid>();
- if (instanceGrid != null)
- m_CameraSwizzleView = instanceGrid.cellSwizzle;
- }
- else
- {
- Debug.LogWarning("GridPalette subasset not found from: " + palette.name);
- m_PreviewUtility.camera.transparencySortMode = TransparencySortMode.Default;
- m_PreviewUtility.camera.transparencySortAxis = new Vector3(0f, 0f, 1f);
- }
-
- foreach (var transform in m_PaletteInstance.GetComponentsInChildren<Transform>())
- transform.gameObject.hideFlags = HideFlags.HideAndDontSave;
-
- // Show all renderers from Palettes from previous versions
- PreviewRenderUtility.SetEnabledRecursive(m_PaletteInstance, true);
-
- // Update preview Grid Mesh for new palette instance
- ResetPreviewGridMesh();
-
- // Restore GridSelection for new palette instance
- if (childPositions != null)
- {
- var transform = m_PaletteInstance.transform;
- while (childPositions.Count > 0)
- {
- var siblingIndex = childPositions.Pop();
- if (siblingIndex < transform.childCount)
- transform = transform.GetChild(siblingIndex);
- }
- GridSelection.Select(transform.gameObject, previousGridSelectionPosition);
- }
- }
-
- m_DelayedResetPaletteInstance = false;
- }
-
- public void DestroyPreviewInstance()
- {
- if (m_PaletteInstance != null)
- {
- Undo.ClearUndo(m_PaletteInstance);
- if (GridSelection.active && GridSelection.target == tilemap.gameObject)
- {
- GridSelection.TransferToStandalone(palette);
- }
- else
- {
- DestroyImmediate(m_PaletteInstance);
- }
- m_PaletteInstance = null;
- }
- }
-
- private void ResetPreviewGridMesh()
- {
- if (m_GridMesh != null)
- {
- DestroyImmediate(m_GridMesh);
- m_GridMesh = null;
- }
- m_GridMaterial = null;
- }
-
- public void FrameEntirePalette()
- {
- Frame(bounds);
- }
-
- private void Frame(RectInt rect)
- {
- if (grid == null)
- return;
-
- var position = grid.CellToLocalInterpolated(new Vector3(rect.center.x, rect.center.y, 0));
- position = GetCameraPosition(position);
-
- var height = (grid.CellToLocal(new Vector3Int(0, rect.yMax, 0)) - grid.CellToLocal(new Vector3Int(0, rect.yMin, 0))).magnitude;
- var width = (grid.CellToLocal(new Vector3Int(rect.xMax, 0, 0)) - grid.CellToLocal(new Vector3Int(rect.xMin, 0, 0))).magnitude;
-
- var cellSize = grid.cellSize;
- width += cellSize.x;
- height += cellSize.y;
-
- var GUIAspect = m_GUIRect.width / m_GUIRect.height;
- var contentAspect = width / height;
-
- m_PreviewUtility.camera.transform.position = position;
- m_PreviewUtility.camera.orthographicSize = (GUIAspect > contentAspect ? height : width / GUIAspect) / 2f;
-
- ClampZoomAndPan();
- }
-
- private void RefreshAllTiles()
- {
- if (tilemap != null)
- tilemap.RefreshAllTiles();
- }
-
- protected override void OnEnable()
- {
- base.OnEnable();
-
- instances.Add(this);
-
- EditorApplication.editorApplicationQuit += EditorApplicationQuit;
- PrefabUtility.prefabInstanceUpdated += PrefabInstanceUpdated;
- Undo.undoRedoPerformed += UndoRedoPerformed;
-
- m_KeyboardPanningID = GUIUtility.GetPermanentControlID();
- m_MousePanningID = GUIUtility.GetPermanentControlID();
-
- InitPreviewUtility();
- ResetPreviewInstance();
- SetupPreviewCameraOnInit();
- }
-
- protected override void OnDisable()
- {
- SavePaletteIfNecessary();
- unlocked = false;
- DestroyPreviewInstance();
-
- if (m_PreviewUtility != null && m_PreviewUtility.camera != null)
- {
- // Save Preview camera coordinates
- m_CameraPosition = m_PreviewUtility.camera.transform.position;
- m_CameraOrthographicSize = m_PreviewUtility.camera.orthographicSize;
- m_CameraPositionSaved = true;
- }
- if (m_PreviewUtility != null)
- m_PreviewUtility.Cleanup();
- m_PreviewUtility = null;
-
- Undo.undoRedoPerformed -= UndoRedoPerformed;
- PrefabUtility.prefabInstanceUpdated -= PrefabInstanceUpdated;
- EditorApplication.editorApplicationQuit -= EditorApplicationQuit;
-
- instances.Remove(this);
-
- base.OnDisable();
- }
-
- private void DisplayClipboardText(GUIContent clipboardText, Rect textPosition)
- {
- Color old = GUI.color;
- GUI.color = Color.gray;
- var infoSize = GUI.skin.label.CalcSize(clipboardText);
- Rect rect = new Rect(textPosition.center.x - infoSize.x * .5f, textPosition.center.y - infoSize.y * .5f, infoSize.x, infoSize.y);
- GUI.Label(rect, clipboardText);
- GUI.color = old;
- }
-
- public void OnClipboardGUI(Rect clipboardPosition)
- {
- if (Event.current.type != EventType.Layout && clipboardPosition.Contains(Event.current.mousePosition) && GridPaintingState.activeGrid != this && unlocked)
- {
- GridPaintingState.activeGrid = this;
- SceneView.RepaintAll();
- }
-
- // Validate palette (case 1017965)
- GUIContent paletteError = null;
- if (palette == null)
- {
- if (GridPaintingState.palettes.Count == 0)
- paletteError = Styles.emptyProjectInfo;
- else
- paletteError = Styles.invalidPaletteInfo;
- }
- else if (palette.GetComponent<Grid>() == null)
- {
- paletteError = Styles.invalidGridInfo;
- }
-
- if (paletteError != null)
- {
- DisplayClipboardText(paletteError, clipboardPosition);
- return;
- }
-
- bool oldEnabled = GUI.enabled;
- GUI.enabled = !showNewEmptyClipboardInfo || DragAndDrop.objectReferences.Length > 0;
- if (Event.current.type == EventType.Repaint)
- guiRect = clipboardPosition;
-
- EditorGUI.BeginChangeCheck();
- OnGUI();
- if (EditorGUI.EndChangeCheck())
- Repaint();
-
- GUI.enabled = oldEnabled;
-
- if (showNewEmptyClipboardInfo)
- {
- DisplayClipboardText(Styles.emptyPaletteInfo, clipboardPosition);
- }
- }
-
- public override void OnGUI()
- {
- if (Mathf.Approximately(guiRect.width, 0f) || Mathf.Approximately(guiRect.height, 0f))
- return;
-
- UpdateMouseGridPosition();
-
- HandleDragAndDrop();
-
- if (m_DelayedResetPaletteInstance)
- {
- var originalSwizzleView = m_CameraSwizzleView;
- ResetPreviewInstance();
- if (palette != null && originalSwizzleView != m_CameraSwizzleView)
- ResetPreviewCamera();
- }
-
- if (palette == null)
- return;
-
- HandlePanAndZoom();
- HandleKeyboardMousePick();
-
- if (showNewEmptyClipboardInfo)
- return;
-
- if (Event.current.type == EventType.Repaint && !m_CameraInitializedToBounds)
- {
- Frame(bounds);
- m_CameraInitializedToBounds = true;
- }
-
- HandleMouseEnterLeave();
-
- if (guiRect.Contains(Event.current.mousePosition) || Event.current.type != EventType.MouseDown)
- base.OnGUI();
-
- if (Event.current.type == EventType.Repaint
- || (unlocked && (inEditMode || GridSelectionTool.IsActive())))
- {
- Render();
- }
- else
- {
- RenderSelectedBrushMarquee();
- CallOnPaintSceneGUI(mouseGridPosition);
- }
- m_PreviousMousePosition = Event.current.mousePosition;
- }
-
- private void OnViewSizeChanged(Rect oldSize, Rect newSize)
- {
- if (Mathf.Approximately(oldSize.height * oldSize.width * newSize.height * newSize.width, 0f))
- return;
-
- Camera cam = m_PreviewUtility.camera;
-
- Vector2 sizeDelta = new Vector2(
- newSize.width / LocalToScreenRatio(newSize.height) - oldSize.width / LocalToScreenRatio(oldSize.height),
- newSize.height / LocalToScreenRatio(newSize.height) - oldSize.height / LocalToScreenRatio(oldSize.height));
-
- cam.transform.Translate(sizeDelta / 2f);
-
- ClampZoomAndPan();
- }
-
- private void EditorApplicationQuit()
- {
- SavePaletteIfNecessary();
- }
-
- private void UndoRedoPerformed()
- {
- if (!unlocked)
- return;
-
- m_PaletteNeedsSave = true;
- RefreshAllTiles();
- Repaint();
- }
-
- private void PrefabInstanceUpdated(GameObject updatedPrefab)
- {
- // case 947462: Reset the palette instance after its prefab has been updated as it could have been changed
- if (m_PaletteInstance != null && PrefabUtility.GetCorrespondingObjectFromSource(updatedPrefab) == palette && !GridPaintingState.savingPalette)
- {
- m_PaletteNeedsSave = true;
- RefreshAllTiles();
- Repaint();
- }
- }
-
- private void HandlePanAndZoom()
- {
- switch (Event.current.type)
- {
- case EventType.MouseDown:
- if (MousePanningEvent() && guiRect.Contains(Event.current.mousePosition) && GUIUtility.hotControl == 0)
- {
- GUIUtility.hotControl = m_MousePanningID;
- m_MouseZoomInitialPosition = Event.current.mousePosition;
- Event.current.Use();
- }
- break;
- case EventType.ValidateCommand:
- if (Event.current.commandName == EventCommandNames.FrameSelected)
- {
- Event.current.Use();
- }
- break;
- case EventType.ExecuteCommand:
- if (Event.current.commandName == EventCommandNames.FrameSelected)
- {
- if (m_ActivePick.HasValue)
- {
- var rect = new RectInt(m_ActivePick.Value.x, m_ActivePick.Value.y,
- m_ActivePick.Value.size.x, m_ActivePick.Value.size.y);
- Frame(rect);
- }
- else
- FrameEntirePalette();
- Event.current.Use();
- }
- break;
- case EventType.ScrollWheel:
- if (guiRect.Contains(Event.current.mousePosition))
- {
- HandleMouseZoom(Event.current.mousePosition);
- Event.current.Use();
- }
- break;
- case EventType.MouseDrag:
- if (GUIUtility.hotControl == m_MousePanningID)
- {
- if (Event.current.alt && Event.current.button == 1)
- {
- HandleMouseZoom(m_MouseZoomInitialPosition);
- }
- else
- {
- Vector3 delta = new Vector3(-Event.current.delta.x, Event.current.delta.y, 0f) / LocalToScreenRatio();
- m_PreviewUtility.camera.transform.Translate(delta);
- ClampZoomAndPan();
- }
- Event.current.Use();
- }
- break;
- case EventType.MouseMove: // Fix mouse cursor being stuck when panning ended outside our window
- if (GUIUtility.hotControl == m_MousePanningID && !MousePanningEvent())
- GUIUtility.hotControl = 0;
- break;
- case EventType.MouseUp:
- if (GUIUtility.hotControl == m_MousePanningID)
- {
- ClampZoomAndPan();
- GUIUtility.hotControl = 0;
- Event.current.Use();
- }
- break;
- case EventType.KeyDown:
- if ((GUIUtility.hotControl == 0 || GUIUtility.hotControl == m_KeyboardPanningID) && !Event.current.shift)
- {
- switch (Event.current.keyCode)
- {
- case KeyCode.LeftArrow:
- m_KeyboardPanning.x = -k_KeyboardPanningSpeed / LocalToScreenRatio();
- GUIUtility.hotControl = m_KeyboardPanningID;
- Event.current.Use();
- break;
- case KeyCode.RightArrow:
- m_KeyboardPanning.x = k_KeyboardPanningSpeed / LocalToScreenRatio();
- GUIUtility.hotControl = m_KeyboardPanningID;
- Event.current.Use();
- break;
- case KeyCode.UpArrow:
- m_KeyboardPanning.y = k_KeyboardPanningSpeed / LocalToScreenRatio();
- GUIUtility.hotControl = m_KeyboardPanningID;
- Event.current.Use();
- break;
- case KeyCode.DownArrow:
- m_KeyboardPanning.y = -k_KeyboardPanningSpeed / LocalToScreenRatio();
- GUIUtility.hotControl = m_KeyboardPanningID;
- Event.current.Use();
- break;
- }
- }
- break;
- case EventType.KeyUp:
- if (GUIUtility.hotControl == m_KeyboardPanningID)
- {
- m_KeyboardPanning = Vector3.zero;
- GUIUtility.hotControl = 0;
- Event.current.Use();
- }
- break;
- case EventType.Repaint:
- if (GUIUtility.hotControl == m_KeyboardPanningID)
- {
- m_PreviewUtility.camera.transform.Translate(m_KeyboardPanning);
- ClampZoomAndPan();
- Repaint();
- }
-
- if (GUIUtility.hotControl == m_MousePanningID)
- EditorGUIUtility.AddCursorRect(guiRect, MouseCursor.Pan);
-
- break;
- }
- }
-
- private void HandleMouseZoom(Vector2 currentMousePosition)
- {
- float zoomDelta = HandleUtility.niceMouseDeltaZoom * (Event.current.shift ? -9 : -3) * k_ZoomSpeed;
- Camera camera = m_PreviewUtility.camera;
- Vector3 oldLocalPos = ScreenToLocal(currentMousePosition);
- camera.orthographicSize = Mathf.Max(.0001f, camera.orthographicSize * (1 + zoomDelta * .001f));
- ClampZoomAndPan();
- Vector3 newLocalPos = ScreenToLocal(currentMousePosition);
- Vector3 localDelta = newLocalPos - oldLocalPos;
- camera.transform.position -= localDelta;
- ClampZoomAndPan();
- }
-
- private void HandleKeyboardMousePick()
- {
- if (GUIUtility.hotControl == 0 || GUIUtility.hotControl == m_KeyboardPanningID)
- {
- if (Event.current.type == EventType.KeyDown && Event.current.shift && m_ActivePick.HasValue)
- {
- var delta = Vector3Int.zero;
- switch (Event.current.keyCode)
- {
- case KeyCode.LeftArrow:
- delta = Vector3Int.left;
- break;
- case KeyCode.RightArrow:
- delta = Vector3Int.right;
- Event.current.Use();
- break;
- case KeyCode.UpArrow:
- delta = Vector3Int.up;
- Event.current.Use();
- break;
- case KeyCode.DownArrow:
- delta = Vector3Int.down;
- break;
- }
-
- if (delta != Vector3Int.zero)
- {
- disableOnBrushPicked = true;
- PickBrush(new BoundsInt(m_ActivePick.Value.position + delta, m_ActivePick.Value.size),
- m_ActivePivot);
- disableOnBrushPicked = false;
- Event.current.Use();
- }
- }
- }
- }
-
- private static bool MousePanningEvent()
- {
- return (Event.current.button == 0 && Event.current.alt || Event.current.button > 0);
- }
-
- private void ClampZoomAndPan()
- {
- float pixelsPerCell = grid.cellSize.y * LocalToScreenRatio();
- if (pixelsPerCell < k_MinZoom)
- m_PreviewUtility.camera.orthographicSize = (grid.cellSize.y * guiRect.height) / (k_MinZoom * 2f);
- else if (pixelsPerCell > k_MaxZoom)
- m_PreviewUtility.camera.orthographicSize = (grid.cellSize.y * guiRect.height) / (k_MaxZoom * 2f);
-
- Camera cam = m_PreviewUtility.camera;
- float cameraOrthographicSize = cam.orthographicSize;
- Rect r = paddedBounds;
-
- var camPos = cam.transform.position;
- var camLimit = Grid.Swizzle(m_CameraSwizzleView, new Vector3(cameraOrthographicSize * (guiRect.width / guiRect.height), cameraOrthographicSize));
- var camMin = camPos - camLimit;
- var camMax = camPos + camLimit;
- var rMin = Grid.Swizzle(m_CameraSwizzleView, r.min);
- var rMax = Grid.Swizzle(m_CameraSwizzleView, r.max);
-
- if (m_CameraSwizzleView != GridLayout.CellSwizzle.ZXY && m_CameraSwizzleView != GridLayout.CellSwizzle.ZYX)
- {
- if (camMin.x < rMin.x)
- {
- camPos += new Vector3(rMin.x - camMin.x, 0f, 0f);
- }
- if (camMax.x > rMax.x)
- {
- camPos += new Vector3(rMax.x - camMax.x, 0f, 0f);
- }
- }
-
- if (m_CameraSwizzleView != GridLayout.CellSwizzle.XZY && m_CameraSwizzleView != GridLayout.CellSwizzle.YZX)
- {
- if (camMin.y < rMin.y)
- {
- camPos += new Vector3(0f, rMin.y - camMin.y, 0f);
- }
-
- if (camMax.y > rMax.y)
- {
- camPos += new Vector3(0f, rMax.y - camMax.y, 0f);
- }
- }
-
- if (m_CameraSwizzleView != GridLayout.CellSwizzle.XYZ && m_CameraSwizzleView != GridLayout.CellSwizzle.YXZ)
- {
- if (camMin.z < rMin.z)
- {
- camPos += new Vector3(0f, 0f, rMin.z - camMin.z);
- }
- if (camMax.z > rMax.z)
- {
- camPos += new Vector3(0f, 0f, rMax.z - camMax.z);
- }
- }
-
- cam.transform.position = camPos;
-
- DestroyImmediate(m_GridMesh);
- m_GridMesh = null;
- }
-
- private void Render()
- {
- if (guiRect.width <= 0f || guiRect.height <= 0f)
- return;
-
- if (m_GridMesh != null && GetGridHash() != m_LastGridHash)
- {
- ResetPreviewInstance();
- ResetPreviewGridMesh();
- }
-
- using (new PreviewInstanceScope(guiRect, m_PreviewUtility, paletteInstance, GridPaintingState.drawGizmos))
- {
- m_PreviewUtility.Render(true);
- if (GridPaintingState.drawGridGizmo)
- RenderGrid();
- CallOnPaintSceneGUI(mouseGridPosition);
- if (GridPaintingState.drawGizmos)
- {
- // Set CameraType to SceneView to force Gizmos to be drawn
- var storedType = m_PreviewUtility.camera.cameraType;
- m_PreviewUtility.camera.cameraType = CameraType.SceneView;
- Handles.Internal_DoDrawGizmos(m_PreviewUtility.camera);
- m_PreviewUtility.camera.cameraType = storedType;
- }
- }
-
- RenderDragAndDropPreview();
- RenderSelectedBrushMarquee();
- CallOnSceneGUI();
-
- m_PreviewUtility.EndAndDrawPreview(guiRect);
- m_LastGridHash = GetGridHash();
- }
-
- private int GetGridHash()
- {
- var gridToHash = prefabGrid;
- if (gridToHash == null)
- return 0;
-
- int hash = gridToHash.GetHashCode();
- unchecked
- {
- hash = hash * 33 + gridToHash.cellGap.GetHashCode();
- hash = hash * 33 + gridToHash.cellLayout.GetHashCode();
- hash = hash * 33 + gridToHash.cellSize.GetHashCode();
- hash = hash * 33 + gridToHash.cellSwizzle.GetHashCode();
- hash = hash * 33 + SceneViewGridManager.sceneViewGridComponentGizmo.Color.GetHashCode();
- }
- return hash;
- }
-
- private void RenderDragAndDropPreview()
- {
- if (!activeDragAndDrop || m_HoverData == null || m_HoverData.Count == 0)
- return;
-
- var rect = TileDragAndDrop.GetMinMaxRect(m_HoverData.Keys);
- rect.position += mouseGridPosition;
- DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
- GridEditorUtility.DrawGridMarquee(grid, new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.width, rect.height, 1)), Color.white);
- }
-
- private void RenderGrid()
- {
- // MeshTopology.Lines doesn't give nice pixel perfect grid so we have to have separate codepath with MeshTopology.Quads specially for palette window here
- if (m_GridMesh == null && grid.cellLayout == GridLayout.CellLayout.Rectangle)
- {
- m_GridMesh = GridEditorUtility.GenerateCachedGridMesh(grid, k_GridColor, 1f / LocalToScreenRatio(), paddedBoundsInt, grid.cellSwizzle == GridLayout.CellSwizzle.XYZ ? MeshTopology.Quads : MeshTopology.Lines);
- }
- GridEditorUtility.DrawGridGizmo(grid, grid.transform, k_GridColor, ref m_GridMesh, ref m_GridMaterial);
- }
-
- private class PreviewInstanceScope : IDisposable
- {
- private readonly bool m_OldFog;
- private readonly bool m_DrawGizmos;
- private readonly Transform[] m_PaletteTransforms;
-
- public PreviewInstanceScope(Rect guiRect, PreviewRenderUtility previewRenderUtility, GameObject paletteInstance, bool drawGizmos)
- {
- m_DrawGizmos = drawGizmos;
- m_OldFog = RenderSettings.fog;
-
- previewRenderUtility.BeginPreview(guiRect, null);
-
- // Draw Background here with user preference color
- Graphics.DrawTexture(new Rect(0.0f, 0.0f
- , (float) 2 * EditorGUIUtility.pixelsPerPoint * guiRect.width
- , (float) 2 * EditorGUIUtility.pixelsPerPoint * guiRect.height)
- , (Texture) Texture2D.grayTexture, new Rect(0.0f, 0.0f, 1f, 1f)
- , 0, 0, 0, 0
- , tilePaletteBackgroundColor.Color, (Material) null);
-
- Unsupported.SetRenderSettingsUseFogNoDirty(false);
- if (m_DrawGizmos)
- {
- m_PaletteTransforms = paletteInstance.GetComponentsInChildren<Transform>();
- foreach (var transform in m_PaletteTransforms)
- transform.gameObject.hideFlags = HideFlags.None;
- // Case 1199516: Set Dirty on palette instance to force a refresh on gizmo drawing
- EditorUtility.SetDirty(paletteInstance);
- Unsupported.SceneTrackerFlushDirty();
- }
- var renderers = paletteInstance.GetComponentsInChildren<Renderer>();
- foreach (var renderer in renderers)
- {
- renderer.allowOcclusionWhenDynamic = false;
- }
- previewRenderUtility.AddManagedGO(paletteInstance);
- Handles.DrawCameraImpl(guiRect, previewRenderUtility.camera, DrawCameraMode.Textured, false, new DrawGridParameters(), true, false);
- }
-
- public void Dispose()
- {
- if (m_DrawGizmos && m_PaletteTransforms != null)
- {
- foreach (var transform in m_PaletteTransforms)
- transform.gameObject.hideFlags = HideFlags.HideAndDontSave;
- }
- Unsupported.SetRenderSettingsUseFogNoDirty(m_OldFog);
- }
- }
-
- private void HandleDragAndDrop()
- {
- if (DragAndDrop.objectReferences.Length == 0 || !guiRect.Contains(Event.current.mousePosition))
- return;
-
- switch (Event.current.type)
- {
- //TODO: Cache this
- case EventType.DragUpdated:
- {
- List<Texture2D> sheets = TileDragAndDrop.GetValidSpritesheets(DragAndDrop.objectReferences);
- List<Sprite> sprites = TileDragAndDrop.GetValidSingleSprites(DragAndDrop.objectReferences);
- List<TileBase> tiles = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
- m_HoverData = TileDragAndDrop.CreateHoverData(sheets, sprites, tiles, tilemap.cellLayout);
-
- if (m_HoverData != null && m_HoverData.Count > 0)
- {
- DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
- Event.current.Use();
- GUI.changed = true;
- }
- }
- break;
- case EventType.DragPerform:
- {
- if (m_HoverData == null || m_HoverData.Count == 0)
- return;
-
- RegisterUndo();
-
- var wasEmpty = TilemapIsEmpty(tilemap);
-
- var targetPosition = mouseGridPosition;
- DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
- var tileSheet = TileDragAndDrop.ConvertToTileSheet(m_HoverData);
- var i = 0;
- foreach (var item in m_HoverData)
- {
- if (i >= tileSheet.Count)
- break;
-
- var offset = Vector3.zero;
- if (item.Value.hasOffset)
- {
- offset = item.Value.positionOffset - tilemap.tileAnchor;
-
- var cellSize = tilemap.cellSize;
- if (wasEmpty)
- {
- cellSize = item.Value.scaleFactor;
- }
- offset.x *= cellSize.x;
- offset.y *= cellSize.y;
- offset.z *= cellSize.z;
- }
-
- SetTile(tilemap
- , targetPosition + item.Key
- , tileSheet[i++]
- , Color.white
- , Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one));
- }
- OnPaletteChanged();
-
- m_PaletteNeedsSave = true;
- FlushHoverData();
- GUI.changed = true;
- SavePaletteIfNecessary();
-
- if (wasEmpty)
- {
- ResetPreviewInstance();
- FrameEntirePalette();
- }
-
- Event.current.Use();
- GUIUtility.ExitGUI();
- }
- break;
- case EventType.Repaint:
- // Handled in Render()
- break;
- }
-
- if (m_HoverData != null && (
- Event.current.type == EventType.DragExited ||
- Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
- {
- DragAndDrop.visualMode = DragAndDropVisualMode.None;
- FlushHoverData();
- Event.current.Use();
- }
- }
-
- internal void SetTile(Tilemap tilemapTarget, Vector2Int position, TileBase tile, Color color, Matrix4x4 matrix)
- {
- Vector3Int pos3 = new Vector3Int(position.x, position.y, zPosition);
- tilemapTarget.SetTile(pos3, tile);
- tilemapTarget.SetColor(pos3, color);
- tilemapTarget.SetTransformMatrix(pos3, tilemapTarget.GetTransformMatrix(pos3) * matrix);
- }
-
- protected override void Paint(Vector3Int position)
- {
- if (gridBrush == null)
- return;
-
- gridBrush.Paint(grid, brushTarget, position);
- OnPaletteChanged();
- }
-
- protected override void Erase(Vector3Int position)
- {
- if (gridBrush == null)
- return;
-
- gridBrush.Erase(grid, brushTarget, position);
- OnPaletteChanged();
- }
-
- protected override void BoxFill(BoundsInt position)
- {
- if (gridBrush == null)
- return;
-
- gridBrush.BoxFill(grid, brushTarget, position);
- OnPaletteChanged();
- }
-
- protected override void BoxErase(BoundsInt position)
- {
- if (gridBrush == null)
- return;
-
- gridBrush.BoxErase(grid, brushTarget, position);
- OnPaletteChanged();
- }
-
- protected override void FloodFill(Vector3Int position)
- {
- if (gridBrush == null)
- return;
-
- gridBrush.FloodFill(grid, brushTarget, position);
- OnPaletteChanged();
- }
-
- protected override void PickBrush(BoundsInt position, Vector3Int pickingStart)
- {
- if (grid == null || gridBrush == null)
- return;
-
- gridBrush.Pick(grid, brushTarget, position, pickingStart);
-
- if (!InGridEditMode())
- TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
-
- m_ActivePick = position;
- m_ActivePivot = pickingStart;
-
- if (!disableOnBrushPicked)
- onBrushPicked?.Invoke();
- }
-
- protected override void Select(BoundsInt position)
- {
- if (grid)
- {
- GridSelection.Select(brushTarget, position);
- gridBrush.Select(grid, brushTarget, position);
- }
- }
-
- protected override void Move(BoundsInt from, BoundsInt to)
- {
- if (grid)
- gridBrush.Move(grid, brushTarget, from, to);
- }
-
- protected override void MoveStart(BoundsInt position)
- {
- if (grid)
- gridBrush.MoveStart(grid, brushTarget, position);
- }
-
- protected override void MoveEnd(BoundsInt position)
- {
- if (grid)
- {
- gridBrush.MoveEnd(grid, brushTarget, position);
- OnPaletteChanged();
- }
- }
-
- protected override bool CustomTool(bool isToolHotControl, TilemapEditorTool tool, Vector3Int position)
- {
- var executed = false;
- if (grid)
- {
- executed = tool.HandleTool(isToolHotControl, grid, brushTarget, position);
- if (executed)
- OnPaletteChanged();
- }
- return executed;
- }
-
- protected override bool IsMouseUpInWindow()
- {
- return Event.current.type == EventType.MouseUp && guiRect.Contains(Event.current.mousePosition);
- }
-
- public override void Repaint()
- {
- if (m_VisualElement != null)
- m_VisualElement.MarkDirtyRepaint();
- }
-
- protected override void ClearGridSelection()
- {
- GridSelection.Clear();
- }
-
- public override bool isActive => grid != null;
-
- protected override void OnBrushPickStarted()
- {
- }
-
- protected override void OnBrushPickDragged(BoundsInt position)
- {
- m_ActivePick = position;
- }
-
- protected override void OnBrushPickCancelled()
- {
- m_ActivePick = null;
- m_ActivePivot = Vector3Int.zero;
- }
-
- private void PingTileAsset(RectInt rect)
- {
- // Only able to ping asset if only one asset is selected
- if (rect.size == Vector2Int.zero && tilemap != null)
- {
- TileBase tile = tilemap.GetTile(new Vector3Int(rect.xMin, rect.yMin, zPosition));
- EditorGUIUtility.PingObject(tile);
- Selection.activeObject = tile;
- }
- }
-
- protected override bool ValidateFloodFillPosition(Vector3Int position)
- {
- return true;
- }
-
- protected override bool PickingIsDefaultTool()
- {
- return !m_Unlocked;
- }
-
- protected override bool CanPickOutsideEditMode()
- {
- return true;
- }
-
- protected override GridLayout.CellLayout CellLayout()
- {
- if (grid != null)
- return grid.cellLayout;
- return GridLayout.CellLayout.Rectangle;
- }
-
- protected override Vector2Int ScreenToGrid(Vector2 screenPosition)
- {
- Vector3 local = ScreenToLocal(screenPosition);
- var localS = Grid.Swizzle(m_CameraSwizzleView, local);
- var result3 = grid.LocalToCell(localS);
- var result = new Vector2Int(result3.x, result3.y);
- return result;
- }
-
- private void RenderSelectedBrushMarquee()
- {
- if (!activeDragAndDrop && !unlocked && m_ActivePick.HasValue)
- {
- DrawSelectionGizmo(m_ActivePick.Value);
- }
- }
-
- private void DrawSelectionGizmo(BoundsInt selectionBounds)
- {
- if (Event.current.type != EventType.Repaint || !GUI.enabled)
- return;
-
- Color color = Color.white;
- if (isPicking)
- color = Color.cyan;
-
- GridEditorUtility.DrawGridMarquee(grid, new BoundsInt(new Vector3Int(selectionBounds.xMin, selectionBounds.yMin, 0), new Vector3Int(selectionBounds.size.x, selectionBounds.size.y, 1)), color);
- }
-
- private void HandleMouseEnterLeave()
- {
- if (guiRect.Contains(Event.current.mousePosition))
- {
- if (m_PreviousMousePosition.HasValue && !guiRect.Contains(m_PreviousMousePosition.Value) || !m_PreviousMousePosition.HasValue)
- {
- if (GridPaintingState.activeBrushEditor != null)
- {
- GridPaintingState.activeBrushEditor.OnMouseEnter();
- }
- }
- }
- else
- {
- if (m_PreviousMousePosition.HasValue && guiRect.Contains(m_PreviousMousePosition.Value) && !guiRect.Contains(Event.current.mousePosition))
- {
- if (GridPaintingState.activeBrushEditor != null)
- {
- GridPaintingState.activeBrushEditor.OnMouseLeave();
- Repaint();
- }
- }
- }
- }
-
- private void CallOnSceneGUI()
- {
- var gridLayout = tilemap != null ? tilemap : grid as GridLayout;
- bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;
- if (hasSelection)
- {
- var rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
- var brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
- GridBrushEditorBase.OnSceneGUIInternal(gridLayout, brushTarget, brushBounds, EditTypeToBrushTool(ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
- }
- if (GridPaintingState.activeBrushEditor != null)
- {
- GridPaintingState.activeBrushEditor.OnSceneGUI(gridLayout, brushTarget);
- if (hasSelection)
- {
- GridPaintingState.activeBrushEditor.OnSelectionSceneGUI(gridLayout, brushTarget);
- if (GridSelectionTool.IsActive() && unlocked)
- {
- var tool = EditorToolManager.activeTool as GridSelectionTool;
- tool.OnToolGUI();
- }
- }
- }
- }
-
- private void CallOnPaintSceneGUI(Vector2Int position)
- {
- if (!activeDragAndDrop && !unlocked && !TilemapEditorTool.IsActive(typeof(SelectTool)) && !TilemapEditorTool.IsActive(typeof(PickingTool)))
- return;
-
- var hasSelection = GridSelection.active && GridSelection.target == brushTarget;
- if (!hasSelection && GridPaintingState.activeGrid != this)
- return;
-
- var brush = GridPaintingState.gridBrush;
- if (brush == null)
- return;
-
- var rect = new RectInt(position, new Vector2Int(1, 1));
-
- if (m_MarqueeStart.HasValue)
- rect = GridEditorUtility.GetMarqueeRect(position, m_MarqueeStart.Value);
- else if (hasSelection)
- rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
-
- var gridLayout = tilemap != null ? tilemap.layoutGrid : grid as GridLayout;
- var brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
-
- if (GridPaintingState.activeBrushEditor != null)
- GridPaintingState.activeBrushEditor.OnPaintSceneGUI(gridLayout, brushTarget, brushBounds,
- EditTypeToBrushTool(ToolManager.activeToolType),
- m_MarqueeStart.HasValue || executing);
- else // Fallback when user hasn't defined custom editor
- GridBrushEditorBase.OnPaintSceneGUIInternal(gridLayout, Selection.activeGameObject, brushBounds,
- EditTypeToBrushTool(ToolManager.activeToolType),
- m_MarqueeStart.HasValue || executing);
- }
-
- protected override void RegisterUndo()
- {
- if (palette != null && paletteInstance != null)
- Undo.RegisterFullObjectHierarchyUndo(paletteInstance, "Edit Palette");
- }
-
- private void OnPaletteChanged()
- {
- m_PaletteUsed = true;
- m_PaletteNeedsSave = true;
- Undo.FlushUndoRecordObjects();
- }
-
- public void CheckRevertIfChanged(string[] paths)
- {
- if (paths != null && m_PaletteNeedsSave && palette != null)
- {
- var currentPalettePath = AssetDatabase.GetAssetPath(palette);
- foreach (var path in paths)
- {
- if (currentPalettePath == path)
- {
- m_PaletteNeedsSave = false;
- ResetPreviewInstance();
- Debug.LogWarningFormat(palette, paletteSavedOutsideClipboard, palette.name);
- break;
- }
- }
- }
- }
-
- public bool SavePaletteIfNecessary()
- {
- bool needsSave = m_PaletteNeedsSave;
- if (needsSave)
- {
- SavePalette();
- m_PaletteNeedsSave = false;
- }
- return needsSave;
- }
-
- private void SavePalette()
- {
- if (palette != null && paletteInstance != null)
- {
- TilePaletteSaveUtility.SaveTilePalette(palette, paletteInstance);
- ResetPreviewInstance();
- Repaint();
- }
- }
-
- public Vector2 GridToScreen(Vector2 gridPosition)
- {
- var gridPosition3 = new Vector3(gridPosition.x, gridPosition.y, 0);
- return LocalToScreen(grid.CellToLocalInterpolated(gridPosition3));
- }
-
- public Vector2 ScreenToLocal(Vector2 screenPosition)
- {
- var viewPosition = m_PreviewUtility.camera.transform.position;
- Vector2 viewXYPosition = Grid.InverseSwizzle(m_CameraSwizzleView, viewPosition);
- screenPosition -= new Vector2(guiRect.xMin, guiRect.yMin);
- var offsetFromCenter = new Vector2(screenPosition.x - guiRect.width * .5f, guiRect.height * .5f - screenPosition.y);
- return viewXYPosition + offsetFromCenter / LocalToScreenRatio();
- }
-
- protected Vector2 LocalToScreen(Vector2 localPosition)
- {
- var viewPosition = m_PreviewUtility.camera.transform.position;
- Vector2 viewXYPosition = Grid.InverseSwizzle(m_CameraSwizzleView, viewPosition);
- var offsetFromCenter = new Vector2(localPosition.x - viewXYPosition.x, viewXYPosition.y - localPosition.y);
- return offsetFromCenter * LocalToScreenRatio() + new Vector2(guiRect.width * .5f + guiRect.xMin, guiRect.height * .5f + guiRect.yMin);
- }
-
- private float LocalToScreenRatio()
- {
- return guiRect.height / (m_PreviewUtility.camera.orthographicSize * 2f);
- }
-
- private float LocalToScreenRatio(float viewHeight)
- {
- return viewHeight / (m_PreviewUtility.camera.orthographicSize * 2f);
- }
-
- private static bool TilemapIsEmpty(Tilemap tilemap)
- {
- return tilemap.GetUsedTilesCount() == 0;
- }
-
- public void UnlockAndEdit()
- {
- unlocked = true;
- m_PaletteNeedsSave = true;
- }
-
- // TODO: Better way of clearing caches than AssetPostprocessor
- public class AssetProcessor : AssetPostprocessor
- {
- public override int GetPostprocessOrder()
- {
- return int.MaxValue;
- }
-
- private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
- {
- foreach (var clipboard in instances)
- {
- clipboard.DelayedResetPreviewInstance();
- }
- }
- }
-
- public class PaletteAssetModificationProcessor : AssetModificationProcessor
- {
- static void OnWillCreateAsset(string assetName)
- {
- SavePalettesIfRequired(null);
- }
-
- static string[] OnWillSaveAssets(string[] paths)
- {
- SavePalettesIfRequired(paths);
- return paths;
- }
-
- static void SavePalettesIfRequired(string[] paths)
- {
- if (GridPaintingState.savingPalette)
- return;
-
- foreach (var clipboard in instances)
- {
- if (clipboard.isModified)
- {
- clipboard.CheckRevertIfChanged(paths);
- clipboard.SavePaletteIfNecessary();
- clipboard.Repaint();
- }
- }
- }
- }
- }
- }
|