From c5374de06f0cf8a2bcf2bd43c4aabaa33cd36d01 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Mon, 9 May 2011 21:55:18 +0200 Subject: 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 --- src/jrummikub/control/TurnControl.java | 75 +++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'src') 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> { + @Override + public int compare(Pair pair1, + Pair 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() { + + @Override + public void handle(Stone stone, Boolean collect) { + handRangeClick(stone, collect); + } + })); + connections.add(view.getTablePanel().getStoneCollectionPanel() .getStoneClickEvent().add(new IListener2() { @@ -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> handPairs = new ArrayList>(); + for (Pair entry : hand) { + handPairs.add(entry); + } + + Collections.sort(handPairs, new HandStoneComparator()); + + List handStones = new ArrayList(); + for (Pair 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); } -- cgit v1.2.3