Revision: 1804
http://svn.sourceforge.net/vassalengine/?rev=1804&view=rev
Author: t1mbyrne
Date: 2007-03-05 14:13:00 -0800 (Mon, 05 Mar 2007)
Log Message:
-----------
Adding feature to count differnt types of pieces contained in a deck (via map-level property)
Modified Paths:
--------------
VASSAL-src/branches/t1mbyrne/VASSAL/build/module/map/DrawPile.java
VASSAL-src/branches/t1mbyrne/VASSAL/counters/Deck.java
Modified: VASSAL-src/branches/t1mbyrne/VASSAL/build/module/map/DrawPile.java
===================================================================
--- VASSAL-src/branches/t1mbyrne/VASSAL/build/module/map/DrawPile.java 2007-03-05 21:30:24 UTC (rev 1803)
+++ VASSAL-src/branches/t1mbyrne/VASSAL/build/module/map/DrawPile.java 2007-03-05 22:13:00 UTC (rev 1804)
@@ -41,6 +41,7 @@
import VASSAL.configure.PlayerIdFormattedStringConfigurer;
import VASSAL.configure.StringEnum;
import VASSAL.configure.VisibilityCondition;
+import VASSAL.configure.StringArrayConfigurer;
import VASSAL.counters.Deck;
import VASSAL.counters.GamePiece;
import VASSAL.counters.Stack;
@@ -75,6 +76,11 @@
return dummy.getShuffleOption().equals(USE_MENU);
}
};
+ private VisibilityCondition typeCountingVisibleCondition = new VisibilityCondition() {
+ public boolean shouldBeVisible() {
+ return dummy.doesTypeCounting();
+ }
+ };
protected static UniqueIdManager idMgr = new UniqueIdManager("Deck");
public void addTo(Buildable parent) {
@@ -114,6 +120,8 @@
public static final String DRAW = "draw";
public static final String COLOR = "color";
public static final String MAXSTACK = "maxStack";
+ public static final String TYPECOUNTING = "typeCounting";
+ public static final String COUNTTYPES = "countTypes";
public static final String RESHUFFLABLE = "reshufflable";
public static final String RESHUFFLE_COMMAND = "reshuffleCommand";
public static final String RESHUFFLE_TARGET = "reshuffleTarget";
@@ -179,7 +187,7 @@
ALLOW_SELECT, FACE_DOWN, DRAW_FACE_UP, FACE_DOWN_REPORT_FORMAT, SHUFFLE, SHUFFLE_REPORT_FORMAT,
SHUFFLE_HOTKEY, REVERSIBLE, REVERSE_REPORT_FORMAT, DRAW, COLOR,
RESHUFFLABLE, RESHUFFLE_COMMAND, RESHUFFLE_MESSAGE, RESHUFFLE_HOTKEY, RESHUFFLE_TARGET,CAN_SAVE,
- MAXSTACK};
+ MAXSTACK,TYPECOUNTING,COUNTTYPES};
}
public String[] getAttributeDescriptions() {
@@ -207,7 +215,9 @@
"Send Hot Key: ",
"Name of deck to send to: ",
"Can be saved-to/loaded-from a file",
- "Maximum Cards to be Displayed in Stack:"};
+ "Maximum Cards to be Displayed in Stack:",
+ "Perform Counting of Piece Types",
+ "Piece types to count"};
}
public Class[] getAttributeTypes() {
@@ -235,7 +245,9 @@
KeyStroke.class,
AssignedDeckPrompt.class,
Boolean.class,
- Integer.class};
+ Integer.class,
+ Boolean.class,
+ String[].class};
}
public static class FormattedStringConfig implements ConfigurerFactory {
@@ -277,8 +289,14 @@
return ColorConfigurer.colorToString(dummy.getOutlineColor());
}
else if (MAXSTACK.equals(key)) {
- return "" + dummy.getMaxStack();
- }
+ return "" + dummy.getMaxStack();
+ }
+ else if (TYPECOUNTING.equals(key)) {
+ return "" + dummy.doesTypeCounting();
+ }
+ else if (COUNTTYPES.equals(key)) {
+ return StringArrayConfigurer.arrayToString(dummy.getCountTypes());
+ }
else if (RESHUFFLABLE.equals(key)) {
return "" + (dummy.getReshuffleCommand().length() > 0);
}
@@ -392,11 +410,25 @@
dummy.setOutlineColor((Color) value);
}
if (MAXSTACK.equals(key)) {
- if (value instanceof String) {
- value = new Integer((String) value);
- }
- dummy.setMaxStack(((Integer) value).intValue());
- }
+ if (value instanceof String) {
+ value = new Integer((String) value);
+ }
+ dummy.setMaxStack(((Integer) value).intValue());
+ }
+ if (TYPECOUNTING.equals(key)) {
+ if (value instanceof Boolean) {
+ dummy.setTypeCounting(Boolean.TRUE.equals(value));
+ }
+ else {
+ dummy.setTypeCounting("true".equals(value));
+ }
+ }
+ if (COUNTTYPES.equals(key)) {
+ if (value instanceof String) {
+ value = StringArrayConfigurer.stringToArray((String) value);
+ }
+ dummy.setCountTypes((String[]) value);
+ }
else if (RESHUFFLABLE.equals(key)) {
reshufflable = "true".equals(value) || Boolean.TRUE.equals(value);
if (!reshufflable) {
@@ -462,6 +494,9 @@
else if (REVERSE_REPORT_FORMAT.equals(name)) {
return reverseFormatVisibleCondition;
}
+ else if (COUNTTYPES.equals(name)) {
+ return typeCountingVisibleCondition;
+ }
else {
return null;
}
Modified: VASSAL-src/branches/t1mbyrne/VASSAL/counters/Deck.java
===================================================================
--- VASSAL-src/branches/t1mbyrne/VASSAL/counters/Deck.java 2007-03-05 21:30:24 UTC (rev 1803)
+++ VASSAL-src/branches/t1mbyrne/VASSAL/counters/Deck.java 2007-03-05 22:13:00 UTC (rev 1804)
@@ -102,6 +102,8 @@
protected boolean faceDown;
protected int dragCount = 0;
protected int maxStack = 10;
+ protected String[] countTypes;
+ protected boolean typeCounting = false;
protected ArrayList nextDraw;
protected KeyCommand[] commands;
protected CommandEncoder commandEncoder = new CommandEncoder() {
@@ -129,38 +131,111 @@
public Deck(String type) {
mySetType(type);
}
+
+ /**
+ * Update map-level count properties for all "types" of pieces that are configured
+ * to be counted. These are held in the String[] countTypes.
+ */
+ private void updateCountsAll() {
+ if (!doesTypeCounting() || getMap() == null) {
+ return;
+ }
+ //Clear out all of the registered count types
+ for (int index = 0; index < countTypes.length; index++) {
+ getMap().getPropertyListener().propertyChange(new PropertyChangeEvent(this,deckName+"_"+countTypes[index],null,""+0));
+ }
+ //Increase all of the pieces with types specified in this deck
+ for (Enumeration e = getPieces(); e.hasMoreElements();) {
+ GamePiece p = (GamePiece) e.nextElement();
+ if (p != null) {
+ updateCounts(p,true);
+ }
+ }
+ }
/**
+ * Update map-level count property for a piece located at index
+ * @param index, increase
+ */
+ private void updateCounts(int index, boolean increase) {
+ if (!doesTypeCounting()) {
+ return;
+ }
+ if (index >= 0 && index < contents.length) {
+ GamePiece p = getPieceAt(index);
+ if (p == null) {
+ //can't figure out the piece, do a full update
+ updateCountsAll();
+ }
+ else {
+ updateCounts(p,increase);
+ }
+ }
+ else {
+ //can't figure out the piece, do a full update
+ updateCountsAll();
+ }
+ }
+
+ /**
+ * Update map-level count property for a piece
+ * @param piece, increase
+ */
+ private void updateCounts(GamePiece p, boolean increase) {
+ if (!doesTypeCounting() || getMap() == null) {
+ return;
+ }
+ Object pieceType = p.getProperty("_deckpiece_type");
+ if (pieceType != null) {
+ String mapProperty = (String) getMap().getProperty(deckName+"_"+pieceType);
+ if (mapProperty != null) {
+ int newValue = (Integer.decode(mapProperty)).intValue();
+ if (increase) {
+ newValue++;
+ }
+ else {
+ newValue--;
+ }
+ getMap().getPropertyListener().propertyChange(new PropertyChangeEvent(this,deckName+"_"+pieceType,null,""+newValue));
+ }
+ }
+ }
+
+ /**
* Set the <deckName>_numPieces property in the containing Map
* @param oldPieceCount
*/
protected void fireNumCardsProperty() {
- if (getMap() != null) {
- getMap().getPropertyListener().propertyChange(new PropertyChangeEvent(this,deckName+"_numPieces",null,String.valueOf(pieceCount)));
- }
+ if (getMap() != null) {
+ getMap().getPropertyListener().propertyChange(new PropertyChangeEvent(this,deckName+"_numPieces",null,String.valueOf(pieceCount)));
+ }
}
protected void insertPieceAt(GamePiece p, int index) {
- super.insertPieceAt(p, index);
- fireNumCardsProperty();
- }
-
- protected void removePieceAt(int index) {
- super.removePieceAt(index);
- fireNumCardsProperty();
- }
+ super.insertPieceAt(p, index);
+ updateCounts(p,true);
+ fireNumCardsProperty();
+ }
- public void removeAll() {
- super.removeAll();
- fireNumCardsProperty();
- }
-
- public void setMap(Map map) {
- super.setMap(map);
- fireNumCardsProperty();
- }
+ protected void removePieceAt(int index) {
+ updateCounts(index,false);
+ super.removePieceAt(index);
+ fireNumCardsProperty();
+ }
- protected void mySetType(String type) {
+ public void removeAll() {
+ super.removeAll();
+ updateCountsAll();
+ fireNumCardsProperty();
+ }
+
+ public void setMap(Map map) {
+ super.setMap(map);
+ updateCountsAll();
+ fireNumCardsProperty();
+ }
+
+ protected void mySetType(String type) {
SequenceEncoder.Decoder st = new SequenceEncoder.Decoder(type, ';');
st.nextToken();
drawOutline = st.nextBoolean(true);
@@ -183,6 +258,8 @@
shuffleKey = st.nextKeyStroke(null);
reshuffleKey = st.nextKeyStroke(null);
maxStack = st.nextInt(10);
+ countTypes = st.nextStringArray(0);
+ typeCounting = st.nextBoolean(false);
if (shuffleListener == null) {
shuffleListener = new KeyStrokeListener(new ActionListener() {
@@ -248,9 +325,17 @@
}
public int getMaxStack() {
- return maxStack;
+ return maxStack;
}
+ public String[] getCountTypes() {
+ return countTypes;
+ }
+
+ public boolean doesTypeCounting() {
+ return typeCounting;
+ }
+
public String getFaceDownMsgFormat() {
return faceDownMsgFormat;
}
@@ -300,9 +385,17 @@
}
public void setMaxStack(int maxStack) {
- this.maxStack = maxStack;
+ this.maxStack = maxStack;
}
+ public void setCountTypes(String[] countTypes) {
+ this.countTypes = countTypes;
+ }
+
+ public void setTypeCounting(boolean typeCounting) {
+ this.typeCounting = typeCounting;
+ }
+
public void setAllowSelectDraw(boolean allowSelectDraw) {
this.allowSelectDraw = allowSelectDraw;
}
@@ -377,7 +470,8 @@
se.append(drawOutline).append(ColorConfigurer.colorToString(outlineColor)).append(String.valueOf(size.width)).append(String.valueOf(size.height)).append(
faceDownOption).append(shuffleOption).append(String.valueOf(allowMultipleDraw)).append(String.valueOf(allowSelectDraw)).append(
String.valueOf(reversible)).append(reshuffleCommand).append(reshuffleTarget).append(reshuffleMsgFormat).append(deckName).append(shuffleMsgFormat)
- .append(reverseMsgFormat).append(faceDownMsgFormat).append(drawFaceUp).append(persistable).append(shuffleKey).append(reshuffleKey).append(String.valueOf(maxStack));
+ .append(reverseMsgFormat).append(faceDownMsgFormat).append(drawFaceUp).append(persistable).append(shuffleKey).append(reshuffleKey).append(String.valueOf(maxStack))
+ .append(countTypes).append(typeCounting);
return ID + se.getValue();
}
@@ -1016,19 +1110,18 @@
}
}
- /**
- * Return the number of cards to be returned by next call to {@link #drawCards()}
- */
- public int getDragCount() {
- return dragCount;
- }
+ /**
+ * Return the number of cards to be returned by next call to {@link #drawCards()}
+ */
+ public int getDragCount() {
+ return dragCount;
+ }
- /**
- * Set the number of cards to be returned by next call to {@link #drawCards()}
- * @param dragCount
- */
- public void setDragCount(int dragCount) {
- this.dragCount = dragCount;
- }
-
+ /**
+ * Set the number of cards to be returned by next call to {@link #drawCards()}
+ * @param dragCount
+ */
+ public void setDragCount(int dragCount) {
+ this.dragCount = dragCount;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|