Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TilePaletteClipboardElement.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. /// <summary>
  7. /// A Visual Element which handles and displays a Tile Palette Clipboard.
  8. /// A Tile Palette Clipboard shows the Active Palette for Grid Painting and allows
  9. /// users to use the Active Brush to assign and pick items for painting.
  10. /// </summary>
  11. public class TilePaletteClipboardElement : IMGUIContainer
  12. {
  13. /// <summary>
  14. /// Factory for TilePaletteClipboardElement.
  15. /// </summary>
  16. public class TilePaletteClipboardElementFactory : UxmlFactory<TilePaletteClipboardElement, TilePaletteClipboardElementUxmlTraits> {}
  17. /// <summary>
  18. /// UxmlTraits for TilePaletteClipboardElement.
  19. /// </summary>
  20. public class TilePaletteClipboardElementUxmlTraits : UxmlTraits {}
  21. private new static readonly string ussClassName = "unity-tilepalette-clipboard-element";
  22. private static readonly string k_Name = L10n.Tr("Tile Palette Clipboard Element");
  23. private GridPaintPaletteClipboard m_TilePaletteClipboard;
  24. private EditorWindow m_Window;
  25. /// <summary>
  26. /// Whether the clipboard is unlocked for editing.
  27. /// </summary>
  28. public bool clipboardUnlocked
  29. {
  30. get => m_TilePaletteClipboard.unlocked;
  31. set => m_TilePaletteClipboard.unlocked = value;
  32. }
  33. /// <summary>
  34. /// Callback when the clipboard unlock status has changed
  35. /// </summary>
  36. public event Action<bool> clipboardUnlockedChanged;
  37. internal GridPaintPaletteClipboard clipboardView => m_TilePaletteClipboard;
  38. /// <summary>
  39. /// Initializes and returns an instance of TilePaletteClipboardElement.
  40. /// </summary>
  41. public TilePaletteClipboardElement()
  42. {
  43. AddToClassList(ussClassName);
  44. name = k_Name;
  45. TilePaletteOverlayUtility.SetStyleSheet(this);
  46. onGUIHandler = OnClipboardGUI;
  47. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  48. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  49. }
  50. private void OnAttachedToPanel(AttachToPanelEvent evt)
  51. {
  52. if (m_TilePaletteClipboard == null)
  53. {
  54. m_TilePaletteClipboard = ScriptableObject.CreateInstance<GridPaintPaletteClipboard>();
  55. m_TilePaletteClipboard.hideFlags = HideFlags.HideAndDontSave;
  56. m_TilePaletteClipboard.unlockedChanged += UnlockChanged;
  57. m_TilePaletteClipboard.unlocked = false;
  58. m_TilePaletteClipboard.attachedVisualElement = this;
  59. }
  60. GridPaintingState.beforePaletteChanged += BeforePaletteChanged;
  61. GridPaintingState.paletteChanged += PaletteChanged;
  62. }
  63. private void UnlockChanged(bool unlocked)
  64. {
  65. clipboardUnlockedChanged?.Invoke(unlocked);
  66. }
  67. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  68. {
  69. m_TilePaletteClipboard.unlockedChanged -= UnlockChanged;
  70. GridPaintingState.beforePaletteChanged -= BeforePaletteChanged;
  71. GridPaintingState.paletteChanged -= PaletteChanged;
  72. Cleanup();
  73. }
  74. /// <summary>
  75. /// Handles cleanup for the Tile Palette Clipboard.
  76. /// </summary>
  77. private void Cleanup()
  78. {
  79. UnityEngine.Object.DestroyImmediate(m_TilePaletteClipboard);
  80. m_TilePaletteClipboard = null;
  81. }
  82. private void BeforePaletteChanged()
  83. {
  84. m_TilePaletteClipboard.OnBeforePaletteSelectionChanged();
  85. }
  86. private void PaletteChanged(GameObject palette)
  87. {
  88. m_TilePaletteClipboard.OnAfterPaletteSelectionChanged();
  89. if (m_Window != null)
  90. m_Window.Repaint();
  91. }
  92. private void OnClipboardGUI()
  93. {
  94. var clipboardRect = GUILayoutUtility.GetRect(layout.width, layout.height);
  95. m_TilePaletteClipboard.OnClipboardGUI(clipboardRect);
  96. }
  97. }
  98. }