VFXVector3Field.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using Action = System.Action;
  5. using FloatField = UnityEditor.VFX.UI.VFXLabeledField<UnityEditor.UIElements.FloatField, float>;
  6. namespace UnityEditor.VFX.UI
  7. {
  8. abstract class VFXVectorNField<T> : VFXControl<T>
  9. {
  10. FloatField[] m_Fields;
  11. VisualElement[] m_FieldParents;
  12. VisualElement[] m_TooltipHolders;
  13. protected abstract int componentCount { get; }
  14. public virtual string GetComponentName(int i)
  15. {
  16. switch (i)
  17. {
  18. case 0:
  19. return "x";
  20. case 1:
  21. return "y";
  22. case 2:
  23. return "z";
  24. case 3:
  25. return "w";
  26. default:
  27. return "a";
  28. }
  29. }
  30. public override void SetEnabled(bool value)
  31. {
  32. for (int i = 0; i < componentCount; ++i)
  33. {
  34. m_Fields[i].SetEnabled(value);
  35. if (value)
  36. {
  37. m_TooltipHolders[i].RemoveFromHierarchy();
  38. }
  39. else
  40. {
  41. m_FieldParents[i].Add(m_TooltipHolders[i]);
  42. }
  43. }
  44. }
  45. void ValueDragFinished()
  46. {
  47. if (onValueDragFinished != null)
  48. onValueDragFinished();
  49. }
  50. void ValueDragStarted()
  51. {
  52. if (onValueDragStarted != null)
  53. onValueDragStarted();
  54. }
  55. public Action onValueDragFinished;
  56. public Action onValueDragStarted;
  57. void CreateTextField()
  58. {
  59. m_Fields = new FloatField[componentCount];
  60. m_FieldParents = new VisualElement[componentCount];
  61. m_TooltipHolders = new VisualElement[componentCount];
  62. for (int i = 0; i < m_Fields.Length; ++i)
  63. {
  64. m_Fields[i] = new FloatField(GetComponentName(i));
  65. m_Fields[i].control.AddToClassList("fieldContainer");
  66. m_Fields[i].AddToClassList("fieldContainer");
  67. m_Fields[i].RegisterCallback<ChangeEvent<float>, int>(OnValueChanged, i);
  68. m_Fields[i].onValueDragFinished = t => ValueDragFinished();
  69. m_Fields[i].onValueDragStarted = t => ValueDragStarted();
  70. m_FieldParents[i] = new VisualElement { name = "FieldParent" };
  71. m_FieldParents[i].Add(m_Fields[i]);
  72. m_FieldParents[i].style.flexGrow = 1;
  73. m_TooltipHolders[i] = new VisualElement { name = "TooltipHolder" };
  74. m_TooltipHolders[i].style.position = UnityEngine.UIElements.Position.Absolute;
  75. m_TooltipHolders[i].style.top = 0;
  76. m_TooltipHolders[i].style.left = 0;
  77. m_TooltipHolders[i].style.right = 0;
  78. m_TooltipHolders[i].style.bottom = 0;
  79. Add(m_FieldParents[i]);
  80. }
  81. m_Fields[0].label.AddToClassList("first");
  82. }
  83. public override bool indeterminate
  84. {
  85. get
  86. {
  87. return m_Fields[0].indeterminate;
  88. }
  89. set
  90. {
  91. foreach (var field in m_Fields)
  92. {
  93. field.indeterminate = value;
  94. }
  95. }
  96. }
  97. protected abstract void SetValueComponent(ref T value, int i, float componentValue);
  98. protected abstract float GetValueComponent(ref T value, int i);
  99. void OnValueChanged(ChangeEvent<float> e, int component)
  100. {
  101. T newValue = value;
  102. SetValueComponent(ref newValue, component, m_Fields[component].value);
  103. SetValueAndNotify(newValue);
  104. }
  105. public VFXVectorNField()
  106. {
  107. CreateTextField();
  108. style.flexDirection = FlexDirection.Row;
  109. }
  110. protected override void ValueToGUI(bool force)
  111. {
  112. T value = this.value;
  113. for (int i = 0; i < m_Fields.Length; ++i)
  114. {
  115. float componentValue = GetValueComponent(ref value, i);
  116. if (!m_Fields[i].control.HasFocus() || force)
  117. {
  118. m_Fields[i].SetValueWithoutNotify(componentValue);
  119. }
  120. m_TooltipHolders[i].tooltip = componentValue.ToString();
  121. }
  122. }
  123. }
  124. class VFXVector3Field : VFXVectorNField<Vector3>
  125. {
  126. protected override int componentCount { get { return 3; } }
  127. protected override void SetValueComponent(ref Vector3 value, int i, float componentValue)
  128. {
  129. switch (i)
  130. {
  131. case 0:
  132. value.x = componentValue;
  133. break;
  134. case 1:
  135. value.y = componentValue;
  136. break;
  137. default:
  138. value.z = componentValue;
  139. break;
  140. }
  141. }
  142. protected override float GetValueComponent(ref Vector3 value, int i)
  143. {
  144. switch (i)
  145. {
  146. case 0:
  147. return value.x;
  148. case 1:
  149. return value.y;
  150. default:
  151. return value.z;
  152. }
  153. }
  154. }
  155. }