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
|
package ru.olamedia.game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.network.ConnectionState;
import ru.olamedia.olacraft.network.ConnectionStateListener;
import ru.olamedia.olacraft.network.discovery.DiscoveryClient;
import ru.olamedia.olacraft.network.discovery.DiscoveryListener;
public class MainMenu extends JPanel implements ActionListener {
private JButton startButton;
private JButton startServerButton;
private JButton discoveryButton;
private DynamicJList hosts;
private DiscoveryClient discoveryClient = DiscoveryClient.getInstance();
private Thread discoveryClientThread;
private static final long serialVersionUID = -271797500986576805L;
private void stylizeButton(JButton b) {
Border line = new LineBorder(Color.BLACK);
Border margin = new EmptyBorder(5, 15, 5, 15);
Border compound = new CompoundBorder(line, margin);
b.setBackground(new Color(1f, 1f, 1f, 0.8f));
b.setBorder(compound);
}
private static boolean DEBUG = true;
@SuppressWarnings("unused")
private void debug(String s) {
if (DEBUG) {
System.out.println("[MainMenu] " + s);
}
}
public MainMenu() {
setSize(GameFrame.getWidth(), GameFrame.getHeight());
setOpaque(false);
// setBackground(new Color(0f, 0f, 0f, 0.8f));
startButton = new JButton();
startButton.setSize(500, 40);
startButton.setLocation((GameFrame.getWidth() - 500) / 2, 200);
startButton.setText("JOIN GAME");
startButton.setEnabled(false);
startButton.setActionCommand("connect");
startButton.addActionListener(this);
stylizeButton(startButton);
startServerButton = new JButton();
startServerButton.setSize(500, 40);
startServerButton.setLocation((GameFrame.getWidth() - 500) / 2, 260);
startServerButton.setText("START SERVER");
startServerButton.setActionCommand("start server");
startServerButton.addActionListener(this);
stylizeButton(startServerButton);
discoveryButton = new JButton();
discoveryButton.setSize(500, 40);
discoveryButton.setLocation((GameFrame.getWidth() - 500) / 2, 320);
discoveryButton.setText("REFRESH");
discoveryButton.setActionCommand("discovery lan");
discoveryButton.addActionListener(this);
stylizeButton(discoveryButton);
hosts = new DynamicJList();
hosts.setSize(500, 150);
hosts.setLocation((GameFrame.getWidth() - 500) / 2, 10);
hosts.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (hosts.getContents().isEmpty()) {
Game.client.setHostname("127.0.0.1");
System.out.println("Selected: none");
startButton.setEnabled(false);
} else {
String host = (String) hosts.getContents().get(e.getFirstIndex());
Game.client.setHostname(host);
System.out.println("Selected: " + host);
startButton.setEnabled(true);
}
}
});
add(hosts);
add(startButton);
add(startServerButton);
add(discoveryButton);
// LAN discover
// InetAddress address = Game.client.discoverHost(54777, 5000);
// System.out.println(address);
setLayout(new BorderLayout());
validate();
Game.client.addStateListener(new ConnectionStateListener() {
@Override
public void onChangeState(ConnectionState state) {
// debug("Client ConnectionState changed");
if (state.isConnected()) {
GameFrame.instance.setGLMode();
Game.instance.player.captureControls();
startButton.setText("LEAVE GAME");
} else {
GameFrame.instance.setUIMode();
startButton.setText("JOIN GAME");
}
}
});
discoveryClient.addHostListener(new DiscoveryListener() {
@Override
public void onHost(InetAddress address) {
if (null == address) {
discoveryButton.setEnabled(true);
} else {
hosts.getContents().addElement(address.getHostAddress());
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("connect")) {
if (null != Game.instance) {
Game.instance = null;
}
Game.instance = new Game(Game.MODE_MULTIPLAYER);
if (Game.client.isConnected()) {
// LEAVE GAME
Game.client.close();
} else {
Game.client.connect();
}
Game.instance.start();
}
if (cmd.equals("start server")) {
startServerButton.setEnabled(false);
if (Game.server.isRunning()) {
Game.server.stop();
startServerButton.setText("START SERVER");
startServerButton.setEnabled(true);
} else {
Game.server.start();
if (Game.server.isRunning()) {
startServerButton.setText("STOP SERVER");
startServerButton.setEnabled(true);
} else {
startServerButton.setEnabled(true);
}
}
}
if (cmd.equals("discovery lan")) {
discoveryButton.setEnabled(false);
discoveryClientThread = new Thread(DiscoveryClient.getInstance(), "DISCOVERY CLIENT");
discoveryClientThread.start();
hosts.getContents().clear();
}
}
}
|