summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/StartTurnPanel.java
blob: 9418a3b5071def681ed4dbe24f5830322099d757 (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
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;
import jrummikub.view.IView.BottomPanelType;

/**
 * A panel that is displayed before a player's turn
 */
@SuppressWarnings("serial")
class StartTurnPanel 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 startTurnLabel;
	private JButton startTurnButton;

	private Event startTurnEvent = new Event();
	private Event acknowledgeInvalidEvent = new Event();
	
	private Event buttonEvent = startTurnEvent;

	/**
	 * Creates a new StartTurnPanel
	 */
	StartTurnPanel() {
		setLayout(null);
		setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
				PANEL_INSET));

		startTurnLabel = new JLabel();
		startTurnLabel.setHorizontalAlignment(JLabel.CENTER);
		startTurnLabel.setHorizontalTextPosition(JLabel.CENTER);
		startTurnLabel.setVerticalAlignment(JLabel.CENTER);
		startTurnLabel.setVerticalTextPosition(JLabel.CENTER);
		startTurnLabel.putClientProperty("html.disable", Boolean.TRUE);
		add(startTurnLabel);

		startTurnButton = new JButton("Zug beginnen");
		startTurnButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				buttonEvent.emit();
			}
		});
		add(startTurnButton);

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				rescale();
			}
		});
	}

	void setCurrentPlayerName(String playerName) {
		startTurnLabel.setText(playerName + " ist jetzt an der Reihe.");
	}
	
	void setInitialMeldError(int points) {
		startTurnLabel.setText("Es wurden weniger als " + points + " Punkte ausgelegt.");
	}
	
	void setInitialMeldFirstError() {
		startTurnLabel.setText("Vor dem Rauskommen darf nicht angelegt werden.");
	}

	IEvent getStartTurnEvent() {
		return startTurnEvent;
	}
	
	IEvent getAcknowledgeInvalidEvent() {
		return acknowledgeInvalidEvent;
	}

	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;

		startTurnLabel.setBounds(x, y, width, firstLineHeight);
		startTurnButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR,
				buttonWidth, buttonHeight);
		startTurnButton.setFont(startTurnButton.getFont().deriveFont(fontSize));
	}

	public void setType(BottomPanelType type) {
		switch (type) {
		case START_TURN_PANEL:
			startTurnButton.setText("Zug beginnen");
			buttonEvent = startTurnEvent;
			break;
		case INVALID_TURN_PANEL:
			startTurnLabel.setText("Es liegen ung\u00FCltige Serien auf dem Tisch.");
			startTurnButton.setText("N\u00E4chster Spieler");
			buttonEvent = acknowledgeInvalidEvent;
			break;
		}
		
	}
}