// Copyright (c) Renewed Vision, LLC. All rights reserved.
#ifndef TextAnimationHelper_h
#define TextAnimationHelper_h

// Show an outline around each text segment for debugging
#define SHOW_SEGMENT_OUTLINEno

#ifdef SHOW_SEGMENT_OUTLINE
#ifdef __METAL__
#define TEX_SIZE(size, tex) size = float2(tex.get_width(), tex.get_height())
#else
#define TEX_SIZE(size, tex)                     \
    {                                           \
        uint width, height;                     \
        inputTex0.GetDimensions(width, height); \
        size = float2(width, height);           \
    }
#endif
#define showSegmentOutline(color)                                                           \
    {                                                                                       \
        float2 texSize;                                                                     \
        TEX_SIZE(texSize, inputTex0);                                                       \
        float2 pixelSize = float2(1.01, 1.01) / texSize;                                    \
        float2 p = input.tex1;                                                              \
        if (p.x <= (u.srcBounds.x + pixelSize.x) || p.y <= (u.srcBounds.y + pixelSize.y) || \
            p.x >= (u.srcBounds.z - pixelSize.x) || p.y >= (u.srcBounds.w - pixelSize.y))   \
            color = float4(1, 0, 0, 1);                                                     \
    }
#endif

float expRamp(float t, float ramp)
{
    return min(1.0f, 1.0f - exp(-t / ramp));
}

float4 handleContinuedMotion(float4 tmpPos, float localTime, float motionRamp, float motionSpeed)
{
#ifdef MOTION_GROW
    tmpPos.xy *= (1.0 + expRamp(localTime, motionRamp) * motionSpeed);
#endif

#ifdef MOTION_SHRINK
    tmpPos.xy *= (1.0 - expRamp(localTime, motionRamp) * motionSpeed);
#endif

#ifdef MOTION_UP
    tmpPos.y -= expRamp(localTime, motionRamp) * motionSpeed;
#endif

#ifdef MOTION_DOWN
    tmpPos.y += expRamp(localTime, motionRamp) * motionSpeed;
#endif

    return tmpPos;
}

float4 getBlur(
    float blurLevel, float4 blur0, float4 blur1, float4 blur2, float4 blur3, float4 blur4, float4 blur5, float blurBias)
{
    float t = saturate(blurLevel);
    t = pow(t, blurBias);
    t *= 5.0;

    uint i = min(uint(t), 4u);  // < 4 prevents discontinuity between blurLevel 1.0 and 0.99
    float f = t - float(i);

    float4 a = select(blur4, select(blur3, select(blur2, select(blur1, blur0, i < 1), i < 2), i < 3), i < 4);

    float4 b = select(blur5, select(blur4, select(blur3, select(blur2, blur1, i < 1), i < 2), i < 3), i < 4);

    return mix(a, b, f);
}

#endif  // TextAnimationHelper_h
