diff --git a/src/magic/ui/viewer/ImageBattlefieldViewer.java b/src/magic/ui/viewer/ImageBattlefieldViewer.java index 437ddeb749..b622654d80 100644 --- a/src/magic/ui/viewer/ImageBattlefieldViewer.java +++ b/src/magic/ui/viewer/ImageBattlefieldViewer.java @@ -13,51 +13,48 @@ import java.util.Set; public class ImageBattlefieldViewer extends JPanel implements ChoiceViewer { - private static final long serialVersionUID = 1L; - - private final ViewerInfo viewerInfo; - private final boolean opponent; - private final ImagePermanentsViewer permanentsViewer; - private final PermanentFilter permanentFilter; - - public ImageBattlefieldViewer(final ViewerInfo viewerInfo,final GameController controller,final boolean opponent) { - - this.viewerInfo=viewerInfo; - this.opponent=opponent; + private static final long serialVersionUID = 1L; + + private final ViewerInfo viewerInfo; + private final boolean opponent; + private final ImagePermanentsViewer permanentsViewer; + private final PermanentFilter permanentFilter; + + public ImageBattlefieldViewer(final ViewerInfo viewerInfo,final GameController controller,final boolean opponent) { + this.viewerInfo=viewerInfo; + this.opponent=opponent; - final Theme theme=ThemeFactory.getInstance().getCurrentTheme(); - - controller.registerChoiceViewer(this); - - setOpaque(false); - - setLayout(new BorderLayout(6,0)); - - final JPanel leftPanel=new JPanel(new BorderLayout()); - leftPanel.setOpaque(false); - add(leftPanel,BorderLayout.WEST); - - final JLabel iconLabel=new JLabel(theme.getIcon(Theme.ICON_SMALL_BATTLEFIELD)); - iconLabel.setOpaque(true); - iconLabel.setBackground(theme.getColor(Theme.COLOR_ICON_BACKGROUND)); - iconLabel.setPreferredSize(new Dimension(24,24)); - iconLabel.setBorder(FontsAndBorders.BLACK_BORDER); - leftPanel.add(iconLabel,BorderLayout.NORTH); - - permanentsViewer=new ImagePermanentsViewer(controller, opponent); - add(permanentsViewer,BorderLayout.CENTER); - - permanentFilter=new PermanentFilter(this,controller); - } - - public void update() { - - permanentsViewer.viewPermanents(permanentFilter.getPermanents(viewerInfo,opponent)); - } - - @Override - public void showValidChoices(final Set validChoices) { - - permanentsViewer.showValidChoices(validChoices); - } -} \ No newline at end of file + final Theme theme=ThemeFactory.getInstance().getCurrentTheme(); + + controller.registerChoiceViewer(this); + + setOpaque(false); + + setLayout(new BorderLayout(6,0)); + + final JPanel leftPanel=new JPanel(new BorderLayout()); + leftPanel.setOpaque(false); + add(leftPanel,BorderLayout.WEST); + + final JLabel iconLabel=new JLabel(theme.getIcon(Theme.ICON_SMALL_BATTLEFIELD)); + iconLabel.setOpaque(true); + iconLabel.setBackground(theme.getColor(Theme.COLOR_ICON_BACKGROUND)); + iconLabel.setPreferredSize(new Dimension(24,24)); + iconLabel.setBorder(FontsAndBorders.BLACK_BORDER); + leftPanel.add(iconLabel,BorderLayout.NORTH); + + permanentsViewer=new ImagePermanentsViewer(controller, opponent); + add(permanentsViewer,BorderLayout.CENTER); + + permanentFilter=new PermanentFilter(this,controller); + } + + public void update() { + permanentsViewer.viewPermanents(permanentFilter.getPermanents(viewerInfo,opponent)); + } + + @Override + public void showValidChoices(final Set validChoices) { + permanentsViewer.showValidChoices(validChoices); + } +} diff --git a/src/magic/ui/viewer/ImagePermanentsViewer.java b/src/magic/ui/viewer/ImagePermanentsViewer.java index 17f822ab39..71a824dfad 100644 --- a/src/magic/ui/viewer/ImagePermanentsViewer.java +++ b/src/magic/ui/viewer/ImagePermanentsViewer.java @@ -14,206 +14,206 @@ import java.util.Set; public class ImagePermanentsViewer extends JPanel { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private static final int POSITION_SPACING = 60; - private static final int HORIZONTAL_SPACING = 40; - private static final int VERTICAL_SPACING = 30; - private static final float CARD_WIDTH = (float) CardImagesProvider.CARD_WIDTH; - private static final float CARD_HEIGHT = (float) CardImagesProvider.CARD_HEIGHT; - private static final float CARD_ASPECT_RATIO = CARD_WIDTH / CARD_HEIGHT; + private static final int POSITION_SPACING = 60; + private static final int HORIZONTAL_SPACING = 40; + private static final int VERTICAL_SPACING = 30; + private static final float CARD_WIDTH = (float) CardImagesProvider.CARD_WIDTH; + private static final float CARD_HEIGHT = (float) CardImagesProvider.CARD_HEIGHT; + private static final float CARD_ASPECT_RATIO = CARD_WIDTH / CARD_HEIGHT; - private final GameController controller; - private final boolean isTop; - - private List viewers; - private Set validChoices; - - public ImagePermanentsViewer(final GameController controller) { - this(controller, false); - } - - public ImagePermanentsViewer(final GameController controller, final boolean isTop) { - this.controller = controller; - this.isTop = isTop; - - setLayout(null); - setOpaque(false); - - viewers=Collections.emptyList(); - validChoices=Collections.emptySet(); - } + private final GameController controller; + private final boolean isTop; + + private List viewers; + private Set validChoices; + + public ImagePermanentsViewer(final GameController controller) { + this(controller, false); + } + + public ImagePermanentsViewer(final GameController controller, final boolean isTop) { + this.controller = controller; + this.isTop = isTop; + + setLayout(null); + setOpaque(false); + + viewers=Collections.emptyList(); + validChoices=Collections.emptySet(); + } - private static int divideIntoRows(final List cards, final int maxCardsPerRow, final int startingRow) { - int numCardsThisRow = 0; - int currentRow = startingRow; - - for(ImagePermanentViewer card : cards) { - if(numCardsThisRow + 1 > maxCardsPerRow) { - // goto next row - currentRow++; - numCardsThisRow = 0; - } - - numCardsThisRow++; - card.setLogicalRow(currentRow); - } - - return currentRow; - } - - private int divideAllIntoRows(final List creatures, final List nonCreatures, final int maxCardsPerRow) { - final List firstCards = (isTop) ? nonCreatures : creatures; - final List secondCards = (isTop) ? creatures : nonCreatures; - - int currentRow = divideIntoRows(firstCards, maxCardsPerRow, 1); - if(firstCards.size() > 0) { // creatures go in separate row from others - currentRow++; - } - - return divideIntoRows(secondCards, maxCardsPerRow, currentRow); - } + private static int divideIntoRows(final List cards, final int maxCardsPerRow, final int startingRow) { + int numCardsThisRow = 0; + int currentRow = startingRow; + + for(ImagePermanentViewer card : cards) { + if(numCardsThisRow + 1 > maxCardsPerRow) { + // goto next row + currentRow++; + numCardsThisRow = 0; + } + + numCardsThisRow++; + card.setLogicalRow(currentRow); + } + + return currentRow; + } + + private int divideAllIntoRows(final List creatures, final List nonCreatures, final int maxCardsPerRow) { + final List firstCards = (isTop) ? nonCreatures : creatures; + final List secondCards = (isTop) ? creatures : nonCreatures; + + int currentRow = divideIntoRows(firstCards, maxCardsPerRow, 1); + if(firstCards.size() > 0) { // creatures go in separate row from others + currentRow++; + } + + return divideIntoRows(secondCards, maxCardsPerRow, currentRow); + } - private int calculateAndSetPositions(final List creatures, final List nonCreatures) { - int currentRow=1; - int x=0; - int y=0; - int maxWidth=0; - int rowHeight=0; - - List aViewers = new ArrayList(); - if(isTop) { - aViewers.addAll(nonCreatures); - aViewers.addAll(creatures); - } else { - aViewers.addAll(creatures); - aViewers.addAll(nonCreatures); - } - - int prevPosition=aViewers.get(0).getPosition(); + private int calculateAndSetPositions(final List creatures, final List nonCreatures) { + int currentRow=1; + int x=0; + int y=0; + int maxWidth=0; + int rowHeight=0; + + List aViewers = new ArrayList(); + if(isTop) { + aViewers.addAll(nonCreatures); + aViewers.addAll(creatures); + } else { + aViewers.addAll(creatures); + aViewers.addAll(nonCreatures); + } + + int prevPosition=aViewers.get(0).getPosition(); - for (final ImagePermanentViewer viewer : aViewers) { - if (currentRow!=viewer.getLogicalRow()) { - currentRow++; - x=0; - y+=VERTICAL_SPACING+rowHeight; - } - if (viewer.getPosition()!=prevPosition) { - prevPosition=viewer.getPosition(); - if (x>0) { - x+=POSITION_SPACING; - } - } - viewer.setLogicalPosition(new Point(x,y)); - final Dimension logicalSize=viewer.getLogicalSize(); - x+=logicalSize.width; - maxWidth=Math.max(maxWidth,x); - x+=HORIZONTAL_SPACING; - rowHeight=Math.max(rowHeight,logicalSize.height); - } + for (final ImagePermanentViewer viewer : aViewers) { + if (currentRow!=viewer.getLogicalRow()) { + currentRow++; + x=0; + y+=VERTICAL_SPACING+rowHeight; + } + if (viewer.getPosition()!=prevPosition) { + prevPosition=viewer.getPosition(); + if (x>0) { + x+=POSITION_SPACING; + } + } + viewer.setLogicalPosition(new Point(x,y)); + final Dimension logicalSize=viewer.getLogicalSize(); + x+=logicalSize.width; + maxWidth=Math.max(maxWidth,x); + x+=HORIZONTAL_SPACING; + rowHeight=Math.max(rowHeight,logicalSize.height); + } - final int maxHeight=y+rowHeight; - final int width=getWidth(); - final int height=getHeight(); - int scaleMult=width; - int scaleDiv=maxWidth; - if ((maxHeight*scaleMult)/scaleDiv>height) { - scaleMult=height; - scaleDiv=maxHeight; - } - if (scaleMult>scaleDiv/2) { - scaleMult=scaleDiv/2; - } - - for (final ImagePermanentViewer viewer : aViewers) { - final Point point=viewer.getLogicalPosition(); - viewer.setLocation((point.x*scaleMult)/scaleDiv,(point.y*scaleMult)/scaleDiv); - final Dimension size=viewer.getLogicalSize(); - viewer.setSize((size.width*scaleMult)/scaleDiv,(size.height*scaleMult)/scaleDiv); - } - - return (1000*scaleMult)/scaleDiv; - } + final int maxHeight=y+rowHeight; + final int width=getWidth(); + final int height=getHeight(); + int scaleMult=width; + int scaleDiv=maxWidth; + if ((maxHeight*scaleMult)/scaleDiv>height) { + scaleMult=height; + scaleDiv=maxHeight; + } + if (scaleMult>scaleDiv/2) { + scaleMult=scaleDiv/2; + } + + for (final ImagePermanentViewer viewer : aViewers) { + final Point point=viewer.getLogicalPosition(); + viewer.setLocation((point.x*scaleMult)/scaleDiv,(point.y*scaleMult)/scaleDiv); + final Dimension size=viewer.getLogicalSize(); + viewer.setSize((size.width*scaleMult)/scaleDiv,(size.height*scaleMult)/scaleDiv); + } + + return (1000*scaleMult)/scaleDiv; + } - private void calculateOptimalPositions(final List creatures, final List nonCreatures) { - final float screenWidth = (float) getWidth(); - final float screenHeight = (float) getHeight(); - final int numCards = creatures.size() + nonCreatures.size(); - - if(numCards > 0 && screenWidth > 0 && screenHeight > 0) { // ignore cases where drawing doesn't matter - int r; - int maxCardsForBestNumRow = 1; - float largestScaledCardSize = 0; - - // approximate number of rows needed to contain all the cards - for(r = (creatures.size() == 0 || nonCreatures.size() == 0) ? 1 : 2; r < numCards; r++) { - float numCardsPerRow = (float) Math.ceil((float) numCards / r); // avoid lost of precision - - // max width and height for a card using this number of rows - float scaledCardHeight = screenHeight / r; - float scaledCardWidth = screenWidth / numCardsPerRow; - - // change width or height to maintain aspect ratio - if (scaledCardWidth / scaledCardHeight > CARD_ASPECT_RATIO) { - // height is limiting factor on size of scaled card - scaledCardWidth = (scaledCardHeight / CARD_HEIGHT) * CARD_WIDTH; - } else { - // width is limiting factor on size of scaled card - scaledCardHeight = (scaledCardWidth / CARD_WIDTH) * CARD_HEIGHT;; - } - numCardsPerRow = (float) Math.ceil(screenWidth / scaledCardWidth); // scaled -> more cards can fit per row - - // set best possible - final float scaledCardSize = scaledCardWidth * scaledCardHeight; - if(scaledCardSize > largestScaledCardSize) { - largestScaledCardSize = scaledCardSize; - maxCardsForBestNumRow = (int) numCardsPerRow; - } - } - - divideAllIntoRows(creatures, nonCreatures, maxCardsForBestNumRow); - calculateAndSetPositions(creatures, nonCreatures); - } - } - - public void viewPermanents(final Collection permanentInfos) { - final List creatures = new ArrayList(); - final List nonCreatures = new ArrayList(); - final List newViewers = new ArrayList(); - - for (final PermanentViewerInfo permanentInfo : permanentInfos) { - ImagePermanentViewer perm = new ImagePermanentViewer(this,permanentInfo); - if(permanentInfo.creature) { - creatures.add(perm); - } else { - nonCreatures.add(perm); - } - newViewers.add(perm); // permanentInfos has a specific order - } - - calculateOptimalPositions(creatures, nonCreatures); - - removeAll(); - for (final ImagePermanentViewer viewer : newViewers) { - add(viewer); - } - viewers=newViewers; - revalidate(); - repaint(); - } - - public GameController getController() { - return controller; - } - - public void showValidChoices(final Set aValidChoices) { - this.validChoices=aValidChoices; - for (final ImagePermanentViewer viewer : viewers) { - viewer.repaint(); - } - } - - public boolean isValidChoice(final PermanentViewerInfo permanentInfo) { - return validChoices.contains(permanentInfo.permanent); - } + private void calculateOptimalPositions(final List creatures, final List nonCreatures) { + final float screenWidth = (float) getWidth(); + final float screenHeight = (float) getHeight(); + final int numCards = creatures.size() + nonCreatures.size(); + + if(numCards > 0 && screenWidth > 0 && screenHeight > 0) { // ignore cases where drawing doesn't matter + int r; + int maxCardsForBestNumRow = 1; + float largestScaledCardSize = 0; + + // approximate number of rows needed to contain all the cards + for(r = (creatures.size() == 0 || nonCreatures.size() == 0) ? 1 : 2; r < numCards; r++) { + float numCardsPerRow = (float) Math.ceil((float) numCards / r); // avoid lost of precision + + // max width and height for a card using this number of rows + float scaledCardHeight = screenHeight / r; + float scaledCardWidth = screenWidth / numCardsPerRow; + + // change width or height to maintain aspect ratio + if (scaledCardWidth / scaledCardHeight > CARD_ASPECT_RATIO) { + // height is limiting factor on size of scaled card + scaledCardWidth = (scaledCardHeight / CARD_HEIGHT) * CARD_WIDTH; + } else { + // width is limiting factor on size of scaled card + scaledCardHeight = (scaledCardWidth / CARD_WIDTH) * CARD_HEIGHT;; + } + numCardsPerRow = (float) Math.ceil(screenWidth / scaledCardWidth); // scaled -> more cards can fit per row + + // set best possible + final float scaledCardSize = scaledCardWidth * scaledCardHeight; + if(scaledCardSize > largestScaledCardSize) { + largestScaledCardSize = scaledCardSize; + maxCardsForBestNumRow = (int) numCardsPerRow; + } + } + + divideAllIntoRows(creatures, nonCreatures, maxCardsForBestNumRow); + calculateAndSetPositions(creatures, nonCreatures); + } + } + + public void viewPermanents(final Collection permanentInfos) { + final List creatures = new ArrayList(); + final List nonCreatures = new ArrayList(); + final List newViewers = new ArrayList(); + + for (final PermanentViewerInfo permanentInfo : permanentInfos) { + ImagePermanentViewer perm = new ImagePermanentViewer(this,permanentInfo); + if(permanentInfo.creature) { + creatures.add(perm); + } else { + nonCreatures.add(perm); + } + newViewers.add(perm); // permanentInfos has a specific order + } + + calculateOptimalPositions(creatures, nonCreatures); + + removeAll(); + for (final ImagePermanentViewer viewer : newViewers) { + add(viewer); + } + viewers=newViewers; + revalidate(); + repaint(); + } + + public GameController getController() { + return controller; + } + + public void showValidChoices(final Set aValidChoices) { + this.validChoices=aValidChoices; + for (final ImagePermanentViewer viewer : viewers) { + viewer.repaint(); + } + } + + public boolean isValidChoice(final PermanentViewerInfo permanentInfo) { + return validChoices.contains(permanentInfo.permanent); + } }