Add middle (and left+right) click zoom support to Image View

This commit is contained in:
drdev
2014-02-05 00:15:24 +00:00
parent e4dd0faf5e
commit d406114118
3 changed files with 82 additions and 64 deletions

View File

@@ -17,7 +17,6 @@
*/ */
package forge.gui.match.controllers; package forge.gui.match.controllers;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelListener;
@@ -31,6 +30,7 @@ import forge.game.card.Card;
import forge.gui.CardPicturePanel; import forge.gui.CardPicturePanel;
import forge.gui.framework.ICDoc; import forge.gui.framework.ICDoc;
import forge.gui.match.views.VPicture; import forge.gui.match.views.VPicture;
import forge.gui.toolbox.FMouseAdapter;
import forge.gui.toolbox.special.CardZoomer; import forge.gui.toolbox.special.CardZoomer;
import forge.item.IPaperCard; import forge.item.IPaperCard;
import forge.item.InventoryItem; import forge.item.InventoryItem;
@@ -118,46 +118,23 @@ public enum CPicture implements ICDoc {
* <li>Displays the alternate image for applicable cards on mouse click. * <li>Displays the alternate image for applicable cards on mouse click.
*/ */
private void setMouseButtonListener() { private void setMouseButtonListener() {
this.picturePanel.addMouseListener(new MouseAdapter() { this.picturePanel.addMouseListener(new FMouseAdapter() {
private final boolean[] buttonsDown = new boolean[4];
@Override @Override
public void mousePressed(final MouseEvent e) { public void onLeftClick(MouseEvent e) {
if (isCardDisplayed()) {
final int button = e.getButton();
if (button < 1 || button > 3) {
return;
}
this.buttonsDown[button] = true;
if (this.buttonsDown[2] || (this.buttonsDown[1] && this.buttonsDown[3])) {
zoomer.doMouseButtonZoom(currentCard, displayedState);
}
}
}
@Override
public void mouseReleased(final MouseEvent e) {
if (isCardDisplayed()) {
final int button = e.getButton();
if (button < 1 || button > 3) {
return;
}
if (!this.buttonsDown[button]) {
return;
}
this.buttonsDown[button] = false;
if (zoomer.isZoomerOpen()) {
if (!this.buttonsDown[1] && !this.buttonsDown[2] && !this.buttonsDown[3]) {
//don't stop zooming until all mouse buttons released
zoomer.closeZoomer();
}
return; //don't handle click event below if zoom was open
}
if (button == 1) {
flipCard(); flipCard();
} }
@Override
public void onMiddleMouseDown(MouseEvent e) {
if (isCardDisplayed()) {
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom(currentCard, displayedState);
}
}
@Override
public void onMiddleMouseUp(MouseEvent e) {
if (isCardDisplayed()) {
CardZoomer.SINGLETON_INSTANCE.closeZoomer();
} }
} }
}); });
@@ -219,10 +196,10 @@ public enum CPicture implements ICDoc {
} }
private boolean isCurrentCardFlippable() { private boolean isCurrentCardFlippable() {
boolean isFlippableMorph = if (currentCard == null) { return false; }
currentCard.isFaceDown() && isAuthorizedToViewFaceDownCard(currentCard);
return (currentCard != null) & return currentCard.isDoubleFaced() || currentCard.isFlipCard() ||
(currentCard.isDoubleFaced() || currentCard.isFlipCard() || isFlippableMorph); (currentCard.isFaceDown() && isAuthorizedToViewFaceDownCard(currentCard));
} }
/** /**

View File

@@ -52,12 +52,18 @@ public abstract class FMouseAdapter extends MouseAdapter {
private boolean hovered; private boolean hovered;
private Point mouseDownLoc; private Point mouseDownLoc;
private Point firstClickLoc; private Point firstClickLoc;
private int firstClickButton; private int downButton, buttonsDown, firstClickButton;
private Component tempMotionListenerComp; private Component tempMotionListenerComp;
@Override @Override
public final void mousePressed(final MouseEvent e) { public final void mousePressed(final MouseEvent e) {
switch (e.getButton()) { final int button = e.getButton();
if (button < 1 || button > 3) {
return;
}
if (downButton == 0) {
downButton = button;
switch (button) {
case 1: case 1:
onLeftMouseDown(e); onLeftMouseDown(e);
break; break;
@@ -68,6 +74,13 @@ public abstract class FMouseAdapter extends MouseAdapter {
onRightMouseDown(e); onRightMouseDown(e);
break; break;
} }
}
buttonsDown += button;
if (buttonsDown == 4 && downButton != 2) {
downButton = 2;
onMiddleMouseDown(e); //treat left and right together as equivalent of middle
}
mouseDownLoc = e.getLocationOnScreen(); mouseDownLoc = e.getLocationOnScreen();
@@ -90,7 +103,7 @@ public abstract class FMouseAdapter extends MouseAdapter {
if (firstClickLoc != null) { if (firstClickLoc != null) {
//if first mouse down resulted in click and second mouse down with same button in close proximity, //if first mouse down resulted in click and second mouse down with same button in close proximity,
//handle double click event (don't wait until second mouse up to improve responsiveness) //handle double click event (don't wait until second mouse up to improve responsiveness)
if (e.getClickCount() % 2 == 0 && e.getButton() == firstClickButton && if (e.getClickCount() % 2 == 0 && downButton == firstClickButton &&
e.getLocationOnScreen().distance(firstClickLoc) <= 3) { e.getLocationOnScreen().distance(firstClickLoc) <= 3) {
switch (firstClickButton) { switch (firstClickButton) {
case 1: case 1:
@@ -117,7 +130,7 @@ public abstract class FMouseAdapter extends MouseAdapter {
resetMouseDownLoc(); resetMouseDownLoc();
} }
if (tempMotionListenerComp == null) { //don't raise drag event if only a temporary motion listener if (tempMotionListenerComp == null) { //don't raise drag event if only a temporary motion listener
switch (e.getButton()) { switch (downButton) {
case 1: case 1:
onLeftMouseDragging(e); onLeftMouseDragging(e);
break; break;
@@ -153,7 +166,17 @@ public abstract class FMouseAdapter extends MouseAdapter {
@Override @Override
public final void mouseReleased(MouseEvent e) { public final void mouseReleased(MouseEvent e) {
switch (e.getButton()) { int button = e.getButton();
if (button < 1 || button > 3 || downButton == 0) {
return;
}
buttonsDown -= button;
if (buttonsDown > 0) { return; } //don't handle mouse up until all mouse buttons up
button = downButton;
downButton = 0;
switch (button) {
case 1: case 1:
onLeftMouseUp(e); onLeftMouseUp(e);
break; break;
@@ -173,13 +196,13 @@ public abstract class FMouseAdapter extends MouseAdapter {
if (mouseDownLoc != null) { if (mouseDownLoc != null) {
//if first click, cache button and mouse down location for determination of double click later //if first click, cache button and mouse down location for determination of double click later
if (e.getClickCount() % 2 == 1) { if (e.getClickCount() % 2 == 1) {
firstClickButton = e.getButton(); firstClickButton = button;
firstClickLoc = mouseDownLoc; firstClickLoc = mouseDownLoc;
} }
resetMouseDownLoc(); resetMouseDownLoc();
switch (e.getButton()) { switch (button) {
case 1: case 1:
onLeftClick(e); onLeftClick(e);
break; break;
@@ -192,7 +215,7 @@ public abstract class FMouseAdapter extends MouseAdapter {
} }
} }
else if (isCompDraggable) { //handle drag drop if click not handled and component is draggable else if (isCompDraggable) { //handle drag drop if click not handled and component is draggable
switch (e.getButton()) { switch (button) {
case 1: case 1:
onLeftMouseDragDrop(e); onLeftMouseDragDrop(e);
break; break;

View File

@@ -25,6 +25,7 @@ import javax.swing.JViewport;
import javax.swing.ScrollPaneConstants; import javax.swing.ScrollPaneConstants;
import forge.ImageCache; import forge.ImageCache;
import forge.game.card.Card;
import forge.gui.deckeditor.DeckProxy; import forge.gui.deckeditor.DeckProxy;
import forge.gui.framework.ILocalRepaint; import forge.gui.framework.ILocalRepaint;
import forge.gui.match.controllers.CDetail; import forge.gui.match.controllers.CDetail;
@@ -37,6 +38,8 @@ import forge.gui.toolbox.FSkin.SkinFont;
import forge.gui.toolbox.FSkin.SkinImage; import forge.gui.toolbox.FSkin.SkinImage;
import forge.gui.toolbox.itemmanager.ItemManager; import forge.gui.toolbox.itemmanager.ItemManager;
import forge.gui.toolbox.itemmanager.ItemManagerModel; import forge.gui.toolbox.itemmanager.ItemManagerModel;
import forge.gui.toolbox.special.CardZoomer;
import forge.item.IPaperCard;
import forge.item.InventoryItem; import forge.item.InventoryItem;
import forge.view.arcane.CardPanel; import forge.view.arcane.CardPanel;
@@ -88,11 +91,26 @@ public class ImageView<T extends InventoryItem> extends ItemView<T> {
@Override @Override
public void onLeftDoubleClick(MouseEvent e) { public void onLeftDoubleClick(MouseEvent e) {
if (hoveredItem != null && hoveredItem.selected) { ItemInfo item = getItemAtPoint(e.getPoint());
if (item != null && item.selected) {
itemManager.activateSelectedItems(); itemManager.activateSelectedItems();
} }
} }
@Override
public void onMiddleMouseDown(MouseEvent e) {
ItemInfo item = getItemAtPoint(e.getPoint());
if (item != null && item.item instanceof IPaperCard) {
Card card = Card.getCardForUi((IPaperCard) item.item);
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom(card);
}
}
@Override
public void onMiddleMouseUp(MouseEvent e) {
CardZoomer.SINGLETON_INSTANCE.closeZoomer();
}
@Override @Override
public void onRightClick(MouseEvent e) { public void onRightClick(MouseEvent e) {
selectItem(e); selectItem(e);