• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.replica.replicaisland;
18 
19 public class MotionBlurComponent extends GameComponent {
20 	private static final int STEP_COUNT = 4;
21 	private static final float STEP_DELAY = 0.1f;
22 	private static final float OPACITY_STEP = 1.0f / (STEP_COUNT + 1);
23 	private BlurRecord[] mHistory;
24 	private RenderComponent mBlurTarget;
25 	private float mStepDelay;
26 	private int mCurrentStep;
27 	private float mTimeSinceLastStep;
28 	private int mTargetPriority;
29 
30 	private class BlurRecord {
31 		public Vector2 position = new Vector2();
32 		public Texture texture;
33 		public int width;
34 		public int height;
35 		public int[] crop = new int[4];
36 	}
MotionBlurComponent()37 	public MotionBlurComponent() {
38         super();
39         mHistory = new BlurRecord[STEP_COUNT];
40         for (int x = 0; x < STEP_COUNT; x++) {
41         	mHistory[x] = new BlurRecord();
42         }
43         reset();
44         setPhase(ComponentPhases.PRE_DRAW.ordinal());
45     }
46 
47 	@Override
reset()48 	public void reset() {
49 		for (int x = 0; x < STEP_COUNT; x++) {
50 			mHistory[x].texture = null;
51 			mHistory[x].position.zero();
52         }
53 		mStepDelay = STEP_DELAY;
54 		mBlurTarget = null;
55 		mCurrentStep = 0;
56 		mTimeSinceLastStep = 0.0f;
57 	}
58 
setTarget(RenderComponent target)59 	public void setTarget(RenderComponent target) {
60 		mBlurTarget = target;
61 	}
62 
63 	@Override
update(float timeDelta, BaseObject parent)64 	public void update(float timeDelta, BaseObject parent) {
65 		if (mBlurTarget != null) {
66 			mTimeSinceLastStep += timeDelta;
67 			if (mTimeSinceLastStep > mStepDelay) {
68 				DrawableBitmap drawable = (DrawableBitmap)mBlurTarget.getDrawable();
69 				if (drawable != null) {
70 					Texture currentTexture = drawable.getTexture();
71 					mTargetPriority = mBlurTarget.getPriority();
72 					mHistory[mCurrentStep].texture = currentTexture;
73 					mHistory[mCurrentStep].position.set(((GameObject)parent).getPosition());
74 					mHistory[mCurrentStep].width = drawable.getWidth();
75 					mHistory[mCurrentStep].height = drawable.getHeight();
76 					final int[] drawableCrop = drawable.getCrop();
77 					mHistory[mCurrentStep].crop[0] = drawableCrop[0];
78 					mHistory[mCurrentStep].crop[1] = drawableCrop[1];
79 					mHistory[mCurrentStep].crop[2] = drawableCrop[2];
80 					mHistory[mCurrentStep].crop[3] = drawableCrop[3];
81 					mCurrentStep = (mCurrentStep + 1) % STEP_COUNT;
82 					mTimeSinceLastStep = 0.0f;
83 				}
84 			}
85 
86 
87             RenderSystem renderer = sSystemRegistry.renderSystem;
88 
89 
90 			final int startStep = mCurrentStep > 0 ? mCurrentStep - 1 : STEP_COUNT - 1;
91 			// draw each step
92 			for (int x = 0; x < STEP_COUNT; x++) {
93 				final int step = (startStep - x) < 0 ? (STEP_COUNT + (startStep - x)) : (startStep - x);
94 				final BlurRecord record = mHistory[step];
95 				if (record.texture != null) {
96 					DrawableBitmap stepImage = sSystemRegistry.drawableFactory.allocateDrawableBitmap();
97 					stepImage.setTexture(record.texture);
98 					stepImage.setWidth(record.width);
99 					stepImage.setHeight(record.height);
100 					stepImage.setCrop(record.crop[0], record.crop[1], record.crop[2], -record.crop[3]);
101 					final float opacity = (STEP_COUNT - x) * OPACITY_STEP;
102 					stepImage.setOpacity(opacity);
103 
104 
105                     renderer.scheduleForDraw(stepImage, record.position, mTargetPriority - (x + 1), true);
106 				}
107 			}
108 		}
109 	}
110 }
111