Implement pause function

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@390 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-08 21:58:16 +02:00
parent 2e376414b9
commit 45d5b3ae10
40 changed files with 642 additions and 425 deletions

View file

@ -17,6 +17,8 @@ public class MockPlayerPanel implements IPlayerPanel {
/** */ /** */
public MockEvent sortByRunsEvent = new MockEvent(); public MockEvent sortByRunsEvent = new MockEvent();
/** */ /** */
public MockEvent pauseEvent = new MockEvent();
/** */
public TurnMode turnMode; public TurnMode turnMode;
@Override @Override
@ -49,5 +51,4 @@ public class MockPlayerPanel implements IPlayerPanel {
public void setEndTurnMode(TurnMode turnMode) { public void setEndTurnMode(TurnMode turnMode) {
this.turnMode = turnMode; this.turnMode = turnMode;
} }
} }

View file

@ -49,6 +49,10 @@ public class MockView implements IView {
public MockEvent1<File> loadEvent = new MockEvent1<File>(); public MockEvent1<File> loadEvent = new MockEvent1<File>();
/** */ /** */
public MockEvent1<File> saveEvent = new MockEvent1<File>(); public MockEvent1<File> saveEvent = new MockEvent1<File>();
/** */
public MockEvent pauseEvent = new MockEvent();
/** */
public MockEvent endPauseEvent = new MockEvent();
@Override @Override
public MockTablePanel getTablePanel() { public MockTablePanel getTablePanel() {
@ -157,4 +161,20 @@ public class MockView implements IView {
@Override @Override
public void clearView() { public void clearView() {
} }
@Override
public void enablePauseMode(boolean enable) {
// TODO Auto-generated method stub
}
@Override
public IEvent getPauseEvent() {
return pauseEvent;
}
@Override
public IEvent getEndPauseEvent() {
return endPauseEvent;
}
} }

View file

@ -51,14 +51,13 @@ public class ApplicationControl {
new IListener3<GameSettings, GameState, IRoundState>() { new IListener3<GameSettings, GameState, IRoundState>() {
@Override @Override
public void handle(GameSettings settings, public void handle(GameSettings settings, GameState gameState,
GameState gameState, IRoundState roundState) { IRoundState roundState) {
settingsControl.abort(); settingsControl.abort();
if (gameControl != null){ if (gameControl != null) {
gameControl.abortGame(); gameControl.abortGame();
} }
gameControl = new GameControl(settings, gameControl = new GameControl(settings, saveControl, view);
saveControl, view);
addGameControlListeners(gameControl); addGameControlListeners(gameControl);
gameControl.continueGame(gameState, roundState); gameControl.continueGame(gameState, roundState);
@ -70,8 +69,7 @@ public class ApplicationControl {
public void handle(GameSettings settings) { public void handle(GameSettings settings) {
saveControl.setGameSettings(settings); saveControl.setGameSettings(settings);
gameControl = new GameControl(settings, gameControl = new GameControl(settings, saveControl, view);
saveControl, view);
addGameControlListeners(gameControl); addGameControlListeners(gameControl);
gameControl.startGame(); gameControl.startGame();

View file

@ -174,8 +174,7 @@ public class GameControl {
view.getScorePanel().setPlayers(gameSettings.getPlayerList()); view.getScorePanel().setPlayers(gameSettings.getPlayerList());
view.getScorePanel().setScores(gameState.getScores()); view.getScorePanel().setScores(gameState.getScores());
view.getScorePanel().setAccumulatedScore( view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
gameState.getAccumulatedScore());
view.getScorePanel().update(); view.getScorePanel().update();
view.showScorePanel(true); view.showScorePanel(true);
} }

View file

@ -102,12 +102,11 @@ public class RoundControl {
: BottomPanelType.COMPUTER_HAND_PANEL); : BottomPanelType.COMPUTER_HAND_PANEL);
view.getTablePanel().setStoneSets(clonedTable.clone()); view.getTablePanel().setStoneSets(clonedTable.clone());
view.setCurrentPlayerName(roundState.getActivePlayer() view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getName()); .getName());
view.setCurrentPlayerColor(roundState.getActivePlayer() view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getColor()); .getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer() view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
.getLaidOut());
if (!isHuman) if (!isHuman)
startTurn(); startTurn();
@ -132,11 +131,11 @@ public class RoundControl {
view.getPlayerPanel().setEndTurnMode(turnMode); view.getPlayerPanel().setEndTurnMode(turnMode);
} }
turnControl = TurnControlFactory.getFactory( turnControl = TurnControlFactory.getFactory(
roundState.getActivePlayer().getPlayerSettings() roundState.getActivePlayer().getPlayerSettings().getTurnControlType())
.getTurnControlType()).create(); .create();
turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand, turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand,
roundState.getActivePlayer().getLaidOut(), turnMode), roundState.getActivePlayer().getLaidOut(), turnMode), roundState
roundState.getGameSettings(), view); .getGameSettings(), view);
turnControl.getEndOfTurnEvent().add(new IListener() { turnControl.getEndOfTurnEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
@ -156,10 +155,8 @@ public class RoundControl {
void deal() { void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) { for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand(); IHand hand = roundState.getNthNextPlayer(i).getHand();
for (int j = 0; j < roundState.getGameSettings() for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
.getNumberOfStonesDealt(); j++) { hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
0));
} }
} }
} }
@ -170,13 +167,11 @@ public class RoundControl {
int totalValue = 0; int totalValue = 0;
for (StoneSet set : newSets) { for (StoneSet set : newSets) {
totalValue += set.classify(roundState.getGameSettings()) totalValue += set.classify(roundState.getGameSettings()).getSecond();
.getSecond();
} }
return totalValue == 0 return totalValue == 0
|| totalValue >= roundState.getGameSettings() || totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
.getInitialMeldThreshold();
} }
private void endOfTurn() { private void endOfTurn() {
@ -216,8 +211,7 @@ public class RoundControl {
} }
if (!roundState.getActivePlayer().getLaidOut()) { if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones // Player touched forbidden stones
if (!tableSetDifference(clonedTable, roundState.getTable()) if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
.isEmpty()) {
rejectMove(); rejectMove();
return; return;
} }
@ -226,8 +220,7 @@ public class RoundControl {
return; return;
} }
} }
Set<Stone> tableDiff = tableDifference(roundState.getTable(), Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
clonedTable);
roundState.setTable(clonedTable); roundState.setTable(clonedTable);
@ -243,8 +236,7 @@ public class RoundControl {
} }
private void rejectMove() { private void rejectMove() {
Set<Stone> tableDiff = tableDifference(roundState.getTable(), Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
clonedTable);
// deal penalty, reset // deal penalty, reset
roundState.getGameHeap().putBack(tableDiff); roundState.getGameHeap().putBack(tableDiff);
dealPenalty(tableDiff.size()); dealPenalty(tableDiff.size());
@ -338,12 +330,10 @@ public class RoundControl {
stonePoints = playerHand.isInitialMeldPossible(roundState stonePoints = playerHand.isInitialMeldPossible(roundState
.getGameSettings()) ? 200 : 100; .getGameSettings()) ? 200 : 100;
} else { } else {
stonePoints = playerHand.getStonePoints(roundState stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
.getGameSettings());
} }
bestScore = updateBestScore(bestScore, -stonePoints, bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
playerHand.getSize());
points.add(-stonePoints); points.add(-stonePoints);
pointSum += stonePoints; pointSum += stonePoints;

View file

@ -110,8 +110,8 @@ public class SaveControl {
return; return;
} }
try { try {
ObjectOutputStream stream = new ObjectOutputStream( ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
new FileOutputStream(file)); file));
stream.writeObject(gameSettings); stream.writeObject(gameSettings);
stream.writeObject(gameState); stream.writeObject(gameState);

