1 /*
<lambda>null2  * Copyright 2018 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 androidx.compose.ui.graphics
18 
19 import androidx.compose.ui.geometry.Offset
20 import androidx.compose.ui.util.fastAny
21 
22 /** A set of vertex data used by [Canvas.drawVertices]. */
23 class Vertices(
24     val vertexMode: VertexMode,
25     positions: List<Offset>,
26     textureCoordinates: List<Offset>,
27     colors: List<Color>,
28     indices: List<Int>
29 ) /*extends NativeFieldWrapperClass2*/ {
30 
31     val positions: FloatArray
32     val textureCoordinates: FloatArray
33     val colors: IntArray
34     val indices: ShortArray
35 
36     init {
37         if (textureCoordinates.size != positions.size)
38             throwIllegalArgumentException("positions and textureCoordinates lengths must match.")
39         if (colors.size != positions.size)
40             throwIllegalArgumentException("positions and colors lengths must match.")
41         if (indices.fastAny { it < 0 || it >= positions.size })
42             throwIllegalArgumentException(
43                 "indices values must be valid indices " + "in the positions list."
44             )
45 
46         this.positions = encodePointList(positions)
47         this.textureCoordinates = encodePointList(textureCoordinates)
48         this.colors = encodeColorList(colors)
49         this.indices = ShortArray(indices.size) { i -> indices[i].toShort() }
50     }
51 
52     private fun encodeColorList(colors: List<Color>): IntArray {
53         return IntArray(colors.size) { i -> colors[i].toArgb() }
54     }
55 
56     private fun encodePointList(points: List<Offset>): FloatArray {
57         return FloatArray(points.size * 2) { i ->
58             val pointIndex = i / 2
59             val point = points[pointIndex]
60             if (i % 2 == 0) {
61                 point.x
62             } else {
63                 point.y
64             }
65         }
66     }
67 }
68