165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.widget;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Lerper model tracks target position by adding (target - source) / divisor to source position
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic final class Lerper {
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final float DEFAULT_DIVISOR = 2.0f;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private float mDivisor = DEFAULT_DIVISOR;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private float mMinDelta = 1 / DEFAULT_DIVISOR;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void setDivisor(float divisor) {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (divisor < 1f) throw new IllegalArgumentException();
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mDivisor = divisor;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mMinDelta = 1 / divisor;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public float getDivisor() {
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mDivisor;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public float getMinDelta() {
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mMinDelta;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public int getValue(int currentValue, int targetValue) {
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int delta = targetValue - currentValue;
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int retValue;
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (delta > 0) {
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // make sure change currentValue and not exceeding targetValue
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            delta = (int)(Math.ceil(delta / mDivisor));
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (delta == 0) {
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                delta = 1;
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = currentValue + delta;
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (retValue > targetValue) {
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                retValue = targetValue;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (delta < 0) {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // make sure change currentValue and not exceeding targetValue
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            delta = (int)(Math.floor(delta / mDivisor));
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (delta == 0) {
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                delta = -1;
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = currentValue + delta;
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (retValue < targetValue) {
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                retValue = targetValue;
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = targetValue;
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return retValue;
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public float getValue(float currentValue, float targetValue) {
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        float delta = targetValue - currentValue;
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        float retValue;
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (delta > mMinDelta) {
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // make sure change currentValue and not exceeding targetValue
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            delta = delta / mDivisor;
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = currentValue + delta;
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (retValue > targetValue) {
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                retValue = targetValue;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (delta < -mMinDelta) {
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // make sure change currentValue and not exceeding targetValue
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            delta = delta / mDivisor;
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = currentValue + delta;
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (retValue < targetValue) {
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                retValue = targetValue;
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            retValue = targetValue;
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return retValue;
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
95