using System; using UnityEngine; using UnityEngine.UIElements; namespace UnityEditor.Tilemaps { /// /// A Visual Element which handles and displays a Tile Palette Clipboard. /// A Tile Palette Clipboard shows the Active Palette for Grid Painting and allows /// users to use the Active Brush to assign and pick items for painting. /// public class TilePaletteClipboardElement : IMGUIContainer { /// /// Factory for TilePaletteClipboardElement. /// public class TilePaletteClipboardElementFactory : UxmlFactory {} /// /// UxmlTraits for TilePaletteClipboardElement. /// public class TilePaletteClipboardElementUxmlTraits : UxmlTraits {} private new static readonly string ussClassName = "unity-tilepalette-clipboard-element"; private static readonly string k_Name = L10n.Tr("Tile Palette Clipboard Element"); private GridPaintPaletteClipboard m_TilePaletteClipboard; private EditorWindow m_Window; /// /// Whether the clipboard is unlocked for editing. /// public bool clipboardUnlocked { get => m_TilePaletteClipboard.unlocked; set => m_TilePaletteClipboard.unlocked = value; } /// /// Callback when the clipboard unlock status has changed /// public event Action clipboardUnlockedChanged; internal GridPaintPaletteClipboard clipboardView => m_TilePaletteClipboard; /// /// Initializes and returns an instance of TilePaletteClipboardElement. /// public TilePaletteClipboardElement() { AddToClassList(ussClassName); name = k_Name; TilePaletteOverlayUtility.SetStyleSheet(this); onGUIHandler = OnClipboardGUI; RegisterCallback(OnAttachedToPanel); RegisterCallback(OnDetachFromPanel); } private void OnAttachedToPanel(AttachToPanelEvent evt) { if (m_TilePaletteClipboard == null) { m_TilePaletteClipboard = ScriptableObject.CreateInstance(); m_TilePaletteClipboard.hideFlags = HideFlags.HideAndDontSave; m_TilePaletteClipboard.unlockedChanged += UnlockChanged; m_TilePaletteClipboard.unlocked = false; m_TilePaletteClipboard.attachedVisualElement = this; } GridPaintingState.beforePaletteChanged += BeforePaletteChanged; GridPaintingState.paletteChanged += PaletteChanged; } private void UnlockChanged(bool unlocked) { clipboardUnlockedChanged?.Invoke(unlocked); } private void OnDetachFromPanel(DetachFromPanelEvent evt) { m_TilePaletteClipboard.unlockedChanged -= UnlockChanged; GridPaintingState.beforePaletteChanged -= BeforePaletteChanged; GridPaintingState.paletteChanged -= PaletteChanged; Cleanup(); } /// /// Handles cleanup for the Tile Palette Clipboard. /// private void Cleanup() { UnityEngine.Object.DestroyImmediate(m_TilePaletteClipboard); m_TilePaletteClipboard = null; } private void BeforePaletteChanged() { m_TilePaletteClipboard.OnBeforePaletteSelectionChanged(); } private void PaletteChanged(GameObject palette) { m_TilePaletteClipboard.OnAfterPaletteSelectionChanged(); if (m_Window != null) m_Window.Repaint(); } private void OnClipboardGUI() { var clipboardRect = GUILayoutUtility.GetRect(layout.width, layout.height); m_TilePaletteClipboard.OnClipboardGUI(clipboardRect); } } }