Refactor so CardZoom can support multiple cards

This commit is contained in:
drdev
2014-11-30 04:14:14 +00:00
parent 27980776d0
commit 4e2f753374
12 changed files with 91 additions and 53 deletions

View File

@@ -79,6 +79,7 @@ public class CardView extends GameEntityView {
this(id0, name0); this(id0, name0);
set(TrackableProperty.Owner, ownerAndController); set(TrackableProperty.Owner, ownerAndController);
set(TrackableProperty.Controller, ownerAndController); set(TrackableProperty.Controller, ownerAndController);
set(TrackableProperty.ImageKey, imageKey);
} }
public PlayerView getOwner() { public PlayerView getOwner() {

View File

@@ -363,18 +363,18 @@ public class CardRenderer {
g.drawText(type, typeFont, foreColor, x, y, availableTypeWidth, lineHeight, false, HAlignment.LEFT, true); g.drawText(type, typeFont, foreColor, x, y, availableTypeWidth, lineHeight, false, HAlignment.LEFT, true);
} }
public static boolean cardListItemTap(CardView card, float x, float y, int count, boolean compactMode) { public static boolean cardListItemTap(List<?> cards, int selectedIndex, float x, float y, int count, boolean compactMode) {
if (x <= getCardListItemHeight(compactMode) * CARD_ART_RATIO) { if (x <= getCardListItemHeight(compactMode) * CARD_ART_RATIO) {
CardZoom.show(card); CardZoom.show(cards, selectedIndex);
return true; return true;
} }
return false; return false;
} }
public static boolean cardListItemTap(IPaperCard pc, float x, float y, int count, boolean compactMode) { public static boolean paperCardListItemTap(List<?> cards, int selectedIndex, float x, float y, int count, boolean compactMode) {
float cardArtHeight = getCardListItemHeight(compactMode); float cardArtHeight = getCardListItemHeight(compactMode);
float cardArtWidth = cardArtHeight * CARD_ART_RATIO; float cardArtWidth = cardArtHeight * CARD_ART_RATIO;
if (x <= cardArtWidth && y <= cardArtHeight) { if (x <= cardArtWidth && y <= cardArtHeight) {
CardZoom.show(CardView.getCardForUi(pc)); CardZoom.show(cards, selectedIndex);
return true; return true;
} }
return false; return false;

View File

@@ -1,29 +1,43 @@
package forge.card; package forge.card;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import forge.Graphics; import forge.Graphics;
import forge.ImageKeys;
import forge.assets.FSkinColor; import forge.assets.FSkinColor;
import forge.assets.FSkinFont; import forge.assets.FSkinFont;
import forge.game.card.CardView; import forge.game.card.CardView;
import forge.item.IPaperCard; import forge.item.IPaperCard;
import forge.item.InventoryItem;
import forge.toolbox.FList; import forge.toolbox.FList;
import forge.toolbox.FOverlay; import forge.toolbox.FOverlay;
import forge.util.FCollectionView;
import forge.util.Utils; import forge.util.Utils;
public class CardZoom extends FOverlay { public class CardZoom extends FOverlay {
private static final float TAB_HEIGHT = Utils.AVG_FINGER_HEIGHT; private static final float TAB_HEIGHT = Utils.AVG_FINGER_HEIGHT;
private static final FSkinFont FONT = FSkinFont.get(14); private static final FSkinFont FONT = FSkinFont.get(14);
private static final CardZoom cardZoom = new CardZoom(); private static final CardZoom cardZoom = new CardZoom();
private static CardView card; private static List<?> items;
private static int currentIndex;
private static CardView currentCard, prevCard, nextCard;
private static boolean zoomMode = true; private static boolean zoomMode = true;
public static <T> void show(final IPaperCard pc0) { public static <T> void show(final Object card) {
card = CardView.getCardForUi(pc0); List<Object> cards0 = new ArrayList<Object>();
cardZoom.show(); show(cards0, 0);
} }
public static <T> void show(final CardView card0) { public static <T> void show(final FCollectionView<?> items0, int currentIndex0) {
card = card0; show((List<?>)items0, currentIndex0);
}
public static <T> void show(final List<?> items0, int currentIndex0) {
items = items0;
currentIndex = currentIndex0;
updateVisibleCards();
cardZoom.show(); cardZoom.show();
} }
@@ -38,6 +52,29 @@ public class CardZoom extends FOverlay {
private CardZoom() { private CardZoom() {
} }
private static void updateVisibleCards() {
currentCard = getCardView(items.get(currentIndex));
prevCard = currentIndex > 0 ? getCardView(items.get(currentIndex - 1)) : null;
nextCard = currentIndex < items.size() - 1 ? getCardView(items.get(currentIndex + 1)) : null;
}
private static CardView getCardView(Object item) {
if (item instanceof Entry) {
item = ((Entry<?, ?>)item).getKey();
}
if (item instanceof CardView) {
return (CardView)item;
}
if (item instanceof IPaperCard) {
return CardView.getCardForUi((IPaperCard)item);
}
if (item instanceof InventoryItem) {
InventoryItem ii = (InventoryItem)item;
return new CardView(-1, ii.getName(), null, ImageKeys.getImageKey(ii, false));
}
return new CardView(-1, item.toString());
}
@Override @Override
public boolean tap(float x, float y, int count) { public boolean tap(float x, float y, int count) {
if (y >= getHeight() - TAB_HEIGHT && zoomMode != (x < getWidth() / 2)) { if (y >= getHeight() - TAB_HEIGHT && zoomMode != (x < getWidth() / 2)) {
@@ -68,8 +105,8 @@ public class CardZoom extends FOverlay {
//draw zoom/details options //draw zoom/details options
FSkinColor foreColor; FSkinColor foreColor;
if (zoomMode) { if (zoomMode) {
if (!CardRenderer.drawZoom(g, card, w, y)) { if (!CardRenderer.drawZoom(g, currentCard, w, y)) {
CardRenderer.drawDetails(g, card, w, y); //draw details if can't draw zoom CardRenderer.drawDetails(g, currentCard, w, y); //draw details if can't draw zoom
} }
g.fillRect(FList.PRESSED_COLOR, 0, y, x, h); g.fillRect(FList.PRESSED_COLOR, 0, y, x, h);
foreColor = FList.FORE_COLOR; foreColor = FList.FORE_COLOR;
@@ -80,7 +117,7 @@ public class CardZoom extends FOverlay {
g.drawText("Zoom", FONT, foreColor, 0, y, x, h, false, HAlignment.CENTER, true); g.drawText("Zoom", FONT, foreColor, 0, y, x, h, false, HAlignment.CENTER, true);
if (!zoomMode) { if (!zoomMode) {
CardRenderer.drawDetails(g, card, w, y); CardRenderer.drawDetails(g, currentCard, w, y);
g.fillRect(FList.PRESSED_COLOR, x, y, w - x, h); g.fillRect(FList.PRESSED_COLOR, x, y, w - x, h);
foreColor = FList.FORE_COLOR; foreColor = FList.FORE_COLOR;
} }

View File

@@ -60,13 +60,13 @@ public class CardManager extends ItemManager<PaperCard> {
} }
@Override @Override
public boolean tap(Entry<PaperCard, Integer> value, float x, float y, int count) { public boolean tap(Integer index, Entry<PaperCard, Integer> value, float x, float y, int count) {
return CardRenderer.cardListItemTap(value.getKey(), x, y, count, compactModeHandler.isCompactMode()); return CardRenderer.cardListItemTap(model.getOrderedList(), index, x, y, count, compactModeHandler.isCompactMode());
} }
@Override @Override
public boolean longPress(Entry<PaperCard, Integer> value, float x, float y) { public boolean longPress(Integer index, Entry<PaperCard, Integer> value, float x, float y) {
CardZoom.show(value.getKey()); CardZoom.show(getFilteredItems().toFlatList(), index);
return true; return true;
} }
}; };

View File

@@ -92,7 +92,7 @@ public final class DeckManager extends ItemManager<DeckProxy> implements IHasGam
} }
@Override @Override
public boolean tap(Entry<DeckProxy, Integer> value, float x, float y, int count) { public boolean tap(Integer index, Entry<DeckProxy, Integer> value, float x, float y, int count) {
float bottomRight = IMAGE_SIZE + 2 * FList.PADDING; float bottomRight = IMAGE_SIZE + 2 * FList.PADDING;
if (x <= bottomRight && y <= bottomRight) { if (x <= bottomRight && y <= bottomRight) {
DeckPreferences prefs = DeckPreferences.getPrefs(value.getKey()); DeckPreferences prefs = DeckPreferences.getPrefs(value.getKey());
@@ -103,7 +103,7 @@ public final class DeckManager extends ItemManager<DeckProxy> implements IHasGam
} }
@Override @Override
public boolean longPress(Entry<DeckProxy, Integer> value, float x, float y) { public boolean longPress(Integer index, Entry<DeckProxy, Integer> value, float x, float y) {
FDeckViewer.show(value.getKey().getDeck()); FDeckViewer.show(value.getKey().getDeck());
return true; return true;
} }

View File

@@ -55,7 +55,7 @@ import java.util.Map.Entry;
public abstract class ItemManager<T extends InventoryItem> extends FContainer implements IItemManager<T> { public abstract class ItemManager<T extends InventoryItem> extends FContainer implements IItemManager<T> {
private ItemPool<T> pool; private ItemPool<T> pool;
private final ItemManagerModel<T> model; protected final ItemManagerModel<T> model;
private Predicate<? super T> filterPredicate = null; private Predicate<? super T> filterPredicate = null;
private final List<ItemFilter<? extends T>> filters = new ArrayList<ItemFilter<? extends T>>(); private final List<ItemFilter<? extends T>> filters = new ArrayList<ItemFilter<? extends T>>();
private boolean hideFilters = false; private boolean hideFilters = false;
@@ -254,8 +254,8 @@ public abstract class ItemManager<T extends InventoryItem> extends FContainer im
public abstract class ItemRenderer { public abstract class ItemRenderer {
public abstract float getItemHeight(); public abstract float getItemHeight();
public abstract boolean tap(Entry<T, Integer> value, float x, float y, int count); public abstract boolean tap(Integer index, Entry<T, Integer> value, float x, float y, int count);
public abstract boolean longPress(Entry<T, Integer> value, float x, float y); public abstract boolean longPress(Integer index, Entry<T, Integer> value, float x, float y);
public abstract void drawValue(Graphics g, Entry<T, Integer> value, FSkinFont font, FSkinColor foreColor, FSkinColor backColor, boolean pressed, float x, float y, float w, float h); public abstract void drawValue(Graphics g, Entry<T, Integer> value, FSkinFont font, FSkinColor foreColor, FSkinColor backColor, boolean pressed, float x, float y, float w, float h);
} }
public abstract ItemRenderer getListItemRenderer(final CompactModeHandler compactModeHandler); public abstract ItemRenderer getListItemRenderer(final CompactModeHandler compactModeHandler);

View File

@@ -86,17 +86,17 @@ public final class SpellShopManager extends ItemManager<InventoryItem> {
} }
@Override @Override
public boolean tap(Entry<InventoryItem, Integer> value, float x, float y, int count) { public boolean tap(Integer index, Entry<InventoryItem, Integer> value, float x, float y, int count) {
if (value.getKey() instanceof PaperCard) { if (value.getKey() instanceof PaperCard) {
return CardRenderer.cardListItemTap((PaperCard)value.getKey(), x, y, count, compactModeHandler.isCompactMode()); return CardRenderer.cardListItemTap(model.getOrderedList(), index, x, y, count, compactModeHandler.isCompactMode());
} }
return false; return false;
} }
@Override @Override
public boolean longPress(Entry<InventoryItem, Integer> value, float x, float y) { public boolean longPress(Integer index, Entry<InventoryItem, Integer> value, float x, float y) {
if (value.getKey() instanceof PaperCard) { if (value.getKey() instanceof PaperCard) {
CardZoom.show((PaperCard)value.getKey()); CardZoom.show(model.getOrderedList(), index);
return true; return true;
} }
return false; return false;

View File

@@ -12,8 +12,6 @@ import forge.card.CardRenderer;
import forge.card.CardRenderer.CardStackPosition; import forge.card.CardRenderer.CardStackPosition;
import forge.card.CardZoom; import forge.card.CardZoom;
import forge.deck.DeckProxy; import forge.deck.DeckProxy;
import forge.game.card.CardView;
import forge.item.IPaperCard;
import forge.item.InventoryItem; import forge.item.InventoryItem;
import forge.item.PaperCard; import forge.item.PaperCard;
import forge.itemmanager.ColumnDef; import forge.itemmanager.ColumnDef;
@@ -873,8 +871,8 @@ public class ImageView<T extends InventoryItem> extends ItemView<T> {
@Override @Override
public boolean longPress(float x, float y) { public boolean longPress(float x, float y) {
ItemInfo item = getItemAtPoint(x + getLeft(), y + getTop()); ItemInfo item = getItemAtPoint(x + getLeft(), y + getTop());
if (item != null && item.item instanceof IPaperCard) { if (item != null) {
CardZoom.show(CardView.getCardForUi((IPaperCard) item.item)); CardZoom.show(orderedItems, orderedItems.indexOf(item));
return true; return true;
} }
return false; return false;

View File

@@ -218,7 +218,7 @@ public final class ItemListView<T extends InventoryItem> extends ItemView<T> {
else { else {
setSelectedIndex(index); setSelectedIndex(index);
} }
if (renderer.tap(value, x, y, count)) { if (renderer.tap(index, value, x, y, count)) {
prevTapIndex = index; prevTapIndex = index;
return true; //don't activate if renderer handles tap return true; //don't activate if renderer handles tap
} }
@@ -234,7 +234,7 @@ public final class ItemListView<T extends InventoryItem> extends ItemView<T> {
@Override @Override
public boolean showMenu(Integer index, Entry<T, Integer> value, FDisplayObject owner, float x, float y) { public boolean showMenu(Integer index, Entry<T, Integer> value, FDisplayObject owner, float x, float y) {
return renderer.longPress(value, x, y); return renderer.longPress(index, value, x, y);
} }
@Override @Override

View File

@@ -265,7 +265,8 @@ public abstract class VCardDisplayArea extends VDisplayArea {
public void run() { public void run() {
if (!selectCard(false)) { if (!selectCard(false)) {
//if no cards in stack can be selected, just show zoom/details for card //if no cards in stack can be selected, just show zoom/details for card
CardZoom.show(getCard()); List<CardView> cards = displayArea.orderedCards;
CardZoom.show(cards, cards.indexOf(getCard()));
} }
} }
}); });
@@ -308,7 +309,8 @@ public abstract class VCardDisplayArea extends VDisplayArea {
@Override @Override
public boolean longPress(float x, float y) { public boolean longPress(float x, float y) {
if (renderedCardContains(x, y)) { if (renderedCardContains(x, y)) {
CardZoom.show(getCard()); List<CardView> cards = displayArea.orderedCards;
CardZoom.show(cards, cards.indexOf(getCard()));
return true; return true;
} }
return false; return false;

View File

@@ -95,7 +95,7 @@ public class FChoiceList<T> extends FList<T> {
selectedIndices.add(index); selectedIndices.add(index);
onSelectionChange(); onSelectionChange();
} }
if (renderer.tap(value, x, y, count)) { if (renderer.tap(index, value, x, y, count)) {
prevTapIndex = index; prevTapIndex = index;
return true; //don't activate if renderer handles tap return true; //don't activate if renderer handles tap
} }
@@ -108,7 +108,7 @@ public class FChoiceList<T> extends FList<T> {
@Override @Override
public boolean showMenu(Integer index, T value, FDisplayObject owner, float x, float y) { public boolean showMenu(Integer index, T value, FDisplayObject owner, float x, float y) {
return renderer.longPress(value, x, y); return renderer.longPress(index, value, x, y);
} }
@Override @Override
@@ -272,8 +272,8 @@ public class FChoiceList<T> extends FList<T> {
protected abstract class ItemRenderer { protected abstract class ItemRenderer {
public abstract FSkinFont getDefaultFont(); public abstract FSkinFont getDefaultFont();
public abstract float getItemHeight(); public abstract float getItemHeight();
public abstract boolean tap(T value, float x, float y, int count); public abstract boolean tap(Integer index, T value, float x, float y, int count);
public abstract boolean longPress(T value, float x, float y); public abstract boolean longPress(Integer index, T value, float x, float y);
public abstract void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h); public abstract void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h);
} }
protected class DefaultItemRenderer extends ItemRenderer { protected class DefaultItemRenderer extends ItemRenderer {
@@ -291,12 +291,12 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
return false; return false;
} }
@Override @Override
public boolean longPress(T value, float x, float y) { public boolean longPress(Integer index, T value, float x, float y) {
return false; return false;
} }
@@ -318,13 +318,13 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
return CardRenderer.cardListItemTap((PaperCard)value, x, y, count, compactModeHandler.isCompactMode()); return CardRenderer.cardListItemTap(items, index, x, y, count, compactModeHandler.isCompactMode());
} }
@Override @Override
public boolean longPress(T value, float x, float y) { public boolean longPress(Integer index, T value, float x, float y) {
CardZoom.show((PaperCard)value); CardZoom.show(items, index);
return true; return true;
} }
@@ -346,13 +346,13 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
return CardRenderer.cardListItemTap((CardView)value, x, y, count, compactModeHandler.isCompactMode()); return CardRenderer.cardListItemTap(items, index, x, y, count, compactModeHandler.isCompactMode());
} }
@Override @Override
public boolean longPress(T value, float x, float y) { public boolean longPress(Integer index, T value, float x, float y) {
CardZoom.show((CardView)value); CardZoom.show(items, index);
return true; return true;
} }
@@ -376,7 +376,7 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
if (x <= VStack.CARD_WIDTH + 2 * FList.PADDING) { if (x <= VStack.CARD_WIDTH + 2 * FList.PADDING) {
CardZoom.show(((SpellAbility)value).getView().getHostCard()); CardZoom.show(((SpellAbility)value).getView().getHostCard());
return true; return true;
@@ -385,7 +385,7 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean longPress(T value, float x, float y) { public boolean longPress(Integer index, T value, float x, float y) {
CardZoom.show(((SpellAbility)value).getView().getHostCard()); CardZoom.show(((SpellAbility)value).getView().getHostCard());
return true; return true;
} }
@@ -413,12 +413,12 @@ public class FChoiceList<T> extends FList<T> {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
return false; return false;
} }
@Override @Override
public boolean longPress(T value, float x, float y) { public boolean longPress(Integer index, T value, float x, float y) {
return false; return false;
} }

View File

@@ -23,7 +23,7 @@ public class FList<T> extends FScrollPane implements Iterable<T> {
public static final FSkinColor LINE_COLOR = FORE_COLOR.alphaColor(0.5f); public static final FSkinColor LINE_COLOR = FORE_COLOR.alphaColor(0.5f);
public static final float LINE_THICKNESS = Utils.scale(1); public static final float LINE_THICKNESS = Utils.scale(1);
private final List<T> items = new ArrayList<T>(); protected final List<T> items = new ArrayList<T>();
private FSkinFont font; private FSkinFont font;
private ListItemRenderer<T> renderer; private ListItemRenderer<T> renderer;
private int pressedIndex = -1; private int pressedIndex = -1;