changed tabs to four spaces

master
melvin 2012-06-16 11:24:36 +08:00
parent e55264be16
commit fae1f5f72a
1 changed files with 221 additions and 221 deletions

View File

@ -29,231 +29,231 @@ import java.util.List;
public class ImagePermanentViewer extends JPanel {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private static final int LOGICAL_X_MARGIN=50;
private static final int LOGICAL_Y_MARGIN=70;
private final ImagePermanentsViewer viewer;
private final PermanentViewerInfo permanentInfo;
private final List<PermanentViewerInfo> linkedInfos;
private final Dimension logicalSize;
private final List<Rectangle> linkedLogicalRectangles;
private List<Rectangle> linkedScreenRectangles;
private Point logicalPosition;
private int logicalRow=1;
public ImagePermanentViewer(final ImagePermanentsViewer viewer,final PermanentViewerInfo permanentInfo) {
this.viewer=viewer;
this.permanentInfo=permanentInfo;
linkedInfos=new ArrayList<PermanentViewerInfo>();
buildLinkedPermanents(linkedInfos,permanentInfo);
linkedLogicalRectangles=new ArrayList<Rectangle>();
logicalSize=calculateLogicalSize(linkedLogicalRectangles);
linkedScreenRectangles=Collections.emptyList();
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent event) {
final int index=getPermanentInfoIndexAt(event.getX(),event.getY());
if (index>=0) {
if (!GeneralConfig.getInstance().isTouchscreen()){
viewer.getController().processClick(linkedInfos.get(index).permanent);
}
else if (event.getClickCount() == 2) {
viewer.getController().processClick(linkedInfos.get(index).permanent);
viewer.getController().hideInfo();
}
}
}
@Override
public void mouseExited(final MouseEvent event) {
viewer.getController().hideInfo();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(final MouseEvent event) {
final int index=getPermanentInfoIndexAt(event.getX(),event.getY());
if (index>=0) {
final PermanentViewerInfo info=linkedInfos.get(index);
final Point pointOnScreen=getLocationOnScreen();
final Rectangle rect=new Rectangle(linkedScreenRectangles.get(index));
rect.x+=pointOnScreen.x;
rect.y+=pointOnScreen.y;
viewer.getController().viewInfoAbove(info.cardDefinition,info.index,rect);
} else {
viewer.getController().hideInfo();
}
}
});
}
private int getPermanentInfoIndexAt(final int x,final int y) {
for (int index=linkedScreenRectangles.size()-1;index>=0;index--) {
final Rectangle rect=linkedScreenRectangles.get(index);
if (x>=rect.x&&y>=rect.y&&x<rect.x+rect.width&&y<rect.y+rect.height) {
return index;
}
}
return -1;
}
private static final int LOGICAL_X_MARGIN=50;
private static final int LOGICAL_Y_MARGIN=70;
private final ImagePermanentsViewer viewer;
private final PermanentViewerInfo permanentInfo;
private final List<PermanentViewerInfo> linkedInfos;
private final Dimension logicalSize;
private final List<Rectangle> linkedLogicalRectangles;
private List<Rectangle> linkedScreenRectangles;
private Point logicalPosition;
private int logicalRow=1;
public ImagePermanentViewer(final ImagePermanentsViewer viewer,final PermanentViewerInfo permanentInfo) {
this.viewer=viewer;
this.permanentInfo=permanentInfo;
linkedInfos=new ArrayList<PermanentViewerInfo>();
buildLinkedPermanents(linkedInfos,permanentInfo);
linkedLogicalRectangles=new ArrayList<Rectangle>();
logicalSize=calculateLogicalSize(linkedLogicalRectangles);
linkedScreenRectangles=Collections.emptyList();
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent event) {
final int index=getPermanentInfoIndexAt(event.getX(),event.getY());
if (index>=0) {
if (!GeneralConfig.getInstance().isTouchscreen()){
viewer.getController().processClick(linkedInfos.get(index).permanent);
}
else if (event.getClickCount() == 2) {
viewer.getController().processClick(linkedInfos.get(index).permanent);
viewer.getController().hideInfo();
}
}
}
@Override
public void mouseExited(final MouseEvent event) {
viewer.getController().hideInfo();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(final MouseEvent event) {
final int index=getPermanentInfoIndexAt(event.getX(),event.getY());
if (index>=0) {
final PermanentViewerInfo info=linkedInfos.get(index);
final Point pointOnScreen=getLocationOnScreen();
final Rectangle rect=new Rectangle(linkedScreenRectangles.get(index));
rect.x+=pointOnScreen.x;
rect.y+=pointOnScreen.y;
viewer.getController().viewInfoAbove(info.cardDefinition,info.index,rect);
} else {
viewer.getController().hideInfo();
}
}
});
}
private int getPermanentInfoIndexAt(final int x,final int y) {
for (int index=linkedScreenRectangles.size()-1;index>=0;index--) {
final Rectangle rect=linkedScreenRectangles.get(index);
if (x>=rect.x&&y>=rect.y&&x<rect.x+rect.width&&y<rect.y+rect.height) {
return index;
}
}
return -1;
}
private void buildLinkedPermanents(final List<PermanentViewerInfo> aLinkedInfos,final PermanentViewerInfo info) {
for (final PermanentViewerInfo blocker : info.blockers) {
buildLinkedPermanents(aLinkedInfos,blocker);
}
aLinkedInfos.addAll(info.linked);
aLinkedInfos.add(info);
}
private Dimension calculateLogicalSize(final List<Rectangle> aLinkedLogicalRectangles) {
int width=0;
int height=0;
int x=-LOGICAL_X_MARGIN;
for (final PermanentViewerInfo linkedInfo : linkedInfos) {
x+=LOGICAL_X_MARGIN;
final int y=linkedInfo.lowered?LOGICAL_Y_MARGIN:0;
final Rectangle rect;
if (linkedInfo.tapped) {
width=Math.max(width,x+CardImagesProvider.CARD_HEIGHT);
height=Math.max(height,y+CardImagesProvider.CARD_WIDTH);
rect=new Rectangle(x,y,CardImagesProvider.CARD_HEIGHT,CardImagesProvider.CARD_WIDTH);
} else {
width=Math.max(width,x+CardImagesProvider.CARD_WIDTH);
height=Math.max(height,y+CardImagesProvider.CARD_HEIGHT);
rect=new Rectangle(x,y,CardImagesProvider.CARD_WIDTH,CardImagesProvider.CARD_HEIGHT);
}
aLinkedLogicalRectangles.add(rect);
}
return new Dimension(width,height);
}
@Override
public void setSize(final int width,final int height) {
super.setSize(width,height);
linkedScreenRectangles=new ArrayList<Rectangle>();
for (final Rectangle logicalRect : linkedLogicalRectangles) {
final Rectangle screenRect=new Rectangle();
screenRect.x=(logicalRect.x*width)/logicalSize.width;
screenRect.y=(logicalRect.y*height)/logicalSize.height;
screenRect.width=(logicalRect.width*width)/logicalSize.width;
screenRect.height=(logicalRect.height*height)/logicalSize.height;
linkedScreenRectangles.add(screenRect);
}
}
public int getPosition() {
return permanentInfo.position;
}
public Dimension getLogicalSize() {
return logicalSize;
}
public void setLogicalPosition(final Point logicalPosition) {
this.logicalPosition=logicalPosition;
}
public Point getLogicalPosition() {
return logicalPosition;
}
public void setLogicalRow(final int logicalRow) {
this.logicalRow=logicalRow;
}
public int getLogicalRow() {
return logicalRow;
}
@Override
public void paintComponent(final Graphics g) {
g.setFont(FontsAndBorders.FONT1);
final FontMetrics metrics=g.getFontMetrics();
final Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
for (int index=0;index<linkedScreenRectangles.size();index++) {
final PermanentViewerInfo linkedInfo=linkedInfos.get(index);
final BufferedImage image=
private void buildLinkedPermanents(final List<PermanentViewerInfo> aLinkedInfos,final PermanentViewerInfo info) {
for (final PermanentViewerInfo blocker : info.blockers) {
buildLinkedPermanents(aLinkedInfos,blocker);
}
aLinkedInfos.addAll(info.linked);
aLinkedInfos.add(info);
}
private Dimension calculateLogicalSize(final List<Rectangle> aLinkedLogicalRectangles) {
int width=0;
int height=0;
int x=-LOGICAL_X_MARGIN;
for (final PermanentViewerInfo linkedInfo : linkedInfos) {
x+=LOGICAL_X_MARGIN;
final int y=linkedInfo.lowered?LOGICAL_Y_MARGIN:0;
final Rectangle rect;
if (linkedInfo.tapped) {
width=Math.max(width,x+CardImagesProvider.CARD_HEIGHT);
height=Math.max(height,y+CardImagesProvider.CARD_WIDTH);
rect=new Rectangle(x,y,CardImagesProvider.CARD_HEIGHT,CardImagesProvider.CARD_WIDTH);
} else {
width=Math.max(width,x+CardImagesProvider.CARD_WIDTH);
height=Math.max(height,y+CardImagesProvider.CARD_HEIGHT);
rect=new Rectangle(x,y,CardImagesProvider.CARD_WIDTH,CardImagesProvider.CARD_HEIGHT);
}
aLinkedLogicalRectangles.add(rect);
}
return new Dimension(width,height);
}
@Override
public void setSize(final int width,final int height) {
super.setSize(width,height);
linkedScreenRectangles=new ArrayList<Rectangle>();
for (final Rectangle logicalRect : linkedLogicalRectangles) {
final Rectangle screenRect=new Rectangle();
screenRect.x=(logicalRect.x*width)/logicalSize.width;
screenRect.y=(logicalRect.y*height)/logicalSize.height;
screenRect.width=(logicalRect.width*width)/logicalSize.width;
screenRect.height=(logicalRect.height*height)/logicalSize.height;
linkedScreenRectangles.add(screenRect);
}
}
public int getPosition() {
return permanentInfo.position;
}
public Dimension getLogicalSize() {
return logicalSize;
}
public void setLogicalPosition(final Point logicalPosition) {
this.logicalPosition=logicalPosition;
}
public Point getLogicalPosition() {
return logicalPosition;
}
public void setLogicalRow(final int logicalRow) {
this.logicalRow=logicalRow;
}
public int getLogicalRow() {
return logicalRow;
}
@Override
public void paintComponent(final Graphics g) {
g.setFont(FontsAndBorders.FONT1);
final FontMetrics metrics=g.getFontMetrics();
final Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
for (int index=0;index<linkedScreenRectangles.size();index++) {
final PermanentViewerInfo linkedInfo=linkedInfos.get(index);
final BufferedImage image=
HighQualityCardImagesProvider.getInstance().getImage(linkedInfo.cardDefinition,linkedInfo.index,false);
final Rectangle linkedRect=linkedScreenRectangles.get(index);
final int x1=linkedRect.x;
final int y1=linkedRect.y;
final int x2=x1+linkedRect.width-1;
final int y2=y1+linkedRect.height-1;
final Rectangle linkedRect=linkedScreenRectangles.get(index);
final int x1=linkedRect.x;
final int y1=linkedRect.y;
final int x2=x1+linkedRect.width-1;
final int y2=y1+linkedRect.height-1;
if (linkedInfo.tapped) {
final AffineTransform transform=new AffineTransform();
final float scale=((float)linkedRect.width)/CardImagesProvider.CARD_HEIGHT;
transform.translate(x1,y1);
transform.scale(scale,scale);
transform.translate(CardImagesProvider.CARD_HEIGHT/2,CardImagesProvider.CARD_WIDTH/2);
transform.rotate(Math.PI/2);
transform.translate(-CardImagesProvider.CARD_WIDTH/2,-CardImagesProvider.CARD_HEIGHT/2);
g2d.drawImage(image,transform,this);
} else {
g.drawImage(image,x1,y1,x2,y2,0,0,CardImagesProvider.CARD_WIDTH,CardImagesProvider.CARD_HEIGHT,this);
}
if (linkedInfo.tapped) {
final AffineTransform transform=new AffineTransform();
final float scale=((float)linkedRect.width)/CardImagesProvider.CARD_HEIGHT;
transform.translate(x1,y1);
transform.scale(scale,scale);
transform.translate(CardImagesProvider.CARD_HEIGHT/2,CardImagesProvider.CARD_WIDTH/2);
transform.rotate(Math.PI/2);
transform.translate(-CardImagesProvider.CARD_WIDTH/2,-CardImagesProvider.CARD_HEIGHT/2);
g2d.drawImage(image,transform,this);
} else {
g.drawImage(image,x1,y1,x2,y2,0,0,CardImagesProvider.CARD_WIDTH,CardImagesProvider.CARD_HEIGHT,this);
}
int ax=x1+1;
final int ay=y2-17;
// Counters
if (linkedInfo.permanent.hasCounters()) {
ax=ImageDrawingUtils.drawCountersInfo(g,this,linkedInfo.permanent,ax,ay);
}
if (permanentInfo.creature) {
// Common combat ability icons.
final long abilityFlags=linkedInfo.abilityFlags;
if (linkedInfo.canNotTap) {
g.drawImage(IconImages.CANNOTTAP.getImage(),ax,ay,this);
ax+=16;
}
ax=ImageDrawingUtils.drawAbilityInfo(g,this, abilityFlags,ax,ay);
}
if (permanentInfo.cardDefinition.isLand()) {
ax=ImageDrawingUtils.drawManaInfo(g,this,permanentInfo.cardDefinition,ax,ay);
}
final String pt=linkedInfo.powerToughness;
if (!pt.isEmpty()) {
final String damage=linkedInfo.damage>0?String.valueOf(linkedInfo.damage):"";
int ax=x1+1;
final int ay=y2-17;
// Counters
if (linkedInfo.permanent.hasCounters()) {
ax=ImageDrawingUtils.drawCountersInfo(g,this,linkedInfo.permanent,ax,ay);
}
if (permanentInfo.creature) {
// Common combat ability icons.
final long abilityFlags=linkedInfo.abilityFlags;
if (linkedInfo.canNotTap) {
g.drawImage(IconImages.CANNOTTAP.getImage(),ax,ay,this);
ax+=16;
}
ax=ImageDrawingUtils.drawAbilityInfo(g,this, abilityFlags,ax,ay);
}
if (permanentInfo.cardDefinition.isLand()) {
ax=ImageDrawingUtils.drawManaInfo(g,this,permanentInfo.cardDefinition,ax,ay);
}
final String pt=linkedInfo.powerToughness;
if (!pt.isEmpty()) {
final String damage=linkedInfo.damage>0?String.valueOf(linkedInfo.damage):"";
final boolean isDamage = damage.length() > 0;
final int ptWidth=metrics.stringWidth(pt);
if (linkedInfo.blocking) {
ImageDrawingUtils.drawCreatureInfo(g,metrics,pt,ptWidth,damage,x1,y1,false);
} else {
ImageDrawingUtils.drawCreatureInfo(g,metrics,pt,ptWidth,damage,x2-ptWidth-4,y2-(isDamage?32:18),true);
}
}
if (viewer.isValidChoice(linkedInfo)) {
if (GeneralConfig.getInstance().isHighlightOverlay() ||
(GeneralConfig.getInstance().isHighlightTheme() &&
ThemeFactory.getInstance().getCurrentTheme().getOptionUseOverlay())) {
final Color choiceColor = viewer.getController().isCombatChoice() ?
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_COMBAT_CHOICE) :
ThemeFactory.getInstance().getCurrentTheme().getChoiceColor();
//draw a transparent overlay of choiceColor
g2d.setPaint(choiceColor);
g2d.fillRect(x1-1,y1-1,x2-x1+2,y2-y1+2);
}
else if (!GeneralConfig.getInstance().isHighlightNone()){
final Color choiceColor = viewer.getController().isCombatChoice() ?
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_COMBAT_CHOICE_BORDER) :
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_CHOICE_BORDER);
//draw a one pixel border of choiceColor
g2d.setPaint(new Color(choiceColor.getRGB()));
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
}
}
}
}
final int ptWidth=metrics.stringWidth(pt);
if (linkedInfo.blocking) {
ImageDrawingUtils.drawCreatureInfo(g,metrics,pt,ptWidth,damage,x1,y1,false);
} else {
ImageDrawingUtils.drawCreatureInfo(g,metrics,pt,ptWidth,damage,x2-ptWidth-4,y2-(isDamage?32:18),true);
}
}
if (viewer.isValidChoice(linkedInfo)) {
if (GeneralConfig.getInstance().isHighlightOverlay() ||
(GeneralConfig.getInstance().isHighlightTheme() &&
ThemeFactory.getInstance().getCurrentTheme().getOptionUseOverlay())) {
final Color choiceColor = viewer.getController().isCombatChoice() ?
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_COMBAT_CHOICE) :
ThemeFactory.getInstance().getCurrentTheme().getChoiceColor();
//draw a transparent overlay of choiceColor
g2d.setPaint(choiceColor);
g2d.fillRect(x1-1,y1-1,x2-x1+2,y2-y1+2);
}
else if (!GeneralConfig.getInstance().isHighlightNone()){
final Color choiceColor = viewer.getController().isCombatChoice() ?
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_COMBAT_CHOICE_BORDER) :
ThemeFactory.getInstance().getCurrentTheme().getColor(Theme.COLOR_CHOICE_BORDER);
//draw a one pixel border of choiceColor
g2d.setPaint(new Color(choiceColor.getRGB()));
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
}
}
}
}
}