summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/WinPanel.java
blob: 4d9aaccd439bb0b0dd55abca80a24fcb7164e21d (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
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 when a player has won
 */
@SuppressWarnings("serial")
class WinPanel extends JPanel {
	private final static int PANEL_INSET = 15;
	private final static int PANEL_SEPARATOR = 10;
	@SuppressWarnings("unused")
	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 waitingLabel;
	private JButton newRoundButton;
	private JButton newGameButton;
	private JButton endProgramButton;

	private Event endProgramEvent = new Event();
	private Event newGameEvent = new Event();
	private Event newRoundEvent = new Event();
	private BottomPanelType type;

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

		waitingLabel = new JLabel("Warte auf Host...");
		waitingLabel.setHorizontalAlignment(JLabel.CENTER);
		waitingLabel.setVerticalAlignment(JLabel.CENTER);
		add(waitingLabel);

		newRoundButton = new JButton("Neue Runde");
		newRoundButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				newRoundEvent.emit();
			}
		});
		add(newRoundButton);

		newGameButton = new JButton("Neues Spiel");
		newGameButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				newGameEvent.emit();
			}
		});
		add(newGameButton);

		endProgramButton = new JButton("Programm verlassen");
		endProgramButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				endProgramEvent.emit();
			}
		});
		add(endProgramButton);

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

	/**
	 * The new round event is emitted when the player wants to start a new round
	 * 
	 * @return the event
	 */
	IEvent getNewRoundEvent() {
		return newRoundEvent;
	}

	IEvent getNewGameEvent() {
		return newGameEvent;
	}

	/**
	 * The end program is emitted when the player wants to quit the program
	 * 
	 * @return the event
	 */
	IEvent getEndProgramEvent() {
		return endProgramEvent;
	}

	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 buttonWidth;
		int buttonHeight;
		int buttonY;
		if (type == BottomPanelType.WIN_PANEL) {
			buttonWidth = (width - 2 * PANEL_SEPARATOR) / 3;
			buttonHeight = height;
			buttonY = y;
		} else {
			buttonWidth = (width - PANEL_SEPARATOR) / 2;
			buttonHeight = height * 2 / 3 - PANEL_SEPARATOR;
			buttonY = y + height - buttonHeight;
		}
		int labelHeight = height - buttonHeight - PANEL_SEPARATOR;

		float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
		if (fontSize > MAX_BUTTON_FONT_SIZE)
			fontSize = MAX_BUTTON_FONT_SIZE;

		if (type == BottomPanelType.WIN_PANEL) {
			rescaleWinPanel(x, buttonWidth, buttonHeight, buttonY, fontSize);
		} else if (type == BottomPanelType.NETWORK_WIN_PANEL) {
			waitingLabel.setBounds(x, y, width, labelHeight);
			waitingLabel.setVisible(true);

			newRoundButton.setVisible(false);

			newGameButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
			newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));

			endProgramButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
					buttonWidth, buttonHeight);
			endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
		}
	}

	private void rescaleWinPanel(int x, int buttonWidth, int buttonHeight,
			int buttonY, float fontSize) {
		waitingLabel.setVisible(false);
		newRoundButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
		newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
		newRoundButton.setVisible(true);

		newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
				buttonWidth, buttonHeight);
		newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));

		endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR),
				buttonY, buttonWidth, buttonHeight);
		endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
	}

	void setType(BottomPanelType type) {
		this.type = type;
		rescale();
	}
}