Skip to content

Commit

Permalink
Merge pull request #4 from liuzhengjin/international-language
Browse files Browse the repository at this point in the history
Chinese language support
  • Loading branch information
Ninjabrain1 committed Dec 27, 2021
2 parents 2617e45 + f0e215b commit 55d9ead
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 121 deletions.
2 changes: 2 additions & 0 deletions src/ninjabrainbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ninjabrainbot.io.KeyboardListener;
import ninjabrainbot.io.NinjabrainBotPreferences;
import ninjabrainbot.io.UpdateChecker;
import ninjabrainbot.util.I18n;
import ninjabrainbot.util.Profiler;

public class Main {
Expand Down Expand Up @@ -41,6 +42,7 @@ public class Main {
// [x] Sigma toggle

public static void main(String[] args) {
System.out.println("lang info: " + I18n.get("lang"));
Profiler.start("Initialize preferences");
preferences = new NinjabrainBotPreferences();
Profiler.stopAndStart("Calculate approximated density");
Expand Down
25 changes: 17 additions & 8 deletions src/ninjabrainbot/calculator/ChunkPrediction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import ninjabrainbot.Main;
import ninjabrainbot.io.NinjabrainBotPreferences;
import ninjabrainbot.util.I18n;

public class ChunkPrediction extends Chunk {

Expand All @@ -30,24 +31,32 @@ public ChunkPrediction() {
}

public String format() {
switch (Main.preferences.strongholdDisplayType.get()) {
final String key = Main.preferences.strongholdDisplayType.get();
switch (key) {
case NinjabrainBotPreferences.FOURFOUR:
return String.format(Locale.US, "Location: (%d, %d), %d blocks away ", fourfour_x, fourfour_z, distance);
return I18n.get("location_blocks", fourfour_x, fourfour_z, distance);
case NinjabrainBotPreferences.EIGHTEIGHT:
return String.format(Locale.US, "Location: (%d, %d), %d blocks away ", fourfour_x + 4, fourfour_z + 4, distance);
case NinjabrainBotPreferences.CHUNK:
return String.format(Locale.US, "Chunk: (%d, %d), %d blocks away ", x, z, distance);
return I18n.get("location_blocks",fourfour_x + 4, fourfour_z + 4, distance);
default:
break;
}
return String.format(Locale.US, "Chunk: (%d, %d), %d blocks away ", x, z, distance);
if (key.equals(NinjabrainBotPreferences.CHUNK)) {
return String.format(Locale.US, I18n.get("chunk_blocks"), x, z, distance);
}
return String.format(Locale.US, I18n.get("chunk_blocks"), x, z, distance);
}

public String formatLocation() {
switch (Main.preferences.strongholdDisplayType.get()) {
final String key = Main.preferences.strongholdDisplayType.get();
switch (key) {
case NinjabrainBotPreferences.FOURFOUR:
return String.format(Locale.US, "(%d, %d)", fourfour_x, fourfour_z);
case NinjabrainBotPreferences.EIGHTEIGHT:
return String.format(Locale.US, "(%d, %d)", fourfour_x + 4, fourfour_z + 4);
case NinjabrainBotPreferences.CHUNK:
default:
break;
}
if (key.equals(NinjabrainBotPreferences.CHUNK)) {
return String.format(Locale.US, "(%d, %d)", x, z);
}
return String.format(Locale.US, "(%d, %d)", x, z);
Expand Down
125 changes: 67 additions & 58 deletions src/ninjabrainbot/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,47 @@
import ninjabrainbot.gui.components.NinjabrainBotFrame;
import ninjabrainbot.gui.components.ThemedComponent;
import ninjabrainbot.io.VersionURL;
import ninjabrainbot.util.I18n;
import ninjabrainbot.util.Profiler;

/**
* Main class for the user interface.
*/
public class GUI {

private MainTextArea mainTextArea;
private MainButtonPanel mainButtonPanel;
private EnderEyePanel enderEyePanel;


private final MainTextArea mainTextArea;
private final EnderEyePanel enderEyePanel;

public NinjabrainBotFrame frame;
public OptionsFrame optionsFrame;
private NotificationsFrame notificationsFrame;
private CalibrationPanel calibrationPanel;

private Font font;
private Font fontLight;
private final NotificationsFrame notificationsFrame;
private final CalibrationPanel calibrationPanel;

public Theme theme;
public SizePreference size;
private ArrayList<ThemedComponent> themedComponents;
private final ArrayList<ThemedComponent> themedComponents;

public Timer autoResetTimer;
private static int autoResetDelay = 15 * 60 * 1000;
private static final int AUTO_RESET_DELAY = 15 * 60 * 1000;

public static final int MAX_THROWS = 10;
private Calculator calculator;
private final Calculator calculator;
private ArrayList<Throw> eyeThrows;
private ArrayList<Throw> eyeThrowsLast;

private Font font;
private Font fontLight;

public GUI() {
theme = Theme.get(Main.preferences.theme.get());
size = SizePreference.get(Main.preferences.size.get());
font = new Font(null, Font.BOLD, 25);
Locale.setDefault(Locale.US);
themedComponents = new ArrayList<ThemedComponent>();
themedComponents = new ArrayList<>();
calculator = new Calculator();
eyeThrows = new ArrayList<Throw>();
eyeThrowsLast = new ArrayList<Throw>();
eyeThrows = new ArrayList<>();
eyeThrowsLast = new ArrayList<>();

Profiler.start("Create frame");
frame = new NinjabrainBotFrame(this);
notificationsFrame = frame.getNotificationsFrame();
Expand All @@ -76,7 +78,13 @@ public GUI() {
Profiler.stopAndStart("Load fonts");
try {
font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getResourceAsStream("/resources/OpenSans-Regular.ttf"));
if (font.canDisplayUpTo(I18n.get("lang")) != -1) {
font = new Font(null);
}
fontLight = Font.createFont(Font.TRUETYPE_FONT, Main.class.getResourceAsStream("/resources/OpenSans-Light.ttf"));
if (fontLight.canDisplayUpTo(I18n.get("lang")) != -1) {
fontLight = new Font(null);
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
ge.registerFont(fontLight);
Expand All @@ -95,23 +103,23 @@ public GUI() {
Profiler.stopAndStart("Create main text area");
mainTextArea = new MainTextArea(this);
frame.add(mainTextArea);

// "Throws" text
Profiler.stopAndStart("Create main button area");
mainButtonPanel = new MainButtonPanel(this);
MainButtonPanel mainButtonPanel = new MainButtonPanel(this);
frame.add(mainButtonPanel);

// Throw panels
Profiler.stopAndStart("Create throw panels");
enderEyePanel = new EnderEyePanel(this);
frame.add(enderEyePanel);

// Settings window
Profiler.stopAndStart("Create settings window");
optionsFrame = new OptionsFrame(this);
calibrationPanel = optionsFrame.getCalibrationPanel();
Profiler.stop();

Profiler.stopAndStart("Update fonts and colors");
updateFontsAndColors();
Profiler.stopAndStart("Update bounds");
Expand All @@ -121,36 +129,37 @@ public GUI() {
frame.setVisible(true);
Profiler.stopAndStart("Set translucency");
setTranslucent(Main.preferences.translucent.get());

// Auto reset timer
autoResetTimer = new Timer(autoResetDelay, p -> {
autoResetTimer = new Timer(AUTO_RESET_DELAY, p -> {
resetThrows();
autoResetTimer.stop();
});
}

public void setTranslucent(boolean t) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT))
if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
frame.setOpacity(t ? 0.75f : 1.0f);
}
}

public void setAlwaysOnTop(boolean b) {
frame.setAlwaysOnTop(b);
optionsFrame.setAlwaysOnTop(b);
notificationsFrame.setAlwaysOnTop(b);
}

public void setNotificationsEnabled(boolean b) {
frame.getNotificationsButton().setVisible(b && frame.getNotificationsButton().hasURL());
}

public void updateTheme() {
theme = Theme.get(Main.preferences.theme.get());
updateFontsAndColors();
}

public void updateSizePreference() {
size = SizePreference.get(Main.preferences.size.get());
updateFontsAndColors();
Expand All @@ -169,11 +178,11 @@ public void setAngleErrorsEnabled(boolean b) {
enderEyePanel.setAngleErrorsEnabled(b);
updateBounds();
}

public Font fontSize(float size, boolean light) {
return light ? fontLight.deriveFont(Font.BOLD, size) : font.deriveFont(Font.BOLD, size);
}

public void registerThemedComponent(ThemedComponent c) {
themedComponents.add(c);
}
Expand All @@ -189,7 +198,7 @@ private void updateBounds() {
frame.setSize(size.WIDTH, frame.getPreferredSize().height);
frame.setShape(new RoundRectangle2D.Double(0, 0, frame.getWidth(), frame.getHeight(), size.WINDOW_ROUNDING, size.WINDOW_ROUNDING));
}

private void updateFontsAndColors() {
// Color and font
frame.getContentPane().setBackground(theme.COLOR_NEUTRAL);
Expand All @@ -201,12 +210,12 @@ private void updateFontsAndColors() {
tc.updateSize(this);
}
}
private FontRenderContext frc = new FontRenderContext(null, true, false);

private final FontRenderContext frc = new FontRenderContext(null, true, false);
public int getTextWidth(String text, Font font) {
return (int) font.getStringBounds(text, frc).getWidth();
}

public void toggleOptionsWindow() {
if (optionsFrame.isVisible()) {
optionsFrame.close();
Expand All @@ -216,7 +225,7 @@ public void toggleOptionsWindow() {
optionsFrame.setLocation(bounds.x + 40, bounds.y + 30);
}
}

public void toggleMinimized() {
frame.toggleMinimized();
}
Expand All @@ -231,22 +240,22 @@ public void resetThrows() {
}
mainTextArea.onReset();
}

public void undo() {
ArrayList<Throw> temp = eyeThrowsLast;
eyeThrowsLast = eyeThrows;
eyeThrows = temp;
onThrowsUpdated();
}

public void removeThrow(Throw t) {
if (eyeThrows.contains(t)) {
saveThrowsForUndo();
eyeThrows.remove(t);
onThrowsUpdated();
}
}

private void processClipboardUpdate(String clipboard) {
Throw t = Throw.parseF3C(clipboard);
if (!calibrationPanel.isCalibrating()) {
Expand Down Expand Up @@ -275,12 +284,13 @@ private void processClipboardUpdate(String clipboard) {
}
}
}

public void changeLastAngle(double delta) {
if (!calibrationPanel.isCalibrating()) {
int i = eyeThrows.size() - 1;
if (i == -1)
if (i == -1) {
return;
}
Throw last = eyeThrows.get(i);
Throw t = new Throw(last.x, last.z, last.alpha + delta, last.correction + delta);
saveThrowsForUndo();
Expand All @@ -292,12 +302,13 @@ public void changeLastAngle(double delta) {
calibrationPanel.changeLastAngle(delta);
}
}

public void toggleLastSTD() {
if (!calibrationPanel.isCalibrating()) {
int i = eyeThrows.size() - 1;
if (i == -1)
if (i == -1) {
return;
}
Throw last = eyeThrows.get(i);
Throw t = last.withToggledSTD();
saveThrowsForUndo();
Expand All @@ -307,15 +318,15 @@ public void toggleLastSTD() {
onThrowsUpdated();
}
}

private void setUpdateURL(VersionURL url) {
frame.setURL(url);
}

public void recalculateStronghold() {
onThrowsUpdated();
}

private void onThrowsUpdated() {
CalculatorResult result = null;
double[] errors = null;
Expand All @@ -324,7 +335,7 @@ private void onThrowsUpdated() {
if (result.success()) {
errors = result.getAngleErrors();
}
}
}
mainTextArea.setResult(result, this);
enderEyePanel.setErrors(errors);
// Update throw panels
Expand All @@ -340,28 +351,26 @@ private void onThrowsUpdated() {
public void onClipboardUpdated(String newClipboard) {
SwingUtilities.invokeLater(() -> processClipboardUpdate(newClipboard));
}

public void onNewUpdateAvailable(VersionURL url) {
SwingUtilities.invokeLater(() -> setUpdateURL(url));
}

private void saveThrowsForUndo() {
eyeThrowsLast.clear();
for (int i = 0; i < eyeThrows.size(); i++) {
eyeThrowsLast.add(eyeThrows.get(i));
}
eyeThrowsLast.addAll(eyeThrows);
}

public Calculator getTriangulator() {
return this.calculator;
}

private void checkIfOffScreen(JFrame frame) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
for (GraphicsDevice gd : lstGDs) {
if (gd.getDefaultConfiguration().getBounds().contains(frame.getBounds()))
for (GraphicsDevice gd : ge.getScreenDevices()) {
if (gd.getDefaultConfiguration().getBounds().contains(frame.getBounds())) {
return;
}
}
frame.setLocation(100, 100);
}
Expand Down

0 comments on commit 55d9ead

Please sign in to comment.