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 KeyAttribute Object for use in the Core
23  * ConstraintLayout & MotionLayout system
24  * This allows multiple KeyAttribute positions to defined in one object.
25  */
26 public class KeyAttributes extends Keys {
27     protected String TYPE = "KeyAttributes";
28     private String[] mTarget = null;
29     private String mTransitionEasing;
30     private Fit mCurveFit = null;
31     private int[] mFrames = null;
32 
33     private Visibility[] mVisibility = null;
34     private float[] mAlpha = null;
35     private float[] mRotation = null;
36     private float[] mRotationX = null;
37     private float[] mRotationY = null;
38     private float[] mPivotX = null;
39     private float[] mPivotY = null;
40     private float[] mTransitionPathRotate = null;
41     private float[] mScaleX = null;
42     private float[] mScaleY = null;
43     private float[] mTranslationX = null;
44     private float[] mTranslationY = null;
45     private float[] mTranslationZ = null;
46 
47     public enum Fit {
48         SPLINE,
49         LINEAR,
50     }
51 
52     public enum Visibility {
53         VISIBLE,
54         INVISIBLE,
55         GONE
56     }
57 
KeyAttributes(int numOfFrames, String... targets)58     KeyAttributes(int numOfFrames, String... targets) {
59         mTarget = targets;
60         mFrames = new int[numOfFrames];
61         // the default is evenly spaced  1 at 50, 2 at 33 & 66, 3 at 25,50,75
62         float gap = 100f / (mFrames.length + 1);
63         for (int i = 0; i < mFrames.length; i++) {
64             mFrames[i] = (int) (i * gap + gap);
65         }
66     }
67 
getTarget()68     public String[] getTarget() {
69         return mTarget;
70     }
71 
setTarget(String[] target)72     public void setTarget(String[] target) {
73         mTarget = target;
74     }
75 
getTransitionEasing()76     public String getTransitionEasing() {
77         return mTransitionEasing;
78     }
79 
setTransitionEasing(String transitionEasing)80     public void setTransitionEasing(String transitionEasing) {
81         mTransitionEasing = transitionEasing;
82     }
83 
getCurveFit()84     public Fit getCurveFit() {
85         return mCurveFit;
86     }
87 
setCurveFit(Fit curveFit)88     public void setCurveFit(Fit curveFit) {
89         mCurveFit = curveFit;
90     }
91 
getVisibility()92     public Visibility[] getVisibility() {
93         return mVisibility;
94     }
95 
setVisibility(Visibility... visibility)96     public void setVisibility(Visibility... visibility) {
97         mVisibility = visibility;
98     }
99 
getAlpha()100     public float[] getAlpha() {
101         return mAlpha;
102     }
103 
setAlpha(float... alpha)104     public void setAlpha(float... alpha) {
105         mAlpha = alpha;
106     }
107 
getRotation()108     public float[] getRotation() {
109         return mRotation;
110     }
111 
setRotation(float... rotation)112     public void setRotation(float... rotation) {
113         mRotation = rotation;
114     }
115 
getRotationX()116     public float[] getRotationX() {
117         return mRotationX;
118     }
119 
setRotationX(float... rotationX)120     public void setRotationX(float... rotationX) {
121         mRotationX = rotationX;
122     }
123 
getRotationY()124     public float[] getRotationY() {
125         return mRotationY;
126     }
127 
setRotationY(float... rotationY)128     public void setRotationY(float... rotationY) {
129         mRotationY = rotationY;
130     }
131 
getPivotX()132     public float[] getPivotX() {
133         return mPivotX;
134     }
135 
setPivotX(float... pivotX)136     public void setPivotX(float... pivotX) {
137         mPivotX = pivotX;
138     }
139 
getPivotY()140     public float[] getPivotY() {
141         return mPivotY;
142     }
143 
setPivotY(float... pivotY)144     public void setPivotY(float... pivotY) {
145         mPivotY = pivotY;
146     }
147 
getTransitionPathRotate()148     public float[] getTransitionPathRotate() {
149         return mTransitionPathRotate;
150     }
151 
setTransitionPathRotate(float... transitionPathRotate)152     public void setTransitionPathRotate(float... transitionPathRotate) {
153         mTransitionPathRotate = transitionPathRotate;
154     }
155 
getScaleX()156     public float[] getScaleX() {
157         return mScaleX;
158     }
159 
setScaleX(float[] scaleX)160     public void setScaleX(float[] scaleX) {
161         mScaleX = scaleX;
162     }
163 
getScaleY()164     public float[] getScaleY() {
165         return mScaleY;
166     }
167 
setScaleY(float[] scaleY)168     public void setScaleY(float[] scaleY) {
169         mScaleY = scaleY;
170     }
171 
getTranslationX()172     public float[] getTranslationX() {
173         return mTranslationX;
174     }
175 
setTranslationX(float[] translationX)176     public void setTranslationX(float[] translationX) {
177         mTranslationX = translationX;
178     }
179 
getTranslationY()180     public float[] getTranslationY() {
181         return mTranslationY;
182     }
183 
setTranslationY(float[] translationY)184     public void setTranslationY(float[] translationY) {
185         mTranslationY = translationY;
186     }
187 
getTranslationZ()188     public float[] getTranslationZ() {
189         return mTranslationZ;
190     }
191 
setTranslationZ(float[] translationZ)192     public void setTranslationZ(float[] translationZ) {
193         mTranslationZ = translationZ;
194     }
195 
196 
197     @Override
toString()198     public String toString() {
199         StringBuilder ret = new StringBuilder();
200         ret.append(TYPE);
201         ret.append(":{\n");
202         attributesToString(ret);
203 
204         ret.append("},\n");
205         return ret.toString();
206     }
207 
attributesToString(StringBuilder builder)208     protected void attributesToString(StringBuilder builder) {
209         append(builder, "target", mTarget);
210         builder.append("frame:").append(Arrays.toString(mFrames)).append(",\n");
211 
212         append(builder, "easing", mTransitionEasing);
213         if (mCurveFit != null) {
214             builder.append("fit:'").append(mCurveFit).append("',\n");
215         }
216         if (mVisibility != null) {
217             builder.append("visibility:'").append(Arrays.toString(mVisibility)).append("',\n");
218         }
219         append(builder, "alpha", mAlpha);
220         append(builder, "rotationX", mRotationX);
221         append(builder, "rotationY", mRotationY);
222         append(builder, "rotationZ", mRotation);
223 
224         append(builder, "pivotX", mPivotX);
225         append(builder, "pivotY", mPivotY);
226         append(builder, "pathRotate", mTransitionPathRotate);
227         append(builder, "scaleX", mScaleX);
228         append(builder, "scaleY", mScaleY);
229         append(builder, "translationX", mTranslationX);
230         append(builder, "translationY", mTranslationY);
231         append(builder, "translationZ", mTranslationZ);
232 
233     }
234 
235 }
236