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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
package jrummikub.view.impl;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Event1;
import jrummikub.util.IListener1;
import jrummikub.util.Pair;
import jrummikub.view.IStoneCollectionPanel;
import jrummikub.view.ITablePanel;
import jrummikub.view.impl.StonePainter.StoneState;
/**
* The implementation of the table
*/
@SuppressWarnings("serial")
class TablePanel extends AbstractStonePanel implements ITablePanel {
private final static ImageIcon BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/felt.png"));
private final static ImageIcon DARK_BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/dark_felt.png"));
private final static ImageIcon BRIGHT_BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/bright_felt.png"));
private final static double MIN_VISIBLE_WIDTH = 10;
private final static double MIN_VISIBLE_HEIGHT = 5;
private final static double HORIZONTAL_MARGIN = 1;
private final static double VERTICAL_MARGIN = 0.7f;
private final static double CONNECTOR_WIDTH = 0.25f;
private final double COLLECTION_RATIO = 0.12f;
private final int COLLECTION_GAP = 5;
private StoneCollectionPanel stoneCollection;
private Iterable<Pair<StoneSet, Position>> stoneSets = Collections.emptySet();
private List<Pair<StoneSet, Position>> pauseStoneSets;
private Collection<StoneSet> invalidStoneSets = Collections.emptyList();
private Collection<Stone> selectedStones = Collections.emptyList();
private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
private Event1<StoneSet> rightConnectorClickEvent = new Event1<StoneSet>();
private StoneSet leftHoveredConnector;
private StoneSet rightHoveredConnector;
private boolean pauseMode = false;
@Override
public Event1<StoneSet> getLeftConnectorClickEvent() {
return leftConnectorClickEvent;
}
@Override
public Event1<StoneSet> getRightConnectorClickEvent() {
return rightConnectorClickEvent;
}
@Override
public void setStoneSets(Iterable<Pair<StoneSet, Position>> stoneSets) {
List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>();
for (Pair<StoneSet, Position> entry : stoneSets) {
double x = entry.getSecond().getX(), y = entry.getSecond().getY();
for (Stone stone : entry.getFirst()) {
stones.add(new Pair<Stone, Position>(stone, new Position(x, y)));
x++;
}
}
setStones(stones);
this.stoneSets = stoneSets;
setScale();
repaint();
}
@Override
public IStoneCollectionPanel getStoneCollectionPanel() {
return stoneCollection;
}
/**
* Sets the currently selected stones
*
* @param stones
* the selected stones
*/
void setSelectedStones(Collection<Stone> stones) {
selectedStones = stones;
stoneCollection.setSelectedStones(stones);
repaint();
}
void setInvalidStoneSets(Collection<StoneSet> sets) {
invalidStoneSets = sets;
repaint();
}
void createPauseStoneSets() {
pauseStoneSets = new ArrayList<Pair<StoneSet, Position>>();
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);
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(Arrays.asList(
stoneP, stonea, stoneu, stones, stonee)), new Position(-2.5, 0)));
}
/**
* Creates a new Table instance
*/
TablePanel() {
setLayout(null);
createPauseStoneSets();
stoneCollection = new StoneCollectionPanel();
stoneCollection.getOtherClickEvent().add(new IListener1<Point>() {
@Override
public void handle(Point p) {
Point p2 = SwingUtilities.convertPoint(stoneCollection, p,
TablePanel.this);
// theres nothing below here
clickAt(p2, 1, false, false, true);
}
});
add(stoneCollection);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
}
private Rectangle2D calculateTableExtent() {
double minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2;
double miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2;
for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
: stoneSets)) {
Position p = entry.getSecond();
StoneSet stoneSet = entry.getFirst();
if (p.getX() < minx)
minx = p.getX();
if (p.getY() < miny)
miny = p.getY();
if (p.getX() + stoneSet.getSize() > maxx)
maxx = p.getX() + stoneSet.getSize();
if (p.getY() + 1 > maxy)
maxy = p.getY() + 1;
}
return new Rectangle2D.Double(minx - HORIZONTAL_MARGIN, miny
- VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - miny + 2
* VERTICAL_MARGIN);
}
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;
int collectionHeight = (int) (height * COLLECTION_RATIO);
stoneCollection
.setBounds(x, y + height - collectionHeight - COLLECTION_GAP, width,
collectionHeight);
setScale();
repaint();
}
private void setScale() {
Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right, height = getHeight()
- insets.top - insets.bottom;
Rectangle2D extent = calculateTableExtent();
double widthScale = width / (double) extent.getWidth()
* StonePainter.WIDTH_SCALE;
double heightScale = height * (1 - COLLECTION_RATIO)
/ (double) extent.getHeight() * StonePainter.HEIGHT_SCALE;
getStonePainter().setScale(Math.min(widthScale, heightScale));
}
@Override
protected boolean handleOtherClickEvent(Position pos) {
for (Pair<StoneSet, Position> entry : stoneSets) {
Position p = entry.getSecond();
StoneSet stoneSet = entry.getFirst();
double x = p.getX(), y = p.getY();
// left connector
Rectangle2D rect = new Rectangle2D.Double(x - CONNECTOR_WIDTH, y,
CONNECTOR_WIDTH, 1);
if (rect.contains(pos.getX(), pos.getY())) {
leftConnectorClickEvent.emit(stoneSet);
return true;
}
// right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
1);
if (rect.contains(pos.getX(), pos.getY())) {
rightConnectorClickEvent.emit(stoneSet);
return true;
}
}
return false;
}
@Override
protected void handleOtherMoveEvent(Position pos) {
StoneSet oldLeftHoveredConnector = leftHoveredConnector;
StoneSet oldRightHoveredConnector = rightHoveredConnector;
leftHoveredConnector = null;
rightHoveredConnector = null;
for (Pair<StoneSet, Position> entry : stoneSets) {
Position p = entry.getSecond();
StoneSet stoneSet = entry.getFirst();
double x = p.getX(), y = p.getY();
// left connector
Rectangle2D rect = new Rectangle2D.Double(x - CONNECTOR_WIDTH, y,
CONNECTOR_WIDTH, 1);
if (rect.contains(pos.getX(), pos.getY())) {
leftHoveredConnector = stoneSet;
break;
}
// right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
1);
if (rect.contains(pos.getX(), pos.getY())) {
rightHoveredConnector = stoneSet;
break;
}
}
if (leftHoveredConnector != oldLeftHoveredConnector
|| rightHoveredConnector != oldRightHoveredConnector) {
repaint();
}
}
@Override
protected Pair<Integer, Integer> getTranslation() {
Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right, height = getHeight()
- insets.top - insets.bottom;
int stoneWidth = getStonePainter().getStoneWidth(), stoneHeight = getStonePainter()
.getStoneHeight();
Rectangle2D extent = calculateTableExtent();
return new Pair<Integer, Integer>((int) (width / 2 - extent.getCenterX()
* stoneWidth),
(int) ((height * (1 - COLLECTION_RATIO)) / 2 - extent.getCenterY()
* stoneHeight));
}
private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos,
Area connectorArea, Area hoveredConnectorArea) {
double x = pos.getX();
int width = getStonePainter().getStoneWidth(), height = getStonePainter()
.getStoneHeight();
Area leftConnectorArea = (stoneSet == leftHoveredConnector ? hoveredConnectorArea
: connectorArea);
Area rightConnectorArea = (stoneSet == rightHoveredConnector ? hoveredConnectorArea
: connectorArea);
// Left connector
leftConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x * width)
- (int) width * CONNECTOR_WIDTH + 1, Math.round(pos.getY() * height),
(int) (width * CONNECTOR_WIDTH), height)));
for (Stone stone : stoneSet) {
StoneState state = invalidStoneSets.contains(stoneSet) ? StoneState.INVALID
: selectedStones.contains(stone) ? StoneState.SELECTED
: StoneState.NORMAL;
getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
state, stone == getHoveredStone());
x++;
}
// Right connector
rightConnectorArea.add(new Area(new Rectangle2D.Double(Math
.round(x * width), Math.round(pos.getY() * height),
(int) (width * CONNECTOR_WIDTH), height)));
}
@Override
protected void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
for (int x = 0; x < getWidth(); x += BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += BACKGROUND.getIconHeight()) {
BACKGROUND.paintIcon(this, g, x, y);
}
}
AffineTransform oldTransform = g.getTransform();
Shape oldClip = g.getClip();
Pair<Integer, Integer> translation = getTranslation();
g.translate(translation.getFirst(), translation.getSecond());
Area connectorArea = new Area();
Area hoveredConnectorArea = new Area();
for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
: stoneSets)) {
paintStoneSet(g, entry.getFirst(), entry.getSecond(), connectorArea,
hoveredConnectorArea);
}
if (pauseMode) {
g.setTransform(oldTransform);
return;
}
g.setClip(connectorArea);
g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += DARK_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += DARK_BACKGROUND.getIconHeight()) {
DARK_BACKGROUND.paintIcon(this, g, x, y);
}
}
g.setClip(oldClip);
g.translate(translation.getFirst(), translation.getSecond());
g.setClip(hoveredConnectorArea);
g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += BRIGHT_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += BRIGHT_BACKGROUND.getIconHeight()) {
BRIGHT_BACKGROUND.paintIcon(this, g, x, y);
}
}
g.setClip(oldClip);
}
void enablePauseMode(boolean enable) {
stoneCollection.setHidden(enable);
pauseMode = enable;
setScale();
repaint();
}
}
|