mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 03:08:02 +00:00
[Desktop] Simplify a bunch of CardPanel image updating code.
This commit is contained in:
@@ -242,7 +242,6 @@ public class CardArea extends CardPanelContainer implements CardPanelMouseListen
|
|||||||
dragPanel.setDisplayEnabled(false);
|
dragPanel.setDisplayEnabled(false);
|
||||||
|
|
||||||
CardPanel.setDragAnimationPanel(new CardPanel(dragPanel.getMatchUI(), dragPanel.getCard()));
|
CardPanel.setDragAnimationPanel(new CardPanel(dragPanel.getMatchUI(), dragPanel.getCard()));
|
||||||
CardPanel.getDragAnimationPanel().setImage(dragPanel);
|
|
||||||
final JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
|
final JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
|
||||||
final JLayeredPane layeredPane = frame.getLayeredPane();
|
final JLayeredPane layeredPane = frame.getLayeredPane();
|
||||||
layeredPane.add(CardPanel.getDragAnimationPanel());
|
layeredPane.add(CardPanel.getDragAnimationPanel());
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ public class CardPanel extends SkinnedPanel implements CardContainer, IDisposabl
|
|||||||
private OutlinedLabel ptText;
|
private OutlinedLabel ptText;
|
||||||
private OutlinedLabel damageText;
|
private OutlinedLabel damageText;
|
||||||
private OutlinedLabel cardIdText;
|
private OutlinedLabel cardIdText;
|
||||||
private final List<CardPanel> imageLoadListeners = new ArrayList<CardPanel>(2);
|
|
||||||
private boolean displayEnabled = true;
|
private boolean displayEnabled = true;
|
||||||
private boolean isAnimationPanel;
|
private boolean isAnimationPanel;
|
||||||
private int cardXOffset, cardYOffset, cardWidth, cardHeight;
|
private int cardXOffset, cardYOffset, cardWidth, cardHeight;
|
||||||
@@ -162,50 +161,27 @@ public class CardPanel extends SkinnedPanel implements CardContainer, IDisposabl
|
|||||||
|
|
||||||
if (card == null) {
|
if (card == null) {
|
||||||
cachedImage = null;
|
cachedImage = null;
|
||||||
setImage((BufferedImage)null);
|
setImage(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedImage = new CachedCardImage(card, matchUI.getLocalPlayers(), imagePanel.getWidth(), imagePanel.getHeight()) {
|
cachedImage = new CachedCardImage(card, matchUI.getLocalPlayers(), imagePanel.getWidth(), imagePanel.getHeight()) {
|
||||||
@Override
|
@Override
|
||||||
public void onImageFetched() {
|
public void onImageFetched() {
|
||||||
if (cachedImage != null)
|
if (cachedImage != null) {
|
||||||
setImage(cachedImage.getImage());
|
setImage(cachedImage.getImage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
setImage(cachedImage.getImage());
|
setImage(cachedImage.getImage());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setImage(final BufferedImage srcImage) {
|
private void setImage(final BufferedImage srcImage) {
|
||||||
if (imagePanel == null) {
|
if (imagePanel == null || imagePanel.getSrcImage() == srcImage) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
synchronized (imagePanel) {
|
imagePanel.setImage(srcImage);
|
||||||
if (imagePanel.getSrcImage() == srcImage) {
|
repaint();
|
||||||
return;
|
|
||||||
}
|
|
||||||
imagePanel.setImage(srcImage);
|
|
||||||
repaint();
|
|
||||||
for (final CardPanel cardPanel : imageLoadListeners) {
|
|
||||||
cardPanel.setImage(srcImage);
|
|
||||||
cardPanel.repaint();
|
|
||||||
}
|
|
||||||
imageLoadListeners.clear();
|
|
||||||
}
|
|
||||||
doLayout();
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setImage(final CardPanel panel) {
|
|
||||||
if (panel == this) {
|
|
||||||
throw new IllegalArgumentException("Can't pass 'this' as argument to CardPanel#setImage");
|
|
||||||
}
|
|
||||||
synchronized (panel.imagePanel) {
|
|
||||||
if (panel.imagePanel.hasImage()) {
|
|
||||||
setImage(panel.imagePanel.getSrcImage());
|
|
||||||
} else {
|
|
||||||
panel.imageLoadListeners.add(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final boolean isDisplayEnabled() {
|
public final boolean isDisplayEnabled() {
|
||||||
|
|||||||
@@ -214,99 +214,6 @@ public abstract class Animation {
|
|||||||
}.run();
|
}.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Animate a {@link CardPanel} moving.
|
|
||||||
*
|
|
||||||
* @param startX
|
|
||||||
* a int.
|
|
||||||
* @param startY
|
|
||||||
* a int.
|
|
||||||
* @param startWidth
|
|
||||||
* a int.
|
|
||||||
* @param endX
|
|
||||||
* a int.
|
|
||||||
* @param endY
|
|
||||||
* a int.
|
|
||||||
* @param endWidth
|
|
||||||
* a int.
|
|
||||||
* @param animationPanel
|
|
||||||
* a {@link forge.view.arcane.CardPanel} object.
|
|
||||||
* @param placeholder
|
|
||||||
* a {@link forge.view.arcane.CardPanel} object.
|
|
||||||
* @param layeredPane
|
|
||||||
* a {@link javax.swing.JLayeredPane} object.
|
|
||||||
* @param speed
|
|
||||||
* a int.
|
|
||||||
*/
|
|
||||||
public static void moveCardToField(final int startX, final int startY, final int startWidth, final int endX,
|
|
||||||
final int endY, final int endWidth, final CardPanel animationPanel, final CardPanel placeholder,
|
|
||||||
final JLayeredPane layeredPane, final int speed) {
|
|
||||||
Animation.invokeLater(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
final int startHeight = Math.round(startWidth * CardPanel.ASPECT_RATIO);
|
|
||||||
final int endHeight = Math.round(endWidth * CardPanel.ASPECT_RATIO);
|
|
||||||
final float a = 2f;
|
|
||||||
final float sqrta = (float) Math.sqrt(1 / a);
|
|
||||||
|
|
||||||
animationPanel.setCardBounds(startX, startY, startWidth, startHeight);
|
|
||||||
animationPanel.setAnimationPanel(true);
|
|
||||||
Container parent = animationPanel.getParent();
|
|
||||||
if (parent != layeredPane) {
|
|
||||||
layeredPane.add(animationPanel);
|
|
||||||
layeredPane.setLayer(animationPanel, JLayeredPane.MODAL_LAYER.intValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
new Animation(700) {
|
|
||||||
@Override
|
|
||||||
protected void update(float percentage) {
|
|
||||||
if (placeholder != null && !placeholder.isShowing()) {
|
|
||||||
cancel();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int currentX = startX + Math.round((endX - startX + endWidth / 2f) * percentage);
|
|
||||||
int currentY = startY + Math.round((endY - startY + endHeight / 2f) * percentage);
|
|
||||||
int currentWidth, currentHeight;
|
|
||||||
int midWidth = Math.max(200, endWidth * 2);
|
|
||||||
int midHeight = Math.round(midWidth * CardPanel.ASPECT_RATIO);
|
|
||||||
if (percentage <= 0.5f) {
|
|
||||||
percentage = percentage * 2;
|
|
||||||
float pp = sqrta * (1 - percentage);
|
|
||||||
percentage = 1 - a * pp * pp;
|
|
||||||
currentWidth = startWidth + Math.round((midWidth - startWidth) * percentage);
|
|
||||||
currentHeight = startHeight + Math.round((midHeight - startHeight) * percentage);
|
|
||||||
} else {
|
|
||||||
percentage = (percentage - 0.5f) * 2;
|
|
||||||
float pp = sqrta * percentage;
|
|
||||||
percentage = a * pp * pp;
|
|
||||||
currentWidth = midWidth + Math.round((endWidth - midWidth) * percentage);
|
|
||||||
currentHeight = midHeight + Math.round((endHeight - midHeight) * percentage);
|
|
||||||
}
|
|
||||||
currentX -= Math.round(currentWidth / 2.0);
|
|
||||||
currentY -= Math.round(currentHeight / 2.0);
|
|
||||||
animationPanel.setCardBounds(currentX, currentY, currentWidth, currentHeight);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onEnd() {
|
|
||||||
EventQueue.invokeLater(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (placeholder != null) {
|
|
||||||
placeholder.setDisplayEnabled(true);
|
|
||||||
placeholder.setImage(animationPanel);
|
|
||||||
}
|
|
||||||
animationPanel.setVisible(false);
|
|
||||||
animationPanel.repaint();
|
|
||||||
layeredPane.remove(animationPanel);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}.run();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Animate a {@link CardPanel} moving.
|
* Animate a {@link CardPanel} moving.
|
||||||
*
|
*
|
||||||
@@ -365,7 +272,6 @@ public abstract class Animation {
|
|||||||
public void run() {
|
public void run() {
|
||||||
if (placeholder != null) {
|
if (placeholder != null) {
|
||||||
placeholder.setDisplayEnabled(true);
|
placeholder.setDisplayEnabled(true);
|
||||||
// placeholder.setImage(animationPanel);
|
|
||||||
placeholder.setCard(placeholder.getCard());
|
placeholder.setCard(placeholder.getCard());
|
||||||
}
|
}
|
||||||
animationPanel.setVisible(false);
|
animationPanel.setVisible(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user