View file

@ -16,7 +16,6 @@ import jrummikub.view.IView;
* Abstract base class for TurnControls * Abstract base class for TurnControls
*/ */
public abstract class AbstractTurnControl implements ITurnControl { public abstract class AbstractTurnControl implements ITurnControl {
protected Event endOfTurnEvent = new Event(); protected Event endOfTurnEvent = new Event();
protected Event redealEvent = new Event(); protected Event redealEvent = new Event();
protected TurnInfo turnInfo; protected TurnInfo turnInfo;
@ -25,7 +24,6 @@ public abstract class AbstractTurnControl implements ITurnControl {
protected ITurnTimer timer; protected ITurnTimer timer;
protected List<Connection> connections = new ArrayList<Connection>(); protected List<Connection> connections = new ArrayList<Connection>();
@Override @Override
public IEvent getEndOfTurnEvent() { public IEvent getEndOfTurnEvent() {
return endOfTurnEvent; return endOfTurnEvent;
@ -36,6 +34,16 @@ public abstract class AbstractTurnControl implements ITurnControl {
return redealEvent; return redealEvent;
} }
private void pauseTurn() {
timer.stopTimer();
view.enablePauseMode(true);
}
private void resumeTurn() {
timer.startTimer();
view.enablePauseMode(false);
}
protected abstract void timeOut(); protected abstract void timeOut();
@Override @Override
@ -52,6 +60,18 @@ public abstract class AbstractTurnControl implements ITurnControl {
timeOut(); timeOut();
} }
})); }));
connections.add(view.getPauseEvent().add(new IListener() {
@Override
public void handle() {
pauseTurn();
}
}));
connections.add(view.getEndPauseEvent().add(new IListener() {
@Override
public void handle() {
resumeTurn();
}
}));
} }
protected void cleanUp() { protected void cleanUp() {

View file

@ -111,8 +111,10 @@ public class BaseAIControl extends AbstractTurnControl {
for (Stone stone : set) { for (Stone stone : set) {
handStones.add(pickUpMatchingStone(stone)); handStones.add(pickUpMatchingStone(stone));
} }
turnInfo.getTable().drop(new StoneSet(handStones), new Position( turnInfo.getTable().drop(
(float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3)); new StoneSet(handStones),
new Position((float) Math.random() * 30 - 15,
(float) Math.random() * 6 - 3));
} }
emitEndOfTurn(); emitEndOfTurn();

View file

@ -97,8 +97,7 @@ public class HumanTurnControl extends AbstractTurnControl {
} }
})); }));
connections.add(view.getPlayerPanel().getRedealEvent() connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
.add(new IListener() {
@Override @Override
public void handle() { public void handle() {
endOfTurn(true); endOfTurn(true);
@ -304,8 +303,10 @@ public class HumanTurnControl extends AbstractTurnControl {
} }
pickUpSelectedStones(); pickUpSelectedStones();
turnInfo.getTable().drop(new StoneSet(selectedStones), new Position(position.getX() turnInfo.getTable().drop(
- selectedStones.size() * 0.5f, position.getY() - 0.5f)); new StoneSet(selectedStones),
new Position(position.getX() - selectedStones.size() * 0.5f, position
.getY() - 0.5f));
selectedStones.clear(); selectedStones.clear();
view.getTablePanel().setStoneSets(turnInfo.getTable()); view.getTablePanel().setStoneSets(turnInfo.getTable());
@ -421,15 +422,14 @@ public class HumanTurnControl extends AbstractTurnControl {
turnInfo.getTable().drop(joinedSet, newPos); turnInfo.getTable().drop(joinedSet, newPos);
} else { } else {
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet); StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
turnInfo.getTable().drop(joinedSet, new Position(newPos.getX() turnInfo.getTable().drop(joinedSet,
- selectedStones.size(), newPos.getY())); new Position(newPos.getX() - selectedStones.size(), newPos.getY()));
} }
} else { } else {
turnInfo.getTable().drop( turnInfo.getTable().drop(
new StoneSet(selectedStones), new StoneSet(selectedStones),
new Position(pos.getX() new Position(pos.getX() + (set.getSize() - selectedStones.size())
+ (set.getSize() - selectedStones.size()) * 0.5f, * 0.5f, pos.getY()));
pos.getY()));
} }
selectedStones.clear(); selectedStones.clear();
@ -521,8 +521,7 @@ public class HumanTurnControl extends AbstractTurnControl {
static class HandStonePositionComparator implements static class HandStonePositionComparator implements
Comparator<Pair<Stone, Position>> { Comparator<Pair<Stone, Position>> {
@Override @Override
public int compare(Pair<Stone, Position> pair1, public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) {
Pair<Stone, Position> pair2) {
Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond(); Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond();
if (pos1.getY() < pos2.getY()) { if (pos1.getY() < pos2.getY()) {
return -1; return -1;

View file

@ -43,6 +43,11 @@ public interface ITurnControl {
*/ */
public void startTurn(); public void startTurn();
/**
* Abort the turn
*/
public void abortTurn();
/** /**
* The TurnInfo class encapsulates all information concerning the current turn * The TurnInfo class encapsulates all information concerning the current turn
*/ */
@ -108,9 +113,4 @@ public interface ITurnControl {
return turnMode; return turnMode;
} }
} }
/**
* Abort the turn
*/
public void abortTurn();
} }

View file

@ -17,7 +17,8 @@ public interface IPlayer extends Serializable {
/** /**
* Set the current hand of the player * Set the current hand of the player
* *
* @param hand the new hand * @param hand
* the new hand
*/ */
public void setHand(IHand hand); public void setHand(IHand hand);

View file

@ -62,8 +62,8 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
} }
/** /**
* Test for rule conflict within the StoneSet and determine whether the set * Test for rule conflict within the StoneSet and determine whether the set is
* is a group or a run * a group or a run
* *
* @param settings * @param settings
* GameSettings * GameSettings
@ -87,14 +87,10 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
if (stones.size() > settings.getHighestValue()) { if (stones.size() > settings.getHighestValue()) {
return new Pair<Type, Integer>(INVALID, 0); return new Pair<Type, Integer>(INVALID, 0);
} else if (stones.size() > settings.getStoneColors().size()) { } else if (stones.size() > settings.getStoneColors().size()) {
return new Pair<Type, Integer>( return new Pair<Type, Integer>(RUN,
RUN, (settings.getHighestValue() * (settings.getHighestValue() + 1)) / 2
(settings.getHighestValue() * (settings
.getHighestValue() + 1))
/ 2
- (stones.size() - settings.getHighestValue()) - (stones.size() - settings.getHighestValue())
* (stones.size() - settings.getHighestValue() - 1) * (stones.size() - settings.getHighestValue() - 1) / 2);
/ 2);
} else { } else {
return new Pair<Type, Integer>(GROUP, stones.size() return new Pair<Type, Integer>(GROUP, stones.size()
* settings.getHighestValue()); * settings.getHighestValue());
@ -187,8 +183,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
return new Pair<StoneSet, StoneSet>(this, null); return new Pair<StoneSet, StoneSet>(this, null);
} }
StoneSet firstSet = new StoneSet(stones.subList(0, position)); StoneSet firstSet = new StoneSet(stones.subList(0, position));
StoneSet secondSet = new StoneSet(stones.subList(position, StoneSet secondSet = new StoneSet(stones.subList(position, stones.size()));
stones.size()));
return new Pair<StoneSet, StoneSet>(firstSet, secondSet); return new Pair<StoneSet, StoneSet>(firstSet, secondSet);
} }

View file

@ -52,8 +52,7 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (currentObject == object) if (currentObject == object)
continue; continue;
Position currentPosition = getPosition(currentObject); Position currentPosition = getPosition(currentObject);
if (!objectsOverlap(object, position, currentObject, if (!objectsOverlap(object, position, currentObject, currentPosition)) {
currentPosition)) {
continue; continue;
} }
// Object would be placed inside the current object // Object would be placed inside the current object
@ -64,16 +63,17 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
// Move object to avoid overlap // Move object to avoid overlap
switch (newDirection) { switch (newDirection) {
case TOP: case TOP:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() - currentObject.getHeight()); - currentObject.getHeight());
break; break;
case BOTTOM: case BOTTOM:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() + object.getHeight()); + object.getHeight());
break; break;
case LEFT: case LEFT:
newPosition = new Position(position.getX() newPosition = new Position(
- currentObject.getWidth(), currentPosition.getY()); position.getX() - currentObject.getWidth(),
currentPosition.getY());
break; break;
case RIGHT: case RIGHT:
newPosition = new Position(position.getX() + object.getWidth(), newPosition = new Position(position.getX() + object.getWidth(),
@ -87,8 +87,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
} }
/** /**
* Checks whether the object may be placed on the given position, computes * Checks whether the object may be placed on the given position, computes new
* new position if not * position if not
* *
* @param object * @param object
* to be dropped * to be dropped
@ -122,15 +122,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) { if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
return false; return false;
} }
if (lessOrEqual(position1.getY() + object1.getHeight(), if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) {
position2.getY())) {
return false; return false;
} }
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) { if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
return false; return false;
} }
if (lessOrEqual(position2.getY() + object2.getHeight(), if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) {
position1.getY())) {
return false; return false;
} }
return true; return true;
@ -171,15 +169,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
double blockingRight = blocking.getSecond().getX() double blockingRight = blocking.getSecond().getX()
+ blocking.getFirst().getWidth(); + blocking.getFirst().getWidth();
double overlapRight = Math.min(objectRight, blockingRight); double overlapRight = Math.min(objectRight, blockingRight);
double overlapLeft = Math.max(position.getX(), blocking.getSecond() double overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
.getX());
double overlapX = overlapRight - overlapLeft; double overlapX = overlapRight - overlapLeft;
double objectBottom = position.getY() + object.getHeight(); double objectBottom = position.getY() + object.getHeight();
double blockingBottom = blocking.getSecond().getY() double blockingBottom = blocking.getSecond().getY()
+ blocking.getFirst().getHeight(); + blocking.getFirst().getHeight();
double overlapBottom = Math.min(objectBottom, blockingBottom); double overlapBottom = Math.min(objectBottom, blockingBottom);
double overlapTop = Math.max(position.getY(), blocking.getSecond() double overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
.getY());
double overlapY = overlapBottom - overlapTop; double overlapY = overlapBottom - overlapTop;
// vertical or horizontal Shift // vertical or horizontal Shift
// TODO magic factor // TODO magic factor

View file

@ -36,8 +36,8 @@ public interface ISettingsPanel {
}; };
/** /**
* The add player event is emitted when the user wants to add a player to * The add player event is emitted when the user wants to add a player to the
* the player list * player list
* *
* @return the event * @return the event
*/ */
@ -84,8 +84,8 @@ public interface ISettingsPanel {
public IEvent1<Integer> getChangeInitialMeldThresholdEvent(); public IEvent1<Integer> getChangeInitialMeldThresholdEvent();
/** /**
* The change StoneSet number event is emitted when the user wants to use * The change StoneSet number event is emitted when the user wants to use more
* more or less than 2 StoneSets per color * or less than 2 StoneSets per color
* *
* @return number of SoneSets * @return number of SoneSets
*/ */

View file

@ -108,8 +108,8 @@ public interface IView {
public void setCurrentPlayerColor(Color color); public void setCurrentPlayerColor(Color color);
/** /**
* Is used for the PlayerPanel to display if a player has laid out along * Is used for the PlayerPanel to display if a player has laid out along with
* with the name * the name
* *
* @param hasLaidOut * @param hasLaidOut
* specifies if the current player has laid out or not * specifies if the current player has laid out or not
@ -132,24 +132,8 @@ public interface IView {
public void setBottomPanel(BottomPanelType type); public void setBottomPanel(BottomPanelType type);
/** /**
* Different types of bottom panels * The menu new game event is emitted when the user selects the new game menu
*/ * entry
public enum BottomPanelType {
/** */
START_GAME_PANEL,
/** */
START_TURN_PANEL,
/** */
HUMAN_HAND_PANEL,
/** */
COMPUTER_HAND_PANEL,
/** */
WIN_PANEL
}
/**
* The menu new game event is emitted when the user selects the new game
* menu entry
* *
* @return the event * @return the event
*/ */
@ -178,5 +162,27 @@ public interface IView {
*/ */
public IEvent1<File> getSaveEvent(); public IEvent1<File> getSaveEvent();
public IEvent getPauseEvent();
public IEvent getEndPauseEvent();
public void clearView(); public void clearView();
void enablePauseMode(boolean enable);
/**
* Different types of bottom panels
*/
public enum BottomPanelType {
/** */
START_GAME_PANEL,
/** */
START_TURN_PANEL,
/** */
HUMAN_HAND_PANEL,
/** */
COMPUTER_HAND_PANEL,
/** */
WIN_PANEL
}
} }

View file

@ -37,8 +37,8 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
HandPanel.class.getResource("/jrummikub/resource/dark_wood.png")); HandPanel.class.getResource("/jrummikub/resource/dark_wood.png"));
BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
BufferedImage.TYPE_INT_RGB); BufferedImage.TYPE_INT_RGB);
DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(), darkImage.getIconHeight(), DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(),
BufferedImage.TYPE_INT_RGB); darkImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0); darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0);
@ -54,6 +54,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
private boolean repaintAll = true; private boolean repaintAll = true;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
/** /**
* Creates a new Board instance * Creates a new Board instance
*/ */
@ -92,8 +93,8 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
int size = height / HEIGHT; int size = height / HEIGHT;
BufferedImage background = isEnabled() ? scaledBackground
BufferedImage background = isEnabled() ? scaledBackground : scaledDarkBackground ; : scaledDarkBackground;
if (repaintAll) { if (repaintAll) {
if (background.getHeight() != size) { if (background.getHeight() != size) {

View file

@ -19,7 +19,21 @@ class ImageUtil {
g.fillRect(0, 0, size, size); g.fillRect(0, 0, size, size);
g.setColor(c); g.setColor(c);
g.fillRect(border, border, size - 2*border, size - 2*border); g.fillRect(border, border, size - 2 * border, size - 2 * border);
return new ImageIcon(image);
}
static ImageIcon createPauseIcon(int size) {
BufferedImage image = new BufferedImage(size, size,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.BLACK);
int barWidth = (int) (size * 0.425f);
g.fillRect(0, 0, barWidth, size);
g.fillRect(size - barWidth, 0, barWidth, size);
return new ImageIcon(image); return new ImageIcon(image);
} }

View file

@ -0,0 +1,95 @@
package jrummikub.view.impl;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
/**
* A panel that is displayed before a player's turn
*/
@SuppressWarnings("serial")
class PausePanel extends JPanel {
private final static int PANEL_INSET = 15;
private final static int PANEL_SEPARATOR = 10;
private final static float PANEL_FIRST_LINE_HEIGHT = 0.375f;
private final static int PANEL_MAX_WIDTH = 180;
private final static float MAX_BUTTON_FONT_SIZE = 12;
private JLabel pauseLabel;
private JButton endPauseButton;
private Event endPauseEvent = new Event();
/**
* Creates a new StartTurnPanel
*/
PausePanel() {
setLayout(null);
setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
PANEL_INSET));
pauseLabel = new JLabel();
pauseLabel.setHorizontalAlignment(JLabel.CENTER);
pauseLabel.setHorizontalTextPosition(JLabel.CENTER);
pauseLabel.setVerticalAlignment(JLabel.CENTER);
pauseLabel.setVerticalTextPosition(JLabel.CENTER);
add(pauseLabel);
endPauseButton = new JButton("Spiel fortsetzen");
endPauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
endPauseEvent.emit();
}
});
add(endPauseButton);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
}
void setCurrentPlayerName(String playerName) {
pauseLabel.setText("Der Zug von " + playerName + " ist pausiert.");
}
IEvent getEndPauseEvent() {
return endPauseEvent;
}
private void rescale() {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
if (width > PANEL_MAX_WIDTH) {
x += (width - PANEL_MAX_WIDTH) / 4;
width = width / 2 + PANEL_MAX_WIDTH / 2;
}
int firstLineHeight = (int) ((height - PANEL_SEPARATOR) * PANEL_FIRST_LINE_HEIGHT);
int buttonWidth = width;
int buttonHeight = height - PANEL_SEPARATOR - firstLineHeight;
float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
if (fontSize > MAX_BUTTON_FONT_SIZE)
fontSize = MAX_BUTTON_FONT_SIZE;
pauseLabel.setBounds(x, y, width, firstLineHeight);
endPauseButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR,
buttonWidth, buttonHeight);
endPauseButton.setFont(endPauseButton.getFont().deriveFont(fontSize));
}
}

