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

TilePaletteActiveTargetsPopup.cs 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using UnityEngine.UIElements.Experimental;
  7. namespace UnityEditor.Tilemaps
  8. {
  9. /// <summary>
  10. /// Popup Field for selecting the Active Target for Grid Painting.
  11. /// </summary>
  12. [UxmlElement]
  13. public sealed partial class TilePaletteActiveTargetsPopup : PopupField<GameObject>
  14. {
  15. private static string k_NullGameObjectName = GridPaintTargetsDropdown.k_CreateNewPaintTargetName;
  16. private static string k_LabelTooltip =
  17. L10n.Tr("Specifies the currently active Tilemap used for painting in the Scene View.");
  18. private static string k_WarningTooltip =
  19. L10n.Tr("Editing Tilemaps in Prefabs will have better performance if edited in Prefab Mode.");
  20. /// <summary>
  21. /// Factory for TilePaletteActiveTargetsPopup.
  22. /// </summary>
  23. [Obsolete("TilePaletteActiveTargetsPopupFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  24. public class TilePaletteActiveTargetsPopupFactory : UxmlFactory<TilePaletteActiveTargetsPopup, TilePaletteActiveTargetsPopupUxmlTraits> {}
  25. /// <summary>
  26. /// UxmlTraits for TilePaletteActiveTargetsPopup.
  27. /// </summary>
  28. [Obsolete("TilePaletteActiveTargetsPopupUxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  29. public class TilePaletteActiveTargetsPopupUxmlTraits : UxmlTraits {}
  30. /// <summary>
  31. /// USS class name of elements of this type.
  32. /// </summary>
  33. private new static readonly string ussClassName = "unity-tilepalette-activetargets-field";
  34. /// <summary>
  35. /// USS class name of labels in elements of this type.
  36. /// </summary>
  37. private new static readonly string labelUssClassName = ussClassName + "__label";
  38. /// <summary>
  39. /// USS class name of input elements in elements of this type.
  40. /// </summary>
  41. private new static readonly string inputUssClassName = ussClassName + "__input";
  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. /// <summary>
  47. /// USS class name of input elements in elements of this type when create target hint is shown.
  48. /// </summary>
  49. private static readonly string createHintUssClassName = ussClassName + "__create";
  50. private readonly VisualElement m_WarningIconElement;
  51. private static List<GameObject> s_InvalidTargetsList = new List<GameObject>();
  52. private bool needCreate
  53. {
  54. get => GridPaintingState.scenePaintTarget == null
  55. && (GridPaintingState.validTargets == null
  56. || GridPaintingState.validTargets.Length == 0);
  57. }
  58. private ValueAnimation<StyleValues> currentAnim;
  59. /// <summary>
  60. /// Initializes and returns an instance of TilePaletteActiveTargetsPopup.
  61. /// </summary>
  62. public TilePaletteActiveTargetsPopup() : this(null) {}
  63. /// <summary>
  64. /// Initializes and returns an instance of TilePaletteActiveTargetsPopup.
  65. /// </summary>
  66. /// <param name="label">Label name for the Popup</param>
  67. public TilePaletteActiveTargetsPopup(string label)
  68. : base(label
  69. , GridPaintingState.validTargets != null ? GridPaintingState.validTargets.ToList() : s_InvalidTargetsList
  70. , GetActiveTargetIndex())
  71. {
  72. AddToClassList(ussClassName);
  73. labelElement.AddToClassList(labelUssClassName);
  74. visualInput.AddToClassList(inputUssClassName);
  75. TilePaletteOverlayUtility.SetStyleSheet(this);
  76. labelElement.tooltip = k_LabelTooltip;
  77. m_WarningIconElement = new VisualElement();
  78. m_WarningIconElement.name = "Warning Icon";
  79. m_WarningIconElement.AddToClassList(warningUssClassName);
  80. m_WarningIconElement.tooltip = k_WarningTooltip;
  81. contentContainer.Add(m_WarningIconElement);
  82. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  83. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  84. m_FormatSelectedValueCallback += FormatSelectedValueCallback;
  85. createMenuCallback += CreateMenuCallback;
  86. UpdateTargets();
  87. SetValueWithoutNotify(GridPaintingState.scenePaintTarget);
  88. }
  89. private void OnAttachedToPanel(AttachToPanelEvent evt)
  90. {
  91. GridPaintingState.scenePaintTargetChanged += OnScenePaintTargetChanged;
  92. GridPaintingState.validTargetsChanged += UpdateTargets;
  93. GridPaintingState.scenePaintTargetEdited += OnScenePaintTargetEdited;
  94. }
  95. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  96. {
  97. GridPaintingState.scenePaintTargetChanged -= OnScenePaintTargetChanged;
  98. GridPaintingState.validTargetsChanged -= UpdateTargets;
  99. GridPaintingState.scenePaintTargetEdited -= OnScenePaintTargetEdited;
  100. }
  101. private string FormatSelectedValueCallback(GameObject go)
  102. {
  103. if (go != null)
  104. return go.name;
  105. if (GridPaintingState.scenePaintTarget != null)
  106. return GridPaintingState.scenePaintTarget.name;
  107. return k_NullGameObjectName;
  108. }
  109. private IGenericMenu CreateMenuCallback()
  110. {
  111. return new TilePaletteActiveTargetsDropdownMenu();
  112. }
  113. private static int GetActiveTargetIndex()
  114. {
  115. if (GridPaintingState.validTargets == null)
  116. return -1;
  117. return Array.IndexOf(GridPaintingState.validTargets, GridPaintingState.scenePaintTarget);
  118. }
  119. private void OnScenePaintTargetChanged(GameObject _)
  120. {
  121. UpdateActiveTarget();
  122. }
  123. private void UpdateChoices()
  124. {
  125. choices.Clear();
  126. if (GridPaintingState.validTargets == null)
  127. return;
  128. foreach (var target in GridPaintingState.validTargets)
  129. {
  130. choices.Add(target);
  131. }
  132. SetValueWithoutNotify(GridPaintingState.scenePaintTarget);
  133. }
  134. private void UpdateActiveTarget()
  135. {
  136. var newIndex = GetActiveTargetIndex();
  137. if (newIndex != -1 && choices.Count < newIndex)
  138. {
  139. UpdateChoices();
  140. newIndex = GetActiveTargetIndex();
  141. }
  142. index = newIndex;
  143. var needWarning = TilePalettePrefabUtility.IsObjectPrefabInstance(GridPaintingState.scenePaintTarget);
  144. m_WarningIconElement.visible = needWarning;
  145. m_WarningIconElement.style.position = needWarning ? Position.Relative : Position.Absolute;
  146. }
  147. private void OnScenePaintTargetEdited(GameObject obj)
  148. {
  149. if (!needCreate
  150. || obj != null)
  151. return;
  152. if (currentAnim?.durationMs > 4000)
  153. {
  154. currentAnim.KeepAlive();
  155. ClearAnim(currentAnim);
  156. }
  157. var target = parent ?? this;
  158. var anim = target.experimental.animation.Start(
  159. new StyleValues()
  160. {
  161. borderColor = Color.yellow,
  162. },
  163. new StyleValues()
  164. {
  165. borderColor = Color.clear,
  166. }, 8000).Ease(Easing.OutQuad);
  167. anim.OnCompleted(() => ClearAnim(anim));
  168. currentAnim = anim;
  169. }
  170. void ClearAnim(IValueAnimation anim)
  171. {
  172. if (currentAnim != null && currentAnim == anim)
  173. {
  174. currentAnim = null;
  175. anim.Stop();
  176. anim.Recycle();
  177. }
  178. }
  179. private void UpdateTargets()
  180. {
  181. UpdateChoices();
  182. UpdateActiveTarget();
  183. }
  184. }
  185. /// <summary>
  186. /// Visual Element displaying the Icon for Active Target for Grid Painting.
  187. /// </summary>
  188. internal class TilePaletteActiveTargetsPopupIcon : VisualElement
  189. {
  190. /// <summary>
  191. /// USS class name of elements of this type.
  192. /// </summary>
  193. private static readonly string ussClassName = "unity-tilepalette-activetargets-icon";
  194. private readonly string kTooltip = L10n.Tr("Active Target");
  195. private readonly string kIconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.ActiveTargetLayers.png";
  196. /// <summary>
  197. /// Constructor for TilePaletteActiveTargetsPopupIcon
  198. /// </summary>
  199. public TilePaletteActiveTargetsPopupIcon()
  200. {
  201. AddToClassList(ussClassName);
  202. TilePaletteOverlayUtility.SetStyleSheet(this);
  203. tooltip = kTooltip;
  204. style.backgroundImage = EditorGUIUtility.LoadIcon(kIconPath);
  205. }
  206. }
  207. }