1 /*
2  * Copyright (C) 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 package androidx.constraintlayout.core.motion.key;
17 
18 import androidx.constraintlayout.core.motion.CustomVariable;
19 import androidx.constraintlayout.core.motion.utils.SplineSet;
20 import androidx.constraintlayout.core.motion.utils.TypedValues;
21 
22 import java.util.HashMap;
23 import java.util.HashSet;
24 
25 /**
26  * Base class in an element in a KeyFrame
27  *
28  */
29 public abstract class MotionKey implements TypedValues {
30     public static int UNSET = -1;
31     public int mFramePosition = UNSET;
32     int mTargetId = UNSET;
33     String mTargetString = null;
34     public int mType;
35     public HashMap<String, CustomVariable> mCustom;
36 
37     // @TODO: add description
getAttributeNames(HashSet<String> attributes)38     public abstract void getAttributeNames(HashSet<String> attributes);
39 
40     public static final String ALPHA = "alpha";
41     public static final String ELEVATION = "elevation";
42     public static final String ROTATION = "rotationZ";
43     public static final String ROTATION_X = "rotationX";
44 
45     public static final String TRANSITION_PATH_ROTATE = "transitionPathRotate";
46     public static final String SCALE_X = "scaleX";
47     public static final String SCALE_Y = "scaleY";
48 
49 
50     public static final String TRANSLATION_X = "translationX";
51     public static final String TRANSLATION_Y = "translationY";
52 
53     public static final String CUSTOM = "CUSTOM";
54 
55     public static final String VISIBILITY = "visibility";
56 
matches(String constraintTag)57     boolean matches(String constraintTag) {
58         if (mTargetString == null || constraintTag == null) return false;
59         return constraintTag.matches(mTargetString);
60     }
61 
62     /**
63      * Defines method to add a a view to splines derived form this key frame.
64      * The values are written to the spline
65      *
66      * @param splines splines to write values to
67      */
addValues(HashMap<String, SplineSet> splines)68     public abstract void addValues(HashMap<String, SplineSet> splines);
69 
70     /**
71      * Return the float given a value. If the value is a "Float" object it is casted
72      *
73      */
toFloat(Object value)74     float toFloat(Object value) {
75         return (value instanceof Float) ? (Float) value : Float.parseFloat(value.toString());
76     }
77 
78     /**
79      * Return the int version of an object if the value is an Integer object it is casted.
80      *
81      *
82      */
toInt(Object value)83     int toInt(Object value) {
84         return (value instanceof Integer) ? (Integer) value : Integer.parseInt(value.toString());
85     }
86 
87     /**
88      * Return the boolean version this object if the object is a Boolean it is casted.
89      *
90      *
91      */
toBoolean(Object value)92     boolean toBoolean(Object value) {
93         return (value instanceof Boolean)
94                 ? (Boolean) value : Boolean.parseBoolean(value.toString());
95     }
96 
97     /**
98      * Key frame can specify the type of interpolation it wants on various attributes
99      * For each string it set it to -1, CurveFit.LINEAR or  CurveFit.SPLINE
100      */
setInterpolation(HashMap<String, Integer> interpolation)101     public void setInterpolation(HashMap<String, Integer> interpolation) {
102     }
103 
104     // @TODO: add description
copy(MotionKey src)105     public MotionKey copy(MotionKey src) {
106         mFramePosition = src.mFramePosition;
107         mTargetId = src.mTargetId;
108         mTargetString = src.mTargetString;
109         mType = src.mType;
110         return this;
111     }
112 
113     // @TODO: add description
114     @Override
clone()115     public abstract MotionKey clone();
116 
117     // @TODO: add description
setViewId(int id)118     public MotionKey setViewId(int id) {
119         mTargetId = id;
120         return this;
121     }
122 
123     /**
124      * sets the frame position
125      */
setFramePosition(int pos)126     public void setFramePosition(int pos) {
127         mFramePosition = pos;
128     }
129 
130     /**
131      * Gets the current frame position
132      */
getFramePosition()133     public int getFramePosition() {
134         return mFramePosition;
135     }
136 
137     // @TODO: add description
138     @Override
setValue(int type, int value)139     public boolean setValue(int type, int value) {
140 
141         switch (type) {
142             case TypedValues.TYPE_FRAME_POSITION:
143                 mFramePosition = value;
144                 return true;
145         }
146         return false;
147     }
148 
149     // @TODO: add description
150     @Override
setValue(int type, float value)151     public boolean setValue(int type, float value) {
152         return false;
153     }
154 
155     // @TODO: add description
156     @Override
setValue(int type, String value)157     public boolean setValue(int type, String value) {
158         switch (type) {
159             case TypedValues.TYPE_TARGET:
160                 mTargetString = value;
161                 return true;
162         }
163         return false;
164     }
165 
166     // @TODO: add description
167     @Override
setValue(int type, boolean value)168     public boolean setValue(int type, boolean value) {
169         return false;
170     }
171 
172     // @TODO: add description
setCustomAttribute(String name, int type, float value)173     public void setCustomAttribute(String name, int type, float value) {
174         mCustom.put(name, new CustomVariable(name, type, value));
175     }
176 
177     // @TODO: add description
setCustomAttribute(String name, int type, int value)178     public void setCustomAttribute(String name, int type, int value) {
179         mCustom.put(name, new CustomVariable(name, type, value));
180     }
181 
182     // @TODO: add description
setCustomAttribute(String name, int type, boolean value)183     public void setCustomAttribute(String name, int type, boolean value) {
184         mCustom.put(name, new CustomVariable(name, type, value));
185     }
186 
187     // @TODO: add description
setCustomAttribute(String name, int type, String value)188     public void setCustomAttribute(String name, int type, String value) {
189         mCustom.put(name, new CustomVariable(name, type, value));
190     }
191 }
192