暫無描述
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.

LabelContainer.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.U2D.Animation.SceneOverlays
  7. {
  8. internal class LabelContainer : VisualElement, INavigableElement
  9. {
  10. static class Styles
  11. {
  12. public const string labelVisual = SpriteSwapOverlay.rootStyle + "__label-visual";
  13. public const string labelContainer = SpriteSwapOverlay.rootStyle + "__label-container";
  14. public const string directionButton = SpriteSwapOverlay.rootStyle + "__label-direction-button";
  15. public const string labelImagesContainer = SpriteSwapOverlay.rootStyle + "__label-images-container";
  16. public const string labelSelected = SpriteSwapOverlay.rootStyle + "__label-selected";
  17. }
  18. const string k_ScrollLeftIcon = "scrollleft_uielements";
  19. const string k_ScrollRightIcon = "scrollright_uielements";
  20. const string k_ScrollLeftIconDark = "d_scrollleft_uielements";
  21. const string k_ScrollRightIconDark = "d_scrollright_uielements";
  22. public event Action<int> onSelectionChange;
  23. public int itemCount => m_LabelImagesContainer.childCount;
  24. public int selectedIndex { get; private set; } = -1;
  25. public VisualElement visualElement => this;
  26. List<Tuple<string, Sprite>> m_Labels;
  27. Button m_PreviousButton;
  28. Button m_NextButton;
  29. ScrollView m_LabelImagesContainer;
  30. bool m_IsFocused;
  31. PropertyAnimationState m_AnimationState;
  32. public LabelContainer()
  33. {
  34. m_Labels = new List<Tuple<string, Sprite>>();
  35. Texture2D previousButtonIcon;
  36. Texture2D nextButtonIcon;
  37. if (EditorGUIUtility.isProSkin)
  38. {
  39. previousButtonIcon = (Texture2D)EditorGUIUtility.IconContent(k_ScrollLeftIconDark).image;
  40. nextButtonIcon = (Texture2D)EditorGUIUtility.IconContent(k_ScrollRightIconDark).image;
  41. }
  42. else
  43. {
  44. previousButtonIcon = (Texture2D)EditorGUIUtility.IconContent(k_ScrollLeftIcon).image;
  45. nextButtonIcon = (Texture2D)EditorGUIUtility.IconContent(k_ScrollRightIcon).image;
  46. }
  47. focusable = true;
  48. m_LabelImagesContainer = new ScrollView
  49. {
  50. horizontalScrollerVisibility = ScrollerVisibility.Hidden,
  51. nestedInteractionKind = ScrollView.NestedInteractionKind.StopScrolling,
  52. mode = ScrollViewMode.Horizontal
  53. };
  54. m_LabelImagesContainer.AddToClassList(Styles.labelImagesContainer);
  55. m_PreviousButton = new Button { style = { backgroundImage = Background.FromTexture2D(previousButtonIcon) } };
  56. m_PreviousButton.clicked += () => m_LabelImagesContainer.horizontalScroller.ScrollPageUp();
  57. m_PreviousButton.AddToClassList(Styles.directionButton);
  58. m_PreviousButton.RemoveFromClassList(Button.ussClassName);
  59. m_NextButton = new Button { style = { backgroundImage = Background.FromTexture2D(nextButtonIcon) } };
  60. m_NextButton.clicked += () => m_LabelImagesContainer.horizontalScroller.ScrollPageDown();
  61. m_NextButton.AddToClassList(Styles.directionButton);
  62. m_NextButton.RemoveFromClassList(Button.ussClassName);
  63. Add(m_PreviousButton);
  64. Add(m_LabelImagesContainer);
  65. Add(m_NextButton);
  66. AddToClassList(Styles.labelContainer);
  67. RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
  68. }
  69. public void SetItems(IList labels)
  70. {
  71. ClearItems();
  72. if (labels == null)
  73. return;
  74. m_Labels = labels as List<Tuple<string, Sprite>>;
  75. if (m_Labels == null)
  76. return;
  77. foreach (var (labelName, labelSprite) in m_Labels)
  78. m_LabelImagesContainer.Add(GetVisualForLabel(labelName, labelSprite));
  79. }
  80. public object GetItem(int index)
  81. {
  82. if (m_Labels == null || index < 0 || index >= m_Labels.Count)
  83. return null;
  84. return m_Labels[index];
  85. }
  86. public void Select(int index)
  87. {
  88. if (index < 0 || index >= itemCount)
  89. return;
  90. selectedIndex = index;
  91. UpdateSelectionVisuals();
  92. onSelectionChange?.Invoke(selectedIndex);
  93. }
  94. void OnGeometryChanged(GeometryChangedEvent evt)
  95. {
  96. UpdateElementSize();
  97. UpdateSelectionVisuals();
  98. UpdateNavigationButtons();
  99. }
  100. void UpdateElementSize()
  101. {
  102. var paddingAndMarginsSize = 4.0f;
  103. style.minHeight = style.maxHeight = SpriteSwapOverlay.Settings.thumbnailSize + paddingAndMarginsSize;
  104. }
  105. void ClearItems()
  106. {
  107. m_Labels.Clear();
  108. selectedIndex = -1;
  109. m_LabelImagesContainer.Clear();
  110. }
  111. void OnItemSelected(PointerDownEvent evt)
  112. {
  113. var image = (VisualElement)evt.currentTarget;
  114. if (image != null)
  115. Select(m_LabelImagesContainer.IndexOf(image));
  116. }
  117. void UpdateSelectionVisuals()
  118. {
  119. foreach (var child in m_LabelImagesContainer.Children())
  120. {
  121. if (m_LabelImagesContainer.IndexOf(child) == selectedIndex)
  122. {
  123. m_LabelImagesContainer.ScrollTo(child);
  124. child.AddToClassList(Styles.labelSelected);
  125. child.style.backgroundColor = GetColorForAnimationState(m_AnimationState);
  126. }
  127. else
  128. {
  129. child.RemoveFromClassList(Styles.labelSelected);
  130. }
  131. }
  132. }
  133. void UpdateNavigationButtons()
  134. {
  135. var enableNavigationButtons = m_LabelImagesContainer.contentContainer.contentRect.width > m_LabelImagesContainer.contentRect.width;
  136. m_PreviousButton.SetEnabled(enableNavigationButtons);
  137. m_NextButton.SetEnabled(enableNavigationButtons);
  138. }
  139. VisualElement GetVisualForLabel(string labelName, Sprite labelSprite)
  140. {
  141. var image = new Image { name = labelName, tooltip = labelName, sprite = labelSprite };
  142. image.style.height = image.style.width = SpriteSwapOverlay.Settings.thumbnailSize;
  143. image.AddToClassList(Styles.labelVisual);
  144. image.RegisterCallback<PointerDownEvent>(OnItemSelected);
  145. return image;
  146. }
  147. public void SetAnimationState(PropertyAnimationState animationState)
  148. {
  149. if (animationState != m_AnimationState)
  150. {
  151. m_AnimationState = animationState;
  152. UpdateSelectionVisuals();
  153. }
  154. }
  155. static Color GetColorForAnimationState(PropertyAnimationState animationState)
  156. {
  157. var color = new Color(88.0f / 255.0f, 88.0f / 255.0f, 88.0f / 255.5f, 1.0f);
  158. if (animationState == PropertyAnimationState.Animated)
  159. color *= AnimationMode.animatedPropertyColor;
  160. else if (animationState == PropertyAnimationState.Candidate)
  161. color *= AnimationMode.candidatePropertyColor;
  162. else if (animationState == PropertyAnimationState.Recording)
  163. color *= AnimationMode.recordedPropertyColor;
  164. return color;
  165. }
  166. }
  167. }