Skip to content

Commit

Permalink
Client: Add language selector to login screen
Browse files Browse the repository at this point in the history
Now users can play the game in a different language without using CLI
flags.
  • Loading branch information
StenAL committed Nov 7, 2024
1 parent bc3fd2e commit 47c0620
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
7 changes: 7 additions & 0 deletions client/src/main/java/agolf/GameApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.awt.Font;
import java.awt.Image;
import org.moparforia.client.Launcher;
import org.moparforia.shared.Locale;

public class GameApplet extends AApplet {

Expand Down Expand Up @@ -242,6 +243,12 @@ protected void trackTestLogin(String username, String password) {
this.gameContainer.connection.writeData("ttlogin\t" + username + "\t" + password);
}

protected void trackTestLogin(String username, String password, Locale locale) {
this.textManager.setLocale(locale, this);
this.gameContainer.connection.writeData("language\t" + locale);
this.trackTestLogin(username, password);
}

public boolean isEmailVerified() {
return this.syncUnknownBool.get();
}
Expand Down
65 changes: 45 additions & 20 deletions client/src/main/java/agolf/TrackTestLoginPanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package agolf;

import com.aapeli.colorgui.Choicer;
import com.aapeli.multiuser.UsernameValidator;
import java.awt.Button;
import java.awt.Color;
Expand All @@ -9,27 +10,30 @@
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.moparforia.shared.Locale;

class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener {
class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener, ItemListener {

private GameApplet gameApplet;
private int width;
private int height;
private Locale locale;
private TextField textFieldName;
private TextField textFieldPassword;
private Button buttonOk;
private Label labelError;
private Label labelName;
private Label labelName2;
private Label labelPassword;
private Label labelPassword2;
private Choicer languageChoicer;

protected TrackTestLoginPanel(GameApplet gameApplet, int width, int height) {
this.gameApplet = gameApplet;
this.width = width;
this.height = height;
this.locale = gameApplet.param.getLocale();
this.setSize(width, height);
this.create();
}
Expand All @@ -39,8 +43,8 @@ public void addNotify() {
this.repaint();
}

public void paint(Graphics var1) {
this.update(var1);
public void paint(Graphics g) {
this.update(g);
}

public void update(Graphics g) {
Expand All @@ -52,7 +56,7 @@ public void actionPerformed(ActionEvent evt) {
String username = this.textFieldName.getText().trim();
String password = this.textFieldPassword.getText().trim();
// String password = '';
this.gameApplet.trackTestLogin(username, password);
this.gameApplet.trackTestLogin(username, password, locale);
}

public void keyPressed(KeyEvent evt) {}
Expand All @@ -72,7 +76,23 @@ private void create() {
this.textFieldPassword.setBackground(Color.white);
this.textFieldPassword.setForeground(Color.black);
textFieldPassword.setEchoChar('*');
// this.add(this.textFieldPassword); //Don't show this field

this.languageChoicer = new Choicer();
this.languageChoicer.setBounds(this.width / 2 - 75, this.height / 2 - 10, 150, 25);
this.languageChoicer.addItem("English");
this.languageChoicer.addItem("Finnish");
this.languageChoicer.addItem("Swedish");
this.languageChoicer.setBackground(Color.white);
this.languageChoicer.setForeground(Color.black);
this.languageChoicer.addItemListener(this);
int selectedLanguageIndex =
switch (locale) {
case EN_US -> 0;
case FI_FI -> 1;
case SV_SE -> 2;
};
this.languageChoicer.setSelectedIndex(selectedLanguageIndex);
this.add(this.languageChoicer);

this.buttonOk = new Button("OK");
this.buttonOk.setBounds(this.width / 2 - 75, this.height / 2 + 50, 75, 25);
Expand All @@ -87,18 +107,6 @@ private void create() {
labelName = new Label("Nickname:");
labelName.setBounds(width / 2 - 200, height / 2 - 60, 75, 25);
add(labelName);
// No more labels needed
// labelName2 = new Label("");
// labelName2.setBounds(width / 2 + 80, height / 2 - 60, 75, 25);
// labelName2.setForeground(Color.red);
// add(labelName2);
// labelPassword = new Label("");
// labelPassword.setBounds(width / 2 - 150, height / 2 - 10, 75, 25);
// add(labelPassword);
// labelPassword2 = new Label("");
// labelPassword2.setBounds(width / 2 + 80, height / 2 - 10, 75, 25);
// labelPassword2.setForeground(Color.red);
// add(labelPassword2);
}

public void keyTyped(KeyEvent e) {}
Expand All @@ -117,4 +125,21 @@ public void keyReleased(KeyEvent e) {
buttonOk.setEnabled(false);
}
}

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == this.languageChoicer) {
switch (this.languageChoicer.getSelectedIndex()) {
case 0:
this.locale = Locale.EN_US;
break;
case 1:
this.locale = Locale.FI_FI;
break;
case 2:
this.locale = Locale.SV_SE;
break;
}
}
}
}
5 changes: 5 additions & 0 deletions client/src/main/java/com/aapeli/client/TextManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public void run() {
}
}

public void setLocale(Locale locale, Applet applet) {
this.locale = locale;
this.loadTexts(applet);
}

public String getGame(String key) {
return this.getGame(key, (String[]) null);
}
Expand Down

0 comments on commit 47c0620

Please sign in to comment.