123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using UnityEngine;
- using UnityEngine.UIElements;
- using UnityEditor.UIElements;
- using System.Collections.Generic;
- using System.Linq;
- using Action = System.Action;
- namespace UnityEditor.VFX.UI
- {
- class VFXMinMaxSliderField : VisualElement, INotifyValueChanged<Vector2>
- {
- protected MinMaxSlider m_Slider;
- VisualElement m_IndeterminateLabel;
- class StartFinishSliderManipulator : Manipulator
- {
- VFXMinMaxSliderField m_Slider;
- bool m_InDrag;
- protected override void RegisterCallbacksOnTarget()
- {
- m_Slider = target.GetFirstOfType<VFXMinMaxSliderField>();
- target.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
- target.RegisterCallback<MouseUpEvent>(OnMouseUp, TrickleDown.TrickleDown);
- }
- protected override void UnregisterCallbacksFromTarget()
- {
- target.UnregisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
- target.UnregisterCallback<MouseUpEvent>(OnMouseUp, TrickleDown.TrickleDown);
- }
- void OnMouseDown(MouseDownEvent e)
- {
- target.RegisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown);
- }
- void OnMouseMove(MouseMoveEvent e)
- {
- if (!m_InDrag)
- {
- m_InDrag = true;
- m_Slider.ValueDragStarted();
- target.UnregisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown); //we care only about the first drag event
- }
- }
- void OnMouseUp(MouseUpEvent e)
- {
- if (m_InDrag)
- m_Slider.ValueDragFinished();
- else
- target.UnregisterCallback<MouseMoveEvent>(OnMouseMove, TrickleDown.TrickleDown);
- m_InDrag = false;
- }
- }
- protected void ValueDragFinished()
- {
- if (onValueDragFinished != null)
- onValueDragFinished();
- }
- protected void ValueDragStarted()
- {
- if (onValueDragStarted != null)
- onValueDragStarted();
- }
- public Action onValueDragFinished;
- public Action onValueDragStarted;
- public VFXMinMaxSliderField()
- {
- AddToClassList("sliderMinMaxField");
- m_Slider = new MinMaxSlider(1, 10, 0, 100);
- m_Slider.RegisterValueChangedCallback(evt => ValueChanged(evt.newValue));
- m_IndeterminateLabel = new Label()
- {
- name = "indeterminate",
- text = VFXControlConstants.indeterminateText
- };
- m_IndeterminateLabel.SetEnabled(false);
- m_Slider.RegisterValueChangedCallback(ValueChanged);
- Add(m_Slider);
- RegisterCallBack();
- }
- protected void RegisterCallBack()
- {
- m_Slider.Children().First().AddManipulator(new StartFinishSliderManipulator());
- }
- void OnFocusLost(BlurEvent e)
- {
- //forward the focus lost event
- using (BlurEvent newE = BlurEvent.GetPooled(this, e.relatedTarget, e.direction, panel.focusController))
- {
- SendEvent(newE);
- }
- e.StopPropagation();
- }
- public Vector2 m_Value;
- public Vector2 value
- {
- get => m_Value;
- set => SetValueAndNotify(value);
- }
- private Vector2 m_Range;
- public Vector2 range
- {
- get
- {
- return m_Range;
- }
- set
- {
- m_Range = value;
- m_IgnoreNotification = true;
- if (m_Slider.lowLimit != m_Range.x || m_Slider.highLimit != m_Range.y)
- {
- m_Slider.lowLimit = m_Range.x;
- m_Slider.highLimit = m_Range.y;
- }
- m_IgnoreNotification = false;
- }
- }
- protected bool m_IgnoreNotification;
- public void SetValueAndNotify(Vector2 newValue)
- {
- if (!EqualityComparer<Vector2>.Default.Equals(value, newValue))
- {
- using (ChangeEvent<Vector2> evt = ChangeEvent<Vector2>.GetPooled(value, newValue))
- {
- evt.target = this;
- SetValueWithoutNotify(newValue);
- SendEvent(evt);
- }
- }
- }
- public void SetValueWithoutNotify(Vector2 newValue)
- {
- m_IgnoreNotification = true;
- m_Value = newValue;
- tooltip = newValue.ToString();
- m_Slider.value = value;
- m_IgnoreNotification = false;
- }
- protected void ValueChanged(ChangeEvent<Vector2> e)
- {
- e.StopPropagation();
- if (!m_IgnoreNotification)
- SetValueAndNotify(e.newValue);
- }
- void ValueChanged(Vector2 newValue)
- {
- SetValueAndNotify(newValue);
- }
- public bool indeterminate
- {
- get { return m_Slider.parent == null; }
- set
- {
- if (indeterminate != value)
- {
- if (value)
- {
- m_Slider.RemoveFromHierarchy();
- Add(m_IndeterminateLabel);
- }
- else
- {
- m_IndeterminateLabel.RemoveFromHierarchy();
- Add(m_Slider);
- }
- m_Slider.SetEnabled(!value);
- }
- }
- }
- public bool hasFocus => m_Slider.HasFocus();
- }
- }
|