summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/HandPanel.java
blob: ebe4f54bc4aa6367d842c3345db69d8955c13907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Collection;
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;
import jrummikub.view.IHandPanel;
import jrummikub.view.impl.StonePainter.StoneState;

/**
 * Implementation of the board
 */
@SuppressWarnings("serial")
class HandPanel extends AbstractStonePanel implements IHandPanel {
	private final static int HEIGHT = 2;

	private final static BufferedImage BACKGROUND;
	private final static BufferedImage DARK_BACKGROUND;
	static {
		ImageIcon image = new ImageIcon(
				HandPanel.class.getResource("/jrummikub/resource/wood.png"));
		ImageIcon darkImage = new ImageIcon(
				HandPanel.class
						.getResource("/jrummikub/resource/dark_wood.png"));
		BACKGROUND = new BufferedImage(image.getIconWidth(),
				image.getIconHeight(), BufferedImage.TYPE_INT_RGB);
		DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(),
				darkImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);

		image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
		darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0);
	}

	private BufferedImage scaledBackground = BACKGROUND;
	private BufferedImage scaledDarkBackground = BACKGROUND;

	private PlayerPanel playerPanel;

	private int currentRow = 0;
	private int maxRow = 0;

	private boolean repaintAll = true;
	private Collection<Stone> selectedStones = Collections.emptyList();

	/**
	 * Creates a new Board instance
	 */
	HandPanel(PlayerPanel playerPanel) {
		this.playerPanel = playerPanel;

		setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY));
	}

	private BufferedImage getScaledBackground(int size, BufferedImage background) {
		BufferedImage scaled = new BufferedImage(size, size,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = scaled.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BICUBIC);

		g.drawImage(background, 0, 0, size, size, null);

		return scaled;
	}

	@Override
	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;
		Graphics2D g = (Graphics2D) g1.create(x, y, width, height);

		int size = height / HEIGHT;

		BufferedImage background = isEnabled() ? scaledBackground
				: scaledDarkBackground;

		if (repaintAll) {
			if (background.getHeight() != size) {
				if (!isEnabled()) {
					scaledDarkBackground = getScaledBackground(size,
							DARK_BACKGROUND);
				} else {
					scaledBackground = getScaledBackground(size, BACKGROUND);
				}
				background = isEnabled() ? scaledBackground
						: scaledDarkBackground;
			}

			for (int i = 0; i < HEIGHT; ++i) {
				for (int xpos = -size * i / 3; xpos < width; xpos += size) {
					g.drawImage(background, 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(),
							selectedStones.contains(entry.getFirst()) ? StoneState.SELECTED
									: StoneState.NORMAL,
							entry.getFirst() == getHoveredStone());
		}
	}

	@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();
	}

	/**
	 * Updates panel after visible row changed
	 */
	void rowUp() {
		currentRow--;
		repaintAll = true;
		repaint();

		playerPanel.updateButtons();
	}

	/**
	 * Updates panel after visible row changed
	 */
	void rowDown() {
		currentRow++;
		repaintAll = true;
		repaint();

		playerPanel.updateButtons();
	}

	@Override
	public void resetCurrentRow() {
		currentRow = Math.max(0, maxRow - 1);

		playerPanel.updateButtons();
	}

	/**
	 * Is there a higher row than the current row
	 * 
	 * @return higher row present
	 */
	boolean canRowUp() {
		return (currentRow > 0);
	}

	/**
	 * Is there a lower row than the current row
	 * 
	 * @return higher row present
	 */
	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
	 */
	void setSelectedStones(Collection<Stone> stones) {
		selectedStones = stones;
		repaint();
	}

	/**
	 * Rescales the hand panel according to window size
	 * 
	 * @param height
	 *            of hand panel
	 * @return width of hand panel
	 */
	int rescale(int height) {
		Insets insets = getInsets();
		int size = (height - insets.top - insets.bottom) / HEIGHT;
		getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);
		int width = Hand.WIDTH * getStonePainter().getStoneWidth()
				+ insets.left + insets.right;
		setSize(new Dimension(width, getHeight()));
		repaintAll = true;
		return width;
	}

}