mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Simplify UI for connecting to server and including loading screen
This commit is contained in:
@@ -35,6 +35,9 @@ public final class SOverlayUtils {
|
|||||||
* A standardized overlay for a game start condition.
|
* A standardized overlay for a game start condition.
|
||||||
*/
|
*/
|
||||||
public static void startGameOverlay() {
|
public static void startGameOverlay() {
|
||||||
|
startGameOverlay("Loading new game...");
|
||||||
|
}
|
||||||
|
public static void startGameOverlay(String message) {
|
||||||
final JPanel overlay = SOverlayUtils.genericOverlay();
|
final JPanel overlay = SOverlayUtils.genericOverlay();
|
||||||
final int w = overlay.getWidth();
|
final int w = overlay.getWidth();
|
||||||
final int h = overlay.getHeight();
|
final int h = overlay.getHeight();
|
||||||
@@ -53,7 +56,7 @@ public final class SOverlayUtils {
|
|||||||
pnl.add(new FLabel.Builder().icon(FSkin.getIcon(FSkinProp.ICO_LOGO))
|
pnl.add(new FLabel.Builder().icon(FSkin.getIcon(FSkinProp.ICO_LOGO))
|
||||||
.iconScaleFactor(1d).iconInBackground().build(),
|
.iconScaleFactor(1d).iconInBackground().build(),
|
||||||
"w " + logoSize + "px!, h " + logoSize + "px!, align center");
|
"w " + logoSize + "px!, h " + logoSize + "px!, align center");
|
||||||
pnl.add(new FLabel.Builder().text("Loading new game...")
|
pnl.add(new FLabel.Builder().text(message)
|
||||||
.fontSize(22).build(), "h " + labelHeight + "px!, align center");
|
.fontSize(22).build(), "h " + labelHeight + "px!, align center");
|
||||||
|
|
||||||
overlay.add(pnl);
|
overlay.add(pnl);
|
||||||
|
|||||||
@@ -4,12 +4,15 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.JMenu;
|
import javax.swing.JMenu;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import forge.FThreads;
|
||||||
import forge.GuiBase;
|
import forge.GuiBase;
|
||||||
import forge.assets.FSkinProp;
|
import forge.assets.FSkinProp;
|
||||||
import forge.gui.FNetOverlay;
|
import forge.gui.FNetOverlay;
|
||||||
|
import forge.gui.SOverlayUtils;
|
||||||
import forge.gui.framework.ICDoc;
|
import forge.gui.framework.ICDoc;
|
||||||
import forge.interfaces.IGuiGame;
|
import forge.interfaces.IGuiGame;
|
||||||
import forge.interfaces.ILobbyListener;
|
import forge.interfaces.ILobbyListener;
|
||||||
@@ -30,6 +33,7 @@ import forge.net.server.FServerManager;
|
|||||||
import forge.net.server.ServerGameLobby;
|
import forge.net.server.ServerGameLobby;
|
||||||
import forge.player.GamePlayerUtil;
|
import forge.player.GamePlayerUtil;
|
||||||
import forge.properties.ForgePreferences.FPref;
|
import forge.properties.ForgePreferences.FPref;
|
||||||
|
import forge.properties.ForgeProfileProperties;
|
||||||
import forge.screens.home.CLobby;
|
import forge.screens.home.CLobby;
|
||||||
import forge.screens.home.VLobby;
|
import forge.screens.home.VLobby;
|
||||||
import forge.screens.home.sanctioned.ConstructedGameMenu;
|
import forge.screens.home.sanctioned.ConstructedGameMenu;
|
||||||
@@ -45,17 +49,45 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
|||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
final void host(final int portNumber) {
|
void connectToServer() {
|
||||||
promptNameIfNeeded();
|
final String url = SOptionPane.showInputDialog("Enter URL of server to join. Leave blank to host your own server.", "Connect to Server");
|
||||||
|
if (url == null) { return; }
|
||||||
|
|
||||||
|
//prompt user for player one name if needed
|
||||||
|
if (StringUtils.isBlank(FModel.getPreferences().getPref(FPref.PLAYER_NAME))) {
|
||||||
|
GamePlayerUtil.setPlayerName();
|
||||||
|
}
|
||||||
|
|
||||||
|
FThreads.invokeInBackgroundThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (url.length() > 0) {
|
||||||
|
join(url);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
host();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void host() {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
SOverlayUtils.startGameOverlay("Starting server...");
|
||||||
|
SOverlayUtils.showOverlay();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
final int port = ForgeProfileProperties.getServerPort();
|
||||||
final FServerManager server = FServerManager.getInstance();
|
final FServerManager server = FServerManager.getInstance();
|
||||||
final ServerGameLobby lobby = new ServerGameLobby();
|
final ServerGameLobby lobby = new ServerGameLobby();
|
||||||
final VLobby view = VSubmenuOnlineLobby.SINGLETON_INSTANCE.setLobby(lobby);
|
final VLobby view = VSubmenuOnlineLobby.SINGLETON_INSTANCE.setLobby(lobby);
|
||||||
|
|
||||||
server.startServer(portNumber);
|
server.startServer(port);
|
||||||
server.setLobby(lobby);
|
server.setLobby(lobby);
|
||||||
|
|
||||||
FNetOverlay.SINGLETON_INSTANCE.showUp("Hosting game");
|
|
||||||
lobby.setListener(new IUpdateable() {
|
lobby.setListener(new IUpdateable() {
|
||||||
@Override
|
@Override
|
||||||
public final void update(final boolean fullUpdate) {
|
public final void update(final boolean fullUpdate) {
|
||||||
@@ -103,11 +135,24 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
|||||||
|
|
||||||
view.update(true);
|
view.update(true);
|
||||||
|
|
||||||
FNetOverlay.SINGLETON_INSTANCE.showUp(String.format("Hosting on port %d", portNumber));
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
SOverlayUtils.hideOverlay();
|
||||||
|
FNetOverlay.SINGLETON_INSTANCE.showUp(String.format("Hosting on port %d", port));
|
||||||
|
VSubmenuOnlineLobby.SINGLETON_INSTANCE.populate();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final void join(final String hostname, final int port) {
|
private void join(final String url) {
|
||||||
promptNameIfNeeded();
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
SOverlayUtils.startGameOverlay("Connecting to server...");
|
||||||
|
SOverlayUtils.showOverlay();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
final IGuiGame gui = GuiBase.getInterface().getNewGuiGame();
|
final IGuiGame gui = GuiBase.getInterface().getNewGuiGame();
|
||||||
final FGameClient client = new FGameClient(FModel.getPreferences().getPref(FPref.PLAYER_NAME), "0", gui);
|
final FGameClient client = new FGameClient(FModel.getPreferences().getPref(FPref.PLAYER_NAME), "0", gui);
|
||||||
@@ -138,16 +183,35 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
|||||||
client.send(event);
|
client.send(event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
final String hostname;
|
||||||
|
int port0 = ForgeProfileProperties.getServerPort();
|
||||||
|
|
||||||
|
//see if port specified in URL
|
||||||
|
int index = url.indexOf(':');
|
||||||
|
if (index >= 0) {
|
||||||
|
hostname = url.substring(0, index);
|
||||||
|
String portStr = url.substring(index + 1);
|
||||||
|
try {
|
||||||
|
port0 = Integer.parseInt(portStr);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hostname = url;
|
||||||
|
}
|
||||||
|
final int port = port0;
|
||||||
|
|
||||||
client.connect(hostname, port);
|
client.connect(hostname, port);
|
||||||
|
|
||||||
FNetOverlay.SINGLETON_INSTANCE.showUp(String.format("Connected to %s:%d", hostname, port));
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
}
|
@Override
|
||||||
|
public void run() {
|
||||||
private static void promptNameIfNeeded() {
|
SOverlayUtils.hideOverlay();
|
||||||
//prompt user for player one name if needed
|
FNetOverlay.SINGLETON_INSTANCE.showUp(String.format("Connected to %s:%d", hostname, port));
|
||||||
if (StringUtils.isBlank(FModel.getPreferences().getPref(FPref.PLAYER_NAME))) {
|
VSubmenuOnlineLobby.SINGLETON_INSTANCE.populate();
|
||||||
GamePlayerUtil.setPlayerName();
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -155,7 +219,7 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see forge.gui.home.ICSubmenu#initialize()
|
* @see forge.gui.home.ICSubmenu#update()
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
|
|||||||
@@ -18,15 +18,12 @@ import forge.gui.framework.IVTopLevelUI;
|
|||||||
import forge.match.GameLobby;
|
import forge.match.GameLobby;
|
||||||
import forge.net.client.FGameClient;
|
import forge.net.client.FGameClient;
|
||||||
import forge.net.server.FServerManager;
|
import forge.net.server.FServerManager;
|
||||||
import forge.properties.ForgeConstants;
|
|
||||||
import forge.screens.home.EMenuGroup;
|
import forge.screens.home.EMenuGroup;
|
||||||
import forge.screens.home.IVSubmenu;
|
import forge.screens.home.IVSubmenu;
|
||||||
import forge.screens.home.VHomeUI;
|
import forge.screens.home.VHomeUI;
|
||||||
import forge.screens.home.VLobby;
|
import forge.screens.home.VLobby;
|
||||||
import forge.toolbox.FButton;
|
import forge.toolbox.FButton;
|
||||||
import forge.toolbox.FLabel;
|
import forge.toolbox.FSkin;
|
||||||
import forge.toolbox.FPanel;
|
|
||||||
import forge.toolbox.FTextField;
|
|
||||||
import forge.util.gui.SOptionPane;
|
import forge.util.gui.SOptionPane;
|
||||||
|
|
||||||
public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IVTopLevelUI {
|
public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IVTopLevelUI {
|
||||||
@@ -55,85 +52,46 @@ public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IVTop
|
|||||||
final JPanel container = VHomeUI.SINGLETON_INSTANCE.getPnlDisplay();
|
final JPanel container = VHomeUI.SINGLETON_INSTANCE.getPnlDisplay();
|
||||||
|
|
||||||
container.removeAll();
|
container.removeAll();
|
||||||
container.setLayout(new MigLayout("insets 0, gap 0, wrap 1, ax right"));
|
|
||||||
|
|
||||||
if (lobby == null) {
|
if (lobby == null) {
|
||||||
final FPanel pnlHost = new FPanel(new MigLayout("insets 5px 10% 5px 10%, wrap 2", "[grow,l]10[grow,r]", "[grow,c][grow,c]"));
|
final FButton btnConnect = new FButton("Connect to Server");
|
||||||
container.add(pnlHost, "west, w 50%!, h 100%!");
|
btnConnect.setFont(FSkin.getFont(20));
|
||||||
|
btnConnect.addActionListener(new ActionListener() {
|
||||||
final FLabel lblServerPort = new FLabel.Builder().text("Server port").build();
|
|
||||||
pnlHost.add(lblServerPort, "w 100!, h 50!");
|
|
||||||
|
|
||||||
final FTextField txtServerPort = new FTextField.Builder().text(String.valueOf(ForgeConstants.SERVER_PORT_NUMBER)).build();
|
|
||||||
txtServerPort.setEditable(false);
|
|
||||||
pnlHost.add(txtServerPort, "wrap");
|
|
||||||
|
|
||||||
final FButton btnHost = new FButton("Host");
|
|
||||||
btnHost.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public final void actionPerformed(final ActionEvent e) {
|
public final void actionPerformed(final ActionEvent e) {
|
||||||
getLayoutControl().host(Integer.parseInt(txtServerPort.getText()));
|
getLayoutControl().connectToServer();
|
||||||
populate();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
pnlHost.add(btnHost, "span 2, wrap, w 200!, h 50!");
|
container.setLayout(new MigLayout("insets 0, gap 0, ax center, ay center"));
|
||||||
|
container.add(btnConnect, "w 300!, h 75!");
|
||||||
final FPanel pnlJoin = new FPanel(new MigLayout("insets 5px 10% 5px 10%, wrap 2", "[grow,l]10[grow,r]", "[grow,c][grow,c][grow,c]"));
|
return;
|
||||||
container.add(pnlJoin, "east, w 50%!, h 100%!");
|
|
||||||
|
|
||||||
final FLabel lblJoinHost = new FLabel.Builder().text("Hostname").build();
|
|
||||||
pnlJoin.add(lblJoinHost, "w 100!, h 50!");
|
|
||||||
|
|
||||||
final FTextField txtJoinHost = new FTextField.Builder().text("localhost").build();
|
|
||||||
pnlJoin.add(txtJoinHost, "wrap, w 250!");
|
|
||||||
|
|
||||||
final FLabel lblJoinPort = new FLabel.Builder().text("Host port").build();
|
|
||||||
pnlJoin.add(lblJoinPort, "w 100!, h 50!");
|
|
||||||
|
|
||||||
final FTextField txtJoinPort = new FTextField.Builder().text(String.valueOf(ForgeConstants.SERVER_PORT_NUMBER)).build();
|
|
||||||
txtJoinPort.setEditable(false);
|
|
||||||
pnlJoin.add(txtJoinPort, "wrap");
|
|
||||||
|
|
||||||
final FButton btnJoin = new FButton("Join");
|
|
||||||
btnJoin.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public final void actionPerformed(final ActionEvent e) {
|
|
||||||
getLayoutControl().join(txtJoinHost.getText(), Integer.parseInt(txtJoinPort.getText()));
|
|
||||||
populate();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
pnlJoin.add(btnJoin, "span 2, w 200!, h 50!");
|
|
||||||
|
|
||||||
if (container.isShowing()) {
|
|
||||||
container.validate();
|
|
||||||
container.repaint();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
lobby.getLblTitle().setText("Online Multiplayer: Lobby");
|
container.setLayout(new MigLayout("insets 0, gap 0, wrap 1, ax right"));
|
||||||
container.add(lobby.getLblTitle(), "w 80%, h 40px!, gap 0 0 15px 15px, span 2, al right, pushx");
|
|
||||||
|
lobby.getLblTitle().setText("Online Multiplayer: Lobby");
|
||||||
for (final FDeckChooser fdc : lobby.getDeckChoosers()) {
|
container.add(lobby.getLblTitle(), "w 80%, h 40px!, gap 0 0 15px 15px, span 2, al right, pushx");
|
||||||
fdc.populate();
|
|
||||||
fdc.getDecksComboBox().addListener(new IDecksComboBoxListener() {
|
for (final FDeckChooser fdc : lobby.getDeckChoosers()) {
|
||||||
@Override
|
fdc.populate();
|
||||||
public final void deckTypeSelected(final DecksComboBoxEvent ev) {
|
fdc.getDecksComboBox().addListener(new IDecksComboBoxListener() {
|
||||||
lobby.focusOnAvatar();
|
@Override
|
||||||
}
|
public final void deckTypeSelected(final DecksComboBoxEvent ev) {
|
||||||
});
|
lobby.focusOnAvatar();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
container.add(lobby.getConstructedFrame(), "gap 20px 20px 20px 0px, push, grow");
|
}
|
||||||
container.add(lobby.getPanelStart(), "gap 0 0 3.5%! 3.5%!, ax center");
|
|
||||||
|
container.add(lobby.getConstructedFrame(), "gap 20px 20px 20px 0px, push, grow");
|
||||||
if (container.isShowing()) {
|
container.add(lobby.getPanelStart(), "gap 0 0 3.5%! 3.5%!, ax center");
|
||||||
container.validate();
|
|
||||||
container.repaint();
|
if (container.isShowing()) {
|
||||||
}
|
container.validate();
|
||||||
|
container.repaint();
|
||||||
if (!lobby.getPlayerPanels().isEmpty()) {
|
}
|
||||||
lobby.changePlayerFocus(0);
|
|
||||||
}
|
if (!lobby.getPlayerPanels().isEmpty()) {
|
||||||
|
lobby.changePlayerFocus(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user