playback: start deslopping wavyslider
This commit is contained in:
@@ -18,29 +18,37 @@
|
|||||||
|
|
||||||
package com.google.android.material.slider
|
package com.google.android.material.slider
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import android.graphics.Matrix
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
|
import android.graphics.Path
|
||||||
|
import android.graphics.PathMeasure
|
||||||
|
import android.graphics.PointF
|
||||||
|
import android.graphics.RectF
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.annotation.Px
|
import androidx.annotation.Px
|
||||||
|
import androidx.core.content.res.getDimensionPixelSizeOrThrow
|
||||||
|
import androidx.core.content.withStyledAttributes
|
||||||
|
import androidx.core.graphics.withSave
|
||||||
|
import androidx.core.view.isVisible
|
||||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||||
import androidx.dynamicanimation.animation.SpringAnimation
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import com.google.android.material.wave.PathPoint
|
import com.google.android.material.motion.MotionUtils
|
||||||
import com.google.android.material.wave.RoundedBlockRenderer
|
|
||||||
import com.google.android.material.wave.WaveMotionUtils
|
|
||||||
import com.google.android.material.wave.WavePathCache
|
|
||||||
import com.google.android.material.wave.applyRampEasing
|
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
import kotlin.math.atan2
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
import org.oxycblt.auxio.R
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Slider with active-track wave rendering that ports MDC LinearProgressIndicator's wavy draw
|
* Slider with active-track wave rendering that ports MDC LinearProgressIndicator's wavy draw
|
||||||
* behavior and keeps phase/amplitude transition behavior aligned with
|
* behavior.
|
||||||
*/
|
*/
|
||||||
class WavySlider
|
class WavySlider
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
@@ -55,8 +63,19 @@ constructor(
|
|||||||
strokeCap = Paint.Cap.BUTT
|
strokeCap = Paint.Cap.BUTT
|
||||||
}
|
}
|
||||||
|
|
||||||
private val wavePathCache = WavePathCache()
|
private val displayedWavePath = Path()
|
||||||
private val roundedBlockRenderer = RoundedBlockRenderer()
|
private val pathMeasure = PathMeasure()
|
||||||
|
private var adjustedWavelength = 0f
|
||||||
|
|
||||||
|
private val cachedWavePath = Path()
|
||||||
|
private val transform = Matrix()
|
||||||
|
private var cachedWavelength = -1
|
||||||
|
private var cachedTrackLength = -1f
|
||||||
|
|
||||||
|
private val drawRect = RectF()
|
||||||
|
private val patchRect = RectF()
|
||||||
|
private val clipRect = RectF()
|
||||||
|
private val roundedRectPath = Path()
|
||||||
|
|
||||||
private val startPoint = PathPoint()
|
private val startPoint = PathPoint()
|
||||||
private val endPoint = PathPoint()
|
private val endPoint = PathPoint()
|
||||||
@@ -85,6 +104,8 @@ constructor(
|
|||||||
private var waveRampProgressMin = 0f
|
private var waveRampProgressMin = 0f
|
||||||
private var waveRampProgressMax = DEFAULT_WAVE_RAMP_PROGRESS_MAX
|
private var waveRampProgressMax = DEFAULT_WAVE_RAMP_PROGRESS_MAX
|
||||||
|
|
||||||
|
@Px private var waveSpeed: Int = 0
|
||||||
|
|
||||||
private val phaseTicker =
|
private val phaseTicker =
|
||||||
object : Runnable {
|
object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
@@ -102,71 +123,31 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val rampAttrs = context.obtainStyledAttributes(attrs, MR.styleable.BaseProgressIndicator)
|
context.withStyledAttributes(attrs, R.styleable.WavySlider) {
|
||||||
waveRampProgressMin =
|
waveRampProgressMin = getFloat(R.styleable.WavySlider_waveAmplitudeRampProgressMin, 0f)
|
||||||
rampAttrs
|
waveRampProgressMax =
|
||||||
.getFloat(MR.styleable.BaseProgressIndicator_waveAmplitudeRampProgressMin, 0f)
|
getFloat(
|
||||||
.coerceIn(0f, 1f)
|
R.styleable.WavySlider_waveAmplitudeRampProgressMax,
|
||||||
waveRampProgressMax =
|
|
||||||
rampAttrs
|
|
||||||
.getFloat(
|
|
||||||
MR.styleable.BaseProgressIndicator_waveAmplitudeRampProgressMax,
|
|
||||||
DEFAULT_WAVE_RAMP_PROGRESS_MAX,
|
DEFAULT_WAVE_RAMP_PROGRESS_MAX,
|
||||||
)
|
)
|
||||||
.coerceIn(0f, 1f)
|
check(waveRampProgressMin in 0.0..1.0) { "rampProgressMin out of range" }
|
||||||
if (waveRampProgressMax - waveRampProgressMin <= EPSILON) {
|
check(waveRampProgressMax in 0.0..1.0) { "rampProgressMax out of range" }
|
||||||
waveRampProgressMax = min(1f, waveRampProgressMin + 0.01f)
|
check(waveRampProgressMax - waveRampProgressMin > EPSILON) {
|
||||||
|
"rampProgressMin must be < rampProgressMax"
|
||||||
|
}
|
||||||
|
|
||||||
|
configuredWavelengthPx = getDimensionPixelSizeOrThrow(R.styleable.WavySlider_wavelength)
|
||||||
|
configuredAmplitudePx =
|
||||||
|
getDimensionPixelSizeOrThrow(R.styleable.WavySlider_waveAmplitude)
|
||||||
|
configuredSpeedPx = getDimensionPixelSizeOrThrow(R.styleable.WavySlider_waveSpeed)
|
||||||
|
check(
|
||||||
|
configuredWavelengthPx > 0 && configuredAmplitudePx > 0 && configuredSpeedPx > 0
|
||||||
|
) {
|
||||||
|
"Invalid wave dimensions, must be >0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rampAttrs.recycle()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Wave amplitude in pixels. */
|
|
||||||
@Px
|
|
||||||
var waveAmplitude: Int = 0
|
|
||||||
set(value) {
|
|
||||||
val sanitized = abs(value)
|
|
||||||
if (field != sanitized) {
|
|
||||||
field = sanitized
|
|
||||||
if (sanitized > 0) {
|
|
||||||
configuredAmplitudePx = sanitized
|
|
||||||
}
|
|
||||||
updateActiveTrackSuppression()
|
|
||||||
ensurePhaseTickerState()
|
|
||||||
invalidate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Wavelength in pixels for determinate rendering. */
|
|
||||||
@Px
|
|
||||||
var wavelengthDeterminate: Int = 0
|
|
||||||
set(value) {
|
|
||||||
val sanitized = abs(value)
|
|
||||||
if (field != sanitized) {
|
|
||||||
field = sanitized
|
|
||||||
if (sanitized > 0) {
|
|
||||||
configuredWavelengthPx = sanitized
|
|
||||||
}
|
|
||||||
wavePathCache.markDirty()
|
|
||||||
updateActiveTrackSuppression()
|
|
||||||
ensurePhaseTickerState()
|
|
||||||
invalidate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Wave speed in px/s. Positive towards 100%, negative towards 0%. */
|
|
||||||
@Px
|
|
||||||
var waveSpeed: Int = 0
|
|
||||||
set(value) {
|
|
||||||
if (field != value) {
|
|
||||||
field = value
|
|
||||||
if (value != 0) {
|
|
||||||
configuredSpeedPx = value
|
|
||||||
}
|
|
||||||
ensurePhaseTickerState()
|
|
||||||
invalidate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onAttachedToWindow() {
|
override fun onAttachedToWindow() {
|
||||||
super.onAttachedToWindow()
|
super.onAttachedToWindow()
|
||||||
ensurePhaseTickerState()
|
ensurePhaseTickerState()
|
||||||
@@ -177,7 +158,7 @@ constructor(
|
|||||||
waveTransitionAnimation = null
|
waveTransitionAnimation = null
|
||||||
removeCallbacks(phaseTicker)
|
removeCallbacks(phaseTicker)
|
||||||
phaseTickerScheduled = false
|
phaseTickerScheduled = false
|
||||||
clearPhaseClock()
|
resetPhaseClock()
|
||||||
super.onDetachedFromWindow()
|
super.onDetachedFromWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,60 +180,37 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mirrors [PatchedLinearProgressIndicator.setWaveEnabled]: keeps geometry stable and animates
|
* Configure whether the wave effect is on or off.
|
||||||
* internal amplitude fraction.
|
*
|
||||||
|
* This will animate the wave in.
|
||||||
|
*
|
||||||
|
* @param enabled Whether to enable the wave or not.
|
||||||
*/
|
*/
|
||||||
fun setWaveEnabled(
|
fun setWaveEnabled(enabled: Boolean) {
|
||||||
enabled: Boolean,
|
|
||||||
@Px wavelengthPx: Int,
|
|
||||||
@Px amplitudePx: Int,
|
|
||||||
@Px speedPx: Int,
|
|
||||||
) {
|
|
||||||
if (wavelengthPx > 0) {
|
|
||||||
configuredWavelengthPx = abs(wavelengthPx)
|
|
||||||
}
|
|
||||||
if (amplitudePx > 0) {
|
|
||||||
configuredAmplitudePx = abs(amplitudePx)
|
|
||||||
}
|
|
||||||
if (speedPx != 0) {
|
|
||||||
configuredSpeedPx = speedPx
|
|
||||||
}
|
|
||||||
|
|
||||||
waveTransitionAnimation?.cancel()
|
waveTransitionAnimation?.cancel()
|
||||||
waveTransitionAnimation = null
|
waveTransitionAnimation = null
|
||||||
|
|
||||||
applyWaveGeometryIfConfigured()
|
waveEnabled = enabled
|
||||||
|
if (enabled) {
|
||||||
val canEnableWave = enabled && canAnimateWave()
|
|
||||||
waveEnabled = canEnableWave
|
|
||||||
updateActiveTrackSuppression()
|
|
||||||
if (canEnableWave) {
|
|
||||||
waveSpeed = configuredSpeedPx
|
waveSpeed = configuredSpeedPx
|
||||||
resetPhaseClock()
|
resetPhaseClock()
|
||||||
transitionToAmplitudeFraction(1f)
|
transitionToAmplitudeFraction(1f)
|
||||||
|
} else if (abs(currentAmplitudeFraction - MIN_VISIBLE_WAVE_FRACTION) < EPSILON) {
|
||||||
|
waveSpeed = 0
|
||||||
|
resetPhaseClock()
|
||||||
} else {
|
} else {
|
||||||
if (abs(currentAmplitudeFraction - MIN_VISIBLE_WAVE_FRACTION) < EPSILON) {
|
transitionToAmplitudeFraction(MIN_VISIBLE_WAVE_FRACTION) { waveSpeed = 0 }
|
||||||
waveSpeed = 0
|
|
||||||
clearPhaseClock()
|
|
||||||
ensurePhaseTickerState()
|
|
||||||
invalidate()
|
|
||||||
} else {
|
|
||||||
transitionToAmplitudeFraction(MIN_VISIBLE_WAVE_FRACTION) {
|
|
||||||
waveSpeed = 0
|
|
||||||
clearPhaseClock()
|
|
||||||
ensurePhaseTickerState()
|
|
||||||
invalidate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
resetPhaseClock()
|
||||||
|
updateActiveTrackSuppression()
|
||||||
|
ensurePhaseTickerState()
|
||||||
|
markDirty()
|
||||||
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
drawWavyActiveTrack(canvas)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun drawWavyActiveTrack(canvas: Canvas) {
|
|
||||||
if (!shouldDrawWave()) {
|
if (!shouldDrawWave()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -270,13 +228,13 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
displayedTrackThickness = trackHeight.toFloat()
|
displayedTrackThickness = trackHeight.toFloat()
|
||||||
displayedCornerRadius = min(displayedTrackThickness / 2f, getTrackCornerSize().toFloat())
|
displayedCornerRadius = min(displayedTrackThickness / 2f, trackCornerSize.toFloat())
|
||||||
displayedInnerCornerRadius =
|
displayedInnerCornerRadius =
|
||||||
min(displayedTrackThickness / 2f, getTrackInsideCornerSize().toFloat())
|
min(displayedTrackThickness / 2f, trackInsideCornerSize.toFloat())
|
||||||
displayedAmplitude = waveAmplitude.toFloat()
|
displayedAmplitude = configuredAmplitudePx.toFloat()
|
||||||
|
|
||||||
ensureCachedWavePath(trackLength, wavelengthDeterminate)
|
ensureCachedWavePath(trackLength, configuredWavelengthPx)
|
||||||
if (wavePathCache.pathMeasure.length <= 0f || wavePathCache.adjustedWavelength <= 0f) {
|
if (pathMeasure.length <= 0f || adjustedWavelength <= 0f) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,7 +267,7 @@ constructor(
|
|||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
) {
|
) {
|
||||||
val clampedProgress = progressFraction.coerceIn(0f, 1f)
|
val clampedProgress = progressFraction.coerceIn(0f, 1f)
|
||||||
val isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL
|
val isRtl = layoutDirection == LAYOUT_DIRECTION_RTL
|
||||||
val direction = if (isRtl) ActiveTrackDirection.RIGHT else ActiveTrackDirection.LEFT
|
val direction = if (isRtl) ActiveTrackDirection.RIGHT else ActiveTrackDirection.LEFT
|
||||||
val thumbTrackPosition =
|
val thumbTrackPosition =
|
||||||
if (isRtl) {
|
if (isRtl) {
|
||||||
@@ -419,10 +377,10 @@ constructor(
|
|||||||
|
|
||||||
if (!drawWavyPath) {
|
if (!drawWavyPath) {
|
||||||
canvas.drawLine(
|
canvas.drawLine(
|
||||||
startPoint.posVec[0],
|
startPoint.posVec.x,
|
||||||
startPoint.posVec[1],
|
startPoint.posVec.y,
|
||||||
endPoint.posVec[0],
|
endPoint.posVec.x,
|
||||||
endPoint.posVec[1],
|
endPoint.posVec.y,
|
||||||
wavePaint,
|
wavePaint,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@@ -434,7 +392,7 @@ constructor(
|
|||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
trackCenterY = trackCenterY,
|
trackCenterY = trackCenterY,
|
||||||
)
|
)
|
||||||
canvas.drawPath(wavePathCache.displayedWavePath, wavePaint)
|
canvas.drawPath(displayedWavePath, wavePaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!useStrokeCap()) {
|
if (!useStrokeCap()) {
|
||||||
@@ -467,31 +425,17 @@ constructor(
|
|||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
trackCenterY: Float,
|
trackCenterY: Float,
|
||||||
) {
|
) {
|
||||||
wavePathCache.calculateDisplayedWavePath(
|
calculateDisplayedWavePath(
|
||||||
trackLength = trackLength,
|
trackLength = trackLength,
|
||||||
start = start,
|
start = start,
|
||||||
end = end,
|
end = end,
|
||||||
amplitudeFraction = amplitudeFraction,
|
amplitudeFraction = amplitudeFraction,
|
||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
displayedAmplitude = displayedAmplitude,
|
|
||||||
startPoint = startPoint,
|
|
||||||
endPoint = endPoint,
|
|
||||||
baseTranslationX = trackSidePadding.toFloat(),
|
baseTranslationX = trackSidePadding.toFloat(),
|
||||||
baseTranslationY = trackCenterY,
|
baseTranslationY = trackCenterY,
|
||||||
scaleAroundPivotY = true,
|
|
||||||
clampSegmentFractions = true,
|
|
||||||
epsilon = EPSILON,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
|
||||||
wavePathCache.ensureCachedWavePath(trackLength, wavelength)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
|
||||||
wavePathCache.invalidateCachedWavePath(trackLength, wavelength)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun drawRoundedBlock(
|
private fun drawRoundedBlock(
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
drawCenter: PathPoint,
|
drawCenter: PathPoint,
|
||||||
@@ -499,13 +443,12 @@ constructor(
|
|||||||
drawHeight: Float,
|
drawHeight: Float,
|
||||||
drawCornerSize: Float,
|
drawCornerSize: Float,
|
||||||
) {
|
) {
|
||||||
roundedBlockRenderer.drawRoundedBlock(
|
drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
paint = wavePaint,
|
paint = wavePaint,
|
||||||
drawCenter = drawCenter,
|
drawCenter = drawCenter,
|
||||||
drawWidth = drawWidth,
|
drawWidth = drawWidth,
|
||||||
drawHeight = drawHeight,
|
drawHeight = drawHeight,
|
||||||
displayedTrackThickness = displayedTrackThickness,
|
|
||||||
drawCornerSize = drawCornerSize,
|
drawCornerSize = drawCornerSize,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -522,13 +465,12 @@ constructor(
|
|||||||
clipCornerSize: Float,
|
clipCornerSize: Float,
|
||||||
clipRight: Boolean,
|
clipRight: Boolean,
|
||||||
) {
|
) {
|
||||||
roundedBlockRenderer.drawRoundedBlock(
|
drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
paint = wavePaint,
|
paint = wavePaint,
|
||||||
drawCenter = drawCenter,
|
drawCenter = drawCenter,
|
||||||
drawWidth = drawWidth,
|
drawWidth = drawWidth,
|
||||||
drawHeight = drawHeight,
|
drawHeight = drawHeight,
|
||||||
displayedTrackThickness = displayedTrackThickness,
|
|
||||||
drawCornerSize = drawCornerSize,
|
drawCornerSize = drawCornerSize,
|
||||||
clipCenter = clipCenter,
|
clipCenter = clipCenter,
|
||||||
clipWidth = clipWidth,
|
clipWidth = clipWidth,
|
||||||
@@ -604,18 +546,7 @@ constructor(
|
|||||||
return ((progressFraction - waveRampProgressMin) / span).coerceIn(0f, 1f)
|
return ((progressFraction - waveRampProgressMin) / span).coerceIn(0f, 1f)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun canAnimateWave(): Boolean =
|
@SuppressLint("PrivateResource")
|
||||||
configuredWavelengthPx > 0 && configuredAmplitudePx > 0 && configuredSpeedPx != 0
|
|
||||||
|
|
||||||
private fun applyWaveGeometryIfConfigured() {
|
|
||||||
if (configuredWavelengthPx > 0 && wavelengthDeterminate != configuredWavelengthPx) {
|
|
||||||
wavelengthDeterminate = configuredWavelengthPx
|
|
||||||
}
|
|
||||||
if (configuredAmplitudePx > 0 && waveAmplitude != configuredAmplitudePx) {
|
|
||||||
waveAmplitude = configuredAmplitudePx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun transitionToAmplitudeFraction(target: Float, onFinished: (() -> Unit)? = null) {
|
private fun transitionToAmplitudeFraction(target: Float, onFinished: (() -> Unit)? = null) {
|
||||||
val clampedTarget = target.coerceIn(MIN_VISIBLE_WAVE_FRACTION, 1f)
|
val clampedTarget = target.coerceIn(MIN_VISIBLE_WAVE_FRACTION, 1f)
|
||||||
if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) {
|
if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) {
|
||||||
@@ -632,9 +563,10 @@ constructor(
|
|||||||
val springAnimation =
|
val springAnimation =
|
||||||
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
||||||
spring =
|
spring =
|
||||||
WaveMotionUtils.resolveWaveTransitionSpring(
|
MotionUtils.resolveThemeSpringForce(
|
||||||
context = context,
|
context,
|
||||||
animateOn = clampedTarget > currentAmplitudeFraction,
|
MR.attr.motionSpringFastEffects,
|
||||||
|
MR.style.Motion_Material3_Spring_Standard_Fast_Effects,
|
||||||
)
|
)
|
||||||
setStartValue(currentAmplitudeFraction)
|
setStartValue(currentAmplitudeFraction)
|
||||||
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
||||||
@@ -684,19 +616,17 @@ constructor(
|
|||||||
postOnAnimation(phaseTicker)
|
postOnAnimation(phaseTicker)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldTickPhase(): Boolean {
|
private fun shouldTickPhase(): Boolean =
|
||||||
return shouldDrawWave() &&
|
shouldDrawWave() &&
|
||||||
waveSpeed != 0 &&
|
waveSpeed != 0 &&
|
||||||
visibility == View.VISIBLE &&
|
isVisible &&
|
||||||
windowVisibility == View.VISIBLE &&
|
windowVisibility == VISIBLE &&
|
||||||
isShown &&
|
isShown &&
|
||||||
alpha > 0f
|
alpha > 0f
|
||||||
}
|
|
||||||
|
|
||||||
private fun updatePhaseFraction() {
|
private fun updatePhaseFraction() {
|
||||||
val wavelength = wavelengthDeterminate
|
|
||||||
val speed = waveSpeed
|
val speed = waveSpeed
|
||||||
if (wavelength <= 0 || speed == 0) {
|
if (speed == 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -710,19 +640,20 @@ constructor(
|
|||||||
applyRampEasing(calculateStartRampFraction(progressFraction, trackLength))
|
applyRampEasing(calculateStartRampFraction(progressFraction, trackLength))
|
||||||
val effectiveSpeed = speed.toFloat() * phaseSpeedScale
|
val effectiveSpeed = speed.toFloat() * phaseSpeedScale
|
||||||
if (effectiveSpeed == 0f) {
|
if (effectiveSpeed == 0f) {
|
||||||
|
// not actually moving once ramp is taken in, move on
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val nowNanos = System.nanoTime()
|
val nowNanos = System.nanoTime()
|
||||||
if (lastPhaseFrameNanos != 0L) {
|
if (lastPhaseFrameNanos != 0L) {
|
||||||
val deltaSeconds = (nowNanos - lastPhaseFrameNanos) / 1_000_000_000f
|
val deltaSeconds = (nowNanos - lastPhaseFrameNanos) / 1_000_000_000f
|
||||||
val delta = deltaSeconds * (effectiveSpeed / wavelength.toFloat())
|
val delta = deltaSeconds * (effectiveSpeed / configuredWavelengthPx.toFloat())
|
||||||
phaseFraction = ((phaseFraction + delta) % 1f + 1f) % 1f
|
// move phase forward, take the decimal
|
||||||
|
// all incoming inputs (phase & delta) are not-zero so no need
|
||||||
|
// to care about negative modulo
|
||||||
|
phaseFraction = (phaseFraction + delta) % 1f
|
||||||
}
|
}
|
||||||
lastPhaseFrameNanos = nowNanos
|
lastPhaseFrameNanos = nowNanos
|
||||||
if (phaseFraction <= 0f) {
|
|
||||||
phaseFraction = MIN_PHASE_FRACTION
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resetPhaseClock() {
|
private fun resetPhaseClock() {
|
||||||
@@ -730,22 +661,10 @@ constructor(
|
|||||||
lastPhaseFrameNanos = 0L
|
lastPhaseFrameNanos = 0L
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearPhaseClock() {
|
private fun shouldDrawWave(): Boolean = waveEnabled || waveTransitionAnimation != null
|
||||||
phaseFraction = 0f
|
|
||||||
lastPhaseFrameNanos = 0L
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun shouldDrawWave(): Boolean =
|
|
||||||
waveAmplitude > 0 &&
|
|
||||||
wavelengthDeterminate > 0 &&
|
|
||||||
currentAmplitudeFraction > 0f &&
|
|
||||||
(waveEnabled || waveTransitionAnimation != null)
|
|
||||||
|
|
||||||
private fun updateActiveTrackSuppression() {
|
private fun updateActiveTrackSuppression() {
|
||||||
val suppress =
|
val suppress = shouldDrawWave()
|
||||||
configuredAmplitudePx > 0 &&
|
|
||||||
configuredWavelengthPx > 0 &&
|
|
||||||
(waveEnabled || waveTransitionAnimation != null)
|
|
||||||
if (suppress == linearActiveTrackSuppressed) {
|
if (suppress == linearActiveTrackSuppressed) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -763,11 +682,247 @@ constructor(
|
|||||||
RIGHT,
|
RIGHT,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun applyRampEasing(fraction: Float): Float {
|
||||||
|
val clamped = fraction.coerceIn(0f, 1f)
|
||||||
|
return clamped * clamped * (3f - 2f * clamped)
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PathPoint(
|
||||||
|
val posVec: PointF = PointF(0f, 0f),
|
||||||
|
val tanVec: PointF = PointF(1f, 0f),
|
||||||
|
) {
|
||||||
|
fun reset() {
|
||||||
|
posVec.x = 0f
|
||||||
|
posVec.y = 0f
|
||||||
|
tanVec.x = 1f
|
||||||
|
tanVec.y = 0f
|
||||||
|
}
|
||||||
|
|
||||||
|
fun translate(dx: Float, dy: Float) {
|
||||||
|
posVec.x += dx
|
||||||
|
posVec.y += dy
|
||||||
|
}
|
||||||
|
|
||||||
|
fun scale(sx: Float, sy: Float, pivotY: Float = 0f) {
|
||||||
|
posVec.x *= sx
|
||||||
|
posVec.y = (posVec.y - pivotY) * sy + pivotY
|
||||||
|
tanVec.x *= sx
|
||||||
|
tanVec.y *= sy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun markDirty() {
|
||||||
|
cachedWavelength = -1
|
||||||
|
cachedTrackLength = -1f
|
||||||
|
adjustedWavelength = 0f
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
|
if (
|
||||||
|
cachedTrackLength == trackLength &&
|
||||||
|
cachedWavelength == wavelength &&
|
||||||
|
adjustedWavelength > 0f
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cachedTrackLength = trackLength
|
||||||
|
cachedWavelength = wavelength
|
||||||
|
invalidateCachedWavePath(trackLength, wavelength)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
|
cachedWavePath.rewind()
|
||||||
|
adjustedWavelength = 0f
|
||||||
|
if (trackLength <= 0f || wavelength <= 0) {
|
||||||
|
pathMeasure.setPath(cachedWavePath, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val cycleCount = max(1, (trackLength / wavelength).toInt())
|
||||||
|
adjustedWavelength = trackLength / cycleCount
|
||||||
|
for (i in 0..cycleCount) {
|
||||||
|
val cycle = i.toFloat()
|
||||||
|
cachedWavePath.cubicTo(
|
||||||
|
2 * cycle + WAVE_SMOOTHNESS,
|
||||||
|
0f,
|
||||||
|
2 * cycle + 1 - WAVE_SMOOTHNESS,
|
||||||
|
1f,
|
||||||
|
2 * cycle + 1,
|
||||||
|
1f,
|
||||||
|
)
|
||||||
|
cachedWavePath.cubicTo(
|
||||||
|
2 * cycle + 1 + WAVE_SMOOTHNESS,
|
||||||
|
1f,
|
||||||
|
2 * cycle + 2 - WAVE_SMOOTHNESS,
|
||||||
|
0f,
|
||||||
|
2 * cycle + 2,
|
||||||
|
0f,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
transform.reset()
|
||||||
|
transform.setScale(adjustedWavelength / 2f, -2f)
|
||||||
|
transform.postTranslate(0f, 1f)
|
||||||
|
cachedWavePath.transform(transform)
|
||||||
|
pathMeasure.setPath(cachedWavePath, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun calculateDisplayedWavePath(
|
||||||
|
trackLength: Float,
|
||||||
|
start: Float,
|
||||||
|
end: Float,
|
||||||
|
amplitudeFraction: Float,
|
||||||
|
phaseFraction: Float,
|
||||||
|
baseTranslationX: Float,
|
||||||
|
baseTranslationY: Float,
|
||||||
|
): Boolean {
|
||||||
|
displayedWavePath.rewind()
|
||||||
|
if (pathMeasure.length <= EPSILON) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var adjustedStart = start
|
||||||
|
var adjustedEnd = end
|
||||||
|
var resultTranslationX = baseTranslationX
|
||||||
|
|
||||||
|
if (adjustedWavelength > EPSILON) {
|
||||||
|
val cycleCount = trackLength / adjustedWavelength
|
||||||
|
if (cycleCount > EPSILON) {
|
||||||
|
val phaseFractionInPath = phaseFraction / cycleCount
|
||||||
|
val ratio = cycleCount / (cycleCount + 1f)
|
||||||
|
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
|
||||||
|
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
|
||||||
|
}
|
||||||
|
resultTranslationX -= phaseFraction * adjustedWavelength
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustedStart = adjustedStart.coerceIn(0f, 1f)
|
||||||
|
adjustedEnd = adjustedEnd.coerceIn(0f, 1f)
|
||||||
|
if (adjustedEnd <= adjustedStart) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val startDistance = adjustedStart * pathMeasure.length
|
||||||
|
val endDistance = adjustedEnd * pathMeasure.length
|
||||||
|
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
||||||
|
|
||||||
|
startPoint.reset()
|
||||||
|
pathMeasure.getPosTan(
|
||||||
|
startDistance,
|
||||||
|
floatArrayOf(startPoint.posVec.x, startPoint.posVec.y),
|
||||||
|
floatArrayOf(startPoint.tanVec.x, startPoint.tanVec.y),
|
||||||
|
)
|
||||||
|
endPoint.reset()
|
||||||
|
pathMeasure.getPosTan(
|
||||||
|
startDistance,
|
||||||
|
floatArrayOf(endPoint.posVec.x, endPoint.posVec.y),
|
||||||
|
floatArrayOf(endPoint.tanVec.x, endPoint.tanVec.y),
|
||||||
|
)
|
||||||
|
|
||||||
|
transform.reset()
|
||||||
|
transform.setTranslate(resultTranslationX, baseTranslationY)
|
||||||
|
startPoint.translate(resultTranslationX, baseTranslationY)
|
||||||
|
endPoint.translate(resultTranslationX, baseTranslationY)
|
||||||
|
|
||||||
|
val scaleY = displayedAmplitude * amplitudeFraction
|
||||||
|
if (scaleY > 0f) {
|
||||||
|
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
|
||||||
|
startPoint.scale(1f, scaleY, baseTranslationY)
|
||||||
|
endPoint.scale(1f, scaleY, baseTranslationY)
|
||||||
|
}
|
||||||
|
|
||||||
|
displayedWavePath.transform(transform)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun drawRoundedBlock(
|
||||||
|
canvas: Canvas,
|
||||||
|
paint: Paint,
|
||||||
|
drawCenter: PathPoint,
|
||||||
|
drawWidth: Float,
|
||||||
|
drawHeight: Float,
|
||||||
|
drawCornerSize: Float,
|
||||||
|
clipCenter: PathPoint? = null,
|
||||||
|
clipWidth: Float = 0f,
|
||||||
|
clipHeight: Float = 0f,
|
||||||
|
clipCornerSize: Float = 0f,
|
||||||
|
clipRight: Boolean = false,
|
||||||
|
) {
|
||||||
|
val localDrawHeight = min(drawHeight, displayedTrackThickness)
|
||||||
|
var localClipWidth = clipWidth
|
||||||
|
var localClipHeight = clipHeight
|
||||||
|
var localClipCornerSize = clipCornerSize
|
||||||
|
|
||||||
|
drawRect.set(-drawWidth / 2f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
||||||
|
paint.style = Paint.Style.FILL
|
||||||
|
canvas.withSave {
|
||||||
|
if (clipCenter != null) {
|
||||||
|
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
||||||
|
localClipCornerSize =
|
||||||
|
min(
|
||||||
|
localClipWidth / 2f,
|
||||||
|
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
||||||
|
)
|
||||||
|
if (clipRight) {
|
||||||
|
val leftEdgeDiff =
|
||||||
|
(clipCenter.posVec.x - localClipCornerSize) -
|
||||||
|
(drawCenter.posVec.x - drawCornerSize)
|
||||||
|
if (leftEdgeDiff > 0f) {
|
||||||
|
clipCenter.translate(-leftEdgeDiff / 2f, 0f)
|
||||||
|
localClipWidth += leftEdgeDiff
|
||||||
|
}
|
||||||
|
patchRect.set(0f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
||||||
|
} else {
|
||||||
|
val rightEdgeDiff =
|
||||||
|
(clipCenter.posVec.x + localClipCornerSize) -
|
||||||
|
(drawCenter.posVec.x + drawCornerSize)
|
||||||
|
if (rightEdgeDiff < 0f) {
|
||||||
|
clipCenter.translate(-rightEdgeDiff / 2f, 0f)
|
||||||
|
localClipWidth -= rightEdgeDiff
|
||||||
|
}
|
||||||
|
patchRect.set(-drawWidth / 2f, -localDrawHeight / 2f, 0f, localDrawHeight / 2f)
|
||||||
|
}
|
||||||
|
|
||||||
|
clipRect.set(
|
||||||
|
-localClipWidth / 2f,
|
||||||
|
-localClipHeight / 2f,
|
||||||
|
localClipWidth / 2f,
|
||||||
|
localClipHeight / 2f,
|
||||||
|
)
|
||||||
|
translate(clipCenter.posVec.x, clipCenter.posVec.y)
|
||||||
|
rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
||||||
|
roundedRectPath.reset()
|
||||||
|
roundedRectPath.addRoundRect(
|
||||||
|
clipRect,
|
||||||
|
localClipCornerSize,
|
||||||
|
localClipCornerSize,
|
||||||
|
Path.Direction.CCW,
|
||||||
|
)
|
||||||
|
clipPath(roundedRectPath)
|
||||||
|
|
||||||
|
rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
||||||
|
translate(-clipCenter.posVec.x, -clipCenter.posVec.y)
|
||||||
|
translate(drawCenter.posVec.x, drawCenter.posVec.y)
|
||||||
|
rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
||||||
|
drawRect(patchRect, paint)
|
||||||
|
drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
||||||
|
} else {
|
||||||
|
translate(drawCenter.posVec.x, drawCenter.posVec.y)
|
||||||
|
rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
||||||
|
drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun vectorToCanvasRotation(vector: PointF): Float =
|
||||||
|
Math.toDegrees(atan2(vector.y, vector.x).toDouble()).toFloat()
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
||||||
const val DEFAULT_WAVE_RAMP_PROGRESS_MAX = 0.03f
|
const val DEFAULT_WAVE_RAMP_PROGRESS_MAX = 0.03f
|
||||||
const val MIN_VISIBLE_WAVE_FRACTION = 0.001f
|
const val MIN_VISIBLE_WAVE_FRACTION = 0.001f
|
||||||
const val MIN_PHASE_FRACTION = 0.0001f
|
const val MIN_PHASE_FRACTION = 0.0001f
|
||||||
const val EPSILON = 0.0001f
|
const val EPSILON = 0.0001f
|
||||||
|
const val WAVE_SMOOTHNESS = 0.48f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,314 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2026 Auxio Project
|
|
||||||
* WaveRenderHelpers.kt is part of Auxio.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.android.material.wave
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.graphics.Canvas
|
|
||||||
import android.graphics.Matrix
|
|
||||||
import android.graphics.Paint
|
|
||||||
import android.graphics.Path
|
|
||||||
import android.graphics.PathMeasure
|
|
||||||
import android.graphics.RectF
|
|
||||||
import androidx.dynamicanimation.animation.SpringForce
|
|
||||||
import com.google.android.material.R as MR
|
|
||||||
import com.google.android.material.motion.MotionUtils
|
|
||||||
import kotlin.math.atan2
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
internal data class PathPoint(
|
|
||||||
val posVec: FloatArray = floatArrayOf(0f, 0f),
|
|
||||||
val tanVec: FloatArray = floatArrayOf(1f, 0f),
|
|
||||||
) {
|
|
||||||
fun reset() {
|
|
||||||
posVec[0] = 0f
|
|
||||||
posVec[1] = 0f
|
|
||||||
tanVec[0] = 1f
|
|
||||||
tanVec[1] = 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
fun translate(dx: Float, dy: Float) {
|
|
||||||
posVec[0] += dx
|
|
||||||
posVec[1] += dy
|
|
||||||
}
|
|
||||||
|
|
||||||
fun scale(sx: Float, sy: Float, pivotY: Float = 0f) {
|
|
||||||
posVec[0] *= sx
|
|
||||||
posVec[1] = (posVec[1] - pivotY) * sy + pivotY
|
|
||||||
tanVec[0] *= sx
|
|
||||||
tanVec[1] *= sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class WavePathCache {
|
|
||||||
val displayedWavePath = Path()
|
|
||||||
val pathMeasure = PathMeasure()
|
|
||||||
var adjustedWavelength = 0f
|
|
||||||
private set
|
|
||||||
|
|
||||||
private val cachedWavePath = Path()
|
|
||||||
private val transform = Matrix()
|
|
||||||
private var cachedWavelength = -1
|
|
||||||
private var cachedTrackLength = -1f
|
|
||||||
|
|
||||||
fun markDirty() {
|
|
||||||
cachedWavelength = -1
|
|
||||||
cachedTrackLength = -1f
|
|
||||||
adjustedWavelength = 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
|
||||||
if (
|
|
||||||
cachedTrackLength == trackLength &&
|
|
||||||
cachedWavelength == wavelength &&
|
|
||||||
adjustedWavelength > 0f
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cachedTrackLength = trackLength
|
|
||||||
cachedWavelength = wavelength
|
|
||||||
invalidateCachedWavePath(trackLength, wavelength)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
|
||||||
cachedWavePath.rewind()
|
|
||||||
adjustedWavelength = 0f
|
|
||||||
if (trackLength <= 0f || wavelength <= 0) {
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val cycleCount = max(1, (trackLength / wavelength).toInt())
|
|
||||||
adjustedWavelength = trackLength / cycleCount
|
|
||||||
for (i in 0..cycleCount) {
|
|
||||||
val cycle = i.toFloat()
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 1 - WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 1,
|
|
||||||
1f,
|
|
||||||
)
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + 1 + WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 2 - WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 2,
|
|
||||||
0f,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
transform.reset()
|
|
||||||
transform.setScale(adjustedWavelength / 2f, -2f)
|
|
||||||
transform.postTranslate(0f, 1f)
|
|
||||||
cachedWavePath.transform(transform)
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun calculateDisplayedWavePath(
|
|
||||||
trackLength: Float,
|
|
||||||
start: Float,
|
|
||||||
end: Float,
|
|
||||||
amplitudeFraction: Float,
|
|
||||||
phaseFraction: Float,
|
|
||||||
displayedAmplitude: Float,
|
|
||||||
startPoint: PathPoint,
|
|
||||||
endPoint: PathPoint,
|
|
||||||
baseTranslationX: Float,
|
|
||||||
baseTranslationY: Float,
|
|
||||||
scaleAroundPivotY: Boolean,
|
|
||||||
clampSegmentFractions: Boolean,
|
|
||||||
epsilon: Float,
|
|
||||||
): Boolean {
|
|
||||||
displayedWavePath.rewind()
|
|
||||||
if (pathMeasure.length <= epsilon) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var adjustedStart = start
|
|
||||||
var adjustedEnd = end
|
|
||||||
var resultTranslationX = baseTranslationX
|
|
||||||
|
|
||||||
if (adjustedWavelength > epsilon) {
|
|
||||||
val cycleCount = trackLength / adjustedWavelength
|
|
||||||
if (cycleCount > epsilon) {
|
|
||||||
val phaseFractionInPath = phaseFraction / cycleCount
|
|
||||||
val ratio = cycleCount / (cycleCount + 1f)
|
|
||||||
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
|
|
||||||
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
|
|
||||||
}
|
|
||||||
resultTranslationX -= phaseFraction * adjustedWavelength
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clampSegmentFractions) {
|
|
||||||
adjustedStart = adjustedStart.coerceIn(0f, 1f)
|
|
||||||
adjustedEnd = adjustedEnd.coerceIn(0f, 1f)
|
|
||||||
if (adjustedEnd <= adjustedStart) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val startDistance = adjustedStart * pathMeasure.length
|
|
||||||
val endDistance = adjustedEnd * pathMeasure.length
|
|
||||||
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
|
||||||
|
|
||||||
startPoint.reset()
|
|
||||||
pathMeasure.getPosTan(startDistance, startPoint.posVec, startPoint.tanVec)
|
|
||||||
endPoint.reset()
|
|
||||||
pathMeasure.getPosTan(endDistance, endPoint.posVec, endPoint.tanVec)
|
|
||||||
|
|
||||||
transform.reset()
|
|
||||||
transform.setTranslate(resultTranslationX, baseTranslationY)
|
|
||||||
startPoint.translate(resultTranslationX, baseTranslationY)
|
|
||||||
endPoint.translate(resultTranslationX, baseTranslationY)
|
|
||||||
|
|
||||||
val scaleY = displayedAmplitude * amplitudeFraction
|
|
||||||
if (scaleY > 0f) {
|
|
||||||
if (scaleAroundPivotY) {
|
|
||||||
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
|
|
||||||
startPoint.scale(1f, scaleY, baseTranslationY)
|
|
||||||
endPoint.scale(1f, scaleY, baseTranslationY)
|
|
||||||
} else {
|
|
||||||
transform.postScale(1f, scaleY)
|
|
||||||
startPoint.scale(1f, scaleY)
|
|
||||||
endPoint.scale(1f, scaleY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
displayedWavePath.transform(transform)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class RoundedBlockRenderer {
|
|
||||||
private val drawRect = RectF()
|
|
||||||
private val patchRect = RectF()
|
|
||||||
private val clipRect = RectF()
|
|
||||||
private val roundedRectPath = Path()
|
|
||||||
|
|
||||||
fun drawRoundedBlock(
|
|
||||||
canvas: Canvas,
|
|
||||||
paint: Paint,
|
|
||||||
drawCenter: PathPoint,
|
|
||||||
drawWidth: Float,
|
|
||||||
drawHeight: Float,
|
|
||||||
displayedTrackThickness: Float,
|
|
||||||
drawCornerSize: Float,
|
|
||||||
clipCenter: PathPoint? = null,
|
|
||||||
clipWidth: Float = 0f,
|
|
||||||
clipHeight: Float = 0f,
|
|
||||||
clipCornerSize: Float = 0f,
|
|
||||||
clipRight: Boolean = false,
|
|
||||||
) {
|
|
||||||
var localDrawHeight = min(drawHeight, displayedTrackThickness)
|
|
||||||
var localClipWidth = clipWidth
|
|
||||||
var localClipHeight = clipHeight
|
|
||||||
var localClipCornerSize = clipCornerSize
|
|
||||||
|
|
||||||
drawRect.set(-drawWidth / 2f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
|
||||||
paint.style = Paint.Style.FILL
|
|
||||||
canvas.save()
|
|
||||||
if (clipCenter != null) {
|
|
||||||
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
|
||||||
localClipCornerSize =
|
|
||||||
min(
|
|
||||||
localClipWidth / 2f,
|
|
||||||
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
|
||||||
)
|
|
||||||
if (clipRight) {
|
|
||||||
val leftEdgeDiff =
|
|
||||||
(clipCenter.posVec[0] - localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] - drawCornerSize)
|
|
||||||
if (leftEdgeDiff > 0f) {
|
|
||||||
clipCenter.translate(-leftEdgeDiff / 2f, 0f)
|
|
||||||
localClipWidth += leftEdgeDiff
|
|
||||||
}
|
|
||||||
patchRect.set(0f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
|
||||||
} else {
|
|
||||||
val rightEdgeDiff =
|
|
||||||
(clipCenter.posVec[0] + localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] + drawCornerSize)
|
|
||||||
if (rightEdgeDiff < 0f) {
|
|
||||||
clipCenter.translate(-rightEdgeDiff / 2f, 0f)
|
|
||||||
localClipWidth -= rightEdgeDiff
|
|
||||||
}
|
|
||||||
patchRect.set(-drawWidth / 2f, -localDrawHeight / 2f, 0f, localDrawHeight / 2f)
|
|
||||||
}
|
|
||||||
|
|
||||||
clipRect.set(
|
|
||||||
-localClipWidth / 2f,
|
|
||||||
-localClipHeight / 2f,
|
|
||||||
localClipWidth / 2f,
|
|
||||||
localClipHeight / 2f,
|
|
||||||
)
|
|
||||||
canvas.translate(clipCenter.posVec[0], clipCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
roundedRectPath.reset()
|
|
||||||
roundedRectPath.addRoundRect(
|
|
||||||
clipRect,
|
|
||||||
localClipCornerSize,
|
|
||||||
localClipCornerSize,
|
|
||||||
Path.Direction.CCW,
|
|
||||||
)
|
|
||||||
canvas.clipPath(roundedRectPath)
|
|
||||||
|
|
||||||
canvas.rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
canvas.translate(-clipCenter.posVec[0], -clipCenter.posVec[1])
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRect(patchRect, paint)
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
} else {
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
}
|
|
||||||
canvas.restore()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun vectorToCanvasRotation(vector: FloatArray): Float =
|
|
||||||
Math.toDegrees(atan2(vector[1], vector[0]).toDouble()).toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object WaveMotionUtils {
|
|
||||||
fun resolveWaveTransitionSpring(context: Context, animateOn: Boolean): SpringForce {
|
|
||||||
val attr =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.attr.motionSpringFastEffects
|
|
||||||
} else {
|
|
||||||
MR.attr.motionSpringDefaultEffects
|
|
||||||
}
|
|
||||||
val defaultStyle =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Fast_Effects
|
|
||||||
} else {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Default_Effects
|
|
||||||
}
|
|
||||||
return MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun applyRampEasing(fraction: Float): Float {
|
|
||||||
val clamped = fraction.coerceIn(0f, 1f)
|
|
||||||
return clamped * clamped * (3f - 2f * clamped)
|
|
||||||
}
|
|
||||||
|
|
||||||
private const val WAVE_SMOOTHNESS = 0.48f
|
|
||||||
@@ -22,7 +22,6 @@ import android.content.Context
|
|||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import com.google.android.material.slider.Slider
|
import com.google.android.material.slider.Slider
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import org.oxycblt.auxio.R
|
|
||||||
import org.oxycblt.auxio.databinding.ViewSeekBarBinding
|
import org.oxycblt.auxio.databinding.ViewSeekBarBinding
|
||||||
import org.oxycblt.auxio.playback.formatDurationDs
|
import org.oxycblt.auxio.playback.formatDurationDs
|
||||||
import org.oxycblt.auxio.util.inflater
|
import org.oxycblt.auxio.util.inflater
|
||||||
@@ -49,10 +48,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
|||||||
|
|
||||||
/** Enables/disables wavy active-track rendering to match playback state. */
|
/** Enables/disables wavy active-track rendering to match playback state. */
|
||||||
fun setWaveEnabled(enabled: Boolean) {
|
fun setWaveEnabled(enabled: Boolean) {
|
||||||
val wavelength = context.resources.getDimensionPixelSize(R.dimen.progress_wavelength)
|
binding.seekBarSlider.setWaveEnabled(enabled)
|
||||||
val amplitude = context.resources.getDimensionPixelSize(R.dimen.progress_amplitude)
|
|
||||||
val speed = context.resources.getDimensionPixelSize(R.dimen.progress_wave_speed)
|
|
||||||
binding.seekBarSlider.setWaveEnabled(enabled, wavelength, amplitude, speed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The current [Listener] attached to this instance. */
|
/** The current [Listener] attached to this instance. */
|
||||||
|
|||||||
@@ -12,6 +12,9 @@
|
|||||||
android:layout_marginBottom="@dimen/spacing_medium"
|
android:layout_marginBottom="@dimen/spacing_medium"
|
||||||
app:waveAmplitudeRampProgressMin="0.0"
|
app:waveAmplitudeRampProgressMin="0.0"
|
||||||
app:waveAmplitudeRampProgressMax="0.03"
|
app:waveAmplitudeRampProgressMax="0.03"
|
||||||
|
app:wavelength="@dimen/progress_wavelength"
|
||||||
|
app:waveSpeed="@dimen/progress_wave_speed"
|
||||||
|
app:waveAmplitude="@dimen/progress_amplitude"
|
||||||
android:valueFrom="0"
|
android:valueFrom="0"
|
||||||
android:valueTo="1" />
|
android:valueTo="1" />
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,14 @@
|
|||||||
<attr name="shapeAppearance" />
|
<attr name="shapeAppearance" />
|
||||||
</declare-styleable>
|
</declare-styleable>
|
||||||
|
|
||||||
|
<declare-styleable name="WavySlider">
|
||||||
|
<attr name="wavelength" format="dimension" />
|
||||||
|
<attr name="waveAmplitude" format="dimension" />
|
||||||
|
<attr name="waveSpeed" format="dimension" />
|
||||||
|
<attr name="waveAmplitudeRampProgressMax" format="float" />
|
||||||
|
<attr name="waveAmplitudeRampProgressMin" format="float" />
|
||||||
|
</declare-styleable>
|
||||||
|
|
||||||
<declare-styleable name="IntListPreference">
|
<declare-styleable name="IntListPreference">
|
||||||
<attr name="entries" format="reference" />
|
<attr name="entries" format="reference" />
|
||||||
<attr name="entryValues" format="reference" />
|
<attr name="entryValues" format="reference" />
|
||||||
|
|||||||
Reference in New Issue
Block a user