From 0d3223ff0ea5d45dfe1652f5177fac92e4390687 Mon Sep 17 00:00:00 2001 From: Plopman Date: Sun, 22 Jun 2014 12:46:06 +0200 Subject: [PATCH 1/2] Deck editor. Simplify code --- .../java/mage/client/cards/CardsList.java | 114 ++++++++---------- .../client/util/CardViewColorComparator.java | 8 +- .../util/CardViewColorDetailedComparator.java | 94 +++++++-------- .../client/util/CardViewCostComparator.java | 8 +- .../client/util/CardViewRarityComparator.java | 8 +- 5 files changed, 97 insertions(+), 135 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/cards/CardsList.java b/Mage.Client/src/main/java/mage/client/cards/CardsList.java index 4713e383db..0a564bba41 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardsList.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardsList.java @@ -44,6 +44,7 @@ import java.awt.event.MouseListener; import java.beans.Beans; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.UUID; import javax.swing.DefaultComboBoxModel; @@ -276,11 +277,7 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar int numColumns = maxWidth / Config.dimensions.frameWidth; int curColumn = 0; int curRow = 0; - int landCount = 0; - int creatureCount = 0; - int sorceryCount = 0; - int instantCount = 0; - int enchantmentCount = 0; + Comparator comparator = null; //FIXME: why we remove all cards? for performance it's better to merge changes // as it is already done in ListView cardArea.removeAll(); @@ -289,58 +286,36 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar List sortedCards = new ArrayList<>(cards.values()); switch (sortSetting.getSortBy()) { case NAME: - Collections.sort(sortedCards, new CardViewNameComparator()); + comparator = new CardViewNameComparator(); break; case RARITY: - Collections.sort(sortedCards, new CardViewRarityComparator()); + comparator = new CardViewRarityComparator(); break; case COLOR: - Collections.sort(sortedCards, new CardViewColorComparator()); + comparator = new CardViewColorComparator(); break; case COLOR_DETAILED: - Collections.sort(sortedCards, new CardViewColorDetailedComparator()); + comparator = new CardViewColorDetailedComparator(); break; case CASTING_COST: - Collections.sort(sortedCards, new CardViewCostComparator()); + comparator = new CardViewCostComparator(); break; } + if(comparator != null){ + Collections.sort(sortedCards, new CardViewNameComparator()); + Collections.sort(sortedCards, comparator); + } CardView lastCard = null; for (CardView card: sortedCards) { if (sortSetting.isPilesToggle()) { if (lastCard == null) { lastCard = card; } - switch (sortSetting.getSortBy()) { - case NAME: - if (!card.getName().equals(lastCard.getName())) { - curColumn++; - curRow = 0; - } - break; - case RARITY: - if (!card.getRarity().equals(lastCard.getRarity())) { - curColumn++; - curRow = 0; - } - break; - case COLOR: - if (card.getColor().compareTo(lastCard.getColor()) != 0) { - curColumn++; - curRow = 0; - } - break; - case COLOR_DETAILED: - if (card.getColor().hashCode() != lastCard.getColor().hashCode()) { - curColumn++; - curRow = 0; - } - break; - case CASTING_COST: - if (card.getConvertedManaCost() != lastCard.getConvertedManaCost()) { - curColumn++; - curRow = 0; - } - break; + if(comparator != null){ + if(comparator.compare(card, lastCard) > 0){ + curColumn++; + curRow = 0; + } } rectangle.setLocation(curColumn * Config.dimensions.frameWidth, curRow * 20); addCard(card, bigCard, gameId, rectangle); @@ -355,23 +330,40 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar curRow++; } } - if (card.getCardTypes().contains(CardType.LAND)) { - landCount++; - } - if (card.getCardTypes().contains(CardType.CREATURE)) { - creatureCount++; - } - if (card.getCardTypes().contains(CardType.SORCERY)) { - sorceryCount++; - } - if (card.getCardTypes().contains(CardType.INSTANT)) { - instantCount++; - } - if (card.getCardTypes().contains(CardType.ENCHANTMENT)) { - enchantmentCount++; - } } } + updateCounts(); + cardArea.setPreferredSize(new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight + 200)); + cardArea.revalidate(); + this.revalidate(); + this.repaint(); + this.setVisible(true); + } + + private void updateCounts(){ + int landCount = 0; + int creatureCount = 0; + int sorceryCount = 0; + int instantCount = 0; + int enchantmentCount = 0; + for (CardView card: cards.values()) { + if (card.getCardTypes().contains(CardType.LAND)) { + landCount++; + } + if (card.getCardTypes().contains(CardType.CREATURE)) { + creatureCount++; + } + if (card.getCardTypes().contains(CardType.SORCERY)) { + sorceryCount++; + } + if (card.getCardTypes().contains(CardType.INSTANT)) { + instantCount++; + } + if (card.getCardTypes().contains(CardType.ENCHANTMENT)) { + enchantmentCount++; + } + } + int count = cards != null ? cards.size() : 0; this.lblCount.setText(Integer.toString(count)); this.lblCreatureCount.setText(Integer.toString(creatureCount)); @@ -379,11 +371,6 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar this.lblSorceryCount.setText(Integer.toString(sorceryCount)); this.lblInstantCount.setText(Integer.toString(instantCount)); this.lblEnchantmentCount.setText(Integer.toString(enchantmentCount)); - cardArea.setPreferredSize(new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight + 200)); - cardArea.revalidate(); - this.revalidate(); - this.repaint(); - this.setVisible(true); } private void addCard(CardView card, BigCard bigCard, UUID gameId, Rectangle rectangle) { @@ -437,7 +424,6 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { - java.awt.GridBagConstraints gridBagConstraints; bgView = new javax.swing.ButtonGroup(); panelControl = new javax.swing.JPanel(); @@ -615,7 +601,7 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar .addComponent(lblLandCount) .addComponent(lblCreatureCount) .addComponent(lblSorceryCount) - .addComponent(lblInstantCount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(lblInstantCount) .addComponent(lblEnchantmentCount) .addComponent(chkPiles)) .addComponent(cbSortBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) @@ -636,7 +622,7 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar .addGroup(layout.createSequentialGroup() .addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(panelControl, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE) + .addComponent(panelControl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panelCardArea))) ); layout.setVerticalGroup( diff --git a/Mage.Client/src/main/java/mage/client/util/CardViewColorComparator.java b/Mage.Client/src/main/java/mage/client/util/CardViewColorComparator.java index 876ec5a4f9..fe41208edc 100644 --- a/Mage.Client/src/main/java/mage/client/util/CardViewColorComparator.java +++ b/Mage.Client/src/main/java/mage/client/util/CardViewColorComparator.java @@ -39,13 +39,7 @@ public class CardViewColorComparator implements Comparator { @Override public int compare(CardView o1, CardView o2) { - int val = o1.getColor().compareTo(o2.getColor()); - if (val == 0) { - return o1.getName().compareTo(o2.getName()); - } - else { - return val; - } + return o1.getColor().compareTo(o2.getColor()); } } \ No newline at end of file diff --git a/Mage.Client/src/main/java/mage/client/util/CardViewColorDetailedComparator.java b/Mage.Client/src/main/java/mage/client/util/CardViewColorDetailedComparator.java index 6e979f7c81..179254b961 100644 --- a/Mage.Client/src/main/java/mage/client/util/CardViewColorDetailedComparator.java +++ b/Mage.Client/src/main/java/mage/client/util/CardViewColorDetailedComparator.java @@ -1,51 +1,45 @@ -/* - * Copyright 2011 BetaSteward_at_googlemail.com. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of BetaSteward_at_googlemail.com. - */ - -package mage.client.util; - -import java.util.Comparator; -import mage.view.CardView; - -/** - * - * @author BetaSteward_at_googlemail.com - */ -public class CardViewColorDetailedComparator implements Comparator { - - @Override - public int compare(CardView o1, CardView o2) { - int val = o1.getColor().hashCode() - o2.getColor().hashCode(); - if (val == 0) { - return o1.getName().compareTo(o2.getName()); - } - else { - return val; - } - } - +/* + * Copyright 2011 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ + +package mage.client.util; + +import java.util.Comparator; +import mage.view.CardView; + +/** + * + * @author BetaSteward_at_googlemail.com + */ +public class CardViewColorDetailedComparator implements Comparator { + + @Override + public int compare(CardView o1, CardView o2) { + return o1.getColor().hashCode() - o2.getColor().hashCode(); + } + } \ No newline at end of file diff --git a/Mage.Client/src/main/java/mage/client/util/CardViewCostComparator.java b/Mage.Client/src/main/java/mage/client/util/CardViewCostComparator.java index fcd3b4c0ce..22eae462b0 100644 --- a/Mage.Client/src/main/java/mage/client/util/CardViewCostComparator.java +++ b/Mage.Client/src/main/java/mage/client/util/CardViewCostComparator.java @@ -39,13 +39,7 @@ public class CardViewCostComparator implements Comparator { @Override public int compare(CardView o1, CardView o2) { - int val = Integer.valueOf(o1.getConvertedManaCost()).compareTo(Integer.valueOf(o2.getConvertedManaCost())); - if (val == 0) { - return o1.getName().compareTo(o2.getName()); - } - else { - return val; - } + return Integer.valueOf(o1.getConvertedManaCost()).compareTo(Integer.valueOf(o2.getConvertedManaCost())); } } diff --git a/Mage.Client/src/main/java/mage/client/util/CardViewRarityComparator.java b/Mage.Client/src/main/java/mage/client/util/CardViewRarityComparator.java index 5389d89778..f8cd723f10 100644 --- a/Mage.Client/src/main/java/mage/client/util/CardViewRarityComparator.java +++ b/Mage.Client/src/main/java/mage/client/util/CardViewRarityComparator.java @@ -39,13 +39,7 @@ public class CardViewRarityComparator implements Comparator { @Override public int compare(CardView o1, CardView o2) { - int val = o1.getRarity().compareTo(o2.getRarity()); - if (val == 0) { - return o1.getName().compareTo(o2.getName()); - } - else { - return val; - } + return o1.getRarity().compareTo(o2.getRarity()); } } \ No newline at end of file From 64b0da7aab5fee46e750c5caee79ecb178640693 Mon Sep 17 00:00:00 2001 From: Plopman Date: Sun, 22 Jun 2014 14:28:04 +0200 Subject: [PATCH 2/2] Deck editor. Don't reload all cards every times. Fix scrolling issue #439 --- .../java/mage/client/cards/CardsList.form | 16 +++--- .../java/mage/client/cards/CardsList.java | 51 +++++++++++++++---- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/cards/CardsList.form b/Mage.Client/src/main/java/mage/client/cards/CardsList.form index 54af84ac6d..eaf7cf9a88 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardsList.form +++ b/Mage.Client/src/main/java/mage/client/cards/CardsList.form @@ -37,7 +37,7 @@ - + @@ -105,7 +105,7 @@ - + @@ -127,7 +127,7 @@ - + @@ -145,7 +145,7 @@ - + @@ -163,7 +163,7 @@ - + @@ -181,7 +181,7 @@ - + @@ -199,7 +199,7 @@ - + @@ -217,7 +217,7 @@ - + diff --git a/Mage.Client/src/main/java/mage/client/cards/CardsList.java b/Mage.Client/src/main/java/mage/client/cards/CardsList.java index 0a564bba41..f983fd866e 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardsList.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardsList.java @@ -45,7 +45,9 @@ import java.beans.Beans; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import javax.swing.DefaultComboBoxModel; import javax.swing.JTable; @@ -87,6 +89,7 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar protected CardEventSource cardEventSource = new CardEventSource(); private Dimension cardDimension; private CardsView cards; + private Map mageCards = new LinkedHashMap<>(); protected BigCard bigCard; protected UUID gameId; private SortSetting sortSetting; @@ -124,6 +127,7 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar ((CardPanel)comp).cleanUp(); } } + mageCards.clear(); cardArea.removeAll(); this.bigCard = null; @@ -277,10 +281,27 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar int numColumns = maxWidth / Config.dimensions.frameWidth; int curColumn = 0; int curRow = 0; + int maxRow = 0; + int maxColumn = 0; Comparator comparator = null; - //FIXME: why we remove all cards? for performance it's better to merge changes - // as it is already done in ListView - cardArea.removeAll(); + Map oldMageCards = mageCards; + mageCards = new LinkedHashMap<>(); + + //Find card view + for(UUID uuid : cards.keySet()){ + if(oldMageCards.containsKey(uuid)){ + mageCards.put(uuid, oldMageCards.get(uuid)); + oldMageCards.remove(uuid); + } + else{ + mageCards.put(uuid, addCard(cards.get(uuid), bigCard, gameId)); + } + } + //Remove unused cards + for(MageCard card : oldMageCards.values()){ + cardArea.remove(card); + } + if (cards != null && cards.size() > 0) { Rectangle rectangle = new Rectangle(Config.dimensions.frameWidth, Config.dimensions.frameHeight); List sortedCards = new ArrayList<>(cards.values()); @@ -314,26 +335,31 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar if(comparator != null){ if(comparator.compare(card, lastCard) > 0){ curColumn++; + maxRow = Math.max(maxRow, curRow); curRow = 0; } } rectangle.setLocation(curColumn * Config.dimensions.frameWidth, curRow * 20); - addCard(card, bigCard, gameId, rectangle); + setCardBounds(mageCards.get(card.getId()), rectangle); + curRow++; lastCard = card; } else { rectangle.setLocation(curColumn * Config.dimensions.frameWidth, curRow * 20); - addCard(card, bigCard, gameId, rectangle); + setCardBounds(mageCards.get(card.getId()), rectangle); curColumn++; if (curColumn == numColumns) { + maxColumn = Math.max(maxColumn, curColumn); curColumn = 0; curRow++; } } } } + maxRow = Math.max(maxRow, curRow); + maxColumn = Math.max(maxColumn, curColumn); updateCounts(); - cardArea.setPreferredSize(new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight + 200)); + cardArea.setPreferredSize(new Dimension((maxColumn+1) * Config.dimensions.frameWidth, Config.dimensions.frameHeight + maxRow*20)); cardArea.revalidate(); this.revalidate(); this.repaint(); @@ -373,17 +399,22 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar this.lblEnchantmentCount.setText(Integer.toString(enchantmentCount)); } - private void addCard(CardView card, BigCard bigCard, UUID gameId, Rectangle rectangle) { + private MageCard addCard(CardView card, BigCard bigCard, UUID gameId) { if (cardDimension == null) { cardDimension = new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight); } MageCard cardImg = Plugins.getInstance().getMageCard(card, bigCard, cardDimension, gameId, true); - cardImg.setBounds(rectangle); cardArea.add(cardImg); - cardArea.moveToFront(cardImg); cardImg.update(card); cardImg.addMouseListener(this); - cardImg.setCardBounds(rectangle.x, rectangle.y, Config.dimensions.frameWidth, Config.dimensions.frameHeight); + return cardImg; + } + + + private void setCardBounds(MageCard card, Rectangle rectangle) { + card.setBounds(rectangle); + card.setCardBounds(rectangle.x, rectangle.y, Config.dimensions.frameWidth, Config.dimensions.frameHeight); + cardArea.moveToFront(card); } @Override