VFXLabeledField.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using System;
  5. namespace UnityEditor.VFX.UI
  6. {
  7. //Copied from mousefield dragger but add notifications needed for delayed fields
  8. class VFXFieldMouseDragger<T>
  9. {
  10. Action m_OnDragFinished;
  11. Action m_OnDragStarted;
  12. public VFXFieldMouseDragger(IValueField<T> drivenField, Action onDragFinished = null, Action onDragStarted = null)
  13. {
  14. m_DrivenField = drivenField;
  15. m_DragElement = null;
  16. m_DragHotZone = new Rect(0, 0, -1, -1);
  17. m_OnDragFinished = onDragFinished;
  18. m_OnDragStarted = onDragStarted;
  19. dragging = false;
  20. }
  21. IValueField<T> m_DrivenField;
  22. VisualElement m_DragElement;
  23. Rect m_DragHotZone;
  24. public bool dragging;
  25. public T startValue;
  26. public void SetDragZone(VisualElement dragElement)
  27. {
  28. SetDragZone(dragElement, new Rect(0, 0, -1, -1));
  29. }
  30. public void SetDragZone(VisualElement dragElement, Rect hotZone)
  31. {
  32. if (m_DragElement != null)
  33. {
  34. m_DragElement.UnregisterCallback<MouseDownEvent>(UpdateValueOnMouseDown);
  35. m_DragElement.UnregisterCallback<MouseMoveEvent>(UpdateValueOnMouseMove);
  36. m_DragElement.UnregisterCallback<MouseUpEvent>(UpdateValueOnMouseUp);
  37. m_DragElement.UnregisterCallback<KeyDownEvent>(UpdateValueOnKeyDown);
  38. }
  39. m_DragElement = dragElement;
  40. m_DragHotZone = hotZone;
  41. if (m_DragElement != null)
  42. {
  43. dragging = false;
  44. m_DragElement.RegisterCallback<MouseDownEvent>(UpdateValueOnMouseDown);
  45. m_DragElement.RegisterCallback<MouseMoveEvent>(UpdateValueOnMouseMove);
  46. m_DragElement.RegisterCallback<MouseUpEvent>(UpdateValueOnMouseUp);
  47. m_DragElement.RegisterCallback<KeyDownEvent>(UpdateValueOnKeyDown);
  48. }
  49. }
  50. void UpdateValueOnMouseDown(MouseDownEvent evt)
  51. {
  52. if (evt.button == 0 && (m_DragHotZone.width < 0 || m_DragHotZone.height < 0 || m_DragHotZone.Contains(m_DragElement.WorldToLocal(evt.mousePosition))))
  53. {
  54. m_DragElement.CaptureMouse();
  55. // Make sure no other elements can capture the mouse!
  56. evt.StopPropagation();
  57. dragging = true;
  58. startValue = m_DrivenField.value;
  59. if (m_OnDragStarted != null)
  60. m_OnDragStarted();
  61. EditorGUIUtility.SetWantsMouseJumping(1);
  62. }
  63. }
  64. void UpdateValueOnMouseMove(MouseMoveEvent evt)
  65. {
  66. if (dragging)
  67. {
  68. DeltaSpeed s = evt.shiftKey ? DeltaSpeed.Fast : (evt.altKey ? DeltaSpeed.Slow : DeltaSpeed.Normal);
  69. m_DrivenField.ApplyInputDeviceDelta(evt.mouseDelta, s, startValue);
  70. }
  71. }
  72. void UpdateValueOnMouseUp(MouseUpEvent evt)
  73. {
  74. if (dragging)
  75. {
  76. dragging = false;
  77. MouseCaptureController.ReleaseMouse();
  78. EditorGUIUtility.SetWantsMouseJumping(0);
  79. if (m_OnDragFinished != null)
  80. m_OnDragFinished();
  81. }
  82. }
  83. void UpdateValueOnKeyDown(KeyDownEvent evt)
  84. {
  85. if (dragging && evt.keyCode == KeyCode.Escape)
  86. {
  87. dragging = false;
  88. m_DrivenField.value = startValue;
  89. MouseCaptureController.ReleaseMouse();
  90. EditorGUIUtility.SetWantsMouseJumping(0);
  91. }
  92. }
  93. }
  94. class VFXLabeledField<T, U> : VisualElement, INotifyValueChanged<U> where T : VisualElement, INotifyValueChanged<U>, new()
  95. {
  96. protected Label m_Label;
  97. protected T m_Control;
  98. public VisualElement m_IndeterminateLabel;
  99. public VFXLabeledField(Label existingLabel)
  100. {
  101. m_Label = existingLabel;
  102. CreateControl();
  103. SetupLabel();
  104. }
  105. bool m_Indeterminate;
  106. public bool indeterminate
  107. {
  108. get { return m_Control.parent == null; }
  109. set
  110. {
  111. if (m_Indeterminate != value)
  112. {
  113. m_Indeterminate = value;
  114. if (value)
  115. {
  116. m_Control.RemoveFromHierarchy();
  117. Add(m_IndeterminateLabel);
  118. }
  119. else
  120. {
  121. m_IndeterminateLabel.RemoveFromHierarchy();
  122. Add(m_Control);
  123. }
  124. }
  125. }
  126. }
  127. public VFXLabeledField(string label)
  128. {
  129. if (!string.IsNullOrEmpty(label))
  130. {
  131. m_Label = new Label() { text = label };
  132. m_Label.AddToClassList("label");
  133. Add(m_Label);
  134. }
  135. style.flexDirection = FlexDirection.Row;
  136. CreateControl();
  137. SetupLabel();
  138. }
  139. void SetupLabel()
  140. {
  141. if (typeof(IValueField<U>).IsAssignableFrom(typeof(T)))
  142. {
  143. m_Label.styleSheets.Add(VFXView.LoadStyleSheet("VFXLabeledField"));
  144. if (typeof(U) == typeof(float))
  145. {
  146. var dragger = new VFXFieldMouseDragger<float>((IValueField<float>)m_Control, DragValueFinished, DragValueStarted);
  147. dragger.SetDragZone(m_Label);
  148. m_Label.AddToClassList("cursor-slide-arrow");
  149. }
  150. else if (typeof(U) == typeof(double))
  151. {
  152. var dragger = new VFXFieldMouseDragger<double>((IValueField<double>)m_Control, DragValueFinished, DragValueStarted);
  153. dragger.SetDragZone(m_Label);
  154. m_Label.AddToClassList("cursor-slide-arrow");
  155. }
  156. else if (typeof(U) == typeof(long))
  157. {
  158. var dragger = new VFXFieldMouseDragger<long>((IValueField<long>)m_Control, DragValueFinished, DragValueStarted);
  159. dragger.SetDragZone(m_Label);
  160. m_Label.AddToClassList("cursor-slide-arrow");
  161. }
  162. else if (typeof(U) == typeof(int))
  163. {
  164. var dragger = new VFXFieldMouseDragger<int>((IValueField<int>)m_Control, DragValueFinished, DragValueStarted);
  165. dragger.SetDragZone(m_Label);
  166. m_Label.AddToClassList("cursor-slide-arrow");
  167. }
  168. }
  169. m_IndeterminateLabel = new Label()
  170. {
  171. name = "indeterminate",
  172. text = VFXControlConstants.indeterminateText
  173. };
  174. m_IndeterminateLabel.SetEnabled(false);
  175. }
  176. void DragValueFinished()
  177. {
  178. if (onValueDragFinished != null)
  179. onValueDragFinished(this);
  180. }
  181. void DragValueStarted()
  182. {
  183. if (onValueDragStarted != null)
  184. onValueDragStarted(this);
  185. }
  186. public Action<VFXLabeledField<T, U>> onValueDragFinished;
  187. public Action<VFXLabeledField<T, U>> onValueDragStarted;
  188. void CreateControl()
  189. {
  190. m_Control = new T();
  191. Add(m_Control);
  192. m_Control.RegisterValueChangedCallback(OnControlChange);
  193. }
  194. void OnControlChange(ChangeEvent<U> e)
  195. {
  196. e.StopPropagation();
  197. using (ChangeEvent<U> evt = ChangeEvent<U>.GetPooled(e.previousValue, e.newValue))
  198. {
  199. evt.target = this;
  200. SendEvent(evt);
  201. }
  202. }
  203. public T control
  204. {
  205. get { return m_Control; }
  206. }
  207. public Label label
  208. {
  209. get { return m_Label; }
  210. }
  211. public void SetValueAndNotify(U newValue)
  212. {
  213. (m_Control as INotifyValueChanged<U>).value = newValue;
  214. }
  215. public void SetValueWithoutNotify(U newValue)
  216. {
  217. (m_Control as INotifyValueChanged<U>).SetValueWithoutNotify(newValue);
  218. }
  219. public U value
  220. {
  221. get { return m_Control.value; }
  222. set { m_Control.value = value; }
  223. }
  224. }
  225. abstract class ValueControl<T> : VisualElement
  226. {
  227. protected Label m_Label;
  228. protected ValueControl(Label existingLabel)
  229. {
  230. m_Label = existingLabel;
  231. }
  232. protected ValueControl(string label)
  233. {
  234. if (!string.IsNullOrEmpty(label))
  235. {
  236. m_Label = new Label() { text = label };
  237. m_Label.AddToClassList("label");
  238. Add(m_Label);
  239. }
  240. style.flexDirection = FlexDirection.Row;
  241. }
  242. public T GetValue()
  243. {
  244. return m_Value;
  245. }
  246. public void SetValue(T value)
  247. {
  248. m_Value = value;
  249. ValueToGUI(false);
  250. }
  251. public T value
  252. {
  253. get { return GetValue(); }
  254. set { SetValue(value); }
  255. }
  256. public void SetMultiplier(T multiplier)
  257. {
  258. m_Multiplier = multiplier;
  259. }
  260. protected T m_Value;
  261. protected T m_Multiplier;
  262. public System.Action OnValueChanged;
  263. protected abstract void ValueToGUI(bool force);
  264. }
  265. }