Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TilePaletteActiveTargetsPopup.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.Tilemaps
  7. {
  8. /// <summary>
  9. /// Popup Field for selecting the Active Target for Grid Painting.
  10. /// </summary>
  11. public sealed class TilePaletteActiveTargetsPopup : PopupField<GameObject>
  12. {
  13. private static string k_NullGameObjectName = L10n.Tr("No Valid Target");
  14. private static string k_LabelTooltip =
  15. L10n.Tr("Specifies the currently active Tilemap used for painting in the Scene View.");
  16. private static string k_WarningTooltip =
  17. L10n.Tr("Editing Tilemaps in Prefabs will have better performance if edited in Prefab Mode.");
  18. /// <summary>
  19. /// Factory for TilePaletteActiveTargetsPopup.
  20. /// </summary>
  21. public class TilePaletteActiveTargetsPopupFactory : UxmlFactory<TilePaletteActiveTargetsPopup, TilePaletteActiveTargetsPopupUxmlTraits> {}
  22. /// <summary>
  23. /// UxmlTraits for TilePaletteActiveTargetsPopup.
  24. /// </summary>
  25. public class TilePaletteActiveTargetsPopupUxmlTraits : UxmlTraits {}
  26. /// <summary>
  27. /// USS class name of elements of this type.
  28. /// </summary>
  29. private new static readonly string ussClassName = "unity-tilepalette-activetargets-field";
  30. /// <summary>
  31. /// USS class name of labels in elements of this type.
  32. /// </summary>
  33. private new static readonly string labelUssClassName = ussClassName + "__label";
  34. /// <summary>
  35. /// USS class name of input elements in elements of this type.
  36. /// </summary>
  37. private new static readonly string inputUssClassName = ussClassName + "__input";
  38. /// <summary>
  39. /// USS class name of input elements in elements of this type when warning is shown.
  40. /// </summary>
  41. private static readonly string inputWarningUssClassName = inputUssClassName + "--warning";
  42. /// <summary>
  43. /// USS class name of warning elements in elements of this type.
  44. /// </summary>
  45. private static readonly string warningUssClassName = ussClassName + "__warning";
  46. private readonly VisualElement m_WarningIconElement;
  47. private static List<GameObject> s_InvalidTargetsList = new List<GameObject>();
  48. /// <summary>
  49. /// Initializes and returns an instance of TilePaletteActiveTargetsPopup.
  50. /// </summary>
  51. public TilePaletteActiveTargetsPopup() : this(null) {}
  52. /// <summary>
  53. /// Initializes and returns an instance of TilePaletteActiveTargetsPopup.
  54. /// </summary>
  55. /// <param name="label">Label name for the Popup</param>
  56. public TilePaletteActiveTargetsPopup(string label)
  57. : base(label
  58. , GridPaintingState.validTargets != null ? GridPaintingState.validTargets.ToList() : s_InvalidTargetsList
  59. , GetActiveTargetIndex())
  60. {
  61. AddToClassList(ussClassName);
  62. labelElement.AddToClassList(labelUssClassName);
  63. visualInput.AddToClassList(inputUssClassName);
  64. TilePaletteOverlayUtility.SetStyleSheet(this);
  65. labelElement.tooltip = k_LabelTooltip;
  66. m_WarningIconElement = new VisualElement();
  67. m_WarningIconElement.name = "Warning Icon";
  68. m_WarningIconElement.AddToClassList(warningUssClassName);
  69. m_WarningIconElement.tooltip = k_WarningTooltip;
  70. contentContainer.Add(m_WarningIconElement);
  71. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  72. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  73. m_FormatSelectedValueCallback += FormatSelectedValueCallback;
  74. createMenuCallback += CreateMenuCallback;
  75. UpdateTargets();
  76. SetValueWithoutNotify(GridPaintingState.scenePaintTarget);
  77. }
  78. private void OnAttachedToPanel(AttachToPanelEvent evt)
  79. {
  80. GridPaintingState.scenePaintTargetChanged += OnScenePaintTargetChanged;
  81. GridPaintingState.validTargetsChanged += UpdateTargets;
  82. }
  83. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  84. {
  85. GridPaintingState.scenePaintTargetChanged -= OnScenePaintTargetChanged;
  86. GridPaintingState.validTargetsChanged -= UpdateTargets;
  87. }
  88. private string FormatSelectedValueCallback(GameObject go)
  89. {
  90. if (go != null)
  91. return go.name;
  92. return k_NullGameObjectName;
  93. }
  94. private IGenericMenu CreateMenuCallback()
  95. {
  96. return new TilePaletteActiveTargetsDropdownMenu();
  97. }
  98. private static int GetActiveTargetIndex()
  99. {
  100. if (GridPaintingState.validTargets == null)
  101. return -1;
  102. return Array.IndexOf(GridPaintingState.validTargets, GridPaintingState.scenePaintTarget);
  103. }
  104. private void OnScenePaintTargetChanged(GameObject _)
  105. {
  106. UpdateActiveTarget();
  107. }
  108. private void UpdateChoices()
  109. {
  110. choices.Clear();
  111. if (GridPaintingState.validTargets == null)
  112. return;
  113. foreach (var target in GridPaintingState.validTargets)
  114. {
  115. choices.Add(target);
  116. }
  117. }
  118. private void UpdateActiveTarget()
  119. {
  120. var newIndex = GetActiveTargetIndex();
  121. if (newIndex != -1 && choices.Count < newIndex)
  122. {
  123. UpdateChoices();
  124. newIndex = GetActiveTargetIndex();
  125. }
  126. index = newIndex;
  127. bool needWarning = TilePalettePrefabUtility.IsObjectPrefabInstance(GridPaintingState.scenePaintTarget);
  128. if (needWarning)
  129. {
  130. visualInput.AddToClassList(inputWarningUssClassName);
  131. }
  132. else
  133. {
  134. visualInput.RemoveFromClassList(inputWarningUssClassName);
  135. }
  136. m_WarningIconElement.visible = needWarning;
  137. m_WarningIconElement.style.position = needWarning ? Position.Relative : Position.Absolute;
  138. }
  139. private void UpdateTargets()
  140. {
  141. UpdateChoices();
  142. UpdateActiveTarget();
  143. }
  144. }
  145. /// <summary>
  146. /// Visual Element displaying the Icon for Active Target for Grid Painting.
  147. /// </summary>
  148. internal class TilePaletteActiveTargetsPopupIcon : VisualElement
  149. {
  150. /// <summary>
  151. /// USS class name of elements of this type.
  152. /// </summary>
  153. private static readonly string ussClassName = "unity-tilepalette-activetargets-icon";
  154. private readonly string kTooltip = L10n.Tr("Active Target");
  155. private readonly string kIconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.ActiveTargetLayers.png";
  156. /// <summary>
  157. /// Constructor for TilePaletteActiveTargetsPopupIcon
  158. /// </summary>
  159. public TilePaletteActiveTargetsPopupIcon()
  160. {
  161. AddToClassList(ussClassName);
  162. TilePaletteOverlayUtility.SetStyleSheet(this);
  163. tooltip = kTooltip;
  164. style.backgroundImage = EditorGUIUtility.LoadIcon(kIconPath);
  165. }
  166. }
  167. }