Skip to content

Commit

Permalink
Merge pull request #3 from German-Immersive-Railroading-Community/bet…
Browse files Browse the repository at this point in the history
…terErrorDialogs

Better error dialogs
  • Loading branch information
MrTroble authored Feb 16, 2021
2 parents 935e6d9 + ace0b0f commit 072eb3f
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 156 deletions.
39 changes: 39 additions & 0 deletions src/main/java/com/troblecodings/launcher/ErrorPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.troblecodings.launcher;

import java.awt.Color;

import com.troblecodings.launcher.node.ImageView;
import com.troblecodings.launcher.node.Label;
import com.troblecodings.launcher.node.MiddlePart;
import com.troblecodings.launcher.util.FontUtil;

public class ErrorPart extends MiddlePart{

private static int counter = 0;

public ErrorPart(final MiddlePart last, final String errorHeader, final String error) {
this.add(new ImageView(0, 0, Launcher.WIDTH, Launcher.HEIGHT, "error.png"));

Label lab1 = new Label((Launcher.WIDTH - 412) / 2, 310, (Launcher.WIDTH + 412) / 2, 330, Color.RED, errorHeader);
lab1.setFont(FontUtil.getFont(30f));
this.add(lab1);

Label lab2 = new Label((Launcher.WIDTH - 412) / 2, 360, (Launcher.WIDTH + 412) / 2, 380, Color.RED, error);
this.add(lab2);

Label num = new Label((Launcher.WIDTH - 412) / 2, 460, (Launcher.WIDTH + 412) / 2, 480, Color.GRAY, String.valueOf(counter));
this.add(num);

Label lab3 = new Label((Launcher.WIDTH - 412) / 2, 510, (Launcher.WIDTH + 412) / 2, 530, Color.GRAY, "Back",
() -> Launcher.INSTANCEL.setPart(last == null ? new LoginPage():last));
lab3.setFont(FontUtil.getFont(20f));
this.add(lab3);
counter++;
}

@Override
public void onExit() {
counter--;
}

}
50 changes: 25 additions & 25 deletions src/main/java/com/troblecodings/launcher/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public HomePage() {
this.add(new ImageView(0, 0, Launcher.WIDTH, Launcher.HEIGHT, "launchback.png"));
logo.scaleChange = true;
this.add(logo);

if (launch == null)
launch = new Button(465, 577, 465 + 350, 677, "launchbutton.png", this::launch);
launch.scaleChange = true;
Expand All @@ -35,42 +35,42 @@ public HomePage() {
connect.setVisible(true);
}
this.add(connect);

this.add(new Label((Launcher.WIDTH - 412) / 2, 685, (Launcher.WIDTH + 412) / 2, 705, Color.GRAY, "Lizenzen & Kredits", () -> Launcher.INSTANCEL.setPart(new CreditPage(this))));

this.add(new Label((Launcher.WIDTH - 412) / 2, 685, (Launcher.WIDTH + 412) / 2, 705, Color.GRAY,
"Lizenzen & Kredits", () -> Launcher.INSTANCEL.setPart(new CreditPage(this))));
}

@Override
public void onExit() {}

public void onExit() {
}

