Add hand row controls to view

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@241 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-16 20:49:11 +02:00
parent be4b1b4120
commit 9ad7f0822e
6 changed files with 129 additions and 62 deletions

View file

@ -10,6 +10,7 @@ import jrummikub.util.Event2;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.util.Pair;
/**
* Mock class for HandPanel
*/
@ -54,15 +55,8 @@ public class MockHandPanel implements IHandPanel {
}
@Override
public void setHandWidth(int width) {
public void resetCurrentRow() {
// TODO Auto-generated method stub
}
@Override
public void setHandHeight(int height) {
// TODO Auto-generated method stub
}
}

View file

@ -1,7 +1,6 @@
package jrummikub.control;
import jrummikub.model.GameState;
import jrummikub.model.Hand;
import jrummikub.util.IListener;
import jrummikub.view.IView;
@ -20,8 +19,6 @@ public class GameControl {
*/
public GameControl(IView view) {
this.view = view;
view.getPlayerPanel().getHandPanel().setHandHeight(Hand.HEIGHT);
view.getPlayerPanel().getHandPanel().setHandWidth(Hand.WIDTH);
view.getNewGameEvent().add(new IListener() {
@Override

View file

@ -99,6 +99,7 @@ public class TurnControl {
}));
view.getPlayerPanel().getHandPanel().setStones(hand.clone());
view.getPlayerPanel().getHandPanel().resetCurrentRow();
view.enableStartTurnPanel(false);
timer.startTimer();

View file

@ -17,18 +17,7 @@ public interface IHandPanel extends IStonePanel, IClickable {
public void setStones(Iterable<Pair<Stone, Position>> stones);
/**
* Set the number of stones that fit on the hand horizontally
*
* @param width
* number of stones
* Resets the rows currently displayed
*/
public void setHandWidth(int width);
/**
* Set the number of stones that fit on the hand vertically
*
* @param height
* number of stones
*/
public void setHandHeight(int height);
void resetCurrentRow();
}

View file

@ -15,6 +15,7 @@ import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.border.MatteBorder;
import jrummikub.model.Hand;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.util.Pair;
@ -25,42 +26,34 @@ import jrummikub.view.IHandPanel;
*/
@SuppressWarnings("serial")
class HandPanel extends AbstractStonePanel implements IHandPanel {
private int handWidth = 1;
private int handHeight = 1;
private final static int HEIGHT = 2;
private final static BufferedImage BACKGROUND;
static {
ImageIcon image = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/wood.png"));
BACKGROUND = new BufferedImage(image.getIconWidth(),
image.getIconHeight(), BufferedImage.TYPE_INT_RGB);
BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
}
private BufferedImage scaledBackground = BACKGROUND;
private PlayerPanel playerPanel;
private int currentRow = 0;
private int maxRow = 0;
private boolean repaintAll = true;
private Collection<Stone> selectedStones = Collections.emptyList();
@Override
public void setHandWidth(int width) {
handWidth = width;
rescale();
repaint();
}
@Override
public void setHandHeight(int height) {
handHeight = height;
rescale();
repaint();
}
/**
* Creates a new Board instance
*/
HandPanel() {
HandPanel(PlayerPanel playerPanel) {
this.playerPanel = playerPanel;
setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY));
addComponentListener(new ComponentAdapter() {
@ -88,26 +81,27 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
protected void paintComponent(Graphics g1) {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top
- insets.bottom;
- insets.right, height = getHeight() - insets.top - insets.bottom;
Graphics2D g = (Graphics2D) g1.create(x, y, width, height);
int size = height / handHeight;
int size = height / HEIGHT;
if (repaintAll) {
if (scaledBackground.getHeight() != size)
scaledBackground = getScaledBackground(size);
for (int i = 0; i < handHeight; ++i) {
for (int i = 0; i < HEIGHT; ++i) {
for (int xpos = -size * i / 3; xpos < width; xpos += size) {
g.drawImage(scaledBackground, xpos, size * i, null);
}
}
}
Pair<Integer, Integer> trans = getTranslation();
g.translate(trans.getFirst(), trans.getSecond());
for (Pair<Stone, Position> entry : getStones()) {
getStonePainter().paintStone(g, entry.getFirst(),
entry.getSecond(),
getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(),
selectedStones.contains(entry.getFirst()),
entry.getFirst() == getHoveredStone());
}
@ -116,15 +110,62 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
@Override
public void setStones(Iterable<Pair<Stone, Position>> stones) {
super.setStones(stones);
maxRow = 0;
for (Pair<Stone, Position> entry : stones) {
if (entry.getSecond().getY() > maxRow) {
maxRow = (int) entry.getSecond().getY();
}
}
repaintAll = true;
repaint();
playerPanel.updateButtons();
}
void rowUp() {
currentRow--;
repaintAll = true;
repaint();
playerPanel.updateButtons();
}
void rowDown() {
currentRow++;
repaintAll = true;
repaint();
playerPanel.updateButtons();
}
@Override
public void resetCurrentRow() {
currentRow = Math.max(0, maxRow - 1);
playerPanel.updateButtons();
}
boolean canRowUp() {
return (currentRow > 0);
}
boolean canRowDown() {
return (currentRow < maxRow);
}
@Override
public Pair<Integer, Integer> getTranslation() {
return new Pair<Integer, Integer>(0, -getStonePainter().getStoneHeight()
* currentRow);
}
/**
* Sets the stones that are to be painted selected
*
* @param stones
* the selected stones
* the selected stones
*/
void setSelectedStones(Collection<Stone> stones) {
selectedStones = stones;
@ -133,11 +174,11 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
private void rescale() {
Insets insets = getInsets();
int size = (getHeight() - insets.top - insets.bottom) / handHeight;
int size = (getHeight() - insets.top - insets.bottom) / HEIGHT;
getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);
setSize(new Dimension(handWidth * getStonePainter().getStoneWidth()
setSize(new Dimension(Hand.WIDTH * getStonePainter().getStoneWidth()
+ insets.left + insets.right, getHeight()));
repaintAll = true;

View file

@ -28,6 +28,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private final static int SIDE_PANEL_SEPARATOR = 10;
private final static float SIDE_PANEL_FIRST_LINE_HEIGHT = 0.375f;
private final static int SIDE_PANEL_MAX_WIDTH = 180;
private final static float HAND_ROW_BUTTON_RATIO = 0.03f;
private final static float MAX_BUTTON_FONT_SIZE = 12;
private final static DecimalFormat secondFormat = new DecimalFormat("00");
@ -39,6 +40,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private JLabel currentPlayerNameLabel;
private JButton sortByGroupsButton;
private JButton sortByRunsButton;
private JButton handRowUpButton;
private JButton handRowDownButton;
private JProgressBar timeBar;
private JButton endTurnButton;
@ -132,6 +135,26 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET,
SIDE_PANEL_INSET, SIDE_PANEL_INSET));
handRowUpButton = new JButton("<html><center>\u25B2");
handRowUpButton.setMargin(new Insets(0, 0, 0, 0));
handRowUpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hand.rowUp();
}
});
rightPanel.add(handRowUpButton);
handRowDownButton = new JButton("<html><center>\u25BC");
handRowDownButton.setMargin(new Insets(0, 0, 0, 0));
handRowDownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hand.rowDown();
}
});
rightPanel.add(handRowDownButton);
timeBar = new JProgressBar(0, 60);
timeBar.setStringPainted(true);
rightPanel.add(timeBar);
@ -141,7 +164,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
endTurnButton.setMargin(new Insets(0, 0, 0, 0));
endTurnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
public void actionPerformed(ActionEvent e) {
endTurnEvent.emit();
}
});
@ -156,16 +179,28 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
int boardWidth = hand.getWidth();
int panelWidth = (width - boardWidth) / 2;
int handButtonWidth = (int) (width * HAND_ROW_BUTTON_RATIO);
int meanPanelWidth = (width - boardWidth) / 2;
int leftPanelWidth = meanPanelWidth - handButtonWidth / 2;
int rightPanelWidth = meanPanelWidth + handButtonWidth / 2;
leftPanel.setBounds(x, y, panelWidth, height);
hand.setBounds(x + panelWidth, y, boardWidth, height);
rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height);
leftPanel.setBounds(x, y, leftPanelWidth, height);
hand.setBounds(x + leftPanelWidth, y, boardWidth, height);
rightPanel.setBounds(x + leftPanelWidth + boardWidth, y, rightPanelWidth,
height);
leftPanel.validate();
rightPanel.validate();
}
void updateButtons() {
handRowUpButton.setEnabled(hand.canRowUp());
handRowUpButton.setForeground(hand.canRowUp() ? Color.BLACK : Color.GRAY);
handRowDownButton.setEnabled(hand.canRowDown());
handRowDownButton.setForeground(hand.canRowDown() ? Color.BLACK
: Color.GRAY);
}
/**
* Creates a new PlayerPanel instance
*/
@ -175,7 +210,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
createLeftPanel();
add(leftPanel);
hand = new HandPanel();
hand = new HandPanel(this);
add(hand);
createRightPanel();
@ -227,10 +262,12 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private class RightPanelResizeListener extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
int handButtonWidth = (int) (getWidth() * HAND_ROW_BUTTON_RATIO);
Insets insets = rightPanel.getInsets();
int x = insets.left, y = insets.top, width = rightPanel.getWidth()
- insets.left - insets.right, height = rightPanel.getHeight()
- insets.top - insets.bottom;
int x = insets.left + handButtonWidth, y = insets.top, width = rightPanel
.getWidth() - insets.left - insets.right - handButtonWidth, height = rightPanel
.getHeight() - insets.top - insets.bottom;
if (width > SIDE_PANEL_MAX_WIDTH) {
x += (width - SIDE_PANEL_MAX_WIDTH) / 4;
@ -244,6 +281,14 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
if (fontSize > MAX_BUTTON_FONT_SIZE)
fontSize = MAX_BUTTON_FONT_SIZE;
handRowUpButton.setBounds(0, 0, handButtonWidth, getHeight() / 2);
handRowUpButton.setFont(handRowUpButton.getFont().deriveFont(
fontSize * 1.5f));
handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth,
getHeight() / 2);
handRowDownButton.setFont(handRowDownButton.getFont().deriveFont(
fontSize * 1.5f));
timeBar.setBounds(x, y, width, firstLineHeight);
endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR,
buttonWidth, buttonHeight);