1 /*
2  * Copyright (C) 2022 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.constraintlayout.core.dsl;
18 
19 import java.util.Arrays;
20 
21 /**
22  * Provides the API for creating a KeyPosition Object for use in the Core
23  * ConstraintLayout & MotionLayout system
24  * This allows multiple KeyPosition positions to defined in one object.
25  */
26 public class KeyPositions extends Keys {
27 
28     private String[] mTarget = null;
29     private String mTransitionEasing = null;
30     private Type mPositionType = null;
31 
32     private int[] mFrames = null;
33     private float[] mPercentWidth = null;
34     private float[] mPercentHeight = null;
35     private float[] mPercentX = null;
36     private float[] mPercentY = null;
37 
38     public enum Type {
39         CARTESIAN,
40         SCREEN,
41         PATH
42     }
43 
KeyPositions(int numOfFrames, String... targets)44     public KeyPositions(int numOfFrames, String... targets) {
45         mTarget = targets;
46         mFrames = new int[numOfFrames];
47         // the default is evenly spaced  1 at 50, 2 at 33 & 66, 3 at 25,50,75
48         float gap = 100f / (mFrames.length + 1);
49         for (int i = 0; i < mFrames.length; i++) {
50             mFrames[i] = (int) (i * gap + gap);
51         }
52     }
53 
getTransitionEasing()54     public String getTransitionEasing() {
55         return mTransitionEasing;
56     }
57 
setTransitionEasing(String transitionEasing)58     public void setTransitionEasing(String transitionEasing) {
59         mTransitionEasing = transitionEasing;
60     }
61 
getFrames()62     public int[] getFrames() {
63         return mFrames;
64     }
65 
setFrames(int... frames)66     public void setFrames(int... frames) {
67         mFrames = frames;
68     }
69 
getPercentWidth()70     public float[] getPercentWidth() {
71         return mPercentWidth;
72     }
73 
setPercentWidth(float... percentWidth)74     public void setPercentWidth(float... percentWidth) {
75         mPercentWidth = percentWidth;
76     }
77 
getPercentHeight()78     public float[] getPercentHeight() {
79         return mPercentHeight;
80     }
81 
setPercentHeight(float... percentHeight)82     public void setPercentHeight(float... percentHeight) {
83         mPercentHeight = percentHeight;
84     }
85 
getPercentX()86     public float[] getPercentX() {
87         return mPercentX;
88     }
89 
setPercentX(float... percentX)90     public void setPercentX(float... percentX) {
91         mPercentX = percentX;
92     }
93 
getPercentY()94     public float[] getPercentY() {
95         return mPercentY;
96     }
97 
setPercentY(float... percentY)98     public void setPercentY(float... percentY) {
99         mPercentY = percentY;
100     }
101 
getPositionType()102     public Type getPositionType() {
103         return mPositionType;
104     }
105 
setPositionType(Type positionType)106     public void setPositionType(Type positionType) {
107         mPositionType = positionType;
108     }
109 
getTarget()110     public String[] getTarget() {
111         return mTarget;
112     }
113 
114     @Override
toString()115     public String toString() {
116         StringBuilder ret = new StringBuilder();
117         ret.append("KeyPositions:{\n");
118 
119         append(ret, "target", mTarget);
120 
121         ret.append("frame:").append(Arrays.toString(mFrames)).append(",\n");
122 
123         if (mPositionType != null) {
124             ret.append("type:'").append(mPositionType).append("',\n");
125         }
126 
127         append(ret, "easing", mTransitionEasing);
128         append(ret, "percentX", mPercentX);
129         append(ret, "percentX", mPercentY);
130         append(ret, "percentWidth", mPercentWidth);
131         append(ret, "percentHeight", mPercentHeight);
132 
133         ret.append("},\n");
134         return ret.toString();
135     }
136 }
137