Inceptum/src/main/java/io/gitlab/jfronny/inceptum/frontend/gui/MainWindow.java

120 lines
5.0 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.inceptum.frontend.gui;
2021-10-27 22:00:08 +02:00
import imgui.ImGui;
2021-10-28 21:31:51 +02:00
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;
2021-10-29 22:50:42 +02:00
import imgui.type.ImInt;
import io.gitlab.jfronny.inceptum.Inceptum;
2021-10-30 22:05:24 +02:00
import io.gitlab.jfronny.inceptum.InceptumGui;
import io.gitlab.jfronny.inceptum.frontend.gui.control.InstanceView;
import io.gitlab.jfronny.inceptum.model.inceptum.install.Steps;
import io.gitlab.jfronny.inceptum.util.ConfigHolder;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.util.MapAppender;
import io.gitlab.jfronny.inceptum.util.MetaHolder;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.api.account.AccountManager;
import io.gitlab.jfronny.inceptum.util.api.account.AuthInfo;
import io.gitlab.jfronny.inceptum.util.api.account.MicrosoftAccount;
2021-10-28 23:27:47 +02:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
2021-10-27 22:00:08 +02:00
public class MainWindow extends Window {
private final ImBoolean darkTheme = new ImBoolean(ConfigHolder.CONFIG.darkTheme);
2021-10-28 21:31:51 +02:00
private final ImBoolean debugTools = new ImBoolean(false);
2021-12-01 16:12:47 +01:00
private final ImInt accountIndex = new ImInt(AccountManager.getSelectedIndex());
2021-10-27 22:00:08 +02:00
public MainWindow() {
2021-10-29 22:50:42 +02:00
super("Inceptum");
2021-10-27 22:00:08 +02:00
}
2021-10-28 21:31:51 +02:00
@Override
public int getFlags() {
return ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.AlwaysAutoResize;
}
2021-10-27 22:00:08 +02:00
@Override
public void draw() {
2021-10-28 21:31:51 +02:00
ImGui.beginMenuBar();
if (ImGui.beginMenu("File")) {
2021-10-30 22:05:24 +02:00
if (ImGui.menuItem("New Instance")) InceptumGui.open(new NewInstanceWindow());
2021-10-30 16:12:39 +02:00
if (ImGui.menuItem("Re-download resources")) {
try {
Utils.clearDirectory(MetaHolder.ASSETS_DIR);
Utils.clearDirectory(MetaHolder.LIBRARIES_DIR, path -> !path.startsWith(MetaHolder.LIBRARIES_DIR.resolve("io/gitlab/jfronny")));
Utils.clearDirectory(MetaHolder.NATIVES_DIR, path -> !path.endsWith("forceload"));
Utils.clearDirectory(MetaHolder.CACHE_DIR);
2022-01-04 23:32:05 +01:00
ReDownloadWatcherWindow window = new ReDownloadWatcherWindow("Re-Downloading");
InceptumGui.open(window);
new Thread(() -> {
try {
Steps.reDownload(window.getConsumer());
window.close();
} catch (IOException e) {
Inceptum.showError("Could not execute refresh task", e);
}
}).start();
2021-10-30 16:12:39 +02:00
} catch (IOException e) {
2021-10-30 22:05:24 +02:00
Inceptum.showError("Could not execute refresh task", e);
2021-10-30 16:12:39 +02:00
}
}
2021-10-30 22:05:24 +02:00
if (ImGui.menuItem("Exit Inceptum")) InceptumGui.exit();
2021-10-29 22:50:42 +02:00
ImGui.endMenu();
}
if (ImGui.beginMenu("Account")) {
2021-10-30 22:05:24 +02:00
if (ImGui.menuItem("New")) InceptumGui.open(new MicrosoftLoginWindow());
2021-10-29 22:50:42 +02:00
AuthInfo selected = AccountManager.getSelectedAccount();
List<MicrosoftAccount> accounts = AccountManager.getAccounts();
2021-12-01 16:12:47 +01:00
int accountsSize = accounts.size();
for (int i = 0; i < accountsSize; i++) {
2021-10-29 22:50:42 +02:00
MicrosoftAccount account = accounts.get(i);
if (selected.equals(account)) accountIndex.set(i);
if (ImGui.radioButton(account.minecraftUsername, accountIndex, i)) {
AccountManager.switchAccount(account);
}
ImGui.sameLine();
if (ImGui.button("X")) AccountManager.removeAccount(account);
}
2021-12-01 16:12:47 +01:00
if (ImGui.radioButton("None", accountIndex, accountsSize)) {
AccountManager.switchAccount(null);
}
2021-10-28 21:31:51 +02:00
ImGui.endMenu();
}
if (ImGui.beginMenu("Settings")) {
if (ImGui.checkbox("Dark Theme", darkTheme)) {
ConfigHolder.CONFIG.darkTheme = darkTheme.get();
ConfigHolder.saveConfig();
2021-10-30 22:05:24 +02:00
InceptumGui.applyTheme();
2021-10-28 21:31:51 +02:00
}
ImGui.endMenu();
}
if (ImGui.beginMenu("Help")) {
2021-10-30 22:05:24 +02:00
if (ImGui.menuItem("About")) InceptumGui.open(new AboutWindow());
if (ImGui.menuItem("Log")) InceptumGui.open(new LogWindow(MapAppender.LOG));
2021-10-28 21:31:51 +02:00
ImGui.checkbox("Debug Tools", debugTools);
ImGui.endMenu();
}
ImGui.endMenuBar();
if (debugTools.get()) {
ImGui.showDemoWindow();
}
List<Path> paths;
try {
if (!Files.exists(MetaHolder.INSTANCE_DIR)) Files.createDirectories(MetaHolder.INSTANCE_DIR);
paths = Utils.ls(MetaHolder.INSTANCE_DIR, (Predicate<Path>) Files::isDirectory);
} catch (IOException e) {
Utils.LOGGER.error("Could not list instances");
return;
}
if (paths.isEmpty()) {
2021-10-28 23:27:47 +02:00
ImGui.text("You have not yet created an instance");
ImGui.text("Use File->New Instance to do so");
}
2021-10-30 16:12:39 +02:00
else InstanceView.draw(paths);
2021-10-27 22:00:08 +02:00
}
}