-
Notifications
You must be signed in to change notification settings - Fork 993
Expand file tree
/
Copy pathFCardPanel.java
More file actions
348 lines (306 loc) · 12 KB
/
Copy pathFCardPanel.java
File metadata and controls
348 lines (306 loc) · 12 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package forge.toolbox;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import forge.Forge;
import forge.Graphics;
import forge.animation.ForgeAnimation;
import forge.card.CardImageRenderer;
import forge.card.CardRenderer;
import forge.card.CardRenderer.CardStackPosition;
import forge.card.CardStateName;
import forge.game.card.CardView;
import forge.game.zone.ZoneType;
import forge.gui.control.PlaybackSpeed;
import forge.screens.match.MatchController;
import forge.util.Utils;
import static forge.assets.FSkin.getDefaultSkinFile;
public class FCardPanel extends FDisplayObject {
public static final float ASPECT_RATIO = 3.5f / 2.5f;
public static final float PADDING = Utils.scale(2);
public static final float TARGET_ORIGIN_FACTOR_X = 0.15f;
public static final float TARGET_ORIGIN_FACTOR_Y = 0.5f;
private CardView card;
private boolean tapped;
private boolean highlighted;
CardTapAnimation tapAnimation;
CardUnTapAnimation untapAnimation;
CardDestroyedAnimation destroyedAnimation;
CardTransformAnimation transformAnimation;
public FCardPanel() {
this(null);
}
public FCardPanel(CardView card0) {
card = card0;
tapAnimation = new CardTapAnimation();
untapAnimation = new CardUnTapAnimation();
destroyedAnimation = new CardDestroyedAnimation();
transformAnimation = new CardTransformAnimation();
}
public CardView getCard() {
return card;
}
public void setCard(CardView card0) {
card = card0;
}
public boolean isHighlighted() {
return highlighted;
}
public void setHighlighted(boolean highlighted0) {
highlighted = highlighted0;
}
public boolean isTapped() {
return tapped;
}
public void setTapped(final boolean tapped0) {
tapped = tapped0;
}
protected float getTappedAngle() {
return -90;
}
protected boolean renderedCardContains(float x, float y) {
float left = PADDING;
float top = PADDING;
float w = getWidth() - 2 * PADDING;
float h = getHeight() - 2 * PADDING;
if (w == h) { //adjust width if needed to make room for tapping
w = h / ASPECT_RATIO;
}
if (tapped) { //rotate box if tapped
top += h - w;
float temp = w;
w = h;
h = temp;
}
return x >= left && x <= left + w && y >= top && y <= top + h;
}
protected float getPadding() {
return PADDING;
}
//allow overriding stack position
protected CardStackPosition getStackPosition() {
return CardStackPosition.Top;
}
@Override
public void draw(Graphics g) {
if (card == null) {
return;
}
boolean animate = Forge.animatedCardTapUntap;
float mod = (isHighlighted() || isHovered()) && !Forge.hasGamepad() ? getWidth() / 16f : 0f;
float padding = getPadding();
float x = padding - mod / 2;
float y = padding - mod / 2;
float w = (getWidth() - 2 * padding) + mod;
float h = (getHeight() - 2 * padding) + mod;
if (w == h) { //adjust width if needed to make room for tapping
w = h / ASPECT_RATIO;
}
float edgeOffset = w / 2f;
if (!ZoneType.Battlefield.equals(card.getZone())) {
rotateTransform(g, x, y, w, h, edgeOffset, false);
return;
}
if (!animate || MatchController.instance.getGameSpeed() == PlaybackSpeed.FAST || (MatchController.instance.getGameView() != null && MatchController.instance.getGameView().isMatchOver())) {
//don't animate if game is fast or match is over
rotateTransform(g, x, y, w, h, edgeOffset, false);
card.updateNeedsTapAnimation(false);
card.updateNeedsUntapAnimation(false);
card.updateNeedsTransformAnimation(false);
} else {
//card destroy animation
if (card.wasDestroyed()) {
if (destroyedAnimation.progress < 1) {
destroyedAnimation.start();
destroyedAnimation.drawCard(g, card, x, y, w, h, edgeOffset);
} else {
rotateTransform(g, x, y, w, h, edgeOffset, animate);
}
return;
}
//tap-untap animation
if (card.needsTapAnimation()) {
//draw tapped
if (tapAnimation.progress < 1) {
tapAnimation.start();
tapAnimation.drawCard(g, card, x, y, w, h, w / 2f, getTappedAngle());
} else {
rotateTransform(g, x, y, w, h, edgeOffset, animate);
}
} else if (card.needsUntapAnimation()) {
//draw untapped
if (untapAnimation.progress < 1) {
untapAnimation.start();
untapAnimation.drawCard(g, card, x, y, w, h, edgeOffset);
} else {
rotateTransform(g, x, y, w, h, edgeOffset, animate);
}
} else {
rotateTransform(g, x, y, w, h, edgeOffset, animate);
}
}
}
private int groupCount;
public int getGroupCount() {
return groupCount;
}
public void setGroupCount(int groupCount0) {
groupCount = groupCount0;
}
private void rotateTransform(Graphics g, float x, float y, float w, float h, float edgeOffset, boolean animate) {
if (tapped) {
g.startRotateTransform(x + edgeOffset, y + h - edgeOffset, getTappedAngle());
}
if (card.needsTransformAnimation() && animate) {
transformAnimation.start();
transformAnimation.drawCard(g, card, x, y, w, h);
} else {
CardRenderer.drawCardWithOverlays(g, card, x, y, w, h, getStackPosition(), groupCount);
if (Forge.hasGamepad() && isHovered())
g.drawRect(3f, Color.LIME, x, y, w, h);
}
if (tapped) {
g.endTransform();
}
}
private class CardDestroyedAnimation extends ForgeAnimation {
private static final float DURATION = 0.6f;
private float progress = 0;
private Texture splatter = Forge.getAssets().getTexture(getDefaultSkinFile("splatter.png"));
private void drawCard(Graphics g, CardView card, float x, float y, float w, float h, float edgeOffset) {
float percentage = progress / DURATION;
if (percentage < 0) {
percentage = 0;
} else if (percentage > 1) {
percentage = 1;
progress = 0;
}
float mod = w * percentage;
float oldAlpha = g.getfloatAlphaComposite();
if (tapped) {
g.startRotateTransform(x + edgeOffset, y + h - edgeOffset, getTappedAngle());
}
CardRenderer.drawCardWithOverlays(g, card, x - mod / 2, y - mod / 2, w + mod, h + mod, getStackPosition());
if (splatter != null) {
g.setAlphaComposite(0.6f);
g.drawCardImage(splatter, null, x - mod / 2, y - mod / 2, w + mod, h + mod, true, false);
g.setAlphaComposite(oldAlpha);
}
if (tapped) {
g.endTransform();
}
}
@Override
protected boolean advance(float dt) {
progress += dt;
return progress < DURATION;
}
@Override
protected void onEnd(boolean endingAll) {
}
}
private class CardTransformAnimation extends ForgeAnimation {
private float DURATION = 0.18f;
private float progress = 0;
private void drawCard(Graphics g, CardView card, float x, float y, float w, float h) {
float percentage = progress / DURATION;
if (percentage < 0) {
percentage = 0;
} else if (percentage > 1) {
percentage = 1;
progress = 0;
}
float mod = percentage;
float y2 = y + (h - (h * mod)) / 2;
float x2 = x + (w - (w * mod)) / 2;
float w2 = w * mod;
float h2 = h * mod;
float gap = (h / 2) - (percentage * (h / 2));
if (card.getCurrentState().getState() == CardStateName.Original) {
DURATION = 0.16f;
//rollback
CardRenderer.drawCardWithOverlays(g, card, x2, y, w2, h, getStackPosition());
} else {
//transform
if (card.getCurrentState().getState() == CardStateName.Backside || card.getCurrentState().getState() == CardStateName.Flipped) {
DURATION = 0.16f;
CardRenderer.drawCardWithOverlays(g, card, x2, y, w2, h, getStackPosition());
} else if (card.getCurrentState().getState() == CardStateName.Meld) {
if (CardRenderer.getMeldCardParts(card.getCurrentState().getImageKey(), false) == CardImageRenderer.forgeArt) {
DURATION = 0.18f;
CardRenderer.drawCardWithOverlays(g, card, x2, y2, w2, h2, getStackPosition());
} else {
//Meld Animation merging
DURATION = 0.25f;
//top card
g.drawImage(CardRenderer.getMeldCardParts(card.getCurrentState().getImageKey(), false), x, y - gap, w, h / 2);
//bottom card
g.drawImage(CardRenderer.getMeldCardParts(card.getCurrentState().getImageKey(), true), x, y + h / 2 + gap, w, h / 2);
}
}
}
}
@Override
protected boolean advance(float dt) {
progress += dt;
return progress < DURATION;
}
@Override
protected void onEnd(boolean endingAll) {
card.updateNeedsTransformAnimation(false);
}
}
private class CardUnTapAnimation extends ForgeAnimation {
private static final float DURATION = 0.18f;
private float progress = 0;
private void drawCard(Graphics g, CardView card, float x, float y, float w, float h, float edgeOffset) {
float percentage = progress / DURATION;
if (percentage < 0) {
percentage = 0;
} else if (percentage > 1) {
percentage = 1;
progress = 0;
}
float angle = -90 + (percentage * 90);
g.startRotateTransform(x + edgeOffset, y + h - edgeOffset, angle);
CardRenderer.drawCardWithOverlays(g, card, x, y, w, h, getStackPosition());
g.endTransform();
}
@Override
protected boolean advance(float dt) {
progress += dt;
return progress < DURATION;
}
@Override
protected void onEnd(boolean endingAll) {
card.updateNeedsUntapAnimation(false);
}
}
private class CardTapAnimation extends ForgeAnimation {
private static final float DURATION = 0.18f;
private float progress = 0;
private void drawCard(Graphics g, CardView card, float x, float y, float w, float h, float edgeOffset, float angle) {
float percentage = progress / DURATION;
if (percentage < 0) {
percentage = 0;
} else if (percentage > 1) {
percentage = 1;
progress = 0;
}
g.startRotateTransform(x + edgeOffset, y + h - edgeOffset, percentage * angle);
CardRenderer.drawCardWithOverlays(g, card, x, y, w, h, getStackPosition());
g.endTransform();
}
@Override
protected boolean advance(float dt) {
progress += dt;
return progress < DURATION;
}
@Override
protected void onEnd(boolean endingAll) {
card.updateNeedsTapAnimation(false);
}
}
public String toString() {
return card == null ? "" : card.toString();
}
}