1 /*
2  * Copyright (C) 2014 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 com.android.tv.settings.widget;
18 
19 /**
20  * Lerper model tracks target position by adding (target - source) / divisor to source position
21  */
22 public final class Lerper {
23 
24     public static final float DEFAULT_DIVISOR = 2.0f;
25 
26     private float mDivisor = DEFAULT_DIVISOR;
27     private float mMinDelta = 1 / DEFAULT_DIVISOR;
28 
setDivisor(float divisor)29     public void setDivisor(float divisor) {
30         if (divisor < 1f) throw new IllegalArgumentException();
31         mDivisor = divisor;
32         mMinDelta = 1 / divisor;
33     }
34 
getDivisor()35     public float getDivisor() {
36         return mDivisor;
37     }
38 
getMinDelta()39     public float getMinDelta() {
40         return mMinDelta;
41     }
42 
getValue(int currentValue, int targetValue)43     public int getValue(int currentValue, int targetValue) {
44         int delta = targetValue - currentValue;
45         int retValue;
46         if (delta > 0) {
47             // make sure change currentValue and not exceeding targetValue
48             delta = (int)(Math.ceil(delta / mDivisor));
49             if (delta == 0) {
50                 delta = 1;
51             }
52             retValue = currentValue + delta;
53             if (retValue > targetValue) {
54                 retValue = targetValue;
55             }
56         } else if (delta < 0) {
57             // make sure change currentValue and not exceeding targetValue
58             delta = (int)(Math.floor(delta / mDivisor));
59             if (delta == 0) {
60                 delta = -1;
61             }
62             retValue = currentValue + delta;
63             if (retValue < targetValue) {
64                 retValue = targetValue;
65             }
66         } else {
67             retValue = targetValue;
68         }
69         return retValue;
70     }
71 
getValue(float currentValue, float targetValue)72     public float getValue(float currentValue, float targetValue) {
73         float delta = targetValue - currentValue;
74         float retValue;
75         if (delta > mMinDelta) {
76             // make sure change currentValue and not exceeding targetValue
77             delta = delta / mDivisor;
78             retValue = currentValue + delta;
79             if (retValue > targetValue) {
80                 retValue = targetValue;
81             }
82         } else if (delta < -mMinDelta) {
83             // make sure change currentValue and not exceeding targetValue
84             delta = delta / mDivisor;
85             retValue = currentValue + delta;
86             if (retValue < targetValue) {
87                 retValue = targetValue;
88             }
89         } else {
90             retValue = targetValue;
91         }
92         return retValue;
93     }
94 }
95