From c93418a526123ec85dbee99d52c769d584a4527c Mon Sep 17 00:00:00 2001 From: drdev Date: Mon, 7 Dec 2015 03:02:39 +0000 Subject: [PATCH] Support showing unowned cards in collection --- .../src/main/java/forge/util/ItemPool.java | 84 ++++++---- .../src/forge/itemmanager/ItemManager.java | 3 +- .../forge/itemmanager/views/ImageView.java | 21 ++- .../forge/itemmanager/views/ItemListView.java | 12 +- .../src/forge/itemmanager/views/ItemView.java | 1 + .../forge/itemmanager/ItemManagerModel.java | 154 ++++++------------ .../forge/planarconquest/ConquestData.java | 3 +- 7 files changed, 132 insertions(+), 146 deletions(-) diff --git a/forge-core/src/main/java/forge/util/ItemPool.java b/forge-core/src/main/java/forge/util/ItemPool.java index 935f5b2f67b..a247e48fa9f 100644 --- a/forge-core/src/main/java/forge/util/ItemPool.java +++ b/forge-core/src/main/java/forge/util/ItemPool.java @@ -90,18 +90,22 @@ public class ItemPool implements Iterable inMap, final Class cls) { - items = inMap; + protected ItemPool(final Map items0, final Class cls) { + if (items0 != null) { + items = items0; + } + else { + items = new HashMap(); //prevent items being null + } myClass = cls; } // Data members - /** The items. */ protected final Map items; - /** The my class. */ - private final Class myClass; // class does not keep this in runtime by - // itself + private final Class myClass; //class does not keep this in runtime by itself + + private boolean allowZero; //whether to allow items with 0 count to remain in pool @Override public final Iterator> iterator() { @@ -109,14 +113,11 @@ public class ItemPool implements Iterable implements Iterable int countAll(Predicate condition, Class cls) { int result = 0; - if (items != null) { - final boolean isSameClass = cls == myClass; - for (final Entry kv : this) { - final T key = kv.getKey(); - @SuppressWarnings("unchecked") - final U castKey = isSameClass || cls.isInstance(key) ? (U)key : null; - if (null == condition || castKey != null && condition.apply(castKey)) - result += kv.getValue(); - } + final boolean isSameClass = cls == myClass; + for (final Entry kv : this) { + final T key = kv.getKey(); + @SuppressWarnings("unchecked") + final U castKey = isSameClass || cls.isInstance(key) ? (U)key : null; + if (null == condition || castKey != null && condition.apply(castKey)) + result += kv.getValue(); } return result; } @@ -151,7 +150,7 @@ public class ItemPool implements Iterable toFlatList() { @@ -176,6 +175,13 @@ public class ItemPool implements Iterable getView() { return new ItemPool(Collections.unmodifiableMap(items), getMyClass()); } @@ -185,14 +191,21 @@ public class ItemPool implements Iterable items) { - for (T item : items) { + public void addAllFlat(final Iterable itms) { + for (T item : itms) { add(item); } } @@ -204,8 +217,8 @@ public class ItemPool implements Iterable void addAllOfTypeFlat(final Iterable items) { - for (U item : items) { + public void addAllOfTypeFlat(final Iterable itms) { + for (U item : itms) { if (myClass.isInstance(item)) { add((T) item); } @@ -214,10 +227,9 @@ public class ItemPool implements Iterable void addAllOfType(final Iterable> map) { - Class myClass = this.getMyClass(); - for (final Entry e : map) { + for (Entry e : map) { if (myClass.isInstance(e.getKey())) { - this.add((T) e.getKey(), e.getValue()); + add((T) e.getKey(), e.getValue()); } } } @@ -228,12 +240,18 @@ public class ItemPool implements Iterable extends FContainer im setPool(pool0, false); } public void setPool(final ItemPool pool0, boolean infinite) { - model.clear(); pool = pool0; + model.clear(); + model.setAllowZero(pool.allowZero()); model.addItems(pool); model.setInfinite(infinite); updateView(true, null); diff --git a/forge-gui-mobile/src/forge/itemmanager/views/ImageView.java b/forge-gui-mobile/src/forge/itemmanager/views/ImageView.java index d45ff9861b2..94d3c047a7f 100644 --- a/forge-gui-mobile/src/forge/itemmanager/views/ImageView.java +++ b/forge-gui-mobile/src/forge/itemmanager/views/ImageView.java @@ -366,8 +366,13 @@ public class ImageView extends ItemView { group = otherItems; } - for (int i = 0; i < qty; i++) { - group.add(new ItemInfo(item, group)); + if (qty > 0) { + for (int i = 0; i < qty; i++) { + group.add(new ItemInfo(item, group, false)); + } + } + else { //add single item for unowned item + group.add(new ItemInfo(item, group, true)); } } @@ -907,13 +912,15 @@ public class ImageView extends ItemView { private class ItemInfo extends FDisplayObject implements Entry { private final T item; private final Group group; + private final boolean unowned; private int index; private CardStackPosition pos; private boolean selected; - private ItemInfo(T item0, Group group0) { + private ItemInfo(T item0, Group group0, boolean unowned0) { item = item0; group = group0; + unowned = unowned0; } @Override @@ -938,6 +945,10 @@ public class ImageView extends ItemView { @Override public void draw(Graphics g) { + if (unowned) { + g.setAlphaComposite(UNOWNED_ALPHA_COMPOSITE); + } + final float x = getLeft() - group.getScrollLeft(); final float y = getTop() - group.getTop() - getScrollValue(); final float w = getWidth(); @@ -961,6 +972,10 @@ public class ImageView extends ItemView { g.drawText(item.getName(), GROUP_HEADER_FONT, Color.WHITE, x + PADDING, y + PADDING, w - 2 * PADDING, h - 2 * PADDING, true, HAlignment.CENTER, false); } } + + if (unowned) { + g.resetAlphaComposite(); + } } } } diff --git a/forge-gui-mobile/src/forge/itemmanager/views/ItemListView.java b/forge-gui-mobile/src/forge/itemmanager/views/ItemListView.java index 64626e4159e..8e574efe99d 100644 --- a/forge-gui-mobile/src/forge/itemmanager/views/ItemListView.java +++ b/forge-gui-mobile/src/forge/itemmanager/views/ItemListView.java @@ -239,6 +239,10 @@ public final class ItemListView extends ItemView { @Override public void drawValue(Graphics g, Integer index, Entry value, FSkinFont font, FSkinColor foreColor, FSkinColor backColor, boolean pressed, float x, float y, float w, float h) { + boolean unowned = (value.getValue() == 0); //fade out item if item isn't owned + if (unowned) { + g.setAlphaComposite(UNOWNED_ALPHA_COMPOSITE); + } if (maxSelections > 1) { if (pressed) { //if multi-select mode, draw SEL_COLOR when pressed g.fillRect(SEL_COLOR, x - FList.PADDING, y - FList.PADDING, w + 2 * FList.PADDING, h + 2 * FList.PADDING); @@ -251,6 +255,9 @@ public final class ItemListView extends ItemView { w -= padding; } renderer.drawValue(g, value, font, foreColor, backColor, pressed, x + 1, y, w - 2, h); //x + 1 and w - 2 to account for left and right borders + if (unowned) { + g.resetAlphaComposite(); + } } }); setFont(FSkinFont.get(14)); @@ -293,11 +300,6 @@ public final class ItemListView extends ItemView { public final class ItemListModel { private final ItemManagerModel model; - /** - * Instantiates a new list model. - * - * @param model0   {@link forge.gui.ItemManager.ItemManagerModel} - */ public ItemListModel(final ItemManagerModel model0) { model = model0; } diff --git a/forge-gui-mobile/src/forge/itemmanager/views/ItemView.java b/forge-gui-mobile/src/forge/itemmanager/views/ItemView.java index f3819758c68..062c243d95f 100644 --- a/forge-gui-mobile/src/forge/itemmanager/views/ItemView.java +++ b/forge-gui-mobile/src/forge/itemmanager/views/ItemView.java @@ -23,6 +23,7 @@ import java.util.Map; import com.badlogic.gdx.math.Rectangle; public abstract class ItemView { + protected static final float UNOWNED_ALPHA_COMPOSITE = 0.35f; private static final FSkinColor BORDER_COLOR = FSkinColor.get(Colors.CLR_TEXT); protected final ItemManager itemManager; diff --git a/forge-gui/src/main/java/forge/itemmanager/ItemManagerModel.java b/forge-gui/src/main/java/forge/itemmanager/ItemManagerModel.java index 5e2041ee5fe..fe1fb161c1c 100644 --- a/forge-gui/src/main/java/forge/itemmanager/ItemManagerModel.java +++ b/forge-gui/src/main/java/forge/itemmanager/ItemManagerModel.java @@ -29,16 +29,7 @@ import forge.itemmanager.ItemColumnConfig.SortState; import forge.util.ItemPool; import forge.util.ItemPoolSorter; -/** - *

- * ItemManagerModel class. - *

- * - * @param - * the generic type - * @author Forge - * @version $Id: ItemManagerModel.java 19857 2013-02-24 08:49:52Z Max mtg $ - */ + public final class ItemManagerModel { private static final int maxSortDepth = 3; @@ -46,102 +37,71 @@ public final class ItemManagerModel { private boolean infiniteSupply; private final CascadeManager cascadeManager = new CascadeManager(); - /** - * Instantiates a new list view model - * - * @param ItemManager0 - * @param genericType0 - */ public ItemManagerModel(final Class genericType0) { - this.data = new ItemPool(genericType0); + data = new ItemPool(genericType0); } - /** - * Clears all data in the model. - */ public void clear() { - this.data.clear(); + data.clear(); } // same thing as above, it was copied to provide sorting (needed by table // views in deck editors) - /** The items ordered. */ private final transient List> itemsOrdered = new ArrayList>(); - /** Whether list is in sync. */ protected transient boolean isListInSync = false; public List> getOrderedList() { - if (!this.isListInSync) { - this.rebuildOrderedList(); + if (!isListInSync) { + rebuildOrderedList(); } - return this.itemsOrdered; + return itemsOrdered; } private void rebuildOrderedList() { - this.itemsOrdered.clear(); - if (this.data != null) { - for (final Entry e : this.data) { - this.itemsOrdered.add(e); + itemsOrdered.clear(); + if (data != null) { + for (final Entry e : data) { + itemsOrdered.add(e); } } - this.isListInSync = true; + isListInSync = true; } public int countDistinct() { - return this.data.countDistinct(); + return data.countDistinct(); } - /** - * Gets all items in the model. - * - * @return ItemPoolView - */ public ItemPool getItems() { - return this.data.getView(); + return data.getView(); } - /** - * Removes a item from the model. - * - * @param item0   {@link forge.Item} object - */ public void removeItem(final T item0, final int qty) { if (isInfinite()) { return; } - final boolean wasThere = this.data.count(item0) > 0; + final boolean wasThere = data.count(item0) > 0; if (wasThere) { - this.data.remove(item0, qty); + data.remove(item0, qty); isListInSync = false; } } public void replaceAll(final T item0, final T replacement0) { - final int count = this.data.count(item0); + final int count = data.count(item0); if (count > 0) { - this.data.removeAll(item0); - this.data.add(replacement0, count); + data.removeAll(item0); + data.add(replacement0, count); isListInSync = false; } } - /** - * Adds a item to the model. - * - * @param item0   {@link forge.Item} object. - */ public void addItem(final T item0, final int qty) { - this.data.add(item0, qty); + data.add(item0, qty); isListInSync = false; } - /** - * Adds multiple copies of multiple items to the model. - * - * @param items0   {@link java.lang.Iterable}> - */ public void addItems(final Iterable> items0) { - this.data.addAll(items0); + data.addAll(items0); isListInSync = false; } @@ -150,29 +110,31 @@ public final class ItemManagerModel { * items in the table have a limited number of copies. */ public void setInfinite(final boolean infinite) { - this.infiniteSupply = infinite; + infiniteSupply = infinite; } public boolean isInfinite() { return infiniteSupply; } + public boolean allowZero() { + return data.allowZero(); + } + public void setAllowZero(boolean allowZero0) { + data.setAllowZero(allowZero0); + } + public CascadeManager getCascadeManager() { return cascadeManager; } - /** - * Resort. - */ public void refreshSort() { - if (this.getOrderedList().isEmpty()) { return; } + if (getOrderedList().isEmpty()) { return; } - Collections.sort(this.getOrderedList(), new MyComparator()); + Collections.sort(getOrderedList(), new MyComparator()); } - /** - * Manages sorting orders for multiple depths of sorting. - */ + //Manages sorting orders for multiple depths of sorting public final class CascadeManager { private final List colsToSort = new ArrayList(3); private Sorter sorter = null; @@ -181,20 +143,21 @@ public final class ItemManagerModel { // If column is first in the cascade, inverts direction of sort. // Otherwise, sorts in ascending direction. public void add(final ItemColumn col0, final boolean forSetup) { - this.sorter = null; + sorter = null; if (forSetup) { //just add column unmodified if setting up sort columns - this.colsToSort.add(0, col0); - } else { + colsToSort.add(0, col0); + } + else { if (colsToSort.size() > 0 && colsToSort.get(0).equals(col0)) { //if column already at top level, just invert col0.getConfig().setSortPriority(1); col0.getConfig().setSortState(col0.getConfig().getSortState() == SortState.ASC ? SortState.DESC : SortState.ASC); } else { //otherwise move column to top level and move others down - this.colsToSort.remove(col0); + colsToSort.remove(col0); col0.getConfig().setSortPriority(1); col0.getConfig().setSortState(col0.getConfig().getDefaultSortState()); - this.colsToSort.add(0, col0); + colsToSort.add(0, col0); } //decrement sort priority on remaining columns @@ -208,28 +171,28 @@ public final class ItemManagerModel { } //unset and remove boundary columns. - if (this.colsToSort.size() > maxSortDepth) { - this.colsToSort.get(maxSortDepth).getConfig().setSortPriority(0); - this.colsToSort.remove(maxSortDepth); + if (colsToSort.size() > maxSortDepth) { + colsToSort.get(maxSortDepth).getConfig().setSortPriority(0); + colsToSort.remove(maxSortDepth); } } public Sorter getSorter() { - if (this.sorter == null) { - this.sorter = createSorter(); + if (sorter == null) { + sorter = createSorter(); } - return this.sorter; + return sorter; } public void reset() { - this.colsToSort.clear(); - this.sorter = null; + colsToSort.clear(); + sorter = null; } private Sorter createSorter() { final List> oneColSorters = new ArrayList>(maxSortDepth); - for (final ItemColumn col : this.colsToSort) { + for (final ItemColumn col : colsToSort) { oneColSorters.add(new ItemPoolSorter( col.getFnSort(), col.getConfig().getSortState().equals(SortState.ASC) ? true : false)); @@ -242,30 +205,18 @@ public final class ItemManagerModel { private final List> sorters; private final int cntFields; - /** - * - * Sorter Constructor. - * - * @param sorters0 - * a List> - */ public Sorter(final List> sorters0) { - this.sorters = sorters0; - this.cntFields = sorters0.size(); + sorters = sorters0; + cntFields = sorters0.size(); } - /* - * (non-Javadoc) - * - * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) - */ @Override public final int compare(final Entry arg0, final Entry arg1) { int lastCompare = 0; int iField = -1; - while ((++iField < this.cntFields) && (lastCompare == 0)) { // reverse + while ((++iField < cntFields) && (lastCompare == 0)) { // reverse // iteration - final ItemPoolSorter sorter = this.sorters.get(iField); + final ItemPoolSorter sorter = sorters.get(iField); if (sorter == null) { break; } @@ -277,13 +228,10 @@ public final class ItemManagerModel { } private final class MyComparator implements Comparator> { - /* (non-Javadoc) - * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) - */ @SuppressWarnings("unchecked") @Override public int compare(final Entry o1, final Entry o2) { return cascadeManager.getSorter().compare((Entry)o1, (Entry)o2); } } -} // ItemManagerModel +} diff --git a/forge-gui/src/main/java/forge/planarconquest/ConquestData.java b/forge-gui/src/main/java/forge/planarconquest/ConquestData.java index 440139c6689..0827a5eac8d 100644 --- a/forge-gui/src/main/java/forge/planarconquest/ConquestData.java +++ b/forge-gui/src/main/java/forge/planarconquest/ConquestData.java @@ -130,7 +130,7 @@ public final class ConquestData { if (collection == null) { collection = new ConquestCollection(); } - manager.setPool(collection); + manager.setPool(collection, true); } public Iterable getUnlockedCards() { @@ -433,6 +433,7 @@ public final class ConquestData { private class ConquestCollection extends ItemPool { private ConquestCollection() { super(PaperCard.class); + setAllowZero(true); //initialize to contain all available cards, with unlocked //having a count of 1 and the rest having a count of 0