You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.2 KiB
124 lines
4.2 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.EventSystems; |
|
|
|
namespace AX.ImageViewer |
|
{ |
|
public class ImageViewerMovement : MonoBehaviour |
|
{ |
|
#region Variables |
|
[SerializeField] |
|
private Canvas canvas; |
|
private RectTransform canvasTR; |
|
private Camera canvasCam; |
|
|
|
[SerializeField] |
|
private RectTransform window; |
|
|
|
[SerializeField] |
|
private ImageViewer imageViewer; |
|
|
|
[SerializeField] |
|
private RectTransform ImagePanelRect; |
|
|
|
private Vector2 offset = new Vector3(); |
|
|
|
private Vector2 initialTouchPos = Vector2.zero; |
|
private Vector2 initialAnchoredPos, initialSizeDelta; |
|
#endregion |
|
|
|
#region Messages |
|
private void Awake() |
|
{ |
|
canvasTR = canvas.GetComponent<RectTransform>(); |
|
} |
|
#endregion |
|
|
|
#region Pointer Events |
|
public void OnDragStarted(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
|
|
canvasCam = pointer.pressEventCamera; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(window, pointer.pressPosition, canvasCam, out initialTouchPos); |
|
} |
|
|
|
public void OnDrag(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
|
|
Vector2 touchPos; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(window, pointer.position, canvasCam, out touchPos); |
|
window.anchoredPosition += touchPos - initialTouchPos; |
|
} |
|
|
|
public void OnResizeStarted(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
|
|
canvasCam = pointer.pressEventCamera; |
|
initialAnchoredPos = window.anchoredPosition; |
|
initialSizeDelta = window.sizeDelta; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTR, pointer.pressPosition, canvasCam, out initialTouchPos); |
|
} |
|
|
|
public void OnResize(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
|
|
Vector2 touchPos; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTR, pointer.position, canvasCam, out touchPos); |
|
|
|
Vector2 delta = touchPos - initialTouchPos; |
|
Vector2 newSize = initialSizeDelta + new Vector2(delta.x, -delta.y); |
|
|
|
if (newSize.x < imageViewer.minWidth) newSize.x = imageViewer.minWidth; |
|
if (newSize.y < imageViewer.minHeight) newSize.y = imageViewer.minHeight; |
|
|
|
newSize.x = (int)newSize.x; |
|
newSize.y = (int)newSize.y; |
|
|
|
delta = newSize - initialSizeDelta; |
|
|
|
window.anchoredPosition = initialAnchoredPos + new Vector2(delta.x * 0.5f, delta.y * -0.5f); |
|
window.sizeDelta = newSize; |
|
} |
|
|
|
//ImagePanelDrag |
|
public void OnImagePanelPointerDown(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
Vector2 mouseDown = pointer.position; |
|
Vector2 mouseUguiPos = new Vector2(); |
|
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTR, mouseDown, pointer.enterEventCamera, out mouseUguiPos); |
|
if(isRect) |
|
offset = ImagePanelRect.anchoredPosition - mouseUguiPos; |
|
} |
|
|
|
public void OnImagePanelDrag(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
Vector2 mouseDrag = pointer.position; |
|
Vector2 uguiPos = new Vector2(); |
|
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTR, mouseDrag, pointer.enterEventCamera, out uguiPos); |
|
if (isRect) |
|
ImagePanelRect.anchoredPosition = offset + uguiPos; |
|
} |
|
|
|
public void OnImagePanelPointerUp(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
offset = Vector2.zero; |
|
} |
|
|
|
public void OnImagePanelEndDrag(BaseEventData data) |
|
{ |
|
PointerEventData pointer = (PointerEventData)data; |
|
offset = Vector2.zero; |
|
} |
|
#endregion |
|
|
|
} |
|
} |
|
|
|
|