2024-07-17 20:15:22 +08:00
|
|
|
|
using Gameplay.Performance;
|
|
|
|
|
|
using System.Collections;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
2024-07-17 18:42:19 +08:00
|
|
|
|
using static UIButtonEx;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
|
|
|
|
|
|
public interface IPinchHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
void OnPinchStart();
|
|
|
|
|
|
void OnPinchEnd();
|
|
|
|
|
|
void OnPinchZoom(float gapDelta);
|
|
|
|
|
|
}
|
2024-07-17 20:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用于图片的双指缩放以及拖拽效果
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PinchZoomModule : MonoBehaviour, IPointerUpHandler, IPointerDownHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
|
2024-06-20 14:58:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
private int firstEnterFingerID = -1;
|
|
|
|
|
|
private int lastEnterFingerID = -1;
|
|
|
|
|
|
private int nowFingersCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
private Vector2 firstFingerPosition;
|
|
|
|
|
|
private Vector2 lastFingerPosition;
|
|
|
|
|
|
private float distance = 0.0f;
|
|
|
|
|
|
|
2024-07-17 20:15:22 +08:00
|
|
|
|
public float zoomSpeed = 0.01f;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
public float minZoom = 0.5f;
|
|
|
|
|
|
public float maxZoom = 2.0f;
|
|
|
|
|
|
|
|
|
|
|
|
float scale = 1.0f;
|
|
|
|
|
|
|
|
|
|
|
|
public float zoomGap = 0.0f;
|
|
|
|
|
|
public float zoomGapDelta = 0.0f;
|
|
|
|
|
|
public float zoomGapDeltaSpeed = 0.1f;
|
|
|
|
|
|
|
2024-07-17 20:15:22 +08:00
|
|
|
|
float scaleFactor;
|
|
|
|
|
|
|
2024-06-20 14:58:48 +08:00
|
|
|
|
private bool isPinchZooming = false;
|
|
|
|
|
|
|
|
|
|
|
|
public IPinchHandler pinchHandler;
|
|
|
|
|
|
|
2024-07-17 18:42:19 +08:00
|
|
|
|
public delegate void OnDragStartedHandler(PointerEventData eventData);
|
|
|
|
|
|
public OnDragStartedHandler OnDragStarted;
|
|
|
|
|
|
public delegate void OnDragCompletedHandler(PointerEventData eventData);
|
|
|
|
|
|
public OnDragCompletedHandler OnDragCompleted;
|
|
|
|
|
|
public delegate void OnDragHandler(PointerEventData eventData);
|
|
|
|
|
|
public OnDragHandler OnDraging;
|
|
|
|
|
|
|
2024-06-20 14:58:48 +08:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
2024-07-17 20:15:22 +08:00
|
|
|
|
if (GetComponent(typeof(IPinchHandler)) as IPinchHandler == null)
|
2024-06-20 14:58:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
var add = gameObject.AddComponent<UIMultiScaleTool>();
|
|
|
|
|
|
add.minScale = minZoom;
|
|
|
|
|
|
add.maxScale = maxZoom;
|
|
|
|
|
|
add.enabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
pinchHandler = GetComponent(typeof(IPinchHandler)) as IPinchHandler;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
|
{
|
|
|
|
|
|
GetComponent<UIMultiScaleTool>().enabled = true;
|
|
|
|
|
|
zoomGap = 0.0f;
|
|
|
|
|
|
scale = 1.0f;
|
2026-01-09 12:49:36 +08:00
|
|
|
|
scaleFactor = UIRoot.Instance.UINormalCanvas.GetComponent<RectTransform>().sizeDelta.x / Screen.width;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
|
{
|
|
|
|
|
|
GetComponent<UIMultiScaleTool>().enabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
nowFingersCount++;
|
|
|
|
|
|
if (nowFingersCount == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
firstEnterFingerID = eventData.pointerId;
|
|
|
|
|
|
firstFingerPosition = eventData.position;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nowFingersCount == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastEnterFingerID = eventData.pointerId;
|
|
|
|
|
|
lastFingerPosition = eventData.position;
|
|
|
|
|
|
|
|
|
|
|
|
distance = Vector2.Distance(firstFingerPosition, lastFingerPosition);
|
2024-07-17 20:15:22 +08:00
|
|
|
|
distance *= scaleFactor;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
|
|
|
|
|
|
isPinchZooming = true;
|
|
|
|
|
|
pinchHandler?.OnPinchStart();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
nowFingersCount--;
|
|
|
|
|
|
if (eventData.pointerId == firstEnterFingerID)
|
|
|
|
|
|
{
|
|
|
|
|
|
firstEnterFingerID = lastEnterFingerID;
|
|
|
|
|
|
firstFingerPosition = lastFingerPosition;
|
|
|
|
|
|
|
|
|
|
|
|
lastEnterFingerID = -1;
|
|
|
|
|
|
if (isPinchZooming)
|
|
|
|
|
|
{
|
|
|
|
|
|
isPinchZooming = false;
|
|
|
|
|
|
pinchHandler?.OnPinchEnd();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-17 20:15:22 +08:00
|
|
|
|
else if (eventData.pointerId == lastEnterFingerID)
|
2024-06-20 14:58:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
lastEnterFingerID = -1;
|
|
|
|
|
|
if (isPinchZooming)
|
|
|
|
|
|
{
|
|
|
|
|
|
isPinchZooming = false;
|
|
|
|
|
|
pinchHandler?.OnPinchEnd();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isPinchZooming)
|
|
|
|
|
|
{
|
2024-07-17 18:42:19 +08:00
|
|
|
|
if (eventData.pointerId == firstEnterFingerID)
|
2024-06-20 14:58:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
firstFingerPosition = eventData.position;
|
|
|
|
|
|
}
|
2024-07-17 18:42:19 +08:00
|
|
|
|
else if (eventData.pointerId == lastEnterFingerID)
|
2024-06-20 14:58:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
lastFingerPosition = eventData.position;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float nowDistance = Vector2.Distance(firstFingerPosition, lastFingerPosition);
|
2024-07-17 20:15:22 +08:00
|
|
|
|
nowDistance *= scaleFactor;
|
|
|
|
|
|
if (distance == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
distance = nowDistance;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
zoomGapDelta = nowDistance / distance;
|
|
|
|
|
|
//zoomGap = zoomGapDelta * zoomSpeed;
|
2024-06-20 14:58:48 +08:00
|
|
|
|
distance = nowDistance;
|
|
|
|
|
|
|
2024-07-17 20:15:22 +08:00
|
|
|
|
scale = Mathf.Clamp(scale * zoomGapDelta, minZoom, maxZoom);//+ zoomGap * zoomGapDeltaSpeed
|
2024-06-20 14:58:48 +08:00
|
|
|
|
|
|
|
|
|
|
pinchHandler?.OnPinchZoom(scale);
|
|
|
|
|
|
}
|
2024-07-17 18:42:19 +08:00
|
|
|
|
else
|
|
|
|
|
|
OnDraging?.Invoke(eventData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnDragStarted?.Invoke(eventData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnDragCompleted?.Invoke(eventData);
|
2024-06-20 14:58:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|