diff --git a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java index 55e41fbae6d..0605f6e8743 100644 --- a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java @@ -40,6 +40,8 @@ import mage.client.cards.Permanent; import mage.client.plugins.impl.Plugins; import mage.client.util.Config; import mage.client.util.audio.AudioManager; +import mage.client.util.layout.CardLayoutStrategy; +import mage.client.util.layout.impl.OldCardLayoutStrategy; import mage.constants.CardType; import mage.utils.CardUtil; import mage.view.PermanentView; @@ -68,10 +70,12 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { protected Map battlefield; private Dimension cardDimension; - private JComponent jPanel; + private JLayeredPane jPanel; private JScrollPane jScrollPane; private int width; + private CardLayoutStrategy layoutStrategy = new OldCardLayoutStrategy(); + //private static int iCounter = 0; private boolean addedPermanent; @@ -188,20 +192,13 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { } } - //TODO: review sorting stuff public void sortLayout() { - int height = Plugins.getInstance().sortPermanents(uiComponentsList, permanents.values()); - BattlefieldPanel.this.jPanel.setPreferredSize(new Dimension(width - 30, height)); - this.jScrollPane.repaint(); - this.jScrollPane.revalidate(); - if (battlefield == null) {return;} - for (PermanentView permanent: battlefield.values()) { - if (permanent.getAttachments() != null) { - groupAttachments(permanent); - } - } + layoutStrategy.doLayout(this, width); + + this.jScrollPane.repaint(); + this.jScrollPane.revalidate(); invalidate(); repaint(); @@ -247,44 +244,6 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { } } - private void groupAttachments(PermanentView permanent) { - MagePermanent perm = permanents.get(permanent.getId()); - if (perm == null) { - return; - } - int position = getPosition(perm); - perm.getLinks().clear(); - Rectangle r = perm.getBounds(); - if (!Plugins.getInstance().isCardPluginLoaded()) { - for (UUID attachmentId: permanent.getAttachments()) { - MagePermanent link = permanents.get(attachmentId); - if (link != null) { - perm.getLinks().add(link); - r.translate(20, 20); - link.setBounds(r); - setPosition(link, ++position); - } - } - } else { - int index = permanent.getAttachments().size(); - for (UUID attachmentId: permanent.getAttachments()) { - MagePermanent link = permanents.get(attachmentId); - if (link != null) { - link.setBounds(r); - perm.getLinks().add(link); - r.translate(8, 10); - perm.setBounds(r); - moveToFront(link); - moveToFront(perm); - jPanel.setComponentZOrder(link, index); - index--; - } - } - jPanel.setComponentZOrder(perm, index); - } - - } - private void removePermanent(UUID permanentId, final int count) { for (Component c: this.jPanel.getComponents()) { final Component comp = c; @@ -312,29 +271,6 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { } } -// private Rectangle findEmptySpace(Dimension size) { -// int battlefieldWidth = this.getWidth(); -// Rectangle r = new Rectangle(size); -// boolean intersects; -// while (true) { -// intersects = false; -// for (MagePermanent perm: permanents.values()) { -// Rectangle pr = perm.getBounds(); -// if (r.intersects(pr)) { -// intersects = true; -// if (pr.x + pr.width + r.width > battlefieldWidth) -// r.setLocation(0, pr.y + pr.height + 1); -// else -// r.translate(pr.x + pr.width - r.x, 0); -// break; -// } -// } -// if (!intersects) -// break; -// } -// return r; -// } - @Override public boolean isOptimizedDrawingEnabled () { return false; @@ -361,7 +297,15 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { this.add(jScrollPane); } - public JComponent getMainPanel() { + public JLayeredPane getMainPanel() { return jPanel; } + + public Map getBattlefield() { + return battlefield; + } + + public Map getUiComponentsList() { + return uiComponentsList; + } } diff --git a/Mage.Client/src/main/java/mage/client/util/layout/CardLayoutStrategy.java b/Mage.Client/src/main/java/mage/client/util/layout/CardLayoutStrategy.java new file mode 100644 index 00000000000..9970e0c1a79 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/util/layout/CardLayoutStrategy.java @@ -0,0 +1,13 @@ +package mage.client.util.layout; + +import javax.swing.*; + +/** + * Interface for operations that modify cards' layout + * + * @author noxx + */ +public interface CardLayoutStrategy { + + void doLayout(JLayeredPane jLayeredPane, int width); +} diff --git a/Mage.Client/src/main/java/mage/client/util/layout/impl/OldCardLayoutStrategy.java b/Mage.Client/src/main/java/mage/client/util/layout/impl/OldCardLayoutStrategy.java new file mode 100644 index 00000000000..5f973b6bf20 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/util/layout/impl/OldCardLayoutStrategy.java @@ -0,0 +1,75 @@ +package mage.client.util.layout.impl; + +import mage.cards.MagePermanent; +import mage.client.game.BattlefieldPanel; +import mage.client.plugins.impl.Plugins; +import mage.client.util.layout.CardLayoutStrategy; +import mage.view.PermanentView; + +import javax.swing.*; +import java.awt.*; +import java.util.Map; +import java.util.UUID; + +/** + * Card layout for client version 1.3.0 and earlier. + * + * Save it here for a while. + * + * @author noxx + */ +public class OldCardLayoutStrategy implements CardLayoutStrategy { + + @Override + public void doLayout(JLayeredPane jLayeredPane, int width) { + Map permanents = ((BattlefieldPanel)jLayeredPane).getPermanents(); + JLayeredPane jPanel = ((BattlefieldPanel)jLayeredPane).getMainPanel(); + + int height = Plugins.getInstance().sortPermanents(((BattlefieldPanel)jLayeredPane).getUiComponentsList(), permanents.values()); + jPanel.setPreferredSize(new Dimension(width - 30, height)); + + for (PermanentView permanent: ((BattlefieldPanel)jLayeredPane).getBattlefield().values()) { + if (permanent.getAttachments() != null) { + groupAttachments(jLayeredPane, jPanel, permanents, permanent); + } + } + } + + private void groupAttachments(JLayeredPane jLayeredPane, JLayeredPane jPanel, Map permanents, PermanentView permanent) { + MagePermanent perm = permanents.get(permanent.getId()); + if (perm == null) { + return; + } + int position = jLayeredPane.getPosition(perm); + perm.getLinks().clear(); + Rectangle r = perm.getBounds(); + if (!Plugins.getInstance().isCardPluginLoaded()) { + for (UUID attachmentId: permanent.getAttachments()) { + MagePermanent link = permanents.get(attachmentId); + if (link != null) { + perm.getLinks().add(link); + r.translate(20, 20); + link.setBounds(r); + jLayeredPane.setPosition(link, ++position); + } + } + } else { + int index = permanent.getAttachments().size(); + for (UUID attachmentId: permanent.getAttachments()) { + MagePermanent link = permanents.get(attachmentId); + if (link != null) { + link.setBounds(r); + perm.getLinks().add(link); + r.translate(8, 10); + perm.setBounds(r); + jLayeredPane.moveToFront(link); + jLayeredPane.moveToFront(perm); + jPanel.setComponentZOrder(link, index); + index--; + } + } + jPanel.setComponentZOrder(perm, index); + } + + } +} diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java b/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java index ed76ae4f3a2..7a451b3d1d7 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/CardPluginImpl.java @@ -120,6 +120,11 @@ public class CardPluginImpl implements CardPlugin { continue; } + // put enchanted lands separately + if (!empty(permanent.getLinks())) { + continue; + } + int insertIndex = -1; // Find lands with the same name. @@ -127,13 +132,19 @@ public class CardPluginImpl implements CardPlugin { Stack stack = allLands.get(i); MagePermanent firstPanel = stack.get(0); if (firstPanel.getOriginal().getName().equals(permanent.getOriginal().getName())) { + // put enchanted lands separately + if (!empty(permanent.getLinks())) { + break; + } + if (!empty(firstPanel.getLinks())) { // Put this land to the left of lands with the same name and attachments. insertIndex = i; break; } - if (!empty(permanent.getLinks()) || stack.size() == landStackMax) { - // If this land has attachments or the stack is full, put it to the right. + + if (stack.size() == landStackMax) { + // If the stack is full, put it to the right. insertIndex = i + 1; continue; }