View file

@ -53,11 +53,13 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private JButton endTurnButton; private JButton endTurnButton;
private JButton keepStonesButton; private JButton keepStonesButton;
private JButton redealButton; private JButton redealButton;
private JButton pauseButton;
private Event sortByGroupsEvent = new Event(); private Event sortByGroupsEvent = new Event();
private Event sortByRunsEvent = new Event(); private Event sortByRunsEvent = new Event();
private Event endTurnEvent = new Event(); private Event endTurnEvent = new Event();
private Event redealEvent = new Event(); private Event redealEvent = new Event();
private Event pauseEvent = new Event();
HandPanel getHandPanel() { HandPanel getHandPanel() {
return hand; return hand;
@ -117,6 +119,10 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
return redealEvent; return redealEvent;
} }
IEvent getPauseEvent() {
return pauseEvent;
}
private void createLeftPanel() { private void createLeftPanel() {
leftPanel = new JPanel(); leftPanel = new JPanel();
leftPanel.setLayout(null); leftPanel.setLayout(null);
@ -206,6 +212,13 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
redealEvent.emit(); redealEvent.emit();
} }
}); });
pauseButton = createButton(rightPanel, null, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pauseEvent.emit();
}
});
} }
private JButton createButton(JPanel panel, String caption, private JButton createButton(JPanel panel, String caption,
@ -271,6 +284,67 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
hand.addComponentListener(rescaleListener); hand.addComponentListener(rescaleListener);
} }
@Override
public void setEndTurnMode(TurnMode turnMode) {
switch (turnMode) {
case MAY_REDEAL:
endTurnButton.setVisible(false);
keepStonesButton.setVisible(true);
redealButton.setVisible(true);
break;
case INSPECT_ONLY:
endTurnButton.setText("N\u00e4chster Spieler");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
case NORMAL_TURN:
endTurnButton.setText("Zug beenden");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
}
}
void showButtons(boolean show) {
currentPlayerNameLabel.setVisible(show);
hasLaidOutLabel.setVisible(show);
sortByGroupsButton.setVisible(show);
sortByRunsButton.setVisible(show);
timeBar.setVisible(show);
pauseButton.setVisible(show);
if (!show) {
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
endTurnButton.setVisible(false);
redealButton.setVisible(false);
keepStonesButton.setVisible(false);
}
}
void enableButtons(boolean enable) {
sortByGroupsButton.setEnabled(enable);
sortByRunsButton.setEnabled(enable);
if (!enable) {
setEndTurnMode(TurnMode.NORMAL_TURN);
endTurnButton.setText("<html><center>Computer denkt nach");
hand.setStones(Collections.<Pair<Stone, Position>> emptyList());
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
}
endTurnButton.setEnabled(enable);
redealButton.setEnabled(enable);
keepStonesButton.setEnabled(enable);
hand.setEnabled(enable);
}
private class LeftPanelResizeListener extends ComponentAdapter { private class LeftPanelResizeListener extends ComponentAdapter {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
@ -341,10 +415,16 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
fontSize * 1.5f)); fontSize * 1.5f));
handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth, handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth,
getHeight() / 2); getHeight() / 2);
handRowDownButton.setFont(handRowDownButton.getFont().deriveFont( handRowDownButton.setFont(handRowDownButton.getFont()
fontSize * 1.5f)); .deriveFont(fontSize));
timeBar.setBounds(x, y, width - firstLineHeight - SIDE_PANEL_SEPARATOR,
firstLineHeight);
pauseButton.setBounds(x + width - firstLineHeight, y, firstLineHeight,
firstLineHeight);
pauseButton.setIcon(ImageUtil
.createPauseIcon((int) (firstLineHeight * 0.5f)));
timeBar.setBounds(x, y, width, firstLineHeight);
endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR,
buttonWidth, buttonHeight); buttonWidth, buttonHeight);
endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize)); endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize));
@ -360,64 +440,4 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
} }
} }
@Override
public void setEndTurnMode(TurnMode turnMode) {
switch (turnMode) {
case MAY_REDEAL:
endTurnButton.setVisible(false);
keepStonesButton.setVisible(true);
redealButton.setVisible(true);
break;
case INSPECT_ONLY:
endTurnButton.setText("N\u00e4chster Spieler");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
case NORMAL_TURN:
endTurnButton.setText("Zug beenden");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
}
}
void showButtons(boolean show) {
currentPlayerNameLabel.setVisible(show);
hasLaidOutLabel.setVisible(show);
sortByGroupsButton.setVisible(show);
sortByRunsButton.setVisible(show);
timeBar.setVisible(show);
if (!show) {
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
endTurnButton.setVisible(false);
redealButton.setVisible(false);
keepStonesButton.setVisible(false);
}
}
void enableButtons(boolean enable) {
sortByGroupsButton.setEnabled(enable);
sortByRunsButton.setEnabled(enable);
if (!enable) {
setEndTurnMode(TurnMode.NORMAL_TURN);
endTurnButton.setText("<html><center>Computer denkt nach");
hand.setStones(Collections.<Pair<Stone, Position>> emptyList());
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
}
endTurnButton.setEnabled(enable);
redealButton.setEnabled(enable);
keepStonesButton.setEnabled(enable);
hand.setEnabled(enable);
}
} }

