using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; using System; namespace UIWidgets { [RequireComponent(typeof(RectTransform))] /// /// Range slider handle. /// public class RangeSliderHandle : UnityEngine.UI.Selectable, IDragHandler, IInitializePotentialDragHandler, ISubmitHandler { /// /// Is horizontal direction? /// public Func IsHorizontal = ReturnTrue; /// /// Decrease on move. /// public Action Decrease = DoNothing; /// /// Increase on move. /// public Action Increase = DoNothing; /// /// The position limits. /// public Func PositionLimits; /// /// On position changed. /// public Action PositionChanged; /// /// On submit. /// public Action OnSubmit = DoNothing; RectTransform rectTransform; /// /// Gets the rect transform. /// /// The rect transform. public RectTransform RectTransform { get { if (rectTransform==null) { rectTransform = transform as RectTransform; } return rectTransform; } } /// /// Determines whether this instance can drag the specified eventData. /// /// true if this instance can drag the specified eventData; otherwise, false. /// Event data. bool CanDrag(PointerEventData eventData) { return IsActive() && IsInteractable() && eventData.button == PointerEventData.InputButton.Left; } static bool ReturnTrue() { return true; } static void DoNothing() { } /// /// Raises the submit event. /// /// Event data. void ISubmitHandler.OnSubmit(BaseEventData eventData) { OnSubmit(); } /// /// Raises the drag event. /// /// Event data. public virtual void OnDrag(PointerEventData eventData) { if (!CanDrag(eventData)) { return; } Vector2 curCursor; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform, eventData.position, eventData.pressEventCamera, out curCursor)) { return ; } Vector2 limits = PositionLimits(); Vector3 new_position = RectTransform.anchoredPosition; if (IsHorizontal()) { new_position.x = Mathf.Clamp(new_position.x + curCursor.x, limits.x, limits.y); } else { new_position.y = Mathf.Clamp(new_position.y + curCursor.y, limits.x, limits.y); } if (IsNewPosition(new_position)) { RectTransform.anchoredPosition = new_position; PositionChanged(IsHorizontal() ? new_position.x : new_position.y); } } /// /// Determines whether this instance is new position the specified pos. /// /// true if this instance is new position the specified pos; otherwise, false. /// Position. bool IsNewPosition(Vector3 pos) { if (IsHorizontal()) { return RectTransform.anchoredPosition.x!=pos.x; } else { return RectTransform.anchoredPosition.y!=pos.y; } } /// /// Raises the initialize potential drag event. /// /// Event data. public virtual void OnInitializePotentialDrag(PointerEventData eventData) { eventData.useDragThreshold = false; } /// /// Raises the move event. /// /// Event data. public override void OnMove(AxisEventData eventData) { if (!IsActive() || !IsInteractable()) { base.OnMove(eventData); return; } switch (eventData.moveDir) { case MoveDirection.Left: if (IsHorizontal() && FindSelectableOnLeft()==null) { Decrease(); } else { base.OnMove(eventData); } break; case MoveDirection.Right: if (IsHorizontal() && FindSelectableOnRight()==null) { Increase(); } else { base.OnMove(eventData); } break; case MoveDirection.Up: if (!IsHorizontal() && FindSelectableOnUp()==null) { Increase(); } else { base.OnMove(eventData); } break; case MoveDirection.Down: if (!IsHorizontal() && FindSelectableOnDown()==null) { Decrease(); } else { base.OnMove(eventData); } break; } } /// /// Finds the selectable on left. /// /// The selectable on left. public override UnityEngine.UI.Selectable FindSelectableOnLeft() { if (navigation.mode == Navigation.Mode.Automatic && IsHorizontal()) return null; return base.FindSelectableOnLeft(); } /// /// Finds the selectable on right. /// /// The selectable on right. public override UnityEngine.UI.Selectable FindSelectableOnRight() { if (navigation.mode == Navigation.Mode.Automatic && IsHorizontal()) return null; return base.FindSelectableOnRight(); } /// /// Finds the selectable on up. /// /// The selectable on up. public override UnityEngine.UI.Selectable FindSelectableOnUp() { if (navigation.mode == Navigation.Mode.Automatic && !IsHorizontal()) return null; return base.FindSelectableOnUp(); } /// /// Finds the selectable on down. /// /// The selectable on down. public override UnityEngine.UI.Selectable FindSelectableOnDown() { if (navigation.mode == Navigation.Mode.Automatic && !IsHorizontal()) return null; return base.FindSelectableOnDown(); } } }