OSDN Git Service

android-2.1_r1 snapshot
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / FloatUtils.java
1 package com.cooliris.media;
2
3 /**
4  * A static class for some useful operations on Floats and Vectors
5  */
6
7 public class FloatUtils {
8     private static final float ANIMATION_SPEED = 4.0f;
9
10     /**
11      * This function animates a float value to another float value
12      * 
13      * @param prevVal
14      *            : The previous value (or the animated value)
15      * @param targetVal
16      *            : The target value
17      * @param timeElapsed
18      *            Time elapsed since the last time this function was called
19      * @return The new animated value that is closer to the target value and
20      *         clamped to the target value
21      */
22     public static final float animate(float prevVal, float targetVal, float timeElapsed) {
23         timeElapsed = timeElapsed * ANIMATION_SPEED;
24         return animateAfterFactoringSpeed(prevVal, targetVal, timeElapsed);
25     }
26
27     /**
28      * This function animates a Tuple3f value to another Tuple3f value
29      * 
30      * @param animVal
31      *            : The animating Tuple
32      * @param targetVal
33      *            : The target value for the Tuple
34      * @param timeElapsed
35      *            : Time elapsed since the last time this function was called
36      */
37     public static final void animate(Vector3f animVal, Vector3f targetVal, float timeElapsed) {
38         timeElapsed = timeElapsed * ANIMATION_SPEED;
39         animVal.x = animateAfterFactoringSpeed(animVal.x, targetVal.x, timeElapsed);
40         animVal.y = animateAfterFactoringSpeed(animVal.y, targetVal.y, timeElapsed);
41         animVal.z = animateAfterFactoringSpeed(animVal.z, targetVal.z, timeElapsed);
42     }
43
44     /**
45      * Clamp a float to a lower bound
46      * 
47      * @param val
48      *            : the input float value
49      * @param minVal
50      *            : the minimum value to use to clamp
51      * @return the clamped value
52      */
53     public static final float clampMin(float val, float minVal) {
54         if (val < minVal)
55             return minVal; // CR: braces
56         else
57             return val;
58     }
59
60     /**
61      * Clamp a float to an upper bound
62      * 
63      * @param val
64      *            : the input float value
65      * @param maxVal
66      *            : the maximum value to use to clamp
67      * @return the clamped value
68      */
69     public static final float clampMax(float val, float maxVal) {
70         if (val > maxVal)
71             return maxVal;
72         else
73             return val;
74     }
75
76     // CR: these comments are barely useful. they mostly just fill space. If
77     // anything, a one-liner would be sufficient.
78     /**
79      * Clamp a float to a lower and upper bound
80      * 
81      * @param val
82      *            : the input float value
83      * @param minVal
84      *            : the minimum value to use to clamp
85      * @param maxVal
86      *            : the maximum value to use to clamp
87      * @return the clamped value
88      */
89     public static final float clamp(float val, float minVal, float maxVal) {
90         if (val < minVal)
91             return minVal;
92         else if (val > maxVal)
93             return maxVal;
94         else
95             return val;
96     }
97
98     /**
99      * Clamp an integer to a lower and upper bound
100      * 
101      * @param val
102      *            : the input float value
103      * @param minVal
104      *            : the minimum value to use to clamp
105      * @param maxVal
106      *            : the maximum value to use to clamp
107      * @return the clamped value
108      */
109     public static final int clamp(int val, int minVal, int maxVal) {
110         if (val < minVal)
111             return minVal;
112         else if (val > maxVal)
113             return maxVal;
114         else
115             return val;
116     }
117
118     /**
119      * Function to check whether a point lies inside a rectangle
120      * 
121      * @param left
122      *            : the x coordinate of the left most point
123      * @param right
124      *            : the x coordinate of the right most point
125      * @param top
126      *            : the y coordinate of the top most point
127      * @param bottom
128      *            : the y coordinate of the bottom most point
129      * @param posX
130      *            : the input point's x coordinate
131      * @param posY
132      *            : the input point's y coordinate
133      * @return true if point is inside the rectangle else return false
134      */
135     public static final boolean boundsContainsPoint(float left, float right, float top, float bottom, float posX, float posY) {
136         // CR: return ... (one statement).
137         if (posX < left || posX > right || posY < top || posY > bottom)
138             return false;
139         else
140             return true;
141     }
142
143     private static final float animateAfterFactoringSpeed(float prevVal, float targetVal, float timeElapsed) {
144         if (prevVal == targetVal)
145             return targetVal;
146         float newVal = prevVal + ((targetVal - prevVal) * timeElapsed);
147         if (Math.abs(newVal - prevVal) < 0.0001f)
148             return targetVal;
149         if (newVal == prevVal) {
150             return targetVal;
151         } else { // } else if (...) { ... }; no need for a new level of
152                  // indentation.
153             if (prevVal > targetVal && newVal < targetVal) {
154                 return targetVal;
155             } else if (prevVal < targetVal && newVal > targetVal) {
156                 return targetVal;
157             } else {
158                 return newVal;
159             }
160         }
161     }
162
163     public static final float max(float scaleX, float scaleY) {
164         if (scaleX > scaleY)
165             return scaleX;
166         else
167             return scaleY;
168     }
169 }