Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 416e8cc4 authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

Fix Squiggly line inconsistency.

The squiggly line was skipping around a bit. This was previously
hidden by the squiggly ball. I took the opportunity to reverse
the line direction so it doesn't move relative to the player
position.

Fixes: 246776610
Test: Manually checked squiggle on device
Change-Id: If9e8bd5bf68214c0d56b4995796c39291512733c
parent 60e003dd
Loading
Loading
Loading
Loading
+47 −48
Original line number Original line Diff line number Diff line
@@ -60,6 +60,8 @@ class SquigglyProgress : Drawable() {
            linePaint.strokeWidth = value
            linePaint.strokeWidth = value
        }
        }


    // Enables a transition region where the amplitude
    // of the wave is reduced linearly across it.
    var transitionEnabled = true
    var transitionEnabled = true
        set(value) {
        set(value) {
            field = value
            field = value
@@ -116,44 +118,40 @@ class SquigglyProgress : Drawable() {
        }
        }


        val progress = level / 10_000f
        val progress = level / 10_000f
        val totalProgressPx = bounds.width() * progress
        val totalWidth = bounds.width().toFloat()
        val waveProgressPx = bounds.width() * (
        val totalProgressPx = totalWidth * progress
        val waveProgressPx = totalWidth * (
            if (!transitionEnabled || progress > matchedWaveEndpoint) progress else
            if (!transitionEnabled || progress > matchedWaveEndpoint) progress else
            lerp(minWaveEndpoint, matchedWaveEndpoint, lerpInv(0f, matchedWaveEndpoint, progress)))
            lerp(minWaveEndpoint, matchedWaveEndpoint, lerpInv(0f, matchedWaveEndpoint, progress)))


        // Build Wiggly Path
        // Build Wiggly Path
        val waveStart = -phaseOffset
        val waveStart = -phaseOffset - waveLength / 2f
        val waveEnd = waveProgressPx
        val waveEnd = if (transitionEnabled) totalWidth else waveProgressPx
        val transitionLength = if (transitionEnabled) transitionPeriods * waveLength else 0.01f


        // helper function, computes amplitude for wave segment
        // helper function, computes amplitude for wave segment
        val computeAmplitude: (Float, Float) -> Float = { x, sign ->
        val computeAmplitude: (Float, Float) -> Float = { x, sign ->
            sign * heightFraction * lineAmplitude *
            if (transitionEnabled) {
                    lerpInvSat(waveEnd, waveEnd - transitionLength, x)
                val length = transitionPeriods * waveLength
                val coeff = lerpInvSat(
                    waveProgressPx + length / 2f,
                    waveProgressPx - length / 2f,
                    x)
                sign * heightFraction * lineAmplitude * coeff
            } else {
                sign * heightFraction * lineAmplitude
            }
        }
        }


        var currentX = waveEnd
        // Reset path object to the start
        var waveSign = if (phaseOffset < waveLength / 2) 1f else -1f
        path.rewind()
        path.rewind()
        path.moveTo(waveStart, 0f)


        // Draw flat line from end to wave endpoint
        // Build the wave, incrementing by half the wavelength each time
        path.moveTo(bounds.width().toFloat(), 0f)
        var currentX = waveStart
        path.lineTo(waveEnd, 0f)
        var waveSign = 1f

        // First wave has shortened wavelength
        // approx quarter wave gets us to first wave peak
        // shouldn't be big enough to notice it's not a sin wave
        currentX -= phaseOffset % (waveLength / 2)
        val controlRatio = 0.25f
        var currentAmp = computeAmplitude(currentX, waveSign)
        var currentAmp = computeAmplitude(currentX, waveSign)
        path.cubicTo(
        val dist = waveLength / 2f
            waveEnd, currentAmp * controlRatio,
        while (currentX < waveEnd) {
            lerp(currentX, waveEnd, controlRatio), currentAmp,
            currentX, currentAmp)

        // Other waves have full wavelength
        val dist = -1 * waveLength / 2f
        while (currentX > waveStart) {
            waveSign = -waveSign
            waveSign = -waveSign
            val nextX = currentX + dist
            val nextX = currentX + dist
            val midX = currentX + dist / 2
            val midX = currentX + dist / 2
@@ -166,34 +164,35 @@ class SquigglyProgress : Drawable() {
            currentX = nextX
            currentX = nextX
        }
        }


        // Draw path; clip to progress position
        // translate to the start position of the progress bar for all draw commands
        val clipTop = lineAmplitude + strokeWidth
        canvas.save()
        canvas.save()
        canvas.translate(bounds.left.toFloat(), bounds.centerY().toFloat())
        canvas.translate(bounds.left.toFloat(), bounds.centerY().toFloat())
        canvas.clipRect(

                0f,
        // Draw path up to progress position
                -lineAmplitude - strokeWidth,
        canvas.save()
                totalProgressPx,
        canvas.clipRect(0f, -1f * clipTop, totalProgressPx, clipTop)
                lineAmplitude + strokeWidth)
        canvas.drawPath(path, wavePaint)
        canvas.drawPath(path, wavePaint)
        canvas.restore()
        canvas.restore()


        // Draw path; clip between progression position & far edge
        if (transitionEnabled) {
            // If there's a smooth transition, we draw the rest of the
            // path in a different color (using different clip params)
            canvas.save()
            canvas.save()
        canvas.translate(bounds.left.toFloat(), bounds.centerY().toFloat())
            canvas.clipRect(totalProgressPx, -1f * clipTop, totalWidth, clipTop)
        canvas.clipRect(
                totalProgressPx,
                -lineAmplitude - strokeWidth,
                bounds.width().toFloat(),
                lineAmplitude + strokeWidth)
            canvas.drawPath(path, linePaint)
            canvas.drawPath(path, linePaint)
            canvas.restore()
            canvas.restore()
        } else {
            // No transition, just draw a flat line to the end of the region.
            // The discontinuity is hidden by the progress bar thumb shape.
            canvas.drawLine(totalProgressPx, 0f, totalWidth, 0f, linePaint)
        }


        // Draw round line cap at the beginning of the wave
        // Draw round line cap at the beginning of the wave
        val startAmp = cos(abs(waveEnd - phaseOffset) / waveLength * TWO_PI)
        val startAmp = cos(abs(waveStart) / waveLength * TWO_PI)
        canvas.drawPoint(
        canvas.drawPoint(0f, startAmp * lineAmplitude * heightFraction, wavePaint)
                bounds.left.toFloat(),

                bounds.centerY() + startAmp * lineAmplitude * heightFraction,
        canvas.restore()
                wavePaint)
    }
    }


    override fun getOpacity(): Int {
    override fun getOpacity(): Int {