|
@@ -0,0 +1,130 @@
|
|
|
+using FairyGUI;
|
|
|
+using GFGGame.Launcher;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+namespace GFGGame
|
|
|
+{
|
|
|
+ public class ScaleGestureController : SingletonMonoBase<ScaleGestureController>
|
|
|
+ {
|
|
|
+ public Transform target; //目标
|
|
|
+
|
|
|
+ //缩放
|
|
|
+ public bool isScaling;
|
|
|
+ Vector3 touch1, touch2, oriPos, scaleCenter;
|
|
|
+ float scale, disX, disY, oriScale;
|
|
|
+ public float scaleSpeed = 1;
|
|
|
+ public float min = 1f;
|
|
|
+ public float max = 1.7f;
|
|
|
+
|
|
|
+
|
|
|
+ //移动
|
|
|
+ private bool isDraging;
|
|
|
+ private Vector3 offset = Vector3.zero;
|
|
|
+ public Vector2 uiSize;
|
|
|
+
|
|
|
+ private void Update()
|
|
|
+ {
|
|
|
+ if (target == null)
|
|
|
+ {
|
|
|
+ isScaling = false;
|
|
|
+ isDraging = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(Input.touchCount != 2)
|
|
|
+ {
|
|
|
+ isScaling = false;
|
|
|
+ }
|
|
|
+ if (isScaling)
|
|
|
+ {
|
|
|
+ //两指缩放比例
|
|
|
+ scale = Vector3.Distance(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position)) / Vector3.Distance(touch1, touch2);
|
|
|
+
|
|
|
+ //利用scaleSpeed控制缩放速度
|
|
|
+ scale = (scale - 1) * scaleSpeed;
|
|
|
+ var targetScale = oriScale + scale;
|
|
|
+ //给缩放比例加限制
|
|
|
+ if (targetScale <= min && scale < 0)
|
|
|
+ {
|
|
|
+ targetScale = min;
|
|
|
+ }
|
|
|
+ if (targetScale >= max && scale > 0)
|
|
|
+ {
|
|
|
+ targetScale = max;
|
|
|
+ }
|
|
|
+
|
|
|
+ LogUtil.LogDev($"scaling {oriScale} {scale}");
|
|
|
+ //缩放目标大小
|
|
|
+ target.localScale = new Vector3(targetScale, targetScale, 1);
|
|
|
+ //改变目标位置,让位置保持不变
|
|
|
+ target.position = new Vector3(oriPos.x - ((target.localScale.x - oriScale) * disX), oriPos.y - ((target.localScale.y - oriScale) * disY), 0);
|
|
|
+ UpdatePos();
|
|
|
+ }
|
|
|
+ if (Input.touchCount == 2)
|
|
|
+ {
|
|
|
+ //两指点位
|
|
|
+ touch1 = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
|
|
|
+ touch2 = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
|
|
|
+
|
|
|
+ //目标初始点位
|
|
|
+ oriPos = new Vector3(target.position.x, target.position.y, 0);
|
|
|
+
|
|
|
+ //两指中点
|
|
|
+ scaleCenter = new Vector3((Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x + Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position).x) / 2, (Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y + Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position).y) / 2, 0);
|
|
|
+
|
|
|
+ //两指中点和目标距离
|
|
|
+ disX = scaleCenter.x - oriPos.x;
|
|
|
+ disY = scaleCenter.y - oriPos.y;
|
|
|
+ oriScale = target.localScale.x;
|
|
|
+ LogUtil.LogDev($"start scale {oriPos} {oriScale}");
|
|
|
+
|
|
|
+
|
|
|
+ isScaling = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!Input.GetMouseButton(0) || isScaling)
|
|
|
+ {
|
|
|
+ isDraging = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (isDraging)
|
|
|
+ {
|
|
|
+ target.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - offset);
|
|
|
+ UpdatePos();
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButtonDown(0))
|
|
|
+ {
|
|
|
+ //LogUtil.LogEditor($"DragGestureController {GRoot.inst.touchTarget} {Input.touchCount}");
|
|
|
+ if (GRoot.inst.touchTarget != null && !(GRoot.inst.touchTarget is GGraph)) return;
|
|
|
+ isDraging = true;
|
|
|
+ offset = Input.mousePosition - Camera.main.WorldToScreenPoint(target.position);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void UpdatePos()
|
|
|
+ {
|
|
|
+ if (target == null) return;
|
|
|
+ Vector2 size = target.Find("Bg").GetComponent<SpriteRenderer>().size * target.localScale.x;
|
|
|
+
|
|
|
+ float deviationWidth = (size.x - uiSize.x / 100) / 2;
|
|
|
+ float deviationHeigh = (size.y - uiSize.y / 100) / 2;
|
|
|
+ Vector2 pos = target.transform.position;
|
|
|
+ if (pos.x <= -deviationWidth)
|
|
|
+ {
|
|
|
+ target.transform.position = new Vector2(-deviationWidth, target.transform.position.y);
|
|
|
+ }
|
|
|
+ if (pos.x >= deviationWidth)
|
|
|
+ {
|
|
|
+ target.transform.position = new Vector2(deviationWidth, target.transform.position.y);
|
|
|
+ }
|
|
|
+ if (pos.y <= -deviationHeigh)
|
|
|
+ {
|
|
|
+ target.transform.position = new Vector2(target.transform.position.x, -deviationHeigh);
|
|
|
+ }
|
|
|
+ if (pos.y >= deviationHeigh)
|
|
|
+ {
|
|
|
+ target.transform.position = new Vector2(target.transform.position.x, deviationHeigh);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|