1/*
2 * Copyright (C) 2016 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
17package com.android.internal.view.animation;
18
19import com.android.layoutlib.bridge.impl.DelegateManager;
20import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21
22import android.util.MathUtils;
23import android.view.animation.AccelerateDecelerateInterpolator;
24import android.view.animation.AccelerateInterpolator;
25import android.view.animation.AnticipateInterpolator;
26import android.view.animation.AnticipateOvershootInterpolator;
27import android.view.animation.BaseInterpolator;
28import android.view.animation.BounceInterpolator;
29import android.view.animation.CycleInterpolator;
30import android.view.animation.DecelerateInterpolator;
31import android.view.animation.Interpolator;
32import android.view.animation.LinearInterpolator;
33import android.view.animation.OvershootInterpolator;
34
35/**
36 * Delegate used to provide new implementation of a select few methods of {@link
37 * NativeInterpolatorFactoryHelper}
38 * <p>
39 * Through the layoutlib_create tool, the original  methods of NativeInterpolatorFactoryHelper have
40 * been replaced by calls to methods of the same name in this delegate class.
41 */
42@SuppressWarnings("unused")
43public class NativeInterpolatorFactoryHelper_Delegate {
44    private static final DelegateManager<Interpolator> sManager = new DelegateManager<>
45            (Interpolator.class);
46
47    public static Interpolator getDelegate(long nativePtr) {
48        return sManager.getDelegate(nativePtr);
49    }
50
51    @LayoutlibDelegate
52    /*package*/ static long createAccelerateDecelerateInterpolator() {
53        return sManager.addNewDelegate(new AccelerateDecelerateInterpolator());
54    }
55
56    @LayoutlibDelegate
57    /*package*/ static long createAccelerateInterpolator(float factor) {
58        return sManager.addNewDelegate(new AccelerateInterpolator(factor));
59    }
60
61    @LayoutlibDelegate
62    /*package*/ static long createAnticipateInterpolator(float tension) {
63        return sManager.addNewDelegate(new AnticipateInterpolator(tension));
64    }
65
66    @LayoutlibDelegate
67    /*package*/ static long createAnticipateOvershootInterpolator(float tension) {
68        return sManager.addNewDelegate(new AnticipateOvershootInterpolator(tension));
69    }
70
71    @LayoutlibDelegate
72    /*package*/ static long createBounceInterpolator() {
73        return sManager.addNewDelegate(new BounceInterpolator());
74    }
75
76    @LayoutlibDelegate
77    /*package*/ static long createCycleInterpolator(float cycles) {
78        return sManager.addNewDelegate(new CycleInterpolator(cycles));
79    }
80
81    @LayoutlibDelegate
82    /*package*/ static long createDecelerateInterpolator(float factor) {
83        return sManager.addNewDelegate(new DecelerateInterpolator(factor));
84    }
85
86    @LayoutlibDelegate
87    /*package*/ static long createLinearInterpolator() {
88        return sManager.addNewDelegate(new LinearInterpolator());
89    }
90
91    @LayoutlibDelegate
92    /*package*/ static long createOvershootInterpolator(float tension) {
93        return sManager.addNewDelegate(new OvershootInterpolator(tension));
94    }
95
96    private static class LutInterpolator extends BaseInterpolator {
97        private final float[] mValues;
98        private final int mSize;
99
100        private LutInterpolator(float[] values) {
101            mValues = values;
102            mSize = mValues.length;
103        }
104
105        @Override
106        public float getInterpolation(float input) {
107            float lutpos = input * mSize;
108            if (lutpos >= (mSize - 1)) {
109                return mValues[mSize - 1];
110            }
111
112            int ipart = (int) lutpos;
113            float weight = lutpos - ipart;
114
115            int i1 = ipart;
116            int i2 = Math.min(i1 + 1, mSize - 1);
117
118            assert i1 >= 0 && i2 >= 0 : "Negatives in the interpolation";
119
120            return MathUtils.lerp(mValues[i1], mValues[i2], weight);
121        }
122    }
123
124    @LayoutlibDelegate
125    /*package*/ static long createLutInterpolator(float[] values) {
126        return sManager.addNewDelegate(new LutInterpolator(values));
127    }
128}
129