View file

@ -62,22 +62,10 @@ class StartTurnPanel extends JPanel {
}); });
} }
/**
* Sets the current player name
*
* @param playerName
* the player name
*/
void setCurrentPlayerName(String playerName) { void setCurrentPlayerName(String playerName) {
startTurnLabel.setText(playerName + " ist jetzt an der Reihe."); startTurnLabel.setText(playerName + " ist jetzt an der Reihe.");
} }
/**
* The start turn event is emitted when the current player has clicked the
* start turn button
*
* @return the event
*/
IEvent getStartTurnEvent() { IEvent getStartTurnEvent() {
return startTurnEvent; return startTurnEvent;
} }

View file

@ -37,6 +37,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements
private Event1<Point> otherClickEvent = new Event1<Point>(); private Event1<Point> otherClickEvent = new Event1<Point>();
private boolean pauseMode = false;
/** /**
* Creates a new StoneCollection instance * Creates a new StoneCollection instance
*/ */
@ -121,6 +123,10 @@ class StoneCollectionPanel extends AbstractStonePanel implements
} }
} }
if (pauseMode) {
return;
}
int inset = (int) (getHeight() * INSET_RATIO); int inset = (int) (getHeight() * INSET_RATIO);
int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2 int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
* inset, height = getHeight(); * inset, height = getHeight();
@ -148,4 +154,9 @@ class StoneCollectionPanel extends AbstractStonePanel implements
} }
} }
} }
void enablePauseMode(boolean enable) {
pauseMode = enable;
repaint();
}
} }

