GUI: added info about combat arrows bug on re-connect

This commit is contained in:
Oleg Agafonov 2020-08-02 14:22:59 +04:00
parent 00411b4a9b
commit c9c1bc2b90
2 changed files with 277 additions and 269 deletions

View file

@ -1,245 +1,238 @@
package mage.client.game;
import mage.cards.MagePermanent;
import mage.client.cards.BigCard;
import mage.client.cards.Permanent;
import mage.client.dialog.PreferencesDialog;
import mage.client.plugins.impl.Plugins;
import mage.client.util.ClientDefaultSettings;
import mage.client.util.GUISizeHelper;
import mage.client.util.audio.AudioManager;
import mage.client.util.layout.CardLayoutStrategy;
import mage.client.util.layout.impl.OldCardLayoutStrategy;
import mage.view.CounterView;
import mage.view.PermanentView;
/* import javax.swing.*;
* BattlefieldPanel.java import javax.swing.border.Border;
* import javax.swing.border.EmptyBorder;
* Created on 10-Jan-2010, 10:43:14 PM import java.awt.*;
*/ import java.awt.event.ComponentAdapter;
package mage.client.game; import java.awt.event.ComponentEvent;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import mage.cards.MagePermanent; /**
import mage.client.cards.BigCard; * @author BetaSteward_at_googlemail.com
import mage.client.cards.Permanent; */
import mage.client.dialog.PreferencesDialog; public class BattlefieldPanel extends javax.swing.JLayeredPane {
import mage.client.plugins.impl.Plugins;
import mage.client.util.ClientDefaultSettings;
import mage.client.util.GUISizeHelper;
import mage.client.util.audio.AudioManager;
import mage.client.util.layout.CardLayoutStrategy;
import mage.client.util.layout.impl.OldCardLayoutStrategy;
import mage.view.CounterView;
import mage.view.PermanentView;
import javax.swing.*; private final Map<UUID, MagePermanent> permanents = new LinkedHashMap<>();
import javax.swing.border.Border; private UUID gameId;
import javax.swing.border.EmptyBorder; private BigCard bigCard;
import java.awt.*; private final Map<String, JComponent> uiComponentsList = new HashMap<>();
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
/** protected Map<UUID, PermanentView> battlefield;
* @author BetaSteward_at_googlemail.com private Dimension cardDimension;
*/
public class BattlefieldPanel extends javax.swing.JLayeredPane {
private final Map<UUID, MagePermanent> permanents = new LinkedHashMap<>(); private JLayeredPane jPanel;
private UUID gameId; private JScrollPane jScrollPane;
private BigCard bigCard;
private final Map<String, JComponent> uiComponentsList = new HashMap<>();
protected Map<UUID, PermanentView> battlefield; private final CardLayoutStrategy layoutStrategy = new OldCardLayoutStrategy();
private Dimension cardDimension;
private JLayeredPane jPanel; //private static int iCounter = 0;
private JScrollPane jScrollPane; private boolean addedPermanent;
private int width; private boolean addedArtifact;
private boolean addedCreature;
private final CardLayoutStrategy layoutStrategy = new OldCardLayoutStrategy(); private boolean removedCreature;
// defines if the battlefield is within a top (means top row of player panels) or a bottom player panel
private boolean topPanelBattlefield;
//private static int iCounter = 0; /**
private boolean addedPermanent; * Creates new form BattlefieldPanel
private boolean addedArtifact; */
private boolean addedCreature; public BattlefieldPanel() {
uiComponentsList.put("battlefieldPanel", this);
initComponents();
uiComponentsList.put("jPanel", jPanel);
setGUISize();
private boolean removedCreature; addComponentListener(new ComponentAdapter() {
// defines if the battlefield is within a top (means top row of player panels) or a bottom player panel @Override
private boolean topPanelBattlefield; public void componentResized(ComponentEvent e) {
updateSize();
}
});
}
/** public void updateSize() {
* Creates new form BattlefieldPanel this.jScrollPane.setSize(this.getWidth(), this.getHeight());
*/ sortLayout();
public BattlefieldPanel() { }
uiComponentsList.put("battlefieldPanel", this);
initComponents();
uiComponentsList.put("jPanel", jPanel);
setGUISize();
addComponentListener(new ComponentAdapter() { public void init(UUID gameId, BigCard bigCard) {
@Override this.gameId = gameId;
public void componentResized(ComponentEvent e) { this.bigCard = bigCard;
int width = e.getComponent().getWidth(); }
int height = e.getComponent().getHeight();
BattlefieldPanel.this.jScrollPane.setSize(width, height);
BattlefieldPanel.this.width = width;
sortLayout();
}
});
}
public void init(UUID gameId, BigCard bigCard) { public void cleanUp() {
this.gameId = gameId; for (Component c : this.jPanel.getComponents()) {
this.bigCard = bigCard; if (c instanceof Permanent || c instanceof MagePermanent) {
} this.jPanel.remove(c);
}
}
permanents.clear();
// Plugins.getInstance().sortPermanents(uiComponentsList, permanents.values());
this.bigCard = null;
}
public void cleanUp() { public void changeGUISize() {
for (Component c : this.jPanel.getComponents()) { setGUISize();
if (c instanceof Permanent || c instanceof MagePermanent) { sortLayout();
this.jPanel.remove(c); }
}
}
permanents.clear();
// Plugins.getInstance().sortPermanents(uiComponentsList, permanents.values());
this.bigCard = null;
}
public void changeGUISize() { private void setGUISize() {
setGUISize(); jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(GUISizeHelper.scrollBarSize, 0));
sortLayout(); jScrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, GUISizeHelper.scrollBarSize));
} cardDimension = GUISizeHelper.battlefieldCardMaxDimension;
}
private void setGUISize() { public boolean isTopPanelBattlefield() {
jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(GUISizeHelper.scrollBarSize, 0)); return topPanelBattlefield;
jScrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, GUISizeHelper.scrollBarSize)); }
cardDimension = GUISizeHelper.battlefieldCardMaxDimension;
}
public boolean isTopPanelBattlefield() { public void setTopPanelBattlefield(boolean topPanelBattlefield) {
return topPanelBattlefield; this.topPanelBattlefield = topPanelBattlefield;
} }
public void setTopPanelBattlefield(boolean topPanelBattlefield) { public void update(Map<UUID, PermanentView> battlefield) {
this.topPanelBattlefield = topPanelBattlefield; boolean changed = false;
}
public void update(Map<UUID, PermanentView> battlefield) { List<PermanentView> permanentsToAdd = new ArrayList<>();
boolean changed = false; for (PermanentView permanent : battlefield.values()) {
if (!permanent.isPhasedIn()) {
continue;
}
MagePermanent oldMagePermanent = permanents.get(permanent.getId());
if (oldMagePermanent == null) {
permanentsToAdd.add(permanent);
changed = true;
} else {
if (!changed) {
changed = oldMagePermanent.getOriginalPermanent().isCreature() != permanent.isCreature();
// Check if there was a chnage in the permanets that are the permanent attached to
if (!changed) {
int attachments = permanent.getAttachments() == null ? 0 : permanent.getAttachments().size();
int attachmentsBefore = oldMagePermanent.getLinks().size();
if (attachments != attachmentsBefore) {
changed = true;
} else if (attachments > 0) {
Set<UUID> attachmentIds = new HashSet<>(permanent.getAttachments());
for (MagePermanent magePermanent : oldMagePermanent.getLinks()) {
if (!attachmentIds.contains(magePermanent.getOriginalPermanent().getId())) {
// that means that the amount of attachments is the same
// but they are different:
// we've just found an attachment on previous view
// that doesn't exist anymore on current view
changed = true;
break;
}
}
}
}
// Check if permanents it now attached to another or no permanent
if (!changed) {
UUID attachedToIdBefore = oldMagePermanent.getOriginalPermanent().getAttachedTo();
UUID attachedToId = permanent.getAttachedTo();
if (attachedToIdBefore == null && attachedToId != null || attachedToId == null && attachedToIdBefore != null
|| (attachedToIdBefore != null && !attachedToIdBefore.equals(attachedToId))) {
changed = true;
}
}
// Check for changes in the counters of the permanent
if (!changed) {
List<CounterView> counters1 = oldMagePermanent.getOriginalPermanent().getCounters();
List<CounterView> counters2 = permanent.getCounters();
if (counters1 == null && counters2 != null || counters1 != null && counters2 == null) {
changed = true;
} else if (counters1 != null && counters2 != null && counters1.size() != counters2.size()) {
changed = true;
}
}
List<PermanentView> permanentsToAdd = new ArrayList<>(); }
for (PermanentView permanent : battlefield.values()) { oldMagePermanent.update(permanent);
if (!permanent.isPhasedIn()) { }
continue; }
}
MagePermanent oldMagePermanent = permanents.get(permanent.getId());
if (oldMagePermanent == null) {
permanentsToAdd.add(permanent);
changed = true;
} else {
if (!changed) {
changed = oldMagePermanent.getOriginalPermanent().isCreature() != permanent.isCreature();
// Check if there was a chnage in the permanets that are the permanent attached to
if (!changed) {
int attachments = permanent.getAttachments() == null ? 0 : permanent.getAttachments().size();
int attachmentsBefore = oldMagePermanent.getLinks().size();
if (attachments != attachmentsBefore) {
changed = true;
} else if (attachments > 0) {
Set<UUID> attachmentIds = new HashSet<>(permanent.getAttachments());
for (MagePermanent magePermanent : oldMagePermanent.getLinks()) {
if (!attachmentIds.contains(magePermanent.getOriginalPermanent().getId())) {
// that means that the amount of attachments is the same
// but they are different:
// we've just found an attachment on previous view
// that doesn't exist anymore on current view
changed = true;
break;
}
}
}
}
// Check if permanents it now attached to another or no permanent
if (!changed) {
UUID attachedToIdBefore = oldMagePermanent.getOriginalPermanent().getAttachedTo();
UUID attachedToId = permanent.getAttachedTo();
if (attachedToIdBefore == null && attachedToId != null || attachedToId == null && attachedToIdBefore != null
|| (attachedToIdBefore != null && !attachedToIdBefore.equals(attachedToId))) {
changed = true;
}
}
// Check for changes in the counters of the permanent
if (!changed) {
List<CounterView> counters1 = oldMagePermanent.getOriginalPermanent().getCounters();
List<CounterView> counters2 = permanent.getCounters();
if (counters1 == null && counters2 != null || counters1 != null && counters2 == null) {
changed = true;
} else if (counters1 != null && counters2 != null && counters1.size() != counters2.size()) {
changed = true;
}
}
} addedArtifact = addedCreature = addedPermanent = false;
oldMagePermanent.update(permanent);
}
}
addedArtifact = addedCreature = addedPermanent = false; int count = permanentsToAdd.size();
for (PermanentView permanent : permanentsToAdd) {
addPermanent(permanent, count);
}
int count = permanentsToAdd.size(); if (addedArtifact) {
for (PermanentView permanent : permanentsToAdd) { AudioManager.playAddArtifact();
addPermanent(permanent, count); } else if (addedCreature) {
} AudioManager.playSummon();
} else if (addedPermanent) {
AudioManager.playAddPermanent();
}
if (addedArtifact) { removedCreature = false;
AudioManager.playAddArtifact();
} else if (addedCreature) {
AudioManager.playSummon();
} else if (addedPermanent) {
AudioManager.playAddPermanent();
}
removedCreature = false; for (Iterator<Entry<UUID, MagePermanent>> iterator = permanents.entrySet().iterator(); iterator.hasNext(); ) {
Entry<UUID, MagePermanent> entry = iterator.next();
if (!battlefield.containsKey(entry.getKey()) || !battlefield.get(entry.getKey()).isPhasedIn()) {
removePermanent(entry.getKey(), 1);
iterator.remove();
changed = true;
}
}
for (Iterator<Entry<UUID, MagePermanent>> iterator = permanents.entrySet().iterator(); iterator.hasNext(); ) { if (removedCreature) {
Entry<UUID, MagePermanent> entry = iterator.next(); AudioManager.playDiedCreature();
if (!battlefield.containsKey(entry.getKey()) || !battlefield.get(entry.getKey()).isPhasedIn()) { }
removePermanent(entry.getKey(), 1);
iterator.remove();
changed = true;
}
}
if (removedCreature) { if (changed) {
AudioManager.playDiedCreature(); this.battlefield = battlefield;
} sortLayout();
}
}
if (changed) { public void sortLayout() {
this.battlefield = battlefield; if (battlefield == null || this.getWidth() < 1) { // Can't do layout when panel is not sized yet
sortLayout(); return;
} }
}
public void sortLayout() { layoutStrategy.doLayout(this, this.getWidth());
if (battlefield == null || this.getWidth() < 1) { // Can't do layout when panel is not sized yet
return;
}
layoutStrategy.doLayout(this, width); this.jScrollPane.repaint();
this.jScrollPane.revalidate();
this.jScrollPane.repaint(); invalidate();
this.jScrollPane.revalidate(); repaint();
}
invalidate(); private void addPermanent(PermanentView permanent, final int count) {
repaint(); if (cardDimension == null) {
} cardDimension = new Dimension(ClientDefaultSettings.dimensions.getFrameWidth(), ClientDefaultSettings.dimensions.getFrameHeight());
}
final MagePermanent perm = Plugins.instance.getMagePermanent(permanent, bigCard, cardDimension, gameId, true, PreferencesDialog.getRenderMode(), true);
private void addPermanent(PermanentView permanent, final int count) { permanents.put(permanent.getId(), perm);
if (cardDimension == null) {
cardDimension = new Dimension(ClientDefaultSettings.dimensions.getFrameWidth(), ClientDefaultSettings.dimensions.getFrameHeight());
}
final MagePermanent perm = Plugins.instance.getMagePermanent(permanent, bigCard, cardDimension, gameId, true, PreferencesDialog.getRenderMode(), true);
permanents.put(permanent.getId(), perm); BattlefieldPanel.this.jPanel.add(perm, 10);
//this.jPanel.add(perm);
BattlefieldPanel.this.jPanel.add(perm, 10); if (!Plugins.instance.isCardPluginLoaded()) {
//this.jPanel.add(perm); moveToFront(perm);
if (!Plugins.instance.isCardPluginLoaded()) { perm.update(permanent);
moveToFront(perm); } else {
perm.update(permanent); moveToFront(jPanel);
} else { Plugins.instance.onAddCard(perm, 1);
moveToFront(jPanel);
Plugins.instance.onAddCard(perm, 1);
/*Thread t = new Thread(new Runnable() { /*Thread t = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -249,76 +242,76 @@
synchronized (this) { synchronized (this) {
threads.add(t); threads.add(t);
}*/ }*/
} }
if (permanent.isArtifact()) { if (permanent.isArtifact()) {
addedArtifact = true; addedArtifact = true;
} else if (permanent.isCreature()) { } else if (permanent.isCreature()) {
addedCreature = true; addedCreature = true;
} else { } else {
addedPermanent = true; addedPermanent = true;
} }
} }
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;
if (comp instanceof Permanent) { if (comp instanceof Permanent) {
if (((Permanent) comp).getPermanentId().equals(permanentId)) { if (((Permanent) comp).getPermanentId().equals(permanentId)) {
comp.setVisible(false); comp.setVisible(false);
this.jPanel.remove(comp); this.jPanel.remove(comp);
} }
} else if (comp instanceof MagePermanent) { } else if (comp instanceof MagePermanent) {
if (((MagePermanent) comp).getOriginal().getId().equals(permanentId)) { if (((MagePermanent) comp).getOriginal().getId().equals(permanentId)) {
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
Plugins.instance.onRemoveCard((MagePermanent) comp, count); Plugins.instance.onRemoveCard((MagePermanent) comp, count);
comp.setVisible(false); comp.setVisible(false);
BattlefieldPanel.this.jPanel.remove(comp); BattlefieldPanel.this.jPanel.remove(comp);
}); });
t.start(); t.start();
} }
if (((MagePermanent) comp).getOriginal().isCreature()) { if (((MagePermanent) comp).getOriginal().isCreature()) {
removedCreature = true; removedCreature = true;
} }
} }
} }
} }
@Override @Override
public boolean isOptimizedDrawingEnabled() { public boolean isOptimizedDrawingEnabled() {
return false; return false;
} }
public Map<UUID, MagePermanent> getPermanents() { public Map<UUID, MagePermanent> getPermanents() {
return permanents; return permanents;
} }
private void initComponents() { private void initComponents() {
setOpaque(false); setOpaque(false);
jPanel = new JLayeredPane(); jPanel = new JLayeredPane();
jPanel.setLayout(null); jPanel.setLayout(null);
jPanel.setOpaque(false); jPanel.setOpaque(false);
jScrollPane = new JScrollPane(jPanel); jScrollPane = new JScrollPane(jPanel);
Border empty = new EmptyBorder(0, 0, 0, 0); Border empty = new EmptyBorder(0, 0, 0, 0);
jScrollPane.setBorder(empty); jScrollPane.setBorder(empty);
jScrollPane.setViewportBorder(empty); jScrollPane.setViewportBorder(empty);
jScrollPane.setOpaque(false); jScrollPane.setOpaque(false);
jScrollPane.getViewport().setOpaque(false); jScrollPane.getViewport().setOpaque(false);
this.add(jScrollPane); this.add(jScrollPane);
} }
public JLayeredPane getMainPanel() { public JLayeredPane getMainPanel() {
return jPanel; return jPanel;
} }
public Map<UUID, PermanentView> getBattlefield() { public Map<UUID, PermanentView> getBattlefield() {
return battlefield; return battlefield;
} }
public Map<String, JComponent> getUiComponentsList() { public Map<String, JComponent> getUiComponentsList() {
return uiComponentsList; return uiComponentsList;
} }
} }

View file

@ -611,6 +611,21 @@ public final class GamePanel extends javax.swing.JPanel {
this.pnlBattlefield.add(topPanel, panelC); this.pnlBattlefield.add(topPanel, panelC);
panelC.gridy = 1; panelC.gridy = 1;
this.pnlBattlefield.add(bottomPanel, panelC); this.pnlBattlefield.add(bottomPanel, panelC);
// TODO: combat arrows aren't visible on re-connect, must click on avatar to update correctrly
// reason: panels aren't visible/located here, so battlefieldpanel see wrong sizes
// recalc all component sizes and update permanents/arrows positions
// if you don't do it here then will catch wrong arrows drawing on re-connect (no sortLayout calls)
/*
this.validate();
for (Map.Entry<UUID, PlayAreaPanel> p : players.entrySet()) {
PlayerView playerView = game.getPlayers().stream().filter(view -> view.getPlayerId().equals(p.getKey())).findFirst().orElse(null);
if (playerView != null) {
p.getValue().getBattlefieldPanel().updateSize();
p.getValue().update(null, playerView, null);
}
}
*/
} }
public synchronized void updateGame(GameView game) { public synchronized void updateGame(GameView game) {