private void launch() {
isLaunching = true;
launch.setActivated(false);
launch.setEnabled(false);
connect.setVisible(true);
new Thread(() -> {
try {
StartupUtil.prestart();
if (AuthUtil.START_PARAMS != null) {
Process pro = StartupUtil.start(AuthUtil.START_PARAMS);
Launcher.INSTANCE.setVisible(false);
pro.waitFor();
Launcher.INSTANCE.setVisible(true);
connect.setVisible(false);
launch.setActivated(true);
isLaunching = false;
return;
if (StartupUtil.prestart()) {
if (AuthUtil.START_PARAMS != null) {
Process pro = StartupUtil.start(AuthUtil.START_PARAMS);
Launcher.INSTANCE.setVisible(false);
pro.waitFor();
Launcher.INSTANCE.setVisible(true);
connect.setVisible(false);
launch.setActivated(true);
isLaunching = false;
return;
}
LoginPage page = new LoginPage();
LoginPage.label.setText("There was an error with your credentials!");
Launcher.INSTANCEL.setPart(page);
}
LoginPage page = new LoginPage();
LoginPage.label.setText("There was an error with your credentials!");
Launcher.INSTANCEL.setPart(page);
connect.setVisible(false);
launch.setActivated(true);
isLaunching = false;
} catch (Throwable e) {
ErrorDialog.createDialog(e);
connect.setVisible(false);
launch.setActivated(true);
isLaunching = false;
}
connect.setVisible(false);
launch.setActivated(true);
isLaunching = false;
}).start();
}

Expand Down
92 changes: 48 additions & 44 deletions src/main/java/com/troblecodings/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.troblecodings.launcher.node.ProgressBar;
import com.troblecodings.launcher.util.AuthUtil;
import com.troblecodings.launcher.util.FileUtil;
import com.troblecodings.launcher.util.FontUtil;
import com.troblecodings.launcher.util.StartupUtil;

import net.cydhra.nidhogg.exception.TooManyRequestsException;
Expand All @@ -50,27 +49,12 @@ public Launcher() {
* Launch the application.
*/
public static void main(String[] args) {
FontUtil.init();
try {
StartupUtil.update();
FileUtil.init();
AuthUtil.auth(null, null);
} catch (TooManyRequestsException e) {
try {
Thread.sleep(10000);
main(args);
return;
} catch (InterruptedException e1) {
ErrorDialog.createDialog(e1);
}
} catch (RuntimeException e2) {
LoginPage.label.setText("There was an error with your saved session!");
} catch (Throwable e1) {
ErrorDialog.createDialog(e1);
}
INSTANCEL = new Launcher();
INSTANCE = new JFrame();
INSTANCEL.setPart(new HomePage());
StartupUtil.update();
EventQueue.invokeLater(() -> {
try {
INSTANCE = new JFrame();
INSTANCE.setTitle("Launcher");
INSTANCE.setResizable(false);
INSTANCE.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand All @@ -81,15 +65,32 @@ public static void main(String[] args) {
INSTANCE.setIconImage(Assets.getImage("icon.png"));
INSTANCE.setBackground(new Color(0, 0, 0, 0));

INSTANCEL = new Launcher();
INSTANCEL.setSize(WIDTH, HEIGHT);
INSTANCE.add(INSTANCEL);
INSTANCEL.init();
INSTANCE.setVisible(true);
INSTANCE.repaint();
} catch (Exception e) {
ErrorDialog.createDialog(e);
}
});

try {
FileUtil.init();
AuthUtil.auth(null, null);
} catch (TooManyRequestsException e) {
try {
Thread.sleep(10000);
main(args);
return;
} catch (InterruptedException e1) {
ErrorDialog.createDialog(e1);
}
} catch (RuntimeException e2) {
LoginPage.label.setText("There - an error with your saved session!");
} catch (Throwable e1) {
ErrorDialog.createDialog(e1);
}
}

private static ArrayList<Node> nodes = new ArrayList<Node>();
Expand All @@ -100,26 +101,25 @@ public static void main(String[] args) {
protected Button settings, home, launch;

public static final ProgressBar bar = new ProgressBar(0, 630, WIDTH, 10, Color.GREEN.darker().darker());

public void init() {
part = new HomePage();

public void init() {
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);

nodes.add(new ImageView(0, 0, WIDTH, HEIGHT, "background.png"));
nodes.add(bar);

Button close = new Button(WIDTH - 79, 20, WIDTH - 27, 87, "closebutton.png", this::exit);
close.scaleChange = true;
close.scaleFactor = 1.1f;
nodes.add(close);

home = new Button((WIDTH - 120) / 2, 20, (WIDTH - 120) / 2 + 120, 42, "homebuttonoff.png", this::home);
settings = new Button((WIDTH - 167) / 2, 20, (WIDTH - 167) / 2 + 167, 42, "settingbuttonoff.png", this::settings);
settings = new Button((WIDTH - 167) / 2, 20, (WIDTH - 167) / 2 + 167, 42, "settingbuttonoff.png",
this::settings);
home.setEnabled(false);

nodes.add(home);
nodes.add(settings);

Expand All @@ -135,13 +135,13 @@ public void init() {
}
setIgnoreRepaint(false);
}

public static BufferedImage buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
public static Graphics2D graph = buffer.createGraphics();

@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Expand All @@ -151,40 +151,44 @@ public void paint(Graphics g) {
part.paint(graph);
g.drawImage(buffer, 0, 0, this);
}

private void settings() {
settings.setEnabled(false);
this.setPart(new SettingsPage());
home.setEnabled(true);
}

private void home() {
home.setEnabled(false);
this.setPart(new HomePage());
settings.setEnabled(true);
}

public MiddlePart getPart() {
return this.part;
}

public void setPart(MiddlePart part) {
this.part.onExit();
if(this.part != null)
this.part.onExit();
System.gc();
this.part = part;
INSTANCE.repaint();
}

private void exit() {
try {
if (Files.notExists(FileUtil.SETTINGSPATH))
Files.createFile(FileUtil.SETTINGSPATH);
Files.write(FileUtil.SETTINGSPATH, (StartupUtil.LWIDTH + System.lineSeparator() +
StartupUtil.LHEIGHT + System.lineSeparator() +
StartupUtil.RAM + System.lineSeparator() +
SettingsPage.NEWBASEDIR).getBytes());
Files.write(FileUtil.SETTINGSPATH,
(StartupUtil.LWIDTH + System.lineSeparator() + StartupUtil.LHEIGHT + System.lineSeparator()
+ StartupUtil.RAM + System.lineSeparator() + SettingsPage.NEWBASEDIR).getBytes());
} catch (IOException e) {
ErrorDialog.createDialog(e);
}
System.exit(0);
}

/*
* I want to take a minute to express my hate against the swing / AWT API Just
* horrible
Expand All @@ -201,18 +205,18 @@ public void mouseEntered(MouseEvent e) {
@Override
public void mouseExited(MouseEvent e) {
}

private static boolean enablePress = true;

@Override
public void mousePressed(MouseEvent e) {
if(!enablePress)
if (!enablePress)
return;
enablePress = false;
dx = e.getX();
dy = e.getY();
for(Node n : nodes) {
if(n.update(e.getX(), e.getY(), e.getButton()))
for (Node n : nodes) {
if (n.update(e.getX(), e.getY(), e.getButton()))
break;
}
part.mousePressed(e);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion src/main/java/com/troblecodings/launcher/node/ImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
import com.troblecodings.launcher.Launcher;
import com.troblecodings.launcher.assets.Assets;

public class ImageView extends Node{
public class ImageView extends Node {

protected final BufferedImage image;
protected final Runnable onButton;

public ImageView(int x1, int y1, int x2, int y2, String name) {
this(x1, y1, x2, y2, name, null);
}

public ImageView(int x1, int y1, int x2, int y2, String name, Runnable btn) {
super(x1, y1, x2, y2);
this.image = Assets.getImage(name);
this.onButton = btn;
}

@Override
public boolean update(int mousex, int mousey, int mousebtn) {
boolean b = super.update(mousex, mousey, mousebtn);
if(clicked && onButton != null)
onButton.run();
return b;
}

@Override
Expand Down
Loading

0 comments on commit 072eb3f

Please sign in to comment.