Skip to content

Commit

Permalink
Add replace shortcut to convert built-in Scroll Rect to Pinchable one
Browse files Browse the repository at this point in the history
  • Loading branch information
LokoSoloGames committed May 29, 2022
1 parent dfab393 commit 0e59895
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Documentations.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 0 additions & 8 deletions Editor.meta

This file was deleted.

16 changes: 0 additions & 16 deletions Editor/LokoSolo.PinchableScrollRect.Editor.asmdef

This file was deleted.

7 changes: 0 additions & 7 deletions Editor/LokoSolo.PinchableScrollRect.Editor.asmdef.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Editor/PinchableScrollRectEditor.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Editor/PinchableScrollRectEditor.cs.meta

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Pinchable ScrollRect allows users to zoom in and out on the ScrollRect with both

## Getting Started
* Use it same as the way you use the Unity ScrollRect component.
* Add a UI/ScrollView from the GameObject Menu
* Select the ScrollRect added, click on the context dropdown menu
* Select "Replace as Pinchable"

## Important Remarks
* PinchInputDetector component must have a higher execution order than PinchableScrollRect component or any IPinchHandler component in order to consume the original OnDrag pointer event beforehand.
Expand Down
3 changes: 3 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

## 入門
* 使用方式與Unity 滾動矩形(ScrollRect) 的方式相同。
* 從 GameObject 選單中創建 UI/ScrollView
* 選擇新創建的的 ScrollRect,點擊其右上選單
* 點選 "Replace as Pinchable"

## 備注
* PinchInputDetector 組件必須具有比 PinchableScrollRect 組件或任何 IPinchHandler 組件更高的執行順序,以先消耗OnDrag 指針事件。
Expand Down
20 changes: 10 additions & 10 deletions Runtime/PinchInputDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
[DisallowMultipleComponent]
public class PinchInputDetector : UIBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {
/* Calling Order: OnBeginDrag -> OnDrag -> OnEndDrag */

private IPinchStartHandler[] pinchStartHandlers;
private IPinchEndHandler[] pinchEndHandlers;
private IPinchZoomHandler[] pinchZoomHandlers;

private int touchCount = 0;
private bool pinching = false;

private PointerEventData firstPointer;
private PointerEventData secondPointer;
private float previousDistance = 0f;
private float delta = 0f;

protected override void Awake() {
base.Awake();
pinchStartHandlers = GetComponents<IPinchStartHandler>();
Expand Down Expand Up @@ -43,7 +43,7 @@ protected virtual void RegisterPointer(PointerEventData eventData) {
// Similar behaviour as OnPointerUp but we are using OnEndDrag to avoid compication
protected virtual void UnregisterPointer(PointerEventData eventData) {
touchCount--;
if(touchCount < 0) Debug.LogError("PinchInputDetector - Touch Count Mismatch");
if (touchCount < 0) Debug.LogError("PinchInputDetector - Touch Count Mismatch");
if (IsEqualPointer(firstPointer, eventData)) {
// first touch removed
if (pinching) {
Expand All @@ -67,7 +67,7 @@ protected virtual void UnregisterPointer(PointerEventData eventData) {
secondPointer = null;
}
}

public virtual void OnBeginDrag(PointerEventData eventData) {
RegisterPointer(eventData);
if (touchCount == 1) return; // default behaviour
Expand All @@ -94,15 +94,15 @@ public virtual void OnDrag(PointerEventData eventData) {
if (IsEqualPointer(firstPointer, eventData)) {
// first touch dragging
firstPointer = eventData;
if(secondPointer != null) CalculateDistanceDelta();
if (secondPointer != null) CalculateDistanceDelta();
if (pinching) {
eventData.Use();
FireOnPinchZoom(new PinchEventData(firstPointer, secondPointer, delta));
}
} else if (IsEqualPointer(secondPointer, eventData)) {
// second touch dragging
secondPointer = eventData;
if(firstPointer != null) CalculateDistanceDelta();
if (firstPointer != null) CalculateDistanceDelta();
if (pinching) {
eventData.Use();
FireOnPinchZoom(new PinchEventData(secondPointer, firstPointer, delta));
Expand All @@ -118,7 +118,7 @@ private bool IsEqualPointer(PointerEventData a, PointerEventData b) {
if (b == null) return false;
return a.pointerId == b.pointerId;
}

protected virtual void CalculateDistanceDelta() {
float newDistance = Vector2.Distance(firstPointer.position, secondPointer.position);
delta = newDistance - previousDistance;
Expand All @@ -131,14 +131,14 @@ protected virtual void FireOnPinchStart(PinchEventData data) {
pinchStartHandlers[i].OnPinchStart(data);
}
}

protected virtual void FireOnPinchEnd(PinchEventData data) {
if (pinchEndHandlers == null) return;
for (int i = 0; i < pinchEndHandlers.Length; i++) {
pinchEndHandlers[i].OnPinchEnd(data);
}
}

protected virtual void FireOnPinchZoom(PinchEventData data) {
if (pinchZoomHandlers == null) return;
for (int i = 0; i < pinchZoomHandlers.Length; i++) {
Expand Down
Loading

0 comments on commit 0e59895

Please sign in to comment.