public void OnEndDrag(PointerEventData eventData) { // find closed item to be centered int focusIndex = 0; float value = (curScrollValue - (int)curScrollValue); float min = float.MaxValue; float tmp = 0.5f * (curScrollValue < 0 ? -1 : 1); for (int i = 0; i < itemList.Count; i++) { float dis = Mathf.Abs(Mathf.Abs(value) - Mathf.Abs((tmp - itemList[i].CenterOffSet))); if (dis < min) { focusIndex = i; min = dis; } } originScrollValue = curScrollValue; float target = ((int)curScrollValue + (tmp - itemList[focusIndex].CenterOffSet)); preCenterItem = curCenterItem; curCenterItem = itemList[focusIndex]; canChangeItem = false; LerpTweenToTarget(originScrollValue, target, false); }
public void FocusOnTargetItem(UIEnhanceItem selectItem) { if (!canChangeItem) { return; } if (curCenterItem == selectItem) { return; } preCenterItem = curCenterItem; curCenterItem = selectItem; // calculate the direction of moving float centerXValue = positionCurve.Evaluate(0.5f) * TotalWidth; bool isRight = selectItem.transform.localPosition.x > centerXValue; // calculate the offset * dFactor int moveIndexCount = GetMoveCurveFactorCount(preCenterItem, selectItem); float deltaValue = (isRight? -1:1) * moveOffsetPerItem * moveIndexCount; canChangeItem = false; LerpTweenToTarget(curScrollValue, curScrollValue + deltaValue, false); }
protected int GetMoveCurveFactorCount(UIEnhanceItem preCenterItem, UIEnhanceItem newCenterItem) { SortEnhanceItem(); int factorCount = Mathf.Abs(newCenterItem.RealIndex) - Mathf.Abs(preCenterItem.RealIndex); return(Mathf.Abs(factorCount)); }
// Update EnhanceItem state with curve fTime value protected virtual void UpdateEnhanceScrollView(float value) { for (int i = 0; i < itemList.Count; i++) { UIEnhanceItem item = itemList[i]; float posX = GetPosXValue(value, item.CenterOffSet); float scaleValue = GetScaleValue(value, item.CenterOffSet); int depth = (int)(depthCurve.Evaluate(value + item.CenterOffSet) * itemList.Count); item.Refresh(posX, scaleValue, depth); } }
protected virtual void Awake() { ItemCount = itemList.Count; moveOffsetPerItem = (Mathf.RoundToInt((1f / ItemCount) * 10000f)) * 0.0001f; if (ItemCount % 2 == 0) { mCenterIndex = ItemCount / 2 - 1; } else { mCenterIndex = ItemCount / 2; } int index = 0; for (int i = ItemCount - 1; i >= 0; i--) { UIEnhanceItem item = itemList[i]; item.CurveOffSetIndex = i; item.CenterOffSet = moveOffsetPerItem * (mCenterIndex - index); item.SetSelectState(false); index++; } // set the center item with startCenterIndex if (startCenterIndex < 0 || startCenterIndex >= ItemCount) { startCenterIndex = mCenterIndex; } TotalWidth = ItemSize.x * ItemCount; // sorted items sortedItemList = new List <UIEnhanceItem>(itemList); curCenterItem = itemList[startCenterIndex]; curScrollValue = 0.5f - curCenterItem.CenterOffSet; LerpTweenToTarget(0f, curScrollValue, true); canChangeItem = true; }
// sort item with X so we can know how much distance we need to move the timeLine(curve time line) public static int SortPosition(UIEnhanceItem a, UIEnhanceItem b) { return(a.transform.localPosition.x.CompareTo(b.transform.localPosition.x)); }