View file

@ -60,8 +60,7 @@ class StonePainter {
int g = (int) (color.getGreen() * BRIGHTER_SCALE); int g = (int) (color.getGreen() * BRIGHTER_SCALE);
int b = (int) (color.getBlue() * BRIGHTER_SCALE); int b = (int) (color.getBlue() * BRIGHTER_SCALE);
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
: b);
} }
private static Color hover(Color color) { private static Color hover(Color color) {
@ -69,8 +68,7 @@ class StonePainter {
int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
: b);
} }
public static Color getColor(StoneColor color) { public static Color getColor(StoneColor color) {
@ -211,8 +209,7 @@ class StonePainter {
defaultStones.put(color, new HashMap<Integer, BufferedImage>()); defaultStones.put(color, new HashMap<Integer, BufferedImage>());
selectedStones.put(color, new HashMap<Integer, BufferedImage>()); selectedStones.put(color, new HashMap<Integer, BufferedImage>());
hoveredStones.put(color, new HashMap<Integer, BufferedImage>()); hoveredStones.put(color, new HashMap<Integer, BufferedImage>());
hoveredSelectedStones.put(color, hoveredSelectedStones.put(color, new HashMap<Integer, BufferedImage>());
new HashMap<Integer, BufferedImage>());
} }
} }
@ -224,8 +221,7 @@ class StonePainter {
setScale(scale); setScale(scale);
} }
private void paintStoneBackground(Graphics2D g, Rectangle r, private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) {
Color background) {
// Paint background // Paint background
g.setColor(background); g.setColor(background);
g.fillRect(r.x, r.y, r.width, r.height); g.fillRect(r.x, r.y, r.width, r.height);
@ -326,9 +322,8 @@ class StonePainter {
pos + (fm.getAscent() - fm.getDescent()) / 2 + 1); pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
} }
g.setColor(color); g.setColor(color);
g.drawString(value, g.drawString(value, (int) (r.x + r.width / 2 - stringRect.getWidth() / 2),
(int) (r.x + r.width / 2 - stringRect.getWidth() / 2), pos pos + (fm.getAscent() - fm.getDescent()) / 2);
+ (fm.getAscent() - fm.getDescent()) / 2);
} }
private void paintCircle(Graphics2D g, Rectangle r, Color background) { private void paintCircle(Graphics2D g, Rectangle r, Color background) {
@ -337,12 +332,11 @@ class StonePainter {
// Paint circle // Paint circle
g.setColor(background.darker()); g.setColor(background.darker());
g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, 170);
170);
g.setColor(brighter(background)); g.setColor(brighter(background));
g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, size,
size, -130, 170); -130, 170);
} }
/** /**
@ -363,16 +357,16 @@ class StonePainter {
boolean selected, boolean hovered) { boolean selected, boolean hovered) {
int width = getStoneWidth(); int width = getStoneWidth();
int height = getStoneHeight(); int height = getStoneHeight();
int x = (int) Math.round(p.getX() * width), y = (int) Math.round(p int x = (int) Math.round(p.getX() * width), y = (int) Math.round(p.getY()
.getY() * height); * height);
if (stone.isJoker()) { if (stone.isJoker()) {
g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), x, y,
x, y, null); null);
} else { } else {
g.drawImage( g.drawImage(
getStoneImage(stone.getColor(), stone.getValue(), selected, getStoneImage(stone.getColor(), stone.getValue(), selected, hovered),
hovered), x, y, null); x, y, null);
} }
} }
} }

View file

@ -11,6 +11,7 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -20,6 +21,7 @@ import javax.swing.SwingUtilities;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.Event1; import jrummikub.util.Event1;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
@ -39,8 +41,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private final static ImageIcon BRIGHT_BACKGROUND = new ImageIcon( private final static ImageIcon BRIGHT_BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/bright_felt.png")); HandPanel.class.getResource("/jrummikub/resource/bright_felt.png"));
private final static double MIN_VISIBLE_WIDTH = 15; private final static double MIN_VISIBLE_WIDTH = 10;
private final static double MIN_VISIBLE_HEIGHT = 7.5f; private final static double MIN_VISIBLE_HEIGHT = 5;
private final static double HORIZONTAL_MARGIN = 1; private final static double HORIZONTAL_MARGIN = 1;
private final static double VERTICAL_MARGIN = 0.7f; private final static double VERTICAL_MARGIN = 0.7f;
private final static double CONNECTOR_WIDTH = 0.25f; private final static double CONNECTOR_WIDTH = 0.25f;
@ -49,8 +51,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private StoneCollectionPanel stoneCollection; private StoneCollectionPanel stoneCollection;
private Iterable<Pair<StoneSet, Position>> stoneSets = Collections private Iterable<Pair<StoneSet, Position>> stoneSets = Collections.emptySet();
.emptySet(); private List<Pair<StoneSet, Position>> pauseStoneSets;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>(); private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
@ -59,6 +62,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private StoneSet leftHoveredConnector; private StoneSet leftHoveredConnector;
private StoneSet rightHoveredConnector; private StoneSet rightHoveredConnector;
private boolean pauseMode = false;
@Override @Override
public Event1<StoneSet> getLeftConnectorClickEvent() { public Event1<StoneSet> getLeftConnectorClickEvent() {
return leftConnectorClickEvent; return leftConnectorClickEvent;
@ -107,12 +112,35 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
repaint(); repaint();
} }
void createPauseStoneSets() {
pauseStoneSets = new ArrayList<Pair<StoneSet, Position>>();
Stone stoneCoffee1 = new Stone(-'\u2615', StoneColor.BLACK);
Stone stoneP = new Stone(-'P', StoneColor.BLACK);
Stone stonea = new Stone(-'a', StoneColor.ORANGE);
Stone stoneu = new Stone(-'u', StoneColor.BLUE);
Stone stones = new Stone(-'s', StoneColor.RED);
Stone stonee = new Stone(-'e', StoneColor.BLACK);
Stone stoneCoffee2 = new Stone(-'\u2615', StoneColor.RED);
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(stoneCoffee1),
new Position(-4, 0)));
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(Arrays.asList(
stoneP, stonea, stoneu, stones, stonee)), new Position(-2.5, 0)));
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(stoneCoffee2),
new Position(3, 0)));
}
/** /**
* Creates a new Table instance * Creates a new Table instance
*/ */
TablePanel() { TablePanel() {
setLayout(null); setLayout(null);
createPauseStoneSets();
stoneCollection = new StoneCollectionPanel(); stoneCollection = new StoneCollectionPanel();
stoneCollection.getOtherClickEvent().add(new IListener1<Point>() { stoneCollection.getOtherClickEvent().add(new IListener1<Point>() {
@ -138,7 +166,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
double minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2; double minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2;
double miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2; double miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2;
for (Pair<StoneSet, Position> entry : stoneSets) { for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
: stoneSets)) {
Position p = entry.getSecond(); Position p = entry.getSecond();
StoneSet stoneSet = entry.getFirst(); StoneSet stoneSet = entry.getFirst();
@ -156,19 +185,19 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
return new Rectangle2D.Double(minx - HORIZONTAL_MARGIN, miny return new Rectangle2D.Double(minx - HORIZONTAL_MARGIN, miny
- VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - miny + 2
- miny + 2 * VERTICAL_MARGIN); * VERTICAL_MARGIN);
} }
private void rescale() { private void rescale() {
Insets insets = getInsets(); Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.right, height = getHeight() - insets.top - insets.bottom;
- insets.bottom;
int collectionHeight = (int) (height * COLLECTION_RATIO); int collectionHeight = (int) (height * COLLECTION_RATIO);
stoneCollection.setBounds(x, y + height - collectionHeight stoneCollection
- COLLECTION_GAP, width, collectionHeight); .setBounds(x, y + height - collectionHeight - COLLECTION_GAP, width,
collectionHeight);
setScale(); setScale();
@ -205,8 +234,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// right connector // right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
CONNECTOR_WIDTH, 1); 1);
if (rect.contains(pos.getX(), pos.getY())) { if (rect.contains(pos.getX(), pos.getY())) {
rightConnectorClickEvent.emit(stoneSet); rightConnectorClickEvent.emit(stoneSet);
return true; return true;
@ -237,8 +266,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// right connector // right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
CONNECTOR_WIDTH, 1); 1);
if (rect.contains(pos.getX(), pos.getY())) { if (rect.contains(pos.getX(), pos.getY())) {
rightHoveredConnector = stoneSet; rightHoveredConnector = stoneSet;
break; break;
@ -260,10 +289,10 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
.getStoneHeight(); .getStoneHeight();
Rectangle2D extent = calculateTableExtent(); Rectangle2D extent = calculateTableExtent();
return new Pair<Integer, Integer>( return new Pair<Integer, Integer>((int) (width / 2 - extent.getCenterX()
(int) (width / 2 - extent.getCenterX() * stoneWidth), * stoneWidth),
(int) ((height * (1 - COLLECTION_RATIO)) / 2 - extent (int) ((height * (1 - COLLECTION_RATIO)) / 2 - extent.getCenterY()
.getCenterY() * stoneHeight)); * stoneHeight));
} }
private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos, private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos,
@ -278,10 +307,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
// Left connector // Left connector
leftConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x leftConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x * width)
* width) - (int) width * CONNECTOR_WIDTH + 1, Math.round(pos.getY() * height),
- (int) width * CONNECTOR_WIDTH + 1, Math.round(pos.getY() (int) (width * CONNECTOR_WIDTH), height)));
* height), (int) (width * CONNECTOR_WIDTH), height)));
for (Stone stone : stoneSet) { for (Stone stone : stoneSet) {
getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
@ -290,8 +318,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// Right connector // Right connector
rightConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x rightConnectorArea.add(new Area(new Rectangle2D.Double(Math
* width), Math.round(pos.getY() * height), .round(x * width), Math.round(pos.getY() * height),
(int) (width * CONNECTOR_WIDTH), height))); (int) (width * CONNECTOR_WIDTH), height)));
} }
@ -314,17 +342,21 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
Area connectorArea = new Area(); Area connectorArea = new Area();
Area hoveredConnectorArea = new Area(); Area hoveredConnectorArea = new Area();
for (Pair<StoneSet, Position> entry : stoneSets) { for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
paintStoneSet(g, entry.getFirst(), entry.getSecond(), : stoneSets)) {
connectorArea, hoveredConnectorArea); paintStoneSet(g, entry.getFirst(), entry.getSecond(), connectorArea,
hoveredConnectorArea);
}
if (pauseMode) {
return;
} }
g.setClip(connectorArea); g.setClip(connectorArea);
g.setTransform(oldTransform); g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += DARK_BACKGROUND.getIconWidth()) { for (int x = 0; x < getWidth(); x += DARK_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += DARK_BACKGROUND for (int y = 0; y < getHeight(); y += DARK_BACKGROUND.getIconHeight()) {
.getIconHeight()) {
DARK_BACKGROUND.paintIcon(this, g, x, y); DARK_BACKGROUND.paintIcon(this, g, x, y);
} }
} }
@ -340,12 +372,18 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
g.setTransform(oldTransform); g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += BRIGHT_BACKGROUND.getIconWidth()) { for (int x = 0; x < getWidth(); x += BRIGHT_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += BRIGHT_BACKGROUND for (int y = 0; y < getHeight(); y += BRIGHT_BACKGROUND.getIconHeight()) {
.getIconHeight()) {
BRIGHT_BACKGROUND.paintIcon(this, g, x, y); BRIGHT_BACKGROUND.paintIcon(this, g, x, y);
} }
} }
g.setClip(oldClip); g.setClip(oldClip);
} }
void enablePauseMode(boolean enable) {
stoneCollection.enablePauseMode(enable);
pauseMode = enable;
repaint();
}
} }

