VFXBoundsRecorderField.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using UnityEditor.UIElements;
  3. using UnityEditor.Experimental.GraphView;
  4. using UnityEngine;
  5. using UnityEngine.VFX;
  6. using UnityEngine.UIElements;
  7. using UnityEditor.VFX;
  8. using System.Collections.Generic;
  9. using UnityEditor;
  10. using System.Linq;
  11. using System.Text;
  12. using UnityEditor.Graphs;
  13. using UnityEditor.SceneManagement;
  14. namespace UnityEditor.VFX.UI
  15. {
  16. class VFXBoundsRecorderField : VisualElement, ISelectable
  17. {
  18. private Button m_Button;
  19. private VisualElement m_Divider;
  20. private VFXView m_View;
  21. public string text
  22. {
  23. get { return m_Button.text; }
  24. set { m_Button.text = value; }
  25. }
  26. private bool m_Selected = false;
  27. public class VFXBoundsRecorderFieldUIFactory : UxmlFactory<VFXBoundsRecorderField>
  28. { }
  29. IVisualElementScheduledItem m_UpdateItem;
  30. private VFXContextUI m_TiedContext;
  31. public VFXBoundsRecorderField()
  32. {
  33. RegisterCallback<MouseDownEvent>(OnMouseDown);
  34. }
  35. public VFXContextUI tiedContext => m_TiedContext;
  36. public void Setup(VFXContextUI initContextUI, VFXView view)
  37. {
  38. m_Button = this.Query<Button>("system-button");
  39. m_Button.style.backgroundColor = new Color(0.16f, 0.16f, 0.16f);
  40. m_Button.clickable.activators.Clear();
  41. m_Button.style.borderBottomColor =
  42. m_Button.style.borderTopColor =
  43. m_Button.style.borderLeftColor =
  44. m_Button.style.borderRightColor = Color.grey * 0.5f;
  45. m_Divider = this.Query("divider");
  46. m_TiedContext = initContextUI;
  47. m_View = view;
  48. m_TiedContext.onSelectionDelegate += OnTiedContextSelection;
  49. }
  50. public void OnTiedContextSelection(bool tiedContextSelected)
  51. {
  52. var selector = GetFirstAncestorOfType<VFXBoundsSelector>();
  53. if (tiedContextSelected && !m_Selected)
  54. {
  55. Select(selector, true);
  56. }
  57. if (!tiedContextSelected && m_Selected)
  58. {
  59. Unselect(selector);
  60. }
  61. }
  62. public void OnSelected()
  63. {
  64. if (!m_Selected)
  65. {
  66. if (enabledSelf)
  67. {
  68. m_Selected = true;
  69. UpdateBorder();
  70. if (!tiedContext.selected)
  71. m_View.AddToSelection(tiedContext);
  72. }
  73. }
  74. }
  75. public void OnUnselected()
  76. {
  77. if (m_Selected)
  78. {
  79. m_Selected = false;
  80. UpdateBorder();
  81. if (tiedContext.selected)
  82. m_View.RemoveFromSelection(tiedContext);
  83. }
  84. }
  85. public bool Unselect()
  86. {
  87. if (m_Selected)
  88. {
  89. var selector = GetFirstAncestorOfType<VFXBoundsSelector>();
  90. Unselect(selector);
  91. return true;
  92. }
  93. return false;
  94. }
  95. void UpdateBorder()
  96. {
  97. m_Button.style.borderBottomColor =
  98. m_Button.style.borderTopColor =
  99. m_Button.style.borderLeftColor =
  100. m_Button.style.borderRightColor = m_Selected ? new Color(68.0f / 255.0f, 192.0f / 255.0f, 255.0f / 255.0f, 1.0f) : Color.grey * 0.5f;
  101. }
  102. void OnMouseDown(MouseDownEvent e)
  103. {
  104. var selector = GetFirstAncestorOfType<VFXBoundsSelector>();
  105. if (IsSelected(selector))
  106. {
  107. if (e.actionKey)
  108. {
  109. Unselect(selector);
  110. }
  111. }
  112. else
  113. {
  114. Select(selector, e.actionKey);
  115. }
  116. e.StopPropagation();
  117. }
  118. public bool IsSelectable()
  119. {
  120. return true;
  121. }
  122. public bool HitTest(Vector2 localPoint)
  123. {
  124. return ContainsPoint(localPoint);
  125. }
  126. public void Select(VisualElement selectionContainer, bool additive)
  127. {
  128. if (selectionContainer is ISelection selection)
  129. {
  130. if (!selection.selection.Contains(this))
  131. {
  132. if (!additive)
  133. {
  134. selection.ClearSelection();
  135. selection.AddToSelection(this);
  136. }
  137. else
  138. {
  139. selection.AddToSelection(this);
  140. }
  141. }
  142. }
  143. }
  144. public void Unselect(VisualElement selectionContainer)
  145. {
  146. if (selectionContainer is ISelection selection)
  147. {
  148. if (selection.selection.Contains(this))
  149. {
  150. selection.RemoveFromSelection(this);
  151. }
  152. }
  153. }
  154. public bool IsSelected(VisualElement selectionContainer)
  155. {
  156. if (selectionContainer is ISelection selection)
  157. {
  158. if (selection.selection.Contains(this))
  159. {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. }
  166. }