diff options
author | Ida Massow <massow@informatik.uni-luebeck.de> | 2011-05-09 21:55:18 +0200 |
---|---|---|
committer | Ida Massow <massow@informatik.uni-luebeck.de> | 2011-05-09 21:55:18 +0200 |
commit | c5374de06f0cf8a2bcf2bd43c4aabaa33cd36d01 (patch) | |
tree | 3830c3f567387de6909635241af52f920c805953 /src/jrummikub/control/TurnControl.java | |
parent | 223f6d7e858021d9d08e8d074c06bf9804518ee4 (diff) | |
download | JRummikub-c5374de06f0cf8a2bcf2bd43c4aabaa33cd36d01.tar JRummikub-c5374de06f0cf8a2bcf2bd43c4aabaa33cd36d01.zip |
HAnd Range selection implementiert und getestet und funktioniert
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@197 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control/TurnControl.java')
-rw-r--r-- | src/jrummikub/control/TurnControl.java | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/src/jrummikub/control/TurnControl.java b/src/jrummikub/control/TurnControl.java index cb18f5e..bf921e4 100644 --- a/src/jrummikub/control/TurnControl.java +++ b/src/jrummikub/control/TurnControl.java @@ -2,6 +2,7 @@ package jrummikub.control; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -16,9 +17,32 @@ import jrummikub.util.IEvent; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.util.IListener2; +import jrummikub.util.Pair; import jrummikub.view.IView; public class TurnControl { + private static class HandStoneComparator implements + Comparator<Pair<Stone, Position>> { + @Override + public int compare(Pair<Stone, Position> pair1, + Pair<Stone, Position> pair2) { + Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond(); + if (pos1.getY() < pos2.getY()) { + return -1; + } else if (pos1.getY() > pos2.getY()) { + return 1; + } else { + if (pos1.getX() < pos2.getX()) { + return -1; + } else if (pos1.getX() > pos2.getX()) { + return 1; + } else { + return 0; + } + } + } + } + private IHand hand; private ITable table; private ITurnTimer timer; @@ -66,6 +90,15 @@ public class TurnControl { } })); + connections.add(view.getPlayerPanel().getHandPanel() + .getRangeClickEvent().add(new IListener2<Stone, Boolean>() { + + @Override + public void handle(Stone stone, Boolean collect) { + handRangeClick(stone, collect); + } + })); + connections.add(view.getTablePanel().getStoneCollectionPanel() .getStoneClickEvent().add(new IListener2<Stone, Boolean>() { @@ -210,7 +243,7 @@ public class TurnControl { stoneClick(stone, collect); return; } - Stone lastStone = selectedStones.get(selectedStones.size()-1); + Stone lastStone = selectedStones.get(selectedStones.size() - 1); StoneSet lastSet = table.findStoneSet(lastStone); StoneSet selectedSet = table.findStoneSet(stone); if (lastSet != selectedSet) { @@ -233,7 +266,45 @@ public class TurnControl { selectedStones.remove(s); selectedStones.add(s); } - + + view.setSelectedStones(selectedStones); + } + + private void handRangeClick(Stone stone, Boolean collect) { + if (selectedStones.isEmpty()) { + stoneClick(stone, collect); + return; + } + Stone lastStone = selectedStones.get(selectedStones.size() - 1); + StoneSet lastSet = table.findStoneSet(lastStone); + if (lastSet != null) { + stoneClick(stone, collect); + return; + } + List<Pair<Stone, Position>> handPairs = new ArrayList<Pair<Stone, Position>>(); + for (Pair<Stone, Position> entry : hand) { + handPairs.add(entry); + } + + Collections.sort(handPairs, new HandStoneComparator()); + + List<Stone> handStones = new ArrayList<Stone>(); + for (Pair<Stone, Position> entry : handPairs) { + handStones.add(entry.getFirst()); + } + + int firstIndex = handStones.indexOf(lastStone); + int lastIndex = handStones.indexOf(stone); + if (firstIndex > lastIndex) { + int temp = firstIndex; + firstIndex = lastIndex; + lastIndex = temp; + } + for (int i = firstIndex; i <= lastIndex; i++) { + Stone s = handStones.get(i); + selectedStones.remove(s); + selectedStones.add(s); + } view.setSelectedStones(selectedStones); } |