| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- namespace SRF.UI
- {
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class DragHandle : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
- {
- private CanvasScaler _canvasScaler;
- private float _delta;
- private float _startValue;
- public RectTransform.Axis Axis = RectTransform.Axis.Horizontal;
- public bool Invert = false;
- public float MaxSize = -1;
- public LayoutElement TargetLayoutElement;
- public RectTransform TargetRectTransform;
- private float Mult
- {
- get { return Invert ? -1 : 1; }
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- if (!Verify())
- {
- return;
- }
- //Debug.Log("OnBeginDrag");
- _startValue = GetCurrentValue();
- _delta = 0;
- }
- public void OnDrag(PointerEventData eventData)
- {
- if (!Verify())
- {
- return;
- }
- //Debug.Log("OnDrag");
- var delta = 0f;
- if (Axis == RectTransform.Axis.Horizontal)
- {
- delta += eventData.delta.x;
- }
- else
- {
- delta += eventData.delta.y;
- }
- if (_canvasScaler != null)
- {
- delta /= _canvasScaler.scaleFactor;
- }
- delta *= Mult;
- _delta += delta;
- SetCurrentValue(Mathf.Clamp(_startValue + _delta, GetMinSize(), GetMaxSize()));
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- if (!Verify())
- {
- return;
- }
- //Debug.Log("OnEndDrag");
- SetCurrentValue(Mathf.Max(_startValue + _delta, GetMinSize()));
- _delta = 0;
- CommitCurrentValue();
- }
- private void Start()
- {
- Verify();
- _canvasScaler = GetComponentInParent<CanvasScaler>();
- }
- private bool Verify()
- {
- if (TargetLayoutElement == null && TargetRectTransform == null)
- {
- Debug.LogWarning(
- "DragHandle: TargetLayoutElement and TargetRectTransform are both null. Disabling behaviour.");
- enabled = false;
- return false;
- }
- return true;
- }
- private float GetCurrentValue()
- {
- if (TargetLayoutElement != null)
- {
- return Axis == RectTransform.Axis.Horizontal
- ? TargetLayoutElement.preferredWidth
- : TargetLayoutElement.preferredHeight;
- }
- if (TargetRectTransform != null)
- {
- return Axis == RectTransform.Axis.Horizontal
- ? TargetRectTransform.sizeDelta.x
- : TargetRectTransform.sizeDelta.y;
- }
- throw new InvalidOperationException();
- }
- private void SetCurrentValue(float value)
- {
- if (TargetLayoutElement != null)
- {
- if (Axis == RectTransform.Axis.Horizontal)
- {
- TargetLayoutElement.preferredWidth = value;
- }
- else
- {
- TargetLayoutElement.preferredHeight = value;
- }
- return;
- }
- if (TargetRectTransform != null)
- {
- var d = TargetRectTransform.sizeDelta;
- if (Axis == RectTransform.Axis.Horizontal)
- {
- d.x = value;
- }
- else
- {
- d.y = value;
- }
- TargetRectTransform.sizeDelta = d;
- return;
- }
- throw new InvalidOperationException();
- }
- private void CommitCurrentValue()
- {
- if (TargetLayoutElement != null)
- {
- if (Axis == RectTransform.Axis.Horizontal)
- {
- TargetLayoutElement.preferredWidth = ((RectTransform) TargetLayoutElement.transform).sizeDelta.x;
- }
- else
- {
- TargetLayoutElement.preferredHeight = ((RectTransform) TargetLayoutElement.transform).sizeDelta.y;
- }
- }
- }
- private float GetMinSize()
- {
- if (TargetLayoutElement == null)
- {
- return 0;
- }
- return Axis == RectTransform.Axis.Horizontal ? TargetLayoutElement.minWidth : TargetLayoutElement.minHeight;
- }
- private float GetMaxSize()
- {
- if (MaxSize > 0)
- {
- return MaxSize;
- }
- return float.MaxValue;
- }
- }
- }
|