Bugfix - after 12820b54d4 refactoring arrow does not disappear from the screen. Rollback.

This commit is contained in:
vraskulin 2017-02-13 18:56:26 +03:00
parent 5e410df39e
commit 09da478b38

View file

@ -1,7 +1,5 @@
package mage.client.util.gui; package mage.client.util.gui;
import com.google.common.collect.MapMaker;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
@ -14,31 +12,37 @@ import java.util.List;
*/ */
public class ArrowBuilder { public class ArrowBuilder {
private static final ArrowBuilder instance; private static ArrowBuilder instance;
static { static {
instance = new ArrowBuilder(); instance = new ArrowBuilder();
} }
/**
* Stores arrow panels per game
*/
private final Map<UUID, JPanel> arrowPanels = new HashMap<>();
private final Map<UUID, Map<Type, List<Arrow>>> map = new MapMaker().weakKeys().weakValues().makeMap();
/**
* The top panel where arrow panels are added to.
*/
private JPanel arrowsManagerPanel;
private int currentWidth;
private int currentHeight;
public static ArrowBuilder getBuilder() { public static ArrowBuilder getBuilder() {
return instance; return instance;
} }
/**
* The top panel where arrow panels are added to.
*/
private JPanel arrowsManagerPanel;
/**
* Stores arrow panels per game
*/
private final Map<UUID, JPanel> arrowPanels = new HashMap<UUID, JPanel>();
private final Map<UUID, Map<Type, List<Arrow>>> map = new HashMap<UUID, Map<Type, java.util.List<Arrow>>>();
private int currentWidth;
private int currentHeight;
public enum Type {
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS;
}
/** /**
* Get the panel where all arrows are being drawn. * Get the panel where all arrows are being drawn.
*
* @return * @return
*/ */
public JPanel getArrowsManagerPanel() { public JPanel getArrowsManagerPanel() {
@ -69,6 +73,17 @@ public class ArrowBuilder {
return arrowPanels.get(gameId); return arrowPanels.get(gameId);
} }
/**
* Not synchronized method for arrows panel.
* Doesn't create JPanel in case the panel doesn't exist.
* Works faster.
*
* @return
*/
/*public JPanel getPanelRef() {
return arrowsManagerPanel;
}*/
/** /**
* Adds new arrow. * Adds new arrow.
* *
@ -85,25 +100,26 @@ public class ArrowBuilder {
arrow.setColor(color); arrow.setColor(color);
arrow.setArrowLocation(startX, startY, endX, endY); arrow.setArrowLocation(startX, startY, endX, endY);
arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise) arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise)
synchronized (map) {
p.add(arrow); p.add(arrow);
Map<Type, List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<>()); Map<Type, java.util.List<Arrow>> innerMap = map.get(gameId);
List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<>()); if (innerMap == null) {
innerMap = new HashMap<Type, List<Arrow>>();
map.put(gameId, innerMap);
}
java.util.List<Arrow> arrows = innerMap.get(type);
if (arrows == null) {
arrows = new ArrayList<Arrow>();
innerMap.put(type, arrows);
}
arrows.add(arrow); arrows.add(arrow);
}
p.revalidate(); p.revalidate();
p.repaint(); p.repaint();
} }
/**
* Not synchronized method for arrows panel.
* Doesn't create JPanel in case the panel doesn't exist.
* Works faster.
*
* @return
*/
/*public JPanel getPanelRef() {
return arrowsManagerPanel;
}*/
/** /**
* Removes all arrows from the screen. * Removes all arrows from the screen.
*/ */
@ -111,6 +127,7 @@ public class ArrowBuilder {
if (map.containsKey(gameId)) { if (map.containsKey(gameId)) {
Map<Type, List<Arrow>> innerMap = map.get(gameId); Map<Type, List<Arrow>> innerMap = map.get(gameId);
JPanel p = getArrowsPanel(gameId); JPanel p = getArrowsPanel(gameId);
synchronized (map) {
if (p != null && p.getComponentCount() > 0) { if (p != null && p.getComponentCount() > 0) {
p.removeAll(); p.removeAll();
p.revalidate(); p.revalidate();
@ -120,17 +137,20 @@ public class ArrowBuilder {
map.remove(gameId); map.remove(gameId);
} }
} }
}
public void removeArrowsByType(UUID gameId, Type type) { public void removeArrowsByType(UUID gameId, Type type) {
if (map.containsKey(gameId)) { if (map.containsKey(gameId)) {
Map<Type, List<Arrow>> innerMap = map.get(gameId); Map<Type, List<Arrow>> innerMap = map.get(gameId);
java.util.List<Arrow> arrows = innerMap.get(type); java.util.List<Arrow> arrows = innerMap.get(type);
if (arrows != null && !arrows.isEmpty()) { if (arrows != null && arrows.size() > 0) {
JPanel p = getArrowsPanel(gameId); JPanel p = getArrowsPanel(gameId);
synchronized (map) {
for (Arrow arrow : arrows) { for (Arrow arrow : arrows) {
p.remove(arrow); p.remove(arrow);
} }
innerMap.put(type, new ArrayList<>()); innerMap.put(type, new ArrayList<Arrow>());
}
p.revalidate(); p.revalidate();
p.repaint(); p.repaint();
} }
@ -161,8 +181,4 @@ public class ArrowBuilder {
} }
} }
public enum Type {
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS
}
} }