Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.graphics.Rect
import android.os.SystemClock
import helium314.keyboard.latin.common.ResizableIntArray
import kotlin.math.max
import kotlin.math.roundToInt

class GestureTrailDrawingPoints {
private val mXCoordinates = ResizableIntArray(DEFAULT_CAPACITY)
Expand Down Expand Up @@ -196,14 +197,28 @@ class GestureTrailDrawingPoints {

private fun getAlpha(elapsedTime: Int, params: GestureTrailDrawingParams): Int {
val fullAlpha = Color.alpha(params.mTrailColor)
if (elapsedTime < params.mFadeoutStartDelay) return fullAlpha
val decreasingAlpha = fullAlpha * (elapsedTime - params.mFadeoutStartDelay) / params.mFadeoutDuration
return fullAlpha - decreasingAlpha
val visible = 1f - smoothStep(progress(elapsedTime - params.mFadeoutStartDelay, params.mFadeoutDuration))
return (fullAlpha * visible).roundToInt().coerceIn(0, fullAlpha)
}

private fun getWidth(elapsedTime: Int, params: GestureTrailDrawingParams): Float {
val deltaWidth = params.mTrailStartWidth - params.mTrailEndWidth
return params.mTrailStartWidth - (deltaWidth * elapsedTime) / params.mTrailLingerDuration
private fun getWidth(elapsedTime: Int, params: GestureTrailDrawingParams): Float =
trailWidth(params.mTrailStartWidth, params.mTrailEndWidth, elapsedTime, params.mTrailLingerDuration)

/** Fraction of [duration] that has passed after [elapsed] ms, between 0 and 1. A duration of 0 or less counts as done. */
// internal for testing
internal fun progress(elapsed: Int, duration: Int): Float =
if (elapsed <= 0) 0f else if (duration <= 0 || elapsed >= duration) 1f else elapsed.toFloat() / duration

/** Eases in and out (zero slope at both ends), so a fade starts and ends softly instead of with a kink. */
// internal for testing
internal fun smoothStep(t: Float): Float {
val x = t.coerceIn(0f, 1f)
return x * x * (3f - 2f * x)
}

/** The head keeps its full width for a moment and the tail thins out gently, instead of a straight taper. */
// internal for testing
internal fun trailWidth(startWidth: Float, endWidth: Float, elapsed: Int, lingerDuration: Int): Float =
startWidth - (startWidth - endWidth) * smoothStep(progress(elapsed, lingerDuration))
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/values/config-common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@
<!-- Minimum distance between gesture trail sampling points. -->
<dimen name="config_gesture_trail_min_sampling_distance">9.6dp</dimen>
<!-- Maximum angular threshold between gesture trails interpolation segments in degree. -->
<integer name="config_gesture_trail_max_interpolation_angular_threshold">15</integer>
<integer name="config_gesture_trail_max_interpolation_angular_threshold">10</integer>
<!-- Maximum distance threshold between gesture trails interpolation segments. -->
<dimen name="config_gesture_trail_max_interpolation_distance_threshold">16.0dp</dimen>
<dimen name="config_gesture_trail_max_interpolation_distance_threshold">12.0dp</dimen>
<!-- Maximum number of gesture trail interpolation segments. -->
<integer name="config_gesture_trail_max_interpolation_segments">6</integer>
<integer name="config_gesture_trail_max_interpolation_segments">8</integer>
<dimen name="config_gesture_trail_start_width">10.0dp</dimen>
<dimen name="config_gesture_trail_end_width">2.5dp</dimen>
<!-- Percentages of gesture preview taril body and shadow, in proportion to the trail width.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.keyboard.internal

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class GestureTrailCurveTest {
private val progress = GestureTrailDrawingPoints.Companion::progress
private val smooth = GestureTrailDrawingPoints.Companion::smoothStep
private val width = GestureTrailDrawingPoints.Companion::trailWidth

@Test fun `progress is clamped and survives bad durations`() {
assertEquals(0f, progress(-50, 200))
assertEquals(0f, progress(0, 200))
assertEquals(0.5f, progress(100, 200))
assertEquals(1f, progress(500, 200))
assertEquals(1f, progress(10, 0)) // a lifespan of 0 must not divide by zero
assertEquals(1f, progress(10, -5))
assertEquals(0f, progress(0, 0))
}

@Test fun `smooth step has the right ends, is monotonic and clamps`() {
assertEquals(0f, smooth(0f))
assertEquals(1f, smooth(1f))
assertEquals(0.5f, smooth(0.5f), 1e-6f)
assertEquals(0f, smooth(-3f))
assertEquals(1f, smooth(7f))
var last = -1f
for (i in 0..100) {
val v = smooth(i / 100f)
assertTrue(v >= last)
last = v
}
}

@Test fun `fade starts and ends softly`() {
// almost no change right after the start and right before the end, unlike a straight line
assertTrue(smooth(0.05f) < 0.05f)
assertTrue(1f - smooth(0.95f) < 0.05f)
}

@Test fun `trail width goes from start to end width and never leaves that range`() {
assertEquals(10f, width(10f, 2.5f, 0, 300))
assertEquals(2.5f, width(10f, 2.5f, 300, 300))
assertEquals(2.5f, width(10f, 2.5f, 9999, 300))
assertEquals(10f, width(10f, 2.5f, -20, 300))
assertEquals(6.25f, width(10f, 2.5f, 150, 300), 1e-4f)
for (t in 0..300 step 10) assertTrue(width(10f, 2.5f, t, 300) in 2.5f..10f)
assertEquals(2.5f, width(10f, 2.5f, 5, 0)) // zero linger duration: treat as fully aged
}

@Test fun `head stays wider than a straight taper early on`() {
val straight = 10f - 7.5f * (60f / 300f)
assertTrue(width(10f, 2.5f, 60, 300) > straight)
}
}
Loading