Skip to content

Commit 7dee85a

Browse files
committed
feat(gesture): smoother gesture trail fade, taper and curves
1 parent a1766d9 commit 7dee85a

3 files changed

Lines changed: 81 additions & 9 deletions

File tree

‎app/src/main/java/helium314/keyboard/keyboard/internal/GestureTrailDrawingPoints.kt‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.graphics.Rect
1313
import android.os.SystemClock
1414
import helium314.keyboard.latin.common.ResizableIntArray
1515
import kotlin.math.max
16+
import kotlin.math.roundToInt
1617

1718
class GestureTrailDrawingPoints {
1819
private val mXCoordinates = ResizableIntArray(DEFAULT_CAPACITY)
@@ -196,14 +197,28 @@ class GestureTrailDrawingPoints {
196197

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

204-
private fun getWidth(elapsedTime: Int, params: GestureTrailDrawingParams): Float {
205-
val deltaWidth = params.mTrailStartWidth - params.mTrailEndWidth
206-
return params.mTrailStartWidth - (deltaWidth * elapsedTime) / params.mTrailLingerDuration
204+
private fun getWidth(elapsedTime: Int, params: GestureTrailDrawingParams): Float =
205+
trailWidth(params.mTrailStartWidth, params.mTrailEndWidth, elapsedTime, params.mTrailLingerDuration)
206+
207+
/** Fraction of [duration] that has passed after [elapsed] ms, between 0 and 1. A duration of 0 or less counts as done. */
208+
// internal for testing
209+
internal fun progress(elapsed: Int, duration: Int): Float =
210+
if (elapsed <= 0) 0f else if (duration <= 0 || elapsed >= duration) 1f else elapsed.toFloat() / duration
211+
212+
/** Eases in and out (zero slope at both ends), so a fade starts and ends softly instead of with a kink. */
213+
// internal for testing
214+
internal fun smoothStep(t: Float): Float {
215+
val x = t.coerceIn(0f, 1f)
216+
return x * x * (3f - 2f * x)
207217
}
218+
219+
/** The head keeps its full width for a moment and the tail thins out gently, instead of a straight taper. */
220+
// internal for testing
221+
internal fun trailWidth(startWidth: Float, endWidth: Float, elapsed: Int, lingerDuration: Int): Float =
222+
startWidth - (startWidth - endWidth) * smoothStep(progress(elapsed, lingerDuration))
208223
}
209224
}

‎app/src/main/res/values/config-common.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@
8282
<!-- Minimum distance between gesture trail sampling points. -->
8383
<dimen name="config_gesture_trail_min_sampling_distance">9.6dp</dimen>
8484
<!-- Maximum angular threshold between gesture trails interpolation segments in degree. -->
85-
<integer name="config_gesture_trail_max_interpolation_angular_threshold">15</integer>
85+
<integer name="config_gesture_trail_max_interpolation_angular_threshold">10</integer>
8686
<!-- Maximum distance threshold between gesture trails interpolation segments. -->
87-
<dimen name="config_gesture_trail_max_interpolation_distance_threshold">16.0dp</dimen>
87+
<dimen name="config_gesture_trail_max_interpolation_distance_threshold">12.0dp</dimen>
8888
<!-- Maximum number of gesture trail interpolation segments. -->
89-
<integer name="config_gesture_trail_max_interpolation_segments">6</integer>
89+
<integer name="config_gesture_trail_max_interpolation_segments">8</integer>
9090
<dimen name="config_gesture_trail_start_width">10.0dp</dimen>
9191
<dimen name="config_gesture_trail_end_width">2.5dp</dimen>
9292
<!-- Percentages of gesture preview taril body and shadow, in proportion to the trail width.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
package helium314.keyboard.keyboard.internal
3+
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import kotlin.test.assertTrue
7+
8+
class GestureTrailCurveTest {
9+
private val progress = GestureTrailDrawingPoints.Companion::progress
10+
private val smooth = GestureTrailDrawingPoints.Companion::smoothStep
11+
private val width = GestureTrailDrawingPoints.Companion::trailWidth
12+
13+
@Test fun `progress is clamped and survives bad durations`() {
14+
assertEquals(0f, progress(-50, 200))
15+
assertEquals(0f, progress(0, 200))
16+
assertEquals(0.5f, progress(100, 200))
17+
assertEquals(1f, progress(500, 200))
18+
assertEquals(1f, progress(10, 0)) // a lifespan of 0 must not divide by zero
19+
assertEquals(1f, progress(10, -5))
20+
assertEquals(0f, progress(0, 0))
21+
}
22+
23+
@Test fun `smooth step has the right ends, is monotonic and clamps`() {
24+
assertEquals(0f, smooth(0f))
25+
assertEquals(1f, smooth(1f))
26+
assertEquals(0.5f, smooth(0.5f), 1e-6f)
27+
assertEquals(0f, smooth(-3f))
28+
assertEquals(1f, smooth(7f))
29+
var last = -1f
30+
for (i in 0..100) {
31+
val v = smooth(i / 100f)
32+
assertTrue(v >= last)
33+
last = v
34+
}
35+
}
36+
37+
@Test fun `fade starts and ends softly`() {
38+
// almost no change right after the start and right before the end, unlike a straight line
39+
assertTrue(smooth(0.05f) < 0.05f)
40+
assertTrue(1f - smooth(0.95f) < 0.05f)
41+
}
42+
43+
@Test fun `trail width goes from start to end width and never leaves that range`() {
44+
assertEquals(10f, width(10f, 2.5f, 0, 300))
45+
assertEquals(2.5f, width(10f, 2.5f, 300, 300))
46+
assertEquals(2.5f, width(10f, 2.5f, 9999, 300))
47+
assertEquals(10f, width(10f, 2.5f, -20, 300))
48+
assertEquals(6.25f, width(10f, 2.5f, 150, 300), 1e-4f)
49+
for (t in 0..300 step 10) assertTrue(width(10f, 2.5f, t, 300) in 2.5f..10f)
50+
assertEquals(2.5f, width(10f, 2.5f, 5, 0)) // zero linger duration: treat as fully aged
51+
}
52+
53+
@Test fun `head stays wider than a straight taper early on`() {
54+
val straight = 10f - 7.5f * (60f / 300f)
55+
assertTrue(width(10f, 2.5f, 60, 300) > straight)
56+
}
57+
}

0 commit comments

Comments
 (0)