From 854f4d49407e45d67dd5754afd21a7e59970ca5b Mon Sep 17 00:00:00 2001 From: Joseph Burton Date: Sun, 3 May 2020 21:06:38 +0100 Subject: Multiplayer support (#221) * First pass on multiplayer * Apply review suggestions * Dedicated Enigma server * Don't jump to references when other users do stuff * Better UI + translations * french translation * Apply review suggestions * Document the protocol * Fix most issues with scrolling. * Apply review suggestions * Fix zip hash issues + add a bit more logging * Optimize zip hash * Fix a couple of login bugs * Add message log and user list * Make Message an abstract class * Make status bar work, add chat box * Hide message log/users list when not connected * Fix status bar not resetting entirely * Run stop server task on server thread to prevent multithreading race conditions * Add c2s message to packet id list * Fix message scroll bar not scrolling to the end * Formatting * User list size -> ushort * Combine contains and remove check * Check removal before sending packet * Add password to login packet * Fix the GUI closing the rename text field when someone else renames something * Update fr_fr.json * oups * Make connection/server create dialogs not useless if it fails once * Refactor UI state updating * Fix imports * Fix Collab menu * Fix NPE when rename not allowed * Make the log file a configurable option * Don't use modified UTF * Update fr_fr.json * Bump version to 0.15.4 * Apparently I can't spell neither words nor semantic versions Co-authored-by: Yanis48 Co-authored-by: 2xsaiko --- .../enigma/gui/elements/CollapsibleTabbedPane.java | 40 +++++++++++ .../java/cuchaz/enigma/gui/elements/MenuBar.java | 79 +++++++++++++++++++--- 2 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cuchaz/enigma/gui/elements/CollapsibleTabbedPane.java (limited to 'src/main/java/cuchaz/enigma/gui/elements') diff --git a/src/main/java/cuchaz/enigma/gui/elements/CollapsibleTabbedPane.java b/src/main/java/cuchaz/enigma/gui/elements/CollapsibleTabbedPane.java new file mode 100644 index 0000000..fb497b1 --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/elements/CollapsibleTabbedPane.java @@ -0,0 +1,40 @@ +package cuchaz.enigma.gui.elements; + +import java.awt.event.MouseEvent; + +import javax.swing.JTabbedPane; + +public class CollapsibleTabbedPane extends JTabbedPane { + + public CollapsibleTabbedPane() { + } + + public CollapsibleTabbedPane(int tabPlacement) { + super(tabPlacement); + } + + public CollapsibleTabbedPane(int tabPlacement, int tabLayoutPolicy) { + super(tabPlacement, tabLayoutPolicy); + } + + @Override + protected void processMouseEvent(MouseEvent e) { + int id = e.getID(); + if (id == MouseEvent.MOUSE_PRESSED) { + if (!isEnabled()) return; + int tabIndex = getUI().tabForCoordinate(this, e.getX(), e.getY()); + if (tabIndex >= 0 && isEnabledAt(tabIndex)) { + if (tabIndex == getSelectedIndex()) { + if (isFocusOwner() && isRequestFocusEnabled()) { + requestFocus(); + } else { + setSelectedIndex(-1); + } + return; + } + } + } + super.processMouseEvent(e); + } + +} diff --git a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index 8098178..f8e4f7e 100644 --- a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java @@ -1,5 +1,18 @@ package cuchaz.enigma.gui.elements; +import cuchaz.enigma.config.Config; +import cuchaz.enigma.config.Themes; +import cuchaz.enigma.gui.Gui; +import cuchaz.enigma.gui.dialog.AboutDialog; +import cuchaz.enigma.gui.dialog.ConnectToServerDialog; +import cuchaz.enigma.gui.dialog.CreateServerDialog; +import cuchaz.enigma.gui.dialog.SearchDialog; +import cuchaz.enigma.gui.stats.StatsMember; +import cuchaz.enigma.gui.util.ScaleUtil; +import cuchaz.enigma.translation.mapping.serde.MappingFormat; +import cuchaz.enigma.utils.I18n; +import cuchaz.enigma.utils.Pair; + import java.awt.Container; import java.awt.Desktop; import java.awt.FlowLayout; @@ -13,21 +26,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; - import javax.swing.*; -import cuchaz.enigma.config.Config; -import cuchaz.enigma.config.Themes; -import cuchaz.enigma.gui.Gui; -import cuchaz.enigma.gui.dialog.AboutDialog; -import cuchaz.enigma.gui.dialog.SearchDialog; -import cuchaz.enigma.gui.stats.StatsMember; -import cuchaz.enigma.gui.util.ScaleUtil; -import cuchaz.enigma.translation.mapping.serde.MappingFormat; -import cuchaz.enigma.utils.I18n; -import cuchaz.enigma.utils.Pair; import javax.swing.*; @@ -49,6 +52,8 @@ public class MenuBar extends JMenuBar { public final JMenuItem dropMappingsMenu; public final JMenuItem exportSourceMenu; public final JMenuItem exportJarMenu; + public final JMenuItem connectToServerMenu; + public final JMenuItem startServerMenu; private final Gui gui; public MenuBar(Gui gui) { @@ -342,6 +347,58 @@ public class MenuBar extends JMenuBar { } } + /* + * Collab menu + */ + { + JMenu menu = new JMenu(I18n.translate("menu.collab")); + this.add(menu); + { + JMenuItem item = new JMenuItem(I18n.translate("menu.collab.connect")); + menu.add(item); + item.addActionListener(event -> { + if (this.gui.getController().getClient() != null) { + this.gui.getController().disconnectIfConnected(null); + return; + } + ConnectToServerDialog.Result result = ConnectToServerDialog.show(this.gui.getFrame()); + if (result == null) { + return; + } + this.gui.getController().disconnectIfConnected(null); + try { + this.gui.getController().createClient(result.getUsername(), result.getIp(), result.getPort(), result.getPassword()); + } catch (IOException e) { + JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.connect.error"), JOptionPane.ERROR_MESSAGE); + this.gui.getController().disconnectIfConnected(null); + } + Arrays.fill(result.getPassword(), (char)0); + }); + this.connectToServerMenu = item; + } + { + JMenuItem item = new JMenuItem(I18n.translate("menu.collab.server.start")); + menu.add(item); + item.addActionListener(event -> { + if (this.gui.getController().getServer() != null) { + this.gui.getController().disconnectIfConnected(null); + return; + } + CreateServerDialog.Result result = CreateServerDialog.show(this.gui.getFrame()); + if (result == null) { + return; + } + this.gui.getController().disconnectIfConnected(null); + try { + this.gui.getController().createServer(result.getPort(), result.getPassword()); + } catch (IOException e) { + JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.server.start.error"), JOptionPane.ERROR_MESSAGE); + this.gui.getController().disconnectIfConnected(null); + } + }); + this.startServerMenu = item; + } + } /* * Help menu */ -- cgit v1.2.3