-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerList.java
135 lines (106 loc) · 4.55 KB
/
ServerList.java
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
/* Copyright 2010 Ben Ruijl, Wouter Smeenk
This file is part of Walled In.
Walled In is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Walled In is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Walled In; see the file LICENSE. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
package walledin.game.gui.components;
import java.util.List;
import walledin.engine.Font;
import walledin.engine.Renderer;
import walledin.engine.gui.AbstractScreen;
import walledin.engine.gui.FontType;
import walledin.engine.gui.ScreenManager.ScreenType;
import walledin.engine.gui.ScreenMouseEvent;
import walledin.engine.gui.ScreenMouseEventListener;
import walledin.engine.gui.components.Button;
import walledin.engine.gui.components.ListView;
import walledin.engine.math.Rectangle;
import walledin.engine.math.Vector2f;
import walledin.game.ClientLogicManager;
import walledin.game.gamemode.GameMode;
import walledin.game.network.ServerData;
public class ServerList extends ListView<ServerData> implements
ScreenMouseEventListener {
private static final String[] columnNames = { "Name", "Players", "Type" };
private static final float[] columnWidth = { 150f, 70f, 150f };
private final AbstractScreen refreshButton;
private List<ServerData> serverList; // list of servers
private final ClientLogicManager clientLogicManager;
public ServerList(final AbstractScreen parent, final Rectangle boudingRect,
final ClientLogicManager clientLogicManager) {
super(parent, boudingRect, 1, columnNames.length, columnNames,
columnWidth);
this.clientLogicManager = clientLogicManager;
refreshButton = new Button(this, "Refresh", new Vector2f(400, 40));
refreshButton.addMouseEventListener(this);
addChild(refreshButton);
show(); // standard is active and visible
}
@Override
public void update(final double delta) {
serverList = clientLogicManager.getClient().getServerList();
/* Start with an empty list. */
resetData();
for (final ServerData serverData : serverList) {
final String[] stringData = { serverData.getName(),
serverData.getPlayers() + "/" + serverData.getMaxPlayers(),
serverData.getGameMode().toString() };
addData(new RowData<ServerData>(serverData, stringData));
}
sortData();
super.update(delta);
}
@Override
public void draw(final Renderer renderer) {
final Font font = getManager().getFont(FontType.BUTTON_CAPTION);
renderer.drawRectOutline(getRectangle());
super.draw(renderer);
}
@Override
public void onMouseDown(final ScreenMouseEvent e) {
super.onMouseDown(e);
}
@Override
public void onMouseHover(final ScreenMouseEvent e) {
super.onMouseHover(e);
}
@Override
protected void onListItemClicked(final ServerData item) {
/* If clicked on server, load the game */
clientLogicManager.getClient().connectToServer(item);
if (clientLogicManager.getClient().isConnected()) {
/* If it is a team game, load the team selection screen */
final GameMode gameMode = item.getGameMode();
if (gameMode == GameMode.BRIDGE_BUILDER
|| gameMode == GameMode.TEAM_DEATHMATCH) {
getManager().getScreen(ScreenType.SELECT_TEAM).initialize();
getManager().getScreen(ScreenType.SELECT_TEAM).show();
} else {
getManager().getScreen(ScreenType.GAME).initialize();
getManager().getScreen(ScreenType.GAME).show();
}
getParent().hide();
}
super.onListItemClicked(item);
}
@Override
public void onMouseClicked(final ScreenMouseEvent e) {
/* If clicked on refresh button, get server list */
if (e.getScreen() == refreshButton) {
getManager().createDialog("Refreshing server list.");
// request a refresh
clientLogicManager.getClient().refreshServerList();
}
super.onMouseClicked(e);
}
}