From 3d0cc9665a84d5b72d372a1a5c81fcb5cf30872b Mon Sep 17 00:00:00 2001 From: Sen Date: Thu, 29 May 2025 13:52:36 +0200 Subject: [PATCH] improve client server guis --- .../src/main/java/client/gui/GuiServer.java | 35 +++++++++++++++---- .../main/java/client/gui/element/Field.java | 14 +++++--- .../client/gui/element/PasswordField.java | 21 +++++++---- .../main/java/client/gui/element/Textbox.java | 6 +++- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/client/src/main/java/client/gui/GuiServer.java b/client/src/main/java/client/gui/GuiServer.java index db29cd9..06d83e2 100644 --- a/client/src/main/java/client/gui/GuiServer.java +++ b/client/src/main/java/client/gui/GuiServer.java @@ -2,12 +2,14 @@ package client.gui; import java.security.KeyPair; import java.security.PublicKey; +import java.util.Base64; import client.gui.GuiConnect.ServerInfo; import client.gui.element.ActButton; import client.gui.element.ButtonCallback; import client.gui.element.Label; import client.gui.element.NavButton; +import client.gui.element.PasswordField; import client.gui.element.PressType; import client.gui.element.Toggle; import client.gui.element.FieldAction; @@ -15,6 +17,7 @@ import client.gui.element.Field; import client.gui.element.FieldCallback; import client.vars.CVarCategory; import client.vars.Variable; +import client.window.Window; import common.color.TextColor; import common.network.IPlayer; import common.util.EncryptUtil; @@ -28,8 +31,8 @@ public class GuiServer extends Gui implements FieldCallback { private Field addrBox; private Field portBox; private Field userBox; - private Field passBox; - private Field accBox; + private PasswordField passBox; + private PasswordField accBox; private Label nameLabel; private Label addrLabel; private Label portLabel; @@ -42,6 +45,8 @@ public class GuiServer extends Gui implements FieldCallback { private Toggle encToggle; private ActButton keyButton; private ActButton resetButton; + private ActButton copyKeyButton; + private ActButton copyIdButton; private KeyPair keypair; private String keyDigest; private PublicKey serverKey; @@ -72,8 +77,8 @@ public class GuiServer extends Gui implements FieldCallback { int port = this.server == null ? this.lastPort : this.server.getPort(); this.portBox = this.add(new Field(404, 20, 76, 24, 5, this, port < 0 ? "" : "" + port)); this.userBox = this.add(new Field(0, 70, 220, 24, IPlayer.MAX_USER_LENGTH, this, IPlayer.VALID_USER, this.server == null ? this.lastUser : this.server.getUser())); - this.passBox = this.add(new Field(0, this.server == null ? 120 : 170, 480, 24, IPlayer.MAX_PASS_LENGTH, this, this.server == null ? this.lastPass : this.server.getPassword())); - this.accBox = this.add(new Field(0, this.server == null ? 170 : 220, 480, 24, IPlayer.MAX_PASS_LENGTH, this, this.server == null ? this.lastAcc : this.server.getAccess())); + this.passBox = this.add(new PasswordField(0, this.server == null ? 120 : 170, 480, 24, IPlayer.MAX_PASS_LENGTH, this, this.server == null ? this.lastPass : this.server.getPassword())); + this.accBox = this.add(new PasswordField(0, this.server == null ? 170 : 220, 480, 24, IPlayer.MAX_PASS_LENGTH, this, this.server == null ? this.lastAcc : this.server.getAccess())); this.add(new ActButton(0, this.server == null ? 220 : 350, 480, 24, new ButtonCallback() { public void use(ActButton elem, PressType action) { GuiServer.this.connect(); @@ -89,7 +94,7 @@ public class GuiServer extends Gui implements FieldCallback { this.passLabel = this.add(new Label(0, this.server == null ? 100 : 150, 480, 20, (this.keypair == null ? "Anmelde" : "Ersatz") + "-Passwort (mind. 8 Zeichen)", true)); this.accLabel = this.add(new Label(0, this.server == null ? 150 : 200, 480, 20, "Server-Passwort (mind. 8 Zeichen)", true)); if(this.server != null) { - this.keyButton = this.add(new ActButton(0, 120, 480, 24, new ButtonCallback() { + this.keyButton = this.add(new ActButton(0, 120, 391, 24, new ButtonCallback() { public void use(ActButton elem, PressType action) { if(GuiServer.this.keypair == null) { GuiServer.this.keypair = EncryptUtil.generateKeyPair(); @@ -97,6 +102,7 @@ public class GuiServer extends Gui implements FieldCallback { GuiServer.this.keyLabel.setText("Anmelde-Pubkey: RSA-2048 " + GuiServer.this.keyDigest); GuiServer.this.keyButton.setText("Schlüsselpaar entfernen"); GuiServer.this.passLabel.setText("Ersatz-Passwort (mind. 8 Zeichen)"); + GuiServer.this.copyKeyButton.enabled = true; } else { final String name = GuiServer.this.nameBox.getText(); @@ -122,6 +128,8 @@ public class GuiServer extends Gui implements FieldCallback { GuiServer.this.serverDigest = sdigest; GuiServer.this.idLabel.setText("Server-Pubkey: " + (key != null ? "RSA-2048 " + GuiServer.this.serverDigest : "nicht vorhanden")); GuiServer.this.resetButton.enabled = key != null; + GuiServer.this.copyIdButton.enabled = key != null; + GuiServer.this.copyKeyButton.enabled = !confirmed; if(confirmed) { GuiServer.this.keypair = null; GuiServer.this.keyDigest = null; @@ -141,22 +149,37 @@ public class GuiServer extends Gui implements FieldCallback { } } }, this.keypair == null ? "Neues Schlüsselpaar generieren" : "Schlüsselpaar entfernen")); + this.copyKeyButton = this.add(new ActButton(395, 120, 85, 24, new ButtonCallback() { + public void use(ActButton elem, PressType action) { + if(GuiServer.this.keypair != null) + Window.setClipboard(Base64.getEncoder().encodeToString(GuiServer.this.keypair.getPublic().getEncoded())); + } + }, "Kopieren")); + this.copyKeyButton.enabled = this.keypair != null; this.keyLabel = this.add(new Label(0, 100, 480, 20, "Anmelde-Pubkey: " + (this.keypair != null ? "RSA-2048 " + this.keyDigest : "nicht vorhanden"), true)); this.encToggle = this.add(new Toggle(0, 250, 480, 24, false, this.server.requiresEncryption(), null, "Nur Verschlüsselte Verbindung akzeptieren")); this.serverKey = this.server.getServerKey(); this.serverDigest = this.serverKey == null ? null : EncryptUtil.getXorSha512Hash(this.serverKey.getEncoded()); this.idLabel = this.add(new Label(0, 280, 480, 20, "Server-Pubkey: " + (this.serverKey != null ? "RSA-2048 " + this.serverDigest : "nicht vorhanden"), true)); - this.resetButton = this.add(new ActButton(0, 300, 480, 24, new ButtonCallback() { + this.resetButton = this.add(new ActButton(0, 300, 391, 24, new ButtonCallback() { public void use(ActButton elem, PressType action) { if(GuiServer.this.serverKey != null) { GuiServer.this.serverKey = null; GuiServer.this.serverDigest = null; GuiServer.this.idLabel.setText("Server-Pubkey: nicht vorhanden"); GuiServer.this.resetButton.enabled = false; + GuiServer.this.copyIdButton.enabled = false; } } }, "Server-Identifizierung / Pubkey zurücksetzen")); this.resetButton.enabled = this.serverKey != null; + this.copyIdButton = this.add(new ActButton(395, 300, 85, 24, new ButtonCallback() { + public void use(ActButton elem, PressType action) { + if(GuiServer.this.serverKey != null) + Window.setClipboard(Base64.getEncoder().encodeToString(GuiServer.this.serverKey.getEncoded())); + } + }, "Kopieren")); + this.copyIdButton.enabled = this.serverKey != null; } this.shift(); } diff --git a/client/src/main/java/client/gui/element/Field.java b/client/src/main/java/client/gui/element/Field.java index bd82813..78340d0 100644 --- a/client/src/main/java/client/gui/element/Field.java +++ b/client/src/main/java/client/gui/element/Field.java @@ -34,7 +34,7 @@ public class Field extends Textbox { public void updateText() { this.textWidth = Drawing.txt_size(this.pos_x + this.margin_x1, this.pos_y + this.margin_y1, this.pos_x + this.margin_x1, this.pos_y + this.margin_y1, - Integer.MAX_VALUE, Integer.MAX_VALUE, this.text).xpos; + Integer.MAX_VALUE, Integer.MAX_VALUE, this.getDrawnText()).xpos; } public void mouserel() { @@ -95,7 +95,7 @@ public class Field extends Textbox { protected void updateCursor(int offset, boolean shift, int x1, int y1, int x2, int y2) { Vec2i coord = Drawing.txt_coord(offset, x1 + this.text_x, y1 + this.text_y, - x1 + this.text_x, y1 + this.text_y, 0x7fffffff, 0x7fffffff, this.text); + x1 + this.text_x, y1 + this.text_y, 0x7fffffff, 0x7fffffff, this.getDrawnText()); this.cursorPos = coord.xpos; if(shift) { if(this.cursorPos < x1) @@ -108,7 +108,7 @@ public class Field extends Textbox { protected int onCursorOffset(int x, int y, int x1, int y1, int x2, int y2) { Offset off = Drawing.txt_offset(x, y, x1 + this.text_x, y1 + this.text_y, - x1 + this.text_x, y1 + this.text_y, 0x7fffffff, 0x7fffffff, this.text); + x1 + this.text_x, y1 + this.text_y, 0x7fffffff, 0x7fffffff, this.getDrawnText()); if(off != null) { this.cursorPos = off.xpos; } @@ -130,17 +130,21 @@ public class Field extends Textbox { protected void drawForeground(int x1, int y1, int x2, int y2) { Drawing.txt_draw(x1 + this.text_x, y1 + this.text_y, x1 + this.text_x, y1 + this.text_y, - Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? this.gm.style.text_field : Util.mulColor(this.gm.style.text_field, 0.5f), this.text); + Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? this.gm.style.text_field : Util.mulColor(this.gm.style.text_field, 0.5f), this.getDrawnText()); if(this.sel_start >= 0 && this.sel_end != this.sel_start) Drawing.txt_overlay(this.sel_start, this.sel_end, x1 + this.text_x, y1 + this.text_y, x1 + this.text_x, y1 + this.text_y, - Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? 0x808080ff : 0x80404040, this.text); + Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? 0x808080ff : 0x80404040, this.getDrawnText()); } protected char getNewline() { return ' '; } + protected String getDrawnText() { + return this.text; + } + diff --git a/client/src/main/java/client/gui/element/PasswordField.java b/client/src/main/java/client/gui/element/PasswordField.java index efd3d9a..49eefa7 100644 --- a/client/src/main/java/client/gui/element/PasswordField.java +++ b/client/src/main/java/client/gui/element/PasswordField.java @@ -1,6 +1,5 @@ package client.gui.element; -import client.renderer.Drawing; import common.util.Util; public class PasswordField extends Field { @@ -8,13 +7,21 @@ public class PasswordField extends Field { super(x, y, w, h, cap, callback, text); } - protected void drawForeground(int x1, int y1, int x2, int y2) { - Drawing.txt_draw(x1 + this.text_x, y1 + this.text_y, - x1 + this.text_x, y1 + this.text_y, - Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? this.gm.style.text_field : Util.mulColor(this.gm.style.text_field, 0.5f), this.text.isEmpty() ? "" : "****"); +// protected void drawForeground(int x1, int y1, int x2, int y2) { +// Drawing.txt_draw(x1 + this.text_x, y1 + this.text_y, +// x1 + this.text_x, y1 + this.text_y, +// Integer.MAX_VALUE, Integer.MAX_VALUE, this.enabled ? this.gm.style.text_field : Util.mulColor(this.gm.style.text_field, 0.5f), this.text.isEmpty() ? "" : "****"); +// } + +// protected int getCursorX(int x1, int x2) { +// return x1; +// } + + protected String getDrawnText() { + return Util.repeatString("*", this.text.length()); } - protected int getCursorX(int x1, int x2) { - return x1; + protected boolean canCopy() { + return false; } } diff --git a/client/src/main/java/client/gui/element/Textbox.java b/client/src/main/java/client/gui/element/Textbox.java index c4e3506..7a03abe 100644 --- a/client/src/main/java/client/gui/element/Textbox.java +++ b/client/src/main/java/client/gui/element/Textbox.java @@ -51,6 +51,10 @@ abstract class Textbox extends Element { return 4; } + protected boolean canCopy() { + return true; + } + public void setText(String str) { if(this.validator != null) str = this.validator.filter(str); @@ -91,7 +95,7 @@ abstract class Textbox extends Element { this.sel_end = this.text.length(); } else if(ctrl && (key == Keysym.C) || (this.editable && (key == Keysym.X))) { - if(this.sel_start >= 0 && this.sel_start != this.sel_end) { // fix empty + if(this.canCopy() && this.sel_start >= 0 && this.sel_start != this.sel_end) { // fix empty String str = Util.strip(this.text, this.sel_start, this.sel_end - this.sel_start, '\n', (char)0, '?'); Window.setClipboard(str); if(key == Keysym.X)