summaryrefslogtreecommitdiffstats
path: root/src/jrummikub
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub')
-rw-r--r--src/jrummikub/control/TurnControl.java75
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);
}