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