1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mechanics.spec 18 19 import com.android.mechanics.spring.SpringParameters 20 21 /** 22 * Key to identify a breakpoint in a [DirectionalMotionSpec]. 23 * 24 * @param debugLabel name of the breakpoint, for tooling and debugging. 25 * @param identity is used to check the equality of two key instances. 26 */ 27 class BreakpointKey(val debugLabel: String? = null, val identity: Any = Object()) { equalsnull28 override fun equals(other: Any?): Boolean { 29 if (this === other) return true 30 if (javaClass != other?.javaClass) return false 31 32 other as BreakpointKey 33 34 return identity == other.identity 35 } 36 hashCodenull37 override fun hashCode(): Int { 38 return identity.hashCode() 39 } 40 toStringnull41 override fun toString(): String { 42 return if (debugLabel != null) "BreakpointKey(label=$debugLabel)" else "BreakpointKey()" 43 } 44 } 45 46 /** 47 * Specification of a breakpoint, in the context of a [DirectionalMotionSpec]. 48 * 49 * The [spring] and [guarantee] define the physics animation for the discontinuity at this 50 * breakpoint.They are applied in the direction of the containing [DirectionalMotionSpec]. 51 * 52 * This [Breakpoint]'s animation definition is valid while the input is within the next segment. If 53 * the animation is still in progress when the input value reaches the next breakpoint, the 54 * remaining animation will be blended with the animation starting at the next breakpoint. 55 * 56 * @param key Identity of the [Breakpoint], unique within a [DirectionalMotionSpec]. 57 * @param position The position of the [Breakpoint], in the domain of the `MotionValue`'s input. 58 * @param spring Parameters of the spring used to animate the breakpoints discontinuity. 59 * @param guarantee Optional constraints to accelerate the completion of the spring motion, based on 60 * `MotionValue`'s input or other non-time signals. 61 */ 62 data class Breakpoint( 63 val key: BreakpointKey, 64 val position: Float, 65 val spring: SpringParameters, 66 val guarantee: Guarantee, 67 ) : Comparable<Breakpoint> { 68 companion object { 69 /** First breakpoint of each spec. */ 70 val minLimit = 71 Breakpoint( 72 BreakpointKey("built-in::min"), 73 Float.NEGATIVE_INFINITY, 74 SpringParameters.Snap, 75 Guarantee.None, 76 ) 77 78 /** Last breakpoint of each spec. */ 79 val maxLimit = 80 Breakpoint( 81 BreakpointKey("built-in::max"), 82 Float.POSITIVE_INFINITY, 83 SpringParameters.Snap, 84 Guarantee.None, 85 ) 86 } 87 compareTonull88 override fun compareTo(other: Breakpoint): Int { 89 return position.compareTo(other.position) 90 } 91 } 92