Merge pull request #2028 from draxdyn/cardpanel_threading

Fix broken threading in CardPanel
This commit is contained in:
LevelX2 2016-06-28 13:28:37 +02:00 committed by GitHub
commit facd5ffa2c

View file

@ -416,37 +416,19 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
String cardType = getType(newGameCard); String cardType = getType(newGameCard);
tooltipText.setText(getText(cardType, newGameCard)); tooltipText.setText(getText(cardType, newGameCard));
Util.threadPool.submit(new Runnable() { tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0;
@Override flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0;
public void run() {
try { if (!loadImage) {
tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; return;
flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0; }
if (!loadImage) {
return; if (gameCard.isTransformed()) {
} // this calls updateImage
BufferedImage srcImage; toggleTransformed();
if (gameCard.isFaceDown()) { } else {
srcImage = getFaceDownImage(); updateImage();
} else { }
srcImage = ImageCache.getImage(gameCard, getCardWidth(), getCardHeight());
}
if (srcImage != null) {
hasImage = true;
setText(gameCard);
setImage(srcImage);
}
if (gameCard.isTransformed()) {
toggleTransformed();
}
setText(gameCard);
} catch (Exception e) {
LOGGER.fatal("Problem during image animation", e);
} catch (Error err) {
LOGGER.error("Problem during image animation", err);
}
}
});
} }
private void setTypeIcon(BufferedImage bufferedImage, String toolTipText) { private void setTypeIcon(BufferedImage bufferedImage, String toolTipText) {
@ -494,7 +476,10 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
private void setImage(BufferedImage srcImage) { private void setImage(BufferedImage srcImage) {
synchronized (imagePanel) { synchronized (imagePanel) {
imagePanel.setImage(srcImage); if(srcImage != null)
imagePanel.setImage(srcImage);
else
imagePanel.clearImage();
repaint(); repaint();
} }
doLayout(); doLayout();
@ -852,15 +837,21 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
return cardYOffset; return cardYOffset;
} }
private int updateImageStamp;
@Override @Override
public void updateImage() { public void updateImage() {
tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0;
flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0;
final CardView gameCard = this.gameCard;
final int stamp = ++updateImageStamp;
Util.threadPool.submit(new Runnable() { Util.threadPool.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; final BufferedImage srcImage;
flippedAngle = isFlipped() ? CardPanel.FLIPPED_ANGLE : 0;
BufferedImage srcImage;
if (gameCard.isFaceDown()) { if (gameCard.isFaceDown()) {
srcImage = getFaceDownImage(); srcImage = getFaceDownImage();
} else if (cardWidth > THUMBNAIL_SIZE_FULL.width) { } else if (cardWidth > THUMBNAIL_SIZE_FULL.width) {
@ -868,11 +859,16 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
} else { } else {
srcImage = ImageCache.getThumbnail(gameCard); srcImage = ImageCache.getThumbnail(gameCard);
} }
if (srcImage != null) { UI.invokeLater(new Runnable() {
hasImage = true; @Override
setText(gameCard); public void run () {
setImage(srcImage); if(stamp == updateImageStamp) {
} hasImage = srcImage != null;
setText(gameCard);
setImage(srcImage);
}
}
});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} catch (Error err) { } catch (Error err) {