mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 18:28:00 +00:00
Support special card and SpellAbility rendering for DualListBox
This commit is contained in:
@@ -158,8 +158,8 @@ public class CardRenderer {
|
||||
return Math.round(MANA_SYMBOL_SIZE + FSkinFont.get(12).getFont().getLineHeight() + 3 * FList.PADDING + 1);
|
||||
}
|
||||
|
||||
private static Map<String, TextureRegion> cardArtCache = new HashMap<String, TextureRegion>();
|
||||
private static final float CARD_ART_RATIO = 1.302f;
|
||||
private static final Map<String, TextureRegion> cardArtCache = new HashMap<String, TextureRegion>();
|
||||
public static final float CARD_ART_RATIO = 1.302f;
|
||||
|
||||
//extract card art from the given card
|
||||
public static TextureRegion getCardArt(PaperCard paperCard) {
|
||||
@@ -244,9 +244,7 @@ public class CardRenderer {
|
||||
}
|
||||
|
||||
public static boolean cardListItemTap(Card card, float x, float y, int count) {
|
||||
float cardArtHeight = getCardListItemHeight();
|
||||
float cardArtWidth = cardArtHeight * CARD_ART_RATIO;
|
||||
if (x <= cardArtWidth && y <= cardArtHeight) {
|
||||
if (x <= getCardListItemHeight() * CARD_ART_RATIO) {
|
||||
CardZoom.show(card);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import forge.Forge.Graphics;
|
||||
import forge.assets.FSkinColor;
|
||||
import forge.assets.FSkinFont;
|
||||
import forge.assets.ImageCache;
|
||||
import forge.assets.TextRenderer;
|
||||
import forge.card.CardDetailUtil;
|
||||
import forge.card.CardZoom;
|
||||
import forge.card.CardDetailUtil.DetailColors;
|
||||
@@ -28,11 +29,12 @@ import forge.toolbox.FLabel;
|
||||
import forge.util.Utils;
|
||||
|
||||
public class VStack extends FDropDown {
|
||||
public static final float CARD_WIDTH = Utils.AVG_FINGER_WIDTH;
|
||||
public static final float CARD_HEIGHT = Math.round(CARD_WIDTH * FCardPanel.ASPECT_RATIO);
|
||||
private static final float PADDING = 3;
|
||||
private static final float CARD_WIDTH = Utils.AVG_FINGER_WIDTH;
|
||||
private static final float CARD_HEIGHT = Math.round(CARD_WIDTH * FCardPanel.ASPECT_RATIO);
|
||||
private static final FSkinFont FONT = FSkinFont.get(11);
|
||||
private static final float ALPHA_COMPOSITE = 0.5f;
|
||||
private static final TextRenderer textRenderer = new TextRenderer(true);
|
||||
|
||||
private final MagicStack stack;
|
||||
private final LobbyPlayer localPlayer;
|
||||
@@ -209,7 +211,7 @@ public class VStack extends FDropDown {
|
||||
g.drawImage(ImageCache.getImage(stackInstance.getSourceCard()), x, y, cardWidth, cardHeight);
|
||||
|
||||
x += cardWidth + padding;
|
||||
g.drawText(text, FONT, foreColor, x, y, w - x - padding, h - y - padding, true, HAlignment.LEFT, true);
|
||||
textRenderer.drawText(g, text, FONT, foreColor, x, y, w - x - padding, h - y - padding, true, HAlignment.LEFT, true);
|
||||
|
||||
if (!isTop) {
|
||||
g.resetAlphaComposite();
|
||||
|
||||
@@ -3,7 +3,14 @@ package forge.toolbox;
|
||||
import forge.Forge.Graphics;
|
||||
import forge.assets.FSkinColor;
|
||||
import forge.assets.FSkinFont;
|
||||
import forge.assets.ImageCache;
|
||||
import forge.assets.TextRenderer;
|
||||
import forge.card.CardRenderer;
|
||||
import forge.card.CardZoom;
|
||||
import forge.game.card.Card;
|
||||
import forge.game.spellability.SpellAbility;
|
||||
import forge.screens.match.views.VPrompt;
|
||||
import forge.screens.match.views.VStack;
|
||||
import forge.toolbox.FEvent.FEventHandler;
|
||||
import forge.toolbox.FEvent.FEventType;
|
||||
import forge.util.Callback;
|
||||
@@ -92,8 +99,27 @@ public class DualListBox<T> extends FDialog {
|
||||
}
|
||||
};
|
||||
|
||||
sourceList = add(new ChoiceList(sourceElements, onAdd));
|
||||
destList = add(new ChoiceList(destElements, onRemove));
|
||||
//determine renderer from item type
|
||||
final ItemRenderer renderer;
|
||||
T item = null;
|
||||
if (sourceElements != null && sourceElements.size() > 0) {
|
||||
item = sourceElements.get(0);
|
||||
}
|
||||
else if (destElements != null && destElements.size() > 0) {
|
||||
item = destElements.get(0);
|
||||
}
|
||||
if (item instanceof Card) {
|
||||
renderer = new CardItemRenderer();
|
||||
}
|
||||
else if (item instanceof SpellAbility) {
|
||||
renderer = new SpellAbilityItemRenderer();
|
||||
}
|
||||
else {
|
||||
renderer = new DefaultItemRenderer();
|
||||
}
|
||||
|
||||
sourceList = add(new ChoiceList(sourceElements, renderer, onAdd));
|
||||
destList = add(new ChoiceList(destElements, renderer, onRemove));
|
||||
if (sourceList.getCount() > 0) { //select first items by default if possible
|
||||
sourceList.selectedIndices.add(0);
|
||||
}
|
||||
@@ -243,16 +269,99 @@ public class DualListBox<T> extends FDialog {
|
||||
okButton.setEnabled(targetReached);
|
||||
}
|
||||
|
||||
private abstract class ItemRenderer {
|
||||
public abstract int getDefaultFontSize();
|
||||
public abstract float getItemHeight();
|
||||
public abstract boolean tap(T value, float x, float y, int count);
|
||||
public abstract void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h);
|
||||
}
|
||||
private class DefaultItemRenderer extends ItemRenderer {
|
||||
@Override
|
||||
public int getDefaultFontSize() {
|
||||
return 12;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getItemHeight() {
|
||||
return ListChooser.DEFAULT_ITEM_HEIGHT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tap(T value, float x, float y, int count) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
|
||||
g.drawText(value.toString(), font, foreColor, x, y, w, h, true, HAlignment.LEFT, true);
|
||||
}
|
||||
}
|
||||
//special renderer for SpellAbilities
|
||||
private class SpellAbilityItemRenderer extends ItemRenderer {
|
||||
private final TextRenderer textRenderer = new TextRenderer(true);
|
||||
|
||||
@Override
|
||||
public int getDefaultFontSize() {
|
||||
return 12;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getItemHeight() {
|
||||
return VStack.CARD_HEIGHT + 2 * FList.PADDING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tap(T value, float x, float y, int count) {
|
||||
if (x <= VStack.CARD_WIDTH + 2 * FList.PADDING) {
|
||||
CardZoom.show(((SpellAbility)value).getHostCard());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
|
||||
SpellAbility spellAbility = (SpellAbility)value;
|
||||
g.drawImage(ImageCache.getImage(spellAbility.getHostCard()), x, y, VStack.CARD_WIDTH, VStack.CARD_HEIGHT);
|
||||
float dx = VStack.CARD_WIDTH + FList.PADDING;
|
||||
x += dx;
|
||||
w -= dx;
|
||||
textRenderer.drawText(g, spellAbility.toString(), font, foreColor, x, y, w, h, true, HAlignment.LEFT, true);
|
||||
}
|
||||
}
|
||||
//special renderer for cards
|
||||
private class CardItemRenderer extends ItemRenderer {
|
||||
@Override
|
||||
public int getDefaultFontSize() {
|
||||
return 14;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getItemHeight() {
|
||||
return CardRenderer.getCardListItemHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tap(T value, float x, float y, int count) {
|
||||
return CardRenderer.cardListItemTap((Card)value, x, y, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
|
||||
CardRenderer.drawCardListItem(g, font, foreColor, (Card)value, 0, x, y, w, h);
|
||||
}
|
||||
}
|
||||
|
||||
private class ChoiceList extends FList<T> {
|
||||
private List<Integer> selectedIndices = new ArrayList<Integer>();
|
||||
|
||||
private ChoiceList(Collection<T> items, final FEventHandler dblTapCommand) {
|
||||
private ChoiceList(Collection<T> items, final ItemRenderer renderer, final FEventHandler dblTapCommand) {
|
||||
super(items != null ? items : new ArrayList<T>()); //handle null without crashing
|
||||
|
||||
setListItemRenderer(new ListItemRenderer<T>() {
|
||||
@Override
|
||||
public float getItemHeight() {
|
||||
return ListChooser.DEFAULT_ITEM_HEIGHT;
|
||||
return renderer.getItemHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -260,6 +369,9 @@ public class DualListBox<T> extends FDialog {
|
||||
Integer index = ChoiceList.this.getIndexOf(value);
|
||||
selectedIndices.clear();
|
||||
selectedIndices.add(index);
|
||||
if (renderer.tap(value, x, y, count)) {
|
||||
return true; //don't activate if renderer handles tap
|
||||
}
|
||||
if (count == 2) {
|
||||
dblTapCommand.handleEvent(new FEvent(ChoiceList.this, FEventType.ACTIVATE, index));
|
||||
}
|
||||
@@ -268,10 +380,10 @@ public class DualListBox<T> extends FDialog {
|
||||
|
||||
@Override
|
||||
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
|
||||
g.drawText(value.toString(), font, foreColor, x, y, w, h, true, HAlignment.LEFT, true);
|
||||
renderer.drawValue(g, value, font, foreColor, pressed, x, y, w, h);
|
||||
}
|
||||
});
|
||||
setFontSize(12);
|
||||
setFontSize(renderer.getDefaultFontSize());
|
||||
}
|
||||
|
||||
//remove any selected indices outside item range
|
||||
|
||||
Reference in New Issue
Block a user