/// <summary> /// On Drag End /// </summary> public void OnDragEnhanceViewEnd() { // find closed item to be centered int closestIndex = 0; float value = (curHorizontalValue - (int)curHorizontalValue); float min = float.MaxValue; float tmp = 0.5f * (curHorizontalValue < 0 ? -1 : 1); for (int i = 0; i < listCoverFlowItems.Count; i++) { float dis = Mathf.Abs(Mathf.Abs(value) - Mathf.Abs((tmp - listCoverFlowItems[i].CenterOffSet))); if (dis < min) { closestIndex = i; min = dis; } } originHorizontalValue = curHorizontalValue; float target = ((int)curHorizontalValue + (tmp - listCoverFlowItems[closestIndex].CenterOffSet)); preCenterItem = curCenterItem; curCenterItem = listCoverFlowItems[closestIndex]; LerpTweenToTarget(originHorizontalValue, target, true); canChangeItem = false; }
/// <summary> /// /// </summary> /// <param name="preCenterItem"></param> /// <param name="newCenterItem"></param> /// <returns></returns> private int GetMoveCurveFactorCount1(CoverFlowItem preCenterItem, CoverFlowItem newCenterItem) { SortCoverFlowItems1(); int factorCount = Mathf.Abs(newCenterItem.RealIndex) - Mathf.Abs(preCenterItem.RealIndex); return(Mathf.Abs(factorCount)); }
/// <summary> /// 重置Depth /// </summary> /// <param name="item"></param> /// <param name="timeValue"></param> void ResetCoverFlowItemDepth1(CoverFlowItem item, float timeValue) { float depthCurveValue = depthCurve.Evaluate(timeValue + item.CenterOffSet); int newDepth = (int)(depthCurveValue * listCoverFlowItems.Count); item.transform.SetSiblingIndex(newDepth); }
/// <summary> /// /// </summary> /// <param name="item"></param> /// <param name="timeValue"></param> void ResetCoverFlowItemPosition1(CoverFlowItem item, float timeValue) { float xValue = GetXPosValue1(timeValue, item.CenterOffSet); float yValue = GetYPosValue1(timeValue, item.CenterOffSet); item.transform.localPosition = new Vector3(xValue, yValue, 0); }
/// <summary> /// 设置水平方向跟随item /// </summary> /// <param name="selectItem"></param> public void SetHorizontalTargetItemIndex(CoverFlowItem selectItem) { if (!canChangeItem) { return; } if (curCenterItem == selectItem) { return; } canChangeItem = false; preCenterItem = curCenterItem; curCenterItem = selectItem; // calculate the direction of moving float centerXValue = positionCurve.Evaluate(0.5f) * totalHorizontalWidth; bool isRight = false; if (selectItem.transform.localPosition.x > centerXValue) { isRight = true; } // calculate the offset * dFactor int moveIndexCount = GetMoveCurveFactorCount1(preCenterItem, selectItem); float dvalue = 0.0f; if (isRight) { dvalue = -dFactor * moveIndexCount; } else { dvalue = dFactor * moveIndexCount; } float originValue = curHorizontalValue; LerpTweenToTarget(originValue, curHorizontalValue + dvalue, true); }
void Start() { canChangeItem = true; int count = listCoverFlowItems.Count; dFactor = (Mathf.RoundToInt((1f / count) * 10000f)) * 0.0001f; int mCenterIndex = count / 2; if (count % 2 == 0) { mCenterIndex = count / 2 - 1; } int index = 0; for (int i = count - 1; i >= 0; i--) { listCoverFlowItems[i].CurveOffSetIndex = i; listCoverFlowItems[i].CenterOffSet = dFactor * (mCenterIndex - index); listCoverFlowItems[i].SetSelectState(false); listCoverFlowItems[i].SetCoverFlow(this); GameObject obj = listCoverFlowItems[i].gameObject; index++; } // set the center item with startCenterIndex if (startCenterIndex < 0 || startCenterIndex >= count) { Debug.LogError("## startCenterIndex < 0 || startCenterIndex >= listEnhanceItems.Count out of index ##"); startCenterIndex = mCenterIndex; } // sorted items listSortedItems = new List <CoverFlowItem>(listCoverFlowItems.ToArray()); totalHorizontalWidth = cellWidth * count; curCenterItem = listCoverFlowItems[startCenterIndex]; curHorizontalValue = 0.5f - curCenterItem.CenterOffSet; LerpTweenToTarget(0f, curHorizontalValue, false); m_IsStarted = true; }
/// <summary> /// sort item with X so we can know how much distance we need to move the timeLine(curve time line) /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static int SortPosition(CoverFlowItem a, CoverFlowItem b) { return(a.transform.localPosition.x.CompareTo(b.transform.localPosition.x)); }
/// <summary> /// 重置Rotation /// </summary> /// <param name="item"></param> /// <param name="timeValue"></param> void ResetCoverFlowRotate1(CoverFlowItem item, float timeValue) { item.transform.localEulerAngles = Vector3.forward * RotateFactor * rotateZCurve.Evaluate(timeValue + item.CenterOffSet); }
/// <summary> /// 重置Scale /// </summary> /// <param name="item"></param> /// <param name="timeValue"></param> void ResetCoverFlowItemScale1(CoverFlowItem item, float timeValue) { item.transform.localScale = Vector3.one * scaleCurve.Evaluate(timeValue + item.CenterOffSet); }