using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
using System;
enum FINGER { FINGER_STATE_NUL, FINGER_STATE_TOUCH, FINGER_STATE_ADD }
public class CardMove : MonoBehaviour
{
private GameObject[] sprites;
private int halfSize;
Vector2 screenCenterPos;
public float startAngle = -90;
float deltaAngle = 20;
public string currentSelectName;
public Vector2 center;
public float Y_axis = 400;
public float X_axis = 100;
private int cardcount;
private float fingerActionSensitivity = Screen.width * 0.05f;
private float fingerBeginX;
private float fingerBeginY;
private float fingerCurrentX;
private float fingerCurrentY;
private float fingerSegmentX;
private float fingerSegmentY;
private FINGER fingerTouchState;
float angle;
public float mouseSpeed = 1;
public Action MoveEndAction;
void Start()
{
deltaAngle = Screen.width / 100f;
Y_axis = Screen.width / 2;
UpdateChild();
}
void InitSprites()
{
screenCenterPos = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
cardcount = transform.childCount;
halfSize = (cardcount - 1) / 2;
sprites = new GameObject[cardcount];
for (int i = 0; i < cardcount; i++)
{
sprites[i] = transform.GetChild(i).gameObject;
SetPosition(i, false);
SetDeeps(i);
}
}
void SetPosition(int index, bool userTweener = true)
{
angle = 0;
if (index < halfSize)
{
angle = startAngle - (halfSize - index) * deltaAngle;
sprites[index].GetComponent<RectTransform>().DOScale(1f, 0.3f);
}
else if (index > halfSize)
{
angle = startAngle + (index - halfSize) * deltaAngle;
sprites[index].GetComponent<RectTransform>().DOScale(1f, 0.3f);
}
else
{
angle = startAngle;
sprites[index].GetComponent<RectTransform>().DOScale(1.5f, 0.3f).OnComplete(MoveEnd);
}
float xpos = Y_axis * Mathf.Cos((angle / 180) * Mathf.PI) + center.x;
float ypos = X_axis * Mathf.Sin((angle / 180) * Mathf.PI) + center.y;
Vector2 pos = new Vector2(xpos, ypos);
if (!userTweener)
{
sprites[index].GetComponent<RectTransform>().DOMove(new Vector2(screenCenterPos.x + pos.x, screenCenterPos.y + pos.y), 0f);
}
else
sprites[index].GetComponent<RectTransform>().DOMove(new Vector2(screenCenterPos.x + pos.x, screenCenterPos.y + pos.y), 0.2f);
}
void MoveEnd()
{
currentSelectName = transform.GetChild(transform.childCount - 1).name;
if (MoveEndAction != null) MoveEndAction();
}
void SetDeeps(int index)
{
int deep = 0;
if (index < halfSize)
{
deep = index;
}
else if (deep > halfSize)
{
deep = sprites.Length - (index + 1);
}
else
{
deep = halfSize;
}
sprites[index].GetComponent<RectTransform>().SetSiblingIndex(deep);
}
public void LeftMove()
{
int length = sprites.Length;
GameObject temp = sprites[0];
for (int i = 0; i < length; i++)
{
if (i == length - 1)
sprites[i] = temp;
else
sprites[i] = sprites[i + 1];
}
for (int i = 0; i < length; i++)
{
if (i < length - 1) SetPosition(i);
else SetPosition(i, false);
SetDeeps(i);
}
}
public void RightMove()
{
int length = sprites.Length;
GameObject temp = sprites[length - 1];
for (int i = length - 1; i >= 0; i--)
{
if (i == 0)
sprites[i] = temp;
else
sprites[i] = sprites[i - 1];
}
for (int i = 0; i < length; i++)
{
if (i == 0) SetPosition(i, false);
else SetPosition(i);
SetDeeps(i);
}
}
private void Update()
{
CheckMouse();
}
private void AddFingerAction()
{
fingerTouchState = FINGER.FINGER_STATE_ADD;
if (Mathf.Abs(fingerSegmentX) > Mathf.Abs(fingerSegmentY))
{
fingerSegmentY = 0;
}
else
{
fingerSegmentX = 0;
}
if (fingerSegmentY == 0)
{
if (fingerSegmentX > 0)
{
RightMove();
}
else
{
LeftMove();
}
}
}
readonly string MYMouseScrollWheel = "Mouse ScrollWheel";
float timer, axis;
void CheckMouse()
{
switch (fingerTouchState)
{
case FINGER.FINGER_STATE_NUL:
if (Input.GetKeyDown(KeyCode.Mouse0))
{
fingerTouchState = FINGER.FINGER_STATE_TOUCH;
fingerBeginX = Input.mousePosition.x;
fingerBeginY = Input.mousePosition.y;
}
break;
case FINGER.FINGER_STATE_TOUCH:
fingerCurrentX = Input.mousePosition.x;
fingerCurrentY = Input.mousePosition.y;
fingerSegmentX = fingerCurrentX - fingerBeginX;
fingerSegmentY = fingerCurrentY - fingerBeginY;
float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;
if (fingerDistance > (fingerActionSensitivity * fingerActionSensitivity))
{
AddFingerAction();
}
break;
case FINGER.FINGER_STATE_ADD:
break;
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
fingerTouchState = FINGER.FINGER_STATE_NUL;
}
timer += Time.deltaTime;
axis = Input.GetAxis(MYMouseScrollWheel);
if (axis > 0 && timer > 0.2f)
{
timer = 0;
LeftMove();
}
else if (axis < 0 && timer > 0.2f)
{
timer = 0;
RightMove();
}
}
void InitData()
{
fingerActionSensitivity = Screen.width * 0.05f;
fingerBeginX = 0;
fingerBeginY = 0;
fingerCurrentX = 0;
fingerCurrentY = 0;
fingerSegmentX = 0;
fingerSegmentY = 0;
}
public void UpdateChild()
{
InitSprites();
InitData();
}
}