暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TilePaletteActivePalettePopup.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Toolbars;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.Tilemaps
  7. {
  8. /// <summary>
  9. /// Popup Field for selecting the Active Palette for Grid Painting.
  10. /// </summary>
  11. [EditorToolbarElement(k_ToolbarId)]
  12. [UxmlElement]
  13. public sealed partial class TilePaletteActivePalettePopup : PopupField<GameObject>
  14. {
  15. internal const string k_ToolbarId = "Tools/Tile Palette Active Palette";
  16. private static string kNullGameObjectName = L10n.Tr("Create New Tile Palette");
  17. private static string kLabelTooltip =
  18. L10n.Tr("Specifies the currently active Palette used for painting in the Scene View.");
  19. /// <summary>
  20. /// Factory for TilePaletteActivePalettePopup.
  21. /// </summary>
  22. [Obsolete("TilePaletteActivePalettePopupFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  23. public class TilePaletteActivePalettePopupFactory : UxmlFactory<TilePaletteActivePalettePopup, TilePaletteActivePalettePopupUxmlTraits> {}
  24. /// <summary>
  25. /// UxmlTraits for TilePaletteActivePalettePopup.
  26. /// </summary>
  27. [Obsolete("TilePaletteActivePalettePopupUxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  28. public class TilePaletteActivePalettePopupUxmlTraits : UxmlTraits {}
  29. /// <summary>
  30. /// USS class name of elements of this type.
  31. /// </summary>
  32. private new static readonly string ussClassName = "unity-tilepalette-activepalettes-field";
  33. /// <summary>
  34. /// USS class name of labels in elements of this type.
  35. /// </summary>
  36. private new static readonly string labelUssClassName = ussClassName + "__label";
  37. /// <summary>
  38. /// USS class name of input elements in elements of this type.
  39. /// </summary>
  40. private new static readonly string inputUssClassName = ussClassName + "__input";
  41. /// <summary>
  42. /// Initializes and returns an instance of TilePaletteActivePalettesPopup.
  43. /// </summary>
  44. public TilePaletteActivePalettePopup() : this(null) {}
  45. /// <summary>
  46. /// Initializes and returns an instance of TilePaletteActivePalettesPopup.
  47. /// </summary>
  48. /// <param name="label">Label name for the Popup</param>
  49. public TilePaletteActivePalettePopup(string label)
  50. : base(label, GridPaintingState.palettes.ToList(), GetActivePaletteIndex())
  51. {
  52. AddToClassList(ussClassName);
  53. labelElement.AddToClassList(labelUssClassName);
  54. visualInput.AddToClassList(inputUssClassName);
  55. TilePaletteOverlayUtility.SetStyleSheet(this);
  56. labelElement.tooltip = kLabelTooltip;
  57. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  58. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  59. m_FormatSelectedValueCallback += FormatSelectedValueCallback;
  60. createMenuCallback += CreateMenuCallback;
  61. UpdateTargets();
  62. SetValueWithoutNotify(GridPaintingState.palette);
  63. }
  64. private void OnAttachedToPanel(AttachToPanelEvent evt)
  65. {
  66. GridPaintingState.paletteChanged += OnPaletteChanged;
  67. GridPaintingState.palettesChanged += UpdateTargets;
  68. }
  69. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  70. {
  71. GridPaintingState.palettesChanged += UpdateTargets;
  72. GridPaintingState.paletteChanged -= OnPaletteChanged;
  73. }
  74. private void OnPaletteChanged(GameObject _)
  75. {
  76. UpdateActivePalette();
  77. }
  78. private string FormatSelectedValueCallback(GameObject go)
  79. {
  80. if (go != null)
  81. return go.name;
  82. return kNullGameObjectName;
  83. }
  84. private IGenericMenu CreateMenuCallback()
  85. {
  86. return new TilePaletteActivePaletteDropdownMenu();
  87. }
  88. private static int GetActivePaletteIndex()
  89. {
  90. return GridPaintingState.palettes.IndexOf(GridPaintingState.palette);
  91. }
  92. private void UpdateChoices()
  93. {
  94. choices.Clear();
  95. foreach (var target in GridPaintingState.palettes)
  96. {
  97. choices.Add(target);
  98. }
  99. }
  100. private void UpdateActivePalette()
  101. {
  102. var newIndex = GetActivePaletteIndex();
  103. if (newIndex != -1 && choices.Count < newIndex)
  104. {
  105. UpdateChoices();
  106. newIndex = GetActivePaletteIndex();
  107. }
  108. index = newIndex;
  109. }
  110. private void UpdateTargets()
  111. {
  112. UpdateChoices();
  113. UpdateActivePalette();
  114. }
  115. }
  116. [EditorToolbarElement(k_ToolbarId)]
  117. internal class TilePaletteActivePalettePopupIcon : VisualElement
  118. {
  119. internal const string k_ToolbarId = "Tools/Tile Palette Active Palette Icon";
  120. private static string kTooltip = L10n.Tr("Active Palette");
  121. /// <summary>
  122. /// USS class name of elements of this type.
  123. /// </summary>
  124. public static readonly string ussClassName = "unity-tilepalette-activepalette-icon";
  125. private readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.TilePalette.png";
  126. public TilePaletteActivePalettePopupIcon()
  127. {
  128. AddToClassList(ussClassName);
  129. TilePaletteOverlayUtility.SetStyleSheet(this);
  130. style.backgroundImage = EditorGUIUtility.LoadIcon(k_IconPath);
  131. tooltip = kTooltip;
  132. }
  133. }
  134. }