VFXMinMaxSliderField.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. using UnityEditor.UIElements;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Action = System.Action;
  8. namespace UnityEditor.VFX.UI
  9. {
  10. class VFXMinMaxSliderField : VisualElement, INotifyValueChanged<Vector2>
  11. {
  12. protected MinMaxSlider m_Slider;
  13. VisualElement m_IndeterminateLabel;
  14. class StartFinishSliderManipulator : Manipulator
  15. {
  16. VFXMinMaxSliderField m_Slider;
  17. bool m_InDrag;
  18. protected override void RegisterCallbacksOnTarget()
  19. {
  20. m_Slider = target.GetFirstOfType<VFXMinMaxSliderField>();
  21. target.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
  22. target.RegisterCallback<MouseUpEvent>(OnMouseUp, TrickleDown.TrickleDown);
  23. }
  24. protected override void UnregisterCallbacksFromTarget()
  25. {
  26. target.UnregisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
  27. target.UnregisterCallback<MouseUpEvent>(OnMouseUp, TrickleDown.TrickleDown);
  28. }
  29. void OnMouseDown(MouseDownEvent e)
  30. {
  31. target.RegisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown);
  32. }
  33. void OnMouseMove(MouseMoveEvent e)
  34. {
  35. if (!m_InDrag)
  36. {
  37. m_InDrag = true;
  38. m_Slider.ValueDragStarted();
  39. target.UnregisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown); //we care only about the first drag event
  40. }
  41. }
  42. void OnMouseUp(MouseUpEvent e)
  43. {
  44. if (m_InDrag)
  45. m_Slider.ValueDragFinished();
  46. else
  47. target.UnregisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown);
  48. m_InDrag = false;
  49. }
  50. }
  51. protected void ValueDragFinished()
  52. {
  53. if (onValueDragFinished != null)
  54. onValueDragFinished();
  55. }
  56. protected void ValueDragStarted()
  57. {
  58. if (onValueDragStarted != null)
  59. onValueDragStarted();
  60. }
  61. public Action onValueDragFinished;
  62. public Action onValueDragStarted;
  63. public VFXMinMaxSliderField()
  64. {
  65. AddToClassList("sliderMinMaxField");
  66. m_Slider = new MinMaxSlider(1, 10, 0, 100);
  67. m_Slider.RegisterValueChangedCallback(evt => ValueChanged(evt.newValue));
  68. m_IndeterminateLabel = new Label()
  69. {
  70. name = "indeterminate",
  71. text = VFXControlConstants.indeterminateText
  72. };
  73. m_IndeterminateLabel.SetEnabled(false);
  74. m_Slider.RegisterValueChangedCallback(ValueChanged);
  75. Add(m_Slider);
  76. RegisterCallBack();
  77. }
  78. protected void RegisterCallBack()
  79. {
  80. m_Slider.Children().First().AddManipulator(new StartFinishSliderManipulator());
  81. }
  82. void OnFocusLost(BlurEvent e)
  83. {
  84. //forward the focus lost event
  85. using (BlurEvent newE = BlurEvent.GetPooled(this, e.relatedTarget, e.direction, panel.focusController))
  86. {
  87. SendEvent(newE);
  88. }
  89. e.StopPropagation();
  90. }
  91. public Vector2 m_Value;
  92. public Vector2 value
  93. {
  94. get => m_Value;
  95. set => SetValueAndNotify(value);
  96. }
  97. private Vector2 m_Range;
  98. public Vector2 range
  99. {
  100. get
  101. {
  102. return m_Range;
  103. }
  104. set
  105. {
  106. m_Range = value;
  107. m_IgnoreNotification = true;
  108. if (m_Slider.lowLimit != m_Range.x || m_Slider.highLimit != m_Range.y)
  109. {
  110. m_Slider.lowLimit = m_Range.x;
  111. m_Slider.highLimit = m_Range.y;
  112. }
  113. m_IgnoreNotification = false;
  114. }
  115. }
  116. protected bool m_IgnoreNotification;
  117. public void SetValueAndNotify(Vector2 newValue)
  118. {
  119. if (!EqualityComparer<Vector2>.Default.Equals(value, newValue))
  120. {
  121. using (ChangeEvent<Vector2> evt = ChangeEvent<Vector2>.GetPooled(value, newValue))
  122. {
  123. evt.target = this;
  124. SetValueWithoutNotify(newValue);
  125. SendEvent(evt);
  126. }
  127. }
  128. }
  129. public void SetValueWithoutNotify(Vector2 newValue)
  130. {
  131. m_IgnoreNotification = true;
  132. m_Value = newValue;
  133. tooltip = newValue.ToString();
  134. m_Slider.value = value;
  135. m_IgnoreNotification = false;
  136. }
  137. protected void ValueChanged(ChangeEvent<Vector2> e)
  138. {
  139. e.StopPropagation();
  140. if (!m_IgnoreNotification)
  141. SetValueAndNotify(e.newValue);
  142. }
  143. void ValueChanged(Vector2 newValue)
  144. {
  145. SetValueAndNotify(newValue);
  146. }
  147. public bool indeterminate
  148. {
  149. get { return m_Slider.parent == null; }
  150. set
  151. {
  152. if (indeterminate != value)
  153. {
  154. if (value)
  155. {
  156. m_Slider.RemoveFromHierarchy();
  157. Add(m_IndeterminateLabel);
  158. }
  159. else
  160. {
  161. m_IndeterminateLabel.RemoveFromHierarchy();
  162. Add(m_Slider);
  163. }
  164. m_Slider.SetEnabled(!value);
  165. }
  166. }
  167. }
  168. public bool hasFocus => m_Slider.HasFocus();
  169. }
  170. }