OSDN Git Service

android-2.1_r1 snapshot
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / CrossFadingTexture.java
1 package com.cooliris.media;
2
3 import javax.microedition.khronos.opengles.GL11;
4
5 import android.util.Log;
6
7 public class CrossFadingTexture {
8     private static final String TAG = "CrossFadingTexture";
9
10     private Texture mTexture;
11     private Texture mFadingTexture;
12     private float mMixRatio = 0.0f;
13     private float mAnimatedMixRatio = 0.0f;
14     private boolean mBindUsingMixed = false;
15     private boolean mBind = false;
16     private boolean mFadeNecessary = false;
17
18     public CrossFadingTexture() {
19     }
20
21     public CrossFadingTexture(Texture initialTexture) {
22         mMixRatio = 1.0f;
23         mAnimatedMixRatio = 1.0f;
24         mFadeNecessary = false;
25         mTexture = initialTexture;
26         mFadingTexture = initialTexture;
27     }
28
29     public void clear() {
30         mTexture = null;
31         mFadingTexture = null;
32     }
33
34     public CrossFadingTexture(Texture source, Texture destination) {
35         mFadingTexture = source;
36         mTexture = destination;
37         mMixRatio = 1.0f;
38         mAnimatedMixRatio = 0.0f;
39         Log.i(TAG, "Creating crossfading texture");
40     }
41
42     public Texture getTexture() {
43         return mTexture;
44     }
45
46     public void setTexture(Texture texture) {
47         if (mTexture == texture || texture == null || mAnimatedMixRatio < 1.0f) {
48             return;
49         }
50         mFadeNecessary = false;
51         if (mFadingTexture == null) {
52             mFadeNecessary = true;
53         }
54         if (mTexture != null) {
55             mFadingTexture = mTexture;
56         } else {
57             mFadingTexture = texture;
58         }
59         mTexture = texture;
60         mAnimatedMixRatio = 0.0f;
61         mMixRatio = 1.0f;
62     }
63
64     public void setTextureImmediate(Texture texture) {
65         if (texture == null || texture.isLoaded() == false || mTexture == texture) {
66             return;
67         }
68         if (mTexture != null) {
69             mFadingTexture = mTexture;
70         }
71         mTexture = texture;
72         mMixRatio = 1.0f;
73     }
74
75     public boolean update(float timeElapsed) {
76         if (mTexture != null && mFadingTexture != null && mTexture.isLoaded() && mFadingTexture.isLoaded()) {
77             mAnimatedMixRatio = FloatUtils.animate(mAnimatedMixRatio, mMixRatio, timeElapsed * 0.5f);
78             return (mMixRatio != mAnimatedMixRatio);
79         } else {
80             mAnimatedMixRatio = 0.0f;
81             return false;
82         }
83     }
84
85     public boolean bind(RenderView view, GL11 gl) {
86         if (mBind) {
87             return true; // Already bound.
88         }
89         if (mFadingTexture != null && mFadingTexture.mState == Texture.STATE_ERROR) {
90             mFadingTexture = null;
91         }
92         if (mTexture != null && mTexture.mState == Texture.STATE_ERROR) {
93             mTexture = null;
94         }
95         mBindUsingMixed = false;
96         boolean fadingTextureLoaded = false;
97         boolean textureLoaded = false;
98         if (mFadingTexture != null) {
99             fadingTextureLoaded = view.bind(mFadingTexture);
100         }
101         if (mTexture != null) {
102             view.bind(mTexture);
103             textureLoaded = mTexture.isLoaded();
104         }
105         if (mFadeNecessary) {
106             if (view.getAlpha() > mAnimatedMixRatio) {
107                 view.setAlpha(mAnimatedMixRatio);
108             }
109             if (mAnimatedMixRatio == 1.0f) {
110                 mFadeNecessary = false;
111             }
112         }
113         if (textureLoaded == false && fadingTextureLoaded == false) {
114             return false;
115         }
116         mBind = true;
117         if (mAnimatedMixRatio <= 0.0f && fadingTextureLoaded) {
118             view.bind(mFadingTexture);
119         } else if (mAnimatedMixRatio >= 1.0f || !fadingTextureLoaded || view.getAlpha() < mAnimatedMixRatio
120                 || mFadingTexture == mTexture) {
121             view.bind(mTexture);
122         } else {
123             mBindUsingMixed = true;
124             view.bindMixed(mFadingTexture, mTexture, mAnimatedMixRatio);
125         }
126         return true;
127     }
128
129     public void unbind(RenderView view, GL11 gl) {
130         if (mBindUsingMixed && mBind) {
131             view.unbindMixed();
132             mBindUsingMixed = false;
133         }
134         mBind = false;
135     }
136 }