* Card editor - Done some code cleanup, done some speed up for card loading time.

This commit is contained in:
LevelX2 2013-09-26 17:26:23 +02:00
parent 0caa1d568f
commit 893a3acb65
8 changed files with 277 additions and 173 deletions

View file

@ -74,28 +74,39 @@ public class CardGrid extends javax.swing.JLayeredPane implements MouseListener,
* Max amount of cards in card grid for which card images will be drawn.
* Done so to solve issue with memory for big piles of cards.
*/
private static final int MAX_IMAGES = 250;
public static final int MAX_IMAGES = 300;
public CardGrid() {
initComponents();
setOpaque(false);
}
@Override
public void loadCards(CardsView showCards, SortBy sortBy, boolean piles, BigCard bigCard, UUID gameId) {
this.loadCards(showCards, sortBy, piles, bigCard, gameId, true);
}
@Override
public void loadCards(CardsView showCards, SortBy sortBy, boolean piles, BigCard bigCard, UUID gameId, boolean merge) {
boolean drawImage = showCards.size() < MAX_IMAGES;
this.bigCard = bigCard;
this.gameId = gameId;
for (CardView card: showCards.values()) {
if (!cards.containsKey(card.getId())) {
addCard(card, bigCard, gameId, drawImage);
if (merge) {
for (CardView card: showCards.values()) {
if (!cards.containsKey(card.getId())) {
addCard(card, bigCard, gameId, drawImage);
}
}
}
for (Iterator<Entry<UUID, MageCard>> i = cards.entrySet().iterator(); i.hasNext();) {
Entry<UUID, MageCard> entry = i.next();
if (!showCards.containsKey(entry.getKey())) {
removeCardImg(entry.getKey());
i.remove();
for (Iterator<Entry<UUID, MageCard>> i = cards.entrySet().iterator(); i.hasNext();) {
Entry<UUID, MageCard> entry = i.next();
if (!showCards.containsKey(entry.getKey())) {
removeCardImg(entry.getKey());
i.remove();
}
}
} else {
this.clear();
for (CardView card: showCards.values()) {
addCard(card, bigCard, gameId, drawImage);
}
}
System.gc();
@ -144,8 +155,9 @@ public class CardGrid extends javax.swing.JLayeredPane implements MouseListener,
MageCard lastCard = null;
for (MageCard cardImg: sortedCards) {
if (piles) {
if (lastCard == null)
if (lastCard == null) {
lastCard = cardImg;
}
switch (sortBy) {
case NAME:
if (!cardImg.getOriginal().getName().equals(lastCard.getOriginal().getName())) {
@ -203,6 +215,19 @@ public class CardGrid extends javax.swing.JLayeredPane implements MouseListener,
repaint();
}
private void clear() {
this.cards.clear();
removeAllCardImg();
}
private void removeAllCardImg() {
for (Component comp: getComponents()) {
if (comp instanceof Card || comp instanceof MageCard) {
remove(comp);
}
}
}
private void removeCardImg(UUID cardId) {
for (Component comp: getComponents()) {
if (comp instanceof Card) {
@ -307,6 +332,11 @@ public class CardGrid extends javax.swing.JLayeredPane implements MouseListener,
revalidate();
repaint();
}
@Override
public int cardsSize() {
return cards.size();
}
}
class CardNameComparator implements Comparator<MageCard> {