• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.launcher3.util.rects
18 
19 import android.graphics.Rect
20 import android.view.View
21 import com.android.launcher3.Utilities
22 
23 /**
24  * Linearly interpolate between two rectangles. The result is stored in the rect the function is
25  * called on.
26  *
27  * @param start the starting rectangle
28  * @param end the ending rectangle
29  * @param t the interpolation factor, where 0 is the start and 1 is the end
30  */
Rectnull31 fun Rect.lerpRect(start: Rect, end: Rect, t: Float) {
32     set(
33         Utilities.mapRange(t, start.left.toFloat(), end.left.toFloat()).toInt(),
34         Utilities.mapRange(t, start.top.toFloat(), end.top.toFloat()).toInt(),
35         Utilities.mapRange(t, start.right.toFloat(), end.right.toFloat()).toInt(),
36         Utilities.mapRange(t, start.bottom.toFloat(), end.bottom.toFloat()).toInt(),
37     )
38 }
39 
40 /** Copy the coordinates of the [view] relative to its parent into this rectangle. */
Rectnull41 fun Rect.set(view: View) {
42     set(0, 0, view.width, view.height)
43     offset(view.x.toInt(), view.y.toInt())
44 }
45