/
SmoothCamera.java
206 lines (160 loc) · 5.78 KB
/
SmoothCamera.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.andengine.engine.camera;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 22:11:17 - 25.03.2010
*/
public class SmoothCamera extends ZoomCamera {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
protected float mMaxVelocityX;
protected float mMaxVelocityY;
protected float mMaxZoomFactorChange;
protected float mTargetCenterX;
protected float mTargetCenterY;
protected float mTargetZoomFactor;
// ===========================================================
// Constructors
// ===========================================================
public SmoothCamera(final float pX, final float pY, final float pWidth, final float pHeight, final float pMaxVelocityX, final float pMaxVelocityY, final float pMaxZoomFactorChange) {
super(pX, pY, pWidth, pHeight);
this.mMaxVelocityX = pMaxVelocityX;
this.mMaxVelocityY = pMaxVelocityY;
this.mMaxZoomFactorChange = pMaxZoomFactorChange;
this.mTargetCenterX = this.getCenterX();
this.mTargetCenterY = this.getCenterY();
this.mTargetZoomFactor = 1.0f;
}
// ===========================================================
// Getter & Setter
// ===========================================================
public float getTargetCenterX() {
return this.mTargetCenterX;
}
public float getTargetCenterY() {
return this.mTargetCenterY;
}
public float getTargetZoomFactor() {
return this.mTargetZoomFactor;
}
@Override
public void setCenter(final float pCenterX, final float pCenterY) {
this.mTargetCenterX = pCenterX;
this.mTargetCenterY = pCenterY;
}
public void setCenterDirect(final float pCenterX, final float pCenterY) {
super.setCenter(pCenterX, pCenterY);
this.mTargetCenterX = pCenterX;
this.mTargetCenterY = pCenterY;
}
@Override
public void setZoomFactor(final float pZoomFactor) {
if(this.mTargetZoomFactor != pZoomFactor) {
if(this.mTargetZoomFactor == this.mZoomFactor) {
this.mTargetZoomFactor = pZoomFactor;
this.onSmoothZoomStarted();
} else {
this.mTargetZoomFactor = pZoomFactor;
}
}
}
public void setZoomFactorDirect(final float pZoomFactor) {
if(this.mTargetZoomFactor != this.mZoomFactor) {
this.mTargetZoomFactor = pZoomFactor;
super.setZoomFactor(pZoomFactor);
this.onSmoothZoomFinished();
} else {
this.mTargetZoomFactor = pZoomFactor;
super.setZoomFactor(pZoomFactor);
}
}
public float getMaxVelocityX() {
return this.mMaxVelocityX;
}
public void setMaxVelocityX(final float pMaxVelocityX) {
this.mMaxVelocityX = pMaxVelocityX;
}
public float getMaxVelocityY() {
return this.mMaxVelocityY;
}
public void setMaxVelocityY(final float pMaxVelocityY) {
this.mMaxVelocityY = pMaxVelocityY;
}
public void setMaxVelocity(final float pMaxVelocityX, final float pMaxVelocityY) {
this.mMaxVelocityX = pMaxVelocityX;
this.mMaxVelocityY = pMaxVelocityY;
}
public float getMaxZoomFactorChange() {
return this.mMaxZoomFactorChange;
}
public void setMaxZoomFactorChange(final float pMaxZoomFactorChange) {
this.mMaxZoomFactorChange = pMaxZoomFactorChange;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
protected void onSmoothZoomStarted() {
}
protected void onSmoothZoomFinished() {
}
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
/* Update center. */
final float currentCenterX = this.getCenterX();
final float currentCenterY = this.getCenterY();
final float targetCenterX = this.mTargetCenterX;
final float targetCenterY = this.mTargetCenterY;
if(currentCenterX != targetCenterX || currentCenterY != targetCenterY) {
final float diffX = targetCenterX - currentCenterX;
final float dX = this.limitToMaxVelocityX(diffX, pSecondsElapsed);
final float diffY = targetCenterY - currentCenterY;
final float dY = this.limitToMaxVelocityY(diffY, pSecondsElapsed);
super.setCenter(currentCenterX + dX, currentCenterY + dY);
}
/* Update zoom. */
final float currentZoom = this.getZoomFactor();
final float targetZoomFactor = this.mTargetZoomFactor;
if(currentZoom != targetZoomFactor) {
final float absoluteZoomDifference = targetZoomFactor - currentZoom;
final float zoomChange = this.limitToMaxZoomFactorChange(absoluteZoomDifference, pSecondsElapsed);
super.setZoomFactor(currentZoom + zoomChange);
if(this.mZoomFactor == this.mTargetZoomFactor) {
this.onSmoothZoomFinished();
}
}
}
private float limitToMaxVelocityX(final float pValue, final float pSecondsElapsed) {
if(pValue > 0) {
return Math.min(pValue, this.mMaxVelocityX * pSecondsElapsed);
} else {
return Math.max(pValue, -this.mMaxVelocityX * pSecondsElapsed);
}
}
private float limitToMaxVelocityY(final float pValue, final float pSecondsElapsed) {
if(pValue > 0) {
return Math.min(pValue, this.mMaxVelocityY * pSecondsElapsed);
} else {
return Math.max(pValue, -this.mMaxVelocityY * pSecondsElapsed);
}
}
private float limitToMaxZoomFactorChange(final float pValue, final float pSecondsElapsed) {
if(pValue > 0) {
return Math.min(pValue, this.mMaxZoomFactorChange * pSecondsElapsed);
} else {
return Math.max(pValue, -this.mMaxZoomFactorChange * pSecondsElapsed);
}
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}