View file

@ -53,10 +53,13 @@ public class View extends JFrame implements IView {
private TablePanel table; private TablePanel table;
private PlayerPanel playerPanel; private PlayerPanel playerPanel;
private StartTurnPanel startTurnPanel; private StartTurnPanel startTurnPanel;
private PausePanel pausePanel;
private WinPanel winPanel; private WinPanel winPanel;
private SettingsPanel settingsPanel; private SettingsPanel settingsPanel;
private ScorePanel scorePanel; private ScorePanel scorePanel;
private BottomPanelType bottomPanelType;
private JFileChooser chooser; private JFileChooser chooser;
private Event menuNewGameEvent = new Event(); private Event menuNewGameEvent = new Event();
@ -114,12 +117,21 @@ public class View extends JFrame implements IView {
return saveEvent; return saveEvent;
} }
@Override
public IEvent getPauseEvent() {
return playerPanel.getPauseEvent();
}
@Override
public IEvent getEndPauseEvent() {
return pausePanel.getEndPauseEvent();
}
@Override @Override
public void clearView() { public void clearView() {
showScorePanel(false); showScorePanel(false);
showSettingsPanel(false); showSettingsPanel(false);
getHandPanel().setStones( getHandPanel().setStones(Collections.<Pair<Stone, Position>> emptyList());
Collections.<Pair<Stone, Position>> emptyList());
getTablePanel().setStoneSets( getTablePanel().setStoneSets(
Collections.<Pair<StoneSet, Position>> emptyList()); Collections.<Pair<StoneSet, Position>> emptyList());
setSelectedStones(Collections.<Stone> emptyList()); setSelectedStones(Collections.<Stone> emptyList());
@ -219,14 +231,18 @@ public class View extends JFrame implements IView {
mainLayer.add(table); mainLayer.add(table);
playerPanel = new PlayerPanel(); playerPanel = new PlayerPanel();
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
0, Color.BLACK)); Color.BLACK));
mainLayer.add(playerPanel); mainLayer.add(playerPanel);
startTurnPanel = new StartTurnPanel(); startTurnPanel = new StartTurnPanel();
startTurnPanel.setVisible(false); startTurnPanel.setVisible(false);
mainLayer.add(startTurnPanel); mainLayer.add(startTurnPanel);
pausePanel = new PausePanel();
pausePanel.setVisible(false);
mainLayer.add(pausePanel);
winPanel = new WinPanel(); winPanel = new WinPanel();
winPanel.setVisible(false); winPanel.setVisible(false);
mainLayer.add(winPanel); mainLayer.add(winPanel);
@ -251,6 +267,14 @@ public class View extends JFrame implements IView {
setVisible(true); setVisible(true);
} }
@Override
public void enablePauseMode(boolean enable) {
table.enablePauseMode(enable);
doSetBottomPanel(enable ? null : bottomPanelType);
pausePanel.setVisible(enable);
}
private void rescale() { private void rescale() {
int width = getContentPane().getWidth(), height = getContentPane() int width = getContentPane().getWidth(), height = getContentPane()
.getHeight(); .getHeight();
@ -270,6 +294,7 @@ public class View extends JFrame implements IView {
table.validate(); table.validate();
playerPanel.setBounds(0, tableHeight, width, playerPanelHeight); playerPanel.setBounds(0, tableHeight, width, playerPanelHeight);
startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight); startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight);
pausePanel.setBounds(0, tableHeight, width, playerPanelHeight);
winPanel.setBounds(0, tableHeight, width, playerPanelHeight); winPanel.setBounds(0, tableHeight, width, playerPanelHeight);
settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2); settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2);
scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2); scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2);
@ -295,6 +320,7 @@ public class View extends JFrame implements IView {
public void setCurrentPlayerName(String playerName) { public void setCurrentPlayerName(String playerName) {
playerPanel.setCurrentPlayerName(playerName); playerPanel.setCurrentPlayerName(playerName);
startTurnPanel.setCurrentPlayerName(playerName); startTurnPanel.setCurrentPlayerName(playerName);
pausePanel.setCurrentPlayerName(playerName);
} }
@Override @Override
@ -329,24 +355,24 @@ public class View extends JFrame implements IView {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Pair<Stone, Position>> createDecorationStones() { private List<Pair<Stone, Position>> createDecorationStones() {
Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone(-'J',
-'J', StoneColor.BLACK), new Position(2.5f, 0)); StoneColor.BLACK), new Position(2.5f, 0));
Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone(-'R',
-'R', StoneColor.ORANGE), new Position(3.5f, 0)); StoneColor.ORANGE), new Position(3.5f, 0));
Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone(-'u',
-'u', StoneColor.BLUE), new Position(4.5f, 0)); StoneColor.BLUE), new Position(4.5f, 0));
Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone(-'m',
-'m', StoneColor.RED), new Position(5.5f, 0)); StoneColor.RED), new Position(5.5f, 0));
Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone(-'m',
-'m', StoneColor.GREEN), new Position(6.5f, 0)); StoneColor.GREEN), new Position(6.5f, 0));
Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone(-'i',
-'i', StoneColor.VIOLET), new Position(7.5f, 0)); StoneColor.VIOLET), new Position(7.5f, 0));
Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone(-'k',
-'k', StoneColor.AQUA), new Position(8.5f, 0)); StoneColor.AQUA), new Position(8.5f, 0));
Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone(-'u',
-'u', StoneColor.GRAY), new Position(9.5f, 0)); StoneColor.GRAY), new Position(9.5f, 0));
Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone(-'b',
-'b', StoneColor.BLACK), new Position(10.5f, 0)); StoneColor.BLACK), new Position(10.5f, 0));
Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone(
StoneColor.RED), new Position(2, 1)); StoneColor.RED), new Position(2, 1));
@ -361,21 +387,26 @@ public class View extends JFrame implements IView {
Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone(
StoneColor.BLACK), new Position(11, 1)); StoneColor.BLACK), new Position(11, 1));
return Arrays.asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, return Arrays
stonek, stoneu2, stoneb, stone1, stone2, stone3, stone4, .asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, stonek,
stone5, stone6); stoneu2, stoneb, stone1, stone2, stone3, stone4, stone5, stone6);
} }
@Override @Override
public void setBottomPanel(BottomPanelType type) { public void setBottomPanel(BottomPanelType type) {
bottomPanelType = type;
doSetBottomPanel(type);
}
private void doSetBottomPanel(BottomPanelType type) {
startTurnPanel.setVisible(type == BottomPanelType.START_TURN_PANEL); startTurnPanel.setVisible(type == BottomPanelType.START_TURN_PANEL);
winPanel.setVisible(type == BottomPanelType.WIN_PANEL); winPanel.setVisible(type == BottomPanelType.WIN_PANEL);
playerPanel.setVisible(type != BottomPanelType.START_TURN_PANEL playerPanel.setVisible(type != BottomPanelType.START_TURN_PANEL
&& type != BottomPanelType.WIN_PANEL); && type != BottomPanelType.WIN_PANEL && type != null);
if (type == BottomPanelType.START_GAME_PANEL) { if (type == BottomPanelType.START_GAME_PANEL) {
table.setStoneSets(Collections table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
.<Pair<StoneSet, Position>> emptyList());
playerPanel.getHandPanel().setStones(createDecorationStones()); playerPanel.getHandPanel().setStones(createDecorationStones());
} }

View file

@ -101,8 +101,7 @@ class WinPanel extends JPanel {
private void rescale() { private void rescale() {
Insets insets = getInsets(); Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.right, height = getHeight() - insets.top - insets.bottom;
- insets.bottom;
if (width > PANEL_MAX_WIDTH) { if (width > PANEL_MAX_WIDTH) {
x += (width - PANEL_MAX_WIDTH) / 4; x += (width - PANEL_MAX_WIDTH) / 4;
@ -118,14 +117,13 @@ class WinPanel extends JPanel {
newRoundButton.setBounds(x, y, buttonWidth, buttonHeight); newRoundButton.setBounds(x, y, buttonWidth, buttonHeight);
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize)); newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y, newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y, buttonWidth,
buttonWidth, buttonHeight); buttonHeight);
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize)); newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR), y, endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR), y,
buttonWidth, buttonHeight); buttonWidth, buttonHeight);
endProgramButton.setFont(endProgramButton.getFont() endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
.deriveFont(fontSize));
} }
} }