mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 12:02:01 -08:00
Moved card layout strategy to separate class
This commit is contained in:
parent
0e71ac5e53
commit
ca49dc7a31
4 changed files with 119 additions and 76 deletions
|
|
@ -40,6 +40,8 @@ import mage.client.cards.Permanent;
|
||||||
import mage.client.plugins.impl.Plugins;
|
import mage.client.plugins.impl.Plugins;
|
||||||
import mage.client.util.Config;
|
import mage.client.util.Config;
|
||||||
import mage.client.util.audio.AudioManager;
|
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.constants.CardType;
|
||||||
import mage.utils.CardUtil;
|
import mage.utils.CardUtil;
|
||||||
import mage.view.PermanentView;
|
import mage.view.PermanentView;
|
||||||
|
|
@ -68,10 +70,12 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane {
|
||||||
protected Map<UUID, PermanentView> battlefield;
|
protected Map<UUID, PermanentView> battlefield;
|
||||||
private Dimension cardDimension;
|
private Dimension cardDimension;
|
||||||
|
|
||||||
private JComponent jPanel;
|
private JLayeredPane jPanel;
|
||||||
private JScrollPane jScrollPane;
|
private JScrollPane jScrollPane;
|
||||||
private int width;
|
private int width;
|
||||||
|
|
||||||
|
private CardLayoutStrategy layoutStrategy = new OldCardLayoutStrategy();
|
||||||
|
|
||||||
//private static int iCounter = 0;
|
//private static int iCounter = 0;
|
||||||
|
|
||||||
private boolean addedPermanent;
|
private boolean addedPermanent;
|
||||||
|
|
@ -188,20 +192,13 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: review sorting stuff
|
|
||||||
public void sortLayout() {
|
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;}
|
if (battlefield == null) {return;}
|
||||||
|
|
||||||
for (PermanentView permanent: battlefield.values()) {
|
layoutStrategy.doLayout(this, width);
|
||||||
if (permanent.getAttachments() != null) {
|
|
||||||
groupAttachments(permanent);
|
this.jScrollPane.repaint();
|
||||||
}
|
this.jScrollPane.revalidate();
|
||||||
}
|
|
||||||
|
|
||||||
invalidate();
|
invalidate();
|
||||||
repaint();
|
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) {
|
private void removePermanent(UUID permanentId, final int count) {
|
||||||
for (Component c: this.jPanel.getComponents()) {
|
for (Component c: this.jPanel.getComponents()) {
|
||||||
final Component comp = c;
|
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
|
@Override
|
||||||
public boolean isOptimizedDrawingEnabled () {
|
public boolean isOptimizedDrawingEnabled () {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -361,7 +297,15 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane {
|
||||||
this.add(jScrollPane);
|
this.add(jScrollPane);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JComponent getMainPanel() {
|
public JLayeredPane getMainPanel() {
|
||||||
return jPanel;
|
return jPanel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<UUID, PermanentView> getBattlefield() {
|
||||||
|
return battlefield;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, JComponent> getUiComponentsList() {
|
||||||
|
return uiComponentsList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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<UUID, MagePermanent> 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<UUID, MagePermanent> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -120,6 +120,11 @@ public class CardPluginImpl implements CardPlugin {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// put enchanted lands separately
|
||||||
|
if (!empty(permanent.getLinks())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
int insertIndex = -1;
|
int insertIndex = -1;
|
||||||
|
|
||||||
// Find lands with the same name.
|
// Find lands with the same name.
|
||||||
|
|
@ -127,13 +132,19 @@ public class CardPluginImpl implements CardPlugin {
|
||||||
Stack stack = allLands.get(i);
|
Stack stack = allLands.get(i);
|
||||||
MagePermanent firstPanel = stack.get(0);
|
MagePermanent firstPanel = stack.get(0);
|
||||||
if (firstPanel.getOriginal().getName().equals(permanent.getOriginal().getName())) {
|
if (firstPanel.getOriginal().getName().equals(permanent.getOriginal().getName())) {
|
||||||
|
// put enchanted lands separately
|
||||||
|
if (!empty(permanent.getLinks())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty(firstPanel.getLinks())) {
|
if (!empty(firstPanel.getLinks())) {
|
||||||
// Put this land to the left of lands with the same name and attachments.
|
// Put this land to the left of lands with the same name and attachments.
|
||||||
insertIndex = i;
|
insertIndex = i;
|
||||||
break;
|
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;
|
insertIndex = i + 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue