コード例 #1
0
    public void Update()
    {
        ld.Reset();

        int activeCount = 0;

        foreach (var op in optionsPool)
        {
            activeCount += op.isFadingOut ? 0 : 1;
        }

        singlePhraseBoxHeight.Portion(ld, activeCount > 0 ? 0 : 1);
        historyPool.active.Portion(ld);
        optionsPool.active.Portion(ld);

        bool lerpSeparator = false;

        if (Math.Abs(separatorPosition.TargetValue - separatorPosition.CurrentValue) > 0.00001f || poolsDirty)
        {
            lerpSeparator = true;
            poolsDirty    = false;
            separatorPosition.Portion(ld, Mathf.Min(0.6f, 0.3f + activeCount * 0.2f));
        }

        // LERP:

        singlePhraseBoxHeight.Lerp(ld);
        historyPool.active.Lerp(ld);
        optionsPool.active.Lerp(ld);

        if (lerpSeparator)
        {
            separatorPosition.Lerp(ld);
        }

        //ld.LerpAndReset();

        var   tf           = singlePhraseBg.rectTransform;
        var   size         = tf.sizeDelta;
        float curBoxFadeIn = singlePhraseBoxHeight.CurrentValue;

        size.y = curBoxFadeIn * 400;
        singlePhraseBg.TrySetAlpha_DisableGameObjectIfZero(curBoxFadeIn * 20);
        singlePhraseText.TrySetAlpha_DisableGameObjectIfZero((curBoxFadeIn - 0.9f) * 10);
        tf.sizeDelta = size;

        if (lerpSeparator)
        {
            UpdateCourners();
        }

        const float scrollbackSpeed = 1000;

        if (state == ScrollingState.None)
        {
            if (Input.GetMouseButton(0))
            {
                state = (Input.mousePosition.y / Screen.height) > separatorPosition.CurrentValue
                    ? ScrollingState.ScrollingHistory
                    : ScrollingState.ScrollingOptions;

                prevousMousePos = Input.mousePosition;

                scrollSoundPlayed = false;
            }
        }
        else
        {
            if (!Input.GetMouseButton(0))
            {
                state = ScrollingState.None;
            }
            else
            {
                float diff = Input.mousePosition.y - prevousMousePos.y;

                bool up = diff > 0;

                if ((!scrollSoundPlayed || (up != scrollSoundPlayedUp)) && Mathf.Abs(diff) > 10)
                {
                    scrollSoundPlayedUp = up;
                    scrollSoundPlayed   = true;
                }

                (state == ScrollingState.ScrollingHistory ? historyScroll : optionsScroll).AddOffset(diff);

                prevousMousePos = Input.mousePosition;
            }
        }

        if (scrollHistoryUpRequested && state != ScrollingState.None)
        {
            scrollHistoryUpRequested = false;
        }

        float pos = historyScroll.offset;

        foreach (var h in historyPool.active)
        {
            var rt   = h.rectTransform;
            var anch = rt.anchoredPosition;
            anch.y = pos;
            rt.anchoredPosition = anch;
            pos += historyScroll.gap;
        }

        if (state != ScrollingState.ScrollingHistory)
        {
            historyScroll.ApplyInnertia();

            bool outOfList = true;

            if (historyScroll.offset > 0 || scrollHistoryUpRequested)
            {
                LerpUtils.IsLerpingBySpeed(ref historyScroll.offset, 0, scrollbackSpeed);
            }
            else if (pos < historyScroll.gap)
            {
                LerpUtils.IsLerpingBySpeed(ref historyScroll.offset, historyScroll.offset + historyScroll.gap - pos, scrollbackSpeed);
            }
            else
            {
                outOfList = false;
            }

            if (outOfList)
            {
                historyScroll.FadeInnertia();
            }
        }

        pos = optionsScroll.offset;

        foreach (var h in optionsPool.active)
        {
            if (!h.isFadingOut)
            {
                var rt   = h.rectTransform;
                var anch = rt.anchoredPosition;
                anch.y = pos;
                rt.anchoredPosition = anch;
                pos -= optionsScroll.gap;
            }
        }

        if (state != ScrollingState.ScrollingOptions)
        {
            optionsScroll.ApplyInnertia();

            bool outOfTheList = true;

            if (optionsScroll.offset < 0)
            {
                LerpUtils.IsLerpingBySpeed(ref optionsScroll.offset, 0, scrollbackSpeed);
            }
            else if (pos > -optionsScroll.gap)
            {
                LerpUtils.IsLerpingBySpeed(ref optionsScroll.offset, optionsScroll.offset - optionsScroll.gap - pos, scrollbackSpeed);
            }
            else
            {
                outOfTheList = false;
            }

            if (outOfTheList)
            {
                optionsScroll.FadeInnertia();
            }
        }
    }