TODOs, license under GPL

This commit is contained in:
JFronny 2021-10-28 21:31:51 +02:00
parent 1d48cb3f38
commit c9f824d673
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
12 changed files with 7818 additions and 69 deletions

15
LICENSE Normal file
View File

@ -0,0 +1,15 @@
GLaunch - A FOSS Launcher for Minecraft written in Java
Copyright (C) 2021 JFronny
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

7682
reference/imgui_demo.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder;
import imgui.ImGui;
import imgui.ImGuiIO;
import imgui.flag.ImGuiConfigFlags;
import imgui.flag.ImGuiWindowFlags;
import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw;
import io.gitlab.jfronny.glaunch.gson.MinecraftArgumentDeserializer;
@ -62,30 +61,37 @@ public class GLaunch {
if (!Files.exists(LIBRARIES_DIR)) Files.createDirectories(LIBRARIES_DIR);
LOGGER.info("Initializing UI");
WINDOWS.add(new MainWindow());
GLaunch gLaunch = new GLaunch();
gLaunch.init();
gLaunch.run();
gLaunch.dispose();
init();
run();
dispose();
}
public static void saveConfig() {
try {
Utils.writeObject(CONFIG_PATH, CONFIG);
} catch (IOException e) {
LOGGER.error("Could not save config", e);
}
}
public static void open(Window window) {
WINDOWS.add(window);
}
private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw();
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
private static final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw();
private static final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
private String glslVersion = null;
private static String glslVersion = null;
/**
* Pointer to the native GLFW window.
*/
protected long handle;
protected static long handle;
/**
* Method to initialize application.
*/
protected void init() {
protected static void init() {
initWindow();
initImGui();
imGuiGlfw.init(handle, true);
@ -95,7 +101,7 @@ public class GLaunch {
/**
* Method to dispose all used application resources and destroy its window.
*/
protected void dispose() {
protected static void dispose() {
imGuiGl3.dispose();
imGuiGlfw.dispose();
ImGui.destroyContext();
@ -108,7 +114,7 @@ public class GLaunch {
/**
* Method to create and initialize GLFW window.
*/
protected void initWindow() {
protected static void initWindow() {
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit()) {
@ -144,7 +150,7 @@ public class GLaunch {
renderBuffer();
}
private void decideGlGlslVersions() {
private static void decideGlGlslVersions() {
final boolean isMac = System.getProperty("os.name").toLowerCase().contains("mac");
if (isMac) {
glslVersion = "#version 150";
@ -162,39 +168,40 @@ public class GLaunch {
/**
* Method to initialize Dear ImGui context. Could be overridden to do custom Dear ImGui setup before application start.
*/
protected void initImGui() {
protected static void initImGui() {
ImGui.createContext();
ImGuiIO io = ImGui.getIO();
io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable);
//io.setConfigViewportsNoDecoration(false);
io.setConfigViewportsNoAutoMerge(true);
//TODO use included icons (https://www.nerdfonts.com/cheat-sheet)
//Nerd Fonts-patched ubuntu font
try (InputStream is = GLaunch.class.getClassLoader().getResourceAsStream("font.ttf")) {
assert is != null;
io.setFontDefault(io.getFonts().addFontFromMemoryTTF(is.readAllBytes(), 16f));
} catch (IOException e) {
e.printStackTrace();
}
applyTheme();
}
/**
* Main application loop.
*/
protected void run() {
protected static void run() {
while (!GLFW.glfwWindowShouldClose(handle)) {
//frame
clearBuffer();
imGuiGlfw.newFrame();
if (CONFIG.darkTheme) ImGui.styleColorsDark();
else ImGui.styleColorsLight();
ImGui.newFrame();
//render
if (GLaunch.WINDOWS.isEmpty()) GLFW.glfwSetWindowShouldClose(handle, true); //TODO fix hs_error_pid
if (GLaunch.WINDOWS.isEmpty()) exit();
else {
for (Window window : GLaunch.WINDOWS.toArray(new Window[0])) {
if (window.isNew()) window.preFirstDraw();
ImGui.begin(window.getName(), ImGuiWindowFlags.MenuBar);
window.draw();
if (ImGui.begin(window.getName(), window.getOpenState(), window.getFlags())) window.draw();
ImGui.end();
if (!window.getOpenState().get()) window.close();
}
}
//end frame
@ -212,12 +219,20 @@ public class GLaunch {
}
}
private void clearBuffer() {
private static void clearBuffer() {
GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT);
}
private void renderBuffer() {
private static void renderBuffer() {
GLFW.glfwSwapBuffers(handle);
GLFW.glfwPollEvents();
}
public static void exit() {
GLFW.glfwSetWindowShouldClose(handle, true); //TODO fix hs_error_pid
}
public static void applyTheme() {
if (CONFIG.darkTheme) ImGui.styleColorsDark();
else ImGui.styleColorsLight();
}
}

View File

@ -0,0 +1,5 @@
package io.gitlab.jfronny.glaunch.util;
public interface ThrowingSupplier<T, TEx extends Throwable> {
T get() throws TEx;
}

View File

@ -52,30 +52,18 @@ public class Utils {
return buf;
}
//TODO generify
public static <T> T downloadObject(String url, Class<T> type) throws IOException {
Path cache = GLaunch.CACHE_DIR.resolve(Integer.toString(url.hashCode()));
try {
String download = downloadString(url);
Files.writeString(cache, download);
return GLaunch.GSON.fromJson(download, type);
} catch (IOException e) {
if (Files.exists(cache)) {
GLaunch.LOGGER.info("Using cache for " + url, e);
try (BufferedReader br = Files.newBufferedReader(cache)) {
return GLaunch.GSON.fromJson(br, type);
} catch (IOException ioE) {
throw new IOException("Could not download object and failed loading cache", ioE);
}
} else
throw new IOException("Could not download object and no cache exists", e);
}
return downloadObject(url, () -> downloadString(url), type);
}
public static <T> T downloadObject(String url, String sha1, Class<T> type) throws IOException {
return downloadObject(url, () -> downloadString(url, sha1), type);
}
private static <T> T downloadObject(String url, ThrowingSupplier<String, IOException> sourceString, Class<T> type) throws IOException {
Path cache = GLaunch.CACHE_DIR.resolve(Integer.toString(url.hashCode()));
try {
String download = downloadString(url, sha1);
String download = sourceString.get();
Files.writeString(cache, download);
return GLaunch.GSON.fromJson(download, type);
} catch (IOException e) {
@ -98,7 +86,7 @@ public class Utils {
}
public static <T> void writeObject(Path file, T object) throws IOException {
try (BufferedWriter bw = Files.newBufferedWriter(file, StandardOpenOption.CREATE)) {
try (BufferedWriter bw = Files.newBufferedWriter(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
GLaunch.GSON.toJson(object, bw);
}
}

View File

@ -0,0 +1,16 @@
package io.gitlab.jfronny.glaunch.windows;
import imgui.ImGui;
public class AboutWindow extends Window {
public AboutWindow() {
super("About");
}
@Override
public void draw() {
ImGui.text("GLaunch Copyright (C) 2021 JFronny");
ImGui.text("This program comes with ABSOLUTELY NO WARRANTY.");
ImGui.text("This is free software, and you are welcome to redistribute it under certain conditions.");
}
}

View File

@ -12,4 +12,9 @@ public class LogWindow extends Window {
public void draw() {
MapAppender.LOG.forEach(ImGui::textUnformatted);
}
@Override
public int getFlags() {
return 0;
}
}

View File

@ -1,24 +1,52 @@
package io.gitlab.jfronny.glaunch.windows;
import imgui.ImGui;
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;
import io.gitlab.jfronny.glaunch.GLaunch;
public class MainWindow extends Window {
private final ImBoolean darkTheme = new ImBoolean(GLaunch.CONFIG.darkTheme);
private final ImBoolean debugTools = new ImBoolean(false);
public MainWindow() {
super("GLaunch");
}
@Override
public int getFlags() {
return ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.AlwaysAutoResize;
}
@Override
public void draw() {
if (ImGui.button("Close")) close();
ImGui.sameLine();
if (ImGui.button("New")) GLaunch.open(new NewInstanceWindow());
ImGui.sameLine();
if (ImGui.button("Style")) GLaunch.open(new StyleWindow());
if (ImGui.button("Log")) GLaunch.open(new LogWindow()); //TODO remember windows
//ImGui.showAboutWindow();
//ImGui.showUserGuide();
//ImGui.showMetricsWindow();
//ImGui.showStyleEditor();
ImGui.beginMenuBar();
if (ImGui.beginMenu("File")) {
if (ImGui.menuItem("New Instance")) GLaunch.open(new NewInstanceWindow());
if (ImGui.menuItem("Exit GLaunch2")) GLaunch.exit();
ImGui.endMenu();
}
if (ImGui.beginMenu("Settings")) {
if (ImGui.checkbox("Dark Theme", darkTheme)) {
GLaunch.CONFIG.darkTheme = darkTheme.get();
GLaunch.saveConfig();
GLaunch.applyTheme();
}
ImGui.endMenu();
}
if (ImGui.beginMenu("Help")) {
if (ImGui.menuItem("About")) GLaunch.open(new AboutWindow());
if (ImGui.menuItem("Log")) GLaunch.open(new LogWindow());
ImGui.checkbox("Debug Tools", debugTools);
ImGui.endMenu();
}
ImGui.endMenuBar();
if (debugTools.get()) {
ImGui.showDemoWindow();
}
//TODO if no instance
ImGui.text("You have not yet created an instance");
ImGui.text("Use File->New Instance to do so");
}
}

View File

@ -36,12 +36,12 @@ public class NewInstanceWindow extends Window {
@Override
public void draw() {
if (ImGui.button("Cancel")) close(); //TODO use actual close button
switch (state) {
case Configure -> {
if (ImGui.checkbox("Show snapshots", snapshots)) {
boolean prev = GLaunch.CONFIG.snapshots;
GLaunch.CONFIG.snapshots = snapshots.get();
GLaunch.saveConfig();
//fix version index
int i = getVersions(GLaunch.CONFIG.snapshots).indexOf(getVersions(prev).get(version.get()));
if (i == -1) version.set(0);

View File

@ -1,17 +0,0 @@
package io.gitlab.jfronny.glaunch.windows;
import imgui.ImGui;
//TODO remove
public class StyleWindow extends Window {
public StyleWindow() {
super("Styles");
}
@Override
public void draw() {
if (ImGui.button("Close")) close();
ImGui.showAboutWindow();
ImGui.showDemoWindow();
}
}

View File

@ -1,10 +1,13 @@
package io.gitlab.jfronny.glaunch.windows;
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;
import io.gitlab.jfronny.glaunch.GLaunch;
public abstract class Window {
private final String name;
private boolean isNew = true;
private ImBoolean openState = new ImBoolean(true);
public Window(String name) {
this.name = name;
@ -19,6 +22,7 @@ public abstract class Window {
}
public void close() {
openState.set(false);
GLaunch.WINDOWS.remove(this);
}
@ -29,4 +33,12 @@ public abstract class Window {
}
return false;
}
public int getFlags() {
return ImGuiWindowFlags.AlwaysAutoResize;
}
public ImBoolean getOpenState() {
return openState;
}
}