package mage.client.util.gui; import mage.constants.CardType; import mage.constants.MageObjectType; import mage.utils.CardUtil; import mage.view.CardView; import mage.view.CounterView; import mage.view.PermanentView; import org.jdesktop.swingx.JXPanel; import org.mage.card.arcane.ManaSymbols; import org.mage.card.arcane.UI; import javax.swing.*; import java.awt.*; import java.util.ArrayList; import mage.client.dialog.MageDialog; import mage.constants.Rarity; public class GuiDisplayUtil { private static final Font cardNameFont = new Font("Calibri", Font.BOLD, 15); private static final Insets DEFAULT_INSETS = new Insets(0, 0, 70, 25); private static final Insets COMPONENT_INSETS = new Insets(0, 0, 40, 40); public static class TextLines { public int basicTextLength; public ArrayList lines; } public static JXPanel getDescription(CardView card, int width, int height) { JXPanel descriptionPanel = new JXPanel(); //descriptionPanel.setAlpha(.8f); descriptionPanel.setBounds(0, 0, width, height); descriptionPanel.setVisible(false); descriptionPanel.setLayout(null); //descriptionPanel.setBorder(BorderFactory.createLineBorder(Color.green)); JButton j = new JButton(""); j.setBounds(0, 0, width, height); j.setBackground(Color.black); j.setLayout(null); JLabel cardText = new JLabel(); cardText.setBounds(5, 5, width - 10, height - 10); cardText.setForeground(Color.white); cardText.setFont(cardNameFont); cardText.setVerticalAlignment(SwingConstants.TOP); j.add(cardText); TextLines textLines = GuiDisplayUtil.getTextLinesfromCardView(card); cardText.setText(getRulefromCardView(card, textLines).toString()); descriptionPanel.add(j); return descriptionPanel; } public static String cleanString(String in) { StringBuilder out = new StringBuilder(); char c; for (int i = 0; i < in.length(); i++) { c = in.charAt(i); if (c == ' ' || c == '-') { out.append('_'); } else if (Character.isLetterOrDigit(c)) { out.append(c); } } return out.toString().toLowerCase(); } public static void keepComponentInsideScreen(int x, int y, Component c) { Dimension screenDim = c.getToolkit().getScreenSize(); GraphicsConfiguration g = c.getGraphicsConfiguration(); if (g != null) { Insets insets = c.getToolkit().getScreenInsets(g); if (x + c.getWidth() > screenDim.width - insets.right) { x = (screenDim.width - insets.right) - c.getWidth(); } else if (x < insets.left) { x = insets.left; } if (y + c.getHeight() > screenDim.height - insets.bottom) { y = (screenDim.height - insets.bottom) - c.getHeight(); } else if (y < insets.top) { y = insets.top; } c.setLocation(x, y); } else { System.out.println("GuiDisplayUtil::keepComponentInsideScreen -> no GraphicsConfiguration"); } } public static Point keepComponentInsideParent(Point l, Point parentPoint, Component c, Component parent) { int dx = parentPoint.x + parent.getWidth() - DEFAULT_INSETS.right - COMPONENT_INSETS.right; if (l.x + c.getWidth() > dx) { l.x = dx - c.getWidth(); } int dy = parentPoint.y + parent.getHeight() - DEFAULT_INSETS.bottom - COMPONENT_INSETS.bottom; if (l.y + c.getHeight() > dy) { l.y = Math.max(10, dy - c.getHeight()); } return l; } public static TextLines getTextLinesfromCardView(CardView card) { TextLines textLines = new TextLines(); textLines.lines = new ArrayList<>(card.getRules()); for (String rule: card.getRules()) { textLines.basicTextLength +=rule.length(); } if (card.getMageObjectType().equals(MageObjectType.PERMANENT)) { if (card.getPairedCard() != null) { textLines.lines.add("Paired with another creature"); textLines.basicTextLength += 30; } } if (card.getMageObjectType().canHaveCounters()) { ArrayList counters = new ArrayList<>(); if (card instanceof PermanentView) { if (((PermanentView) card).getCounters() != null) { counters = new ArrayList<>(((PermanentView) card).getCounters()); } } else { if (card.getCounters() != null) { counters = new ArrayList<>(card.getCounters()); } } if (!counters.isEmpty()) { StringBuilder sb = new StringBuilder(); int index = 0; for (CounterView counter : counters) { if (counter.getCount() > 0) { if (index == 0) { sb.append("Counters: "); } else { sb.append(", "); } sb.append(counter.getCount()).append(" x ").append(counter.getName()).append(""); index++; } } textLines.lines.add(sb.toString()); textLines.basicTextLength += 50; } } if (card.getMageObjectType().isPermanent() && card instanceof PermanentView) { int damage = ((PermanentView)card).getDamage(); if (damage > 0) { textLines.lines.add("Damage dealt: " + damage + ""); textLines.basicTextLength += 50; } } return textLines; } public static StringBuilder getRulefromCardView(CardView card, TextLines textLines) { String manaCost = ""; for (String m : card.getManaCost()) { manaCost += m; } String castingCost = UI.getDisplayManaCost(manaCost); castingCost = ManaSymbols.replaceSymbolsWithHTML(castingCost, ManaSymbols.Type.CARD); int symbolCount = 0; int offset = 0; while ((offset = castingCost.indexOf(""); buffer.append(""); buffer.append("
"); buffer.append(card.getDisplayName()).append(" [").append(card.getId().toString().substring(0,3)).append("]"); buffer.append(""); if (!card.isSplitCard()) { buffer.append(castingCost); } buffer.append("
"); buffer.append("
"); if(card.getColor().isWhite()) { buffer.append("W"); } if(card.getColor().isBlue()) { buffer.append("U"); } if(card.getColor().isBlack()) { buffer.append("B"); } if(card.getColor().isRed()) { buffer.append("R"); } if(card.getColor().isGreen()) { buffer.append("G"); } if(!card.getColor().isColorless()) { buffer.append("  "); } buffer.append(getTypes(card)); buffer.append(""); String rarity ; if (card.getRarity() == null) { rarity = Rarity.COMMON.getCode(); buffer.append(""); }else { switch (card.getRarity()) { case RARE: buffer.append(""); break; case UNCOMMON: buffer.append(""); break; case COMMON: buffer.append(""); break; case MYTHIC: buffer.append(""); break; } rarity = card.getRarity().getCode(); } if (card.getExpansionSetCode() != null) { buffer.append(ManaSymbols.replaceSetCodeWithHTML(card.getExpansionSetCode().toUpperCase(), rarity)); } buffer.append("
"); String pt = ""; if (CardUtil.isCreature(card)) { pt = card.getPower() + "/" + card.getToughness(); } else if (CardUtil.isPlaneswalker(card)) { pt = card.getLoyalty(); } buffer.append(""); buffer.append("
"); buffer.append(pt).append(""); if (!card.isControlledByOwner()) { if (card instanceof PermanentView) { buffer.append("[").append(((PermanentView) card).getNameOwner()).append("] "); } else { buffer.append("[only controlled] "); } } if (!card.getMageObjectType().equals(MageObjectType.NULL)) { buffer.append(card.getMageObjectType().toString()); } buffer.append("
"); StringBuilder rule = new StringBuilder("
"); if (card.isSplitCard()) { rule.append(""); rule.append("
"); rule.append(card.getLeftSplitName()); rule.append(""); rule.append(card.getLeftSplitCosts().getText()); rule.append("
"); for (String ruling : card.getLeftSplitRules()) { if (ruling != null && !ruling.replace(".", "").trim().isEmpty()) { rule.append("

").append(ruling).append("

"); } } rule.append(""); rule.append("
"); rule.append(card.getRightSplitName()); rule.append(""); rule.append(card.getRightSplitCosts().getText()); rule.append("
"); for (String ruling : card.getRightSplitRules()) { if (ruling != null && !ruling.replace(".", "").trim().isEmpty()) { rule.append("

").append(ruling).append("

"); } } } if (textLines.lines.size() > 0) { for (String textLine : textLines.lines) { if (textLine != null && !textLine.replace(".", "").trim().isEmpty()) { rule.append("

").append(textLine).append("

"); } } } String legal = rule.toString(); if (legal.length() > 0) { // this 2 replaces were only done with the empty string, is it any longer needed? (LevelX2) // legal = legal.replaceAll("#([^#]+)#", "$1"); // legal = legal.replaceAll("\\s*//\\s*", "
"); // legal = legal.replace("\r\n", "
"); legal = legal.replaceAll("\\{this\\}", card.getName().isEmpty() ? "this":card.getName()); legal = legal.replaceAll("\\{source\\}", card.getName().isEmpty() ? "this":card.getName()); buffer.append(ManaSymbols.replaceSymbolsWithHTML(legal, ManaSymbols.Type.CARD)); } buffer.append("
"); return buffer; } private static String getResourcePath(String image) { return GuiDisplayUtil.class.getClassLoader().getResource(image).toString(); } private static String getTypes(CardView card) { String types = ""; for (String superType : card.getSuperTypes()) { types += superType + " "; } for (CardType cardType : card.getCardTypes()) { types += cardType.toString() + " "; } if (card.getSubTypes().size() > 0) { types += "- "; } for (String subType : card.getSubTypes()) { types += subType + " "; } return types.trim(); } }