Fixed up more things to do with JavaFX, and added some extra stuff for the MinecraftScreen

This commit is contained in:
coolGi
2023-03-17 22:29:28 +10:30
parent 64bed83ddb
commit aac99f1682
6 changed files with 21 additions and 141 deletions
@@ -16,6 +16,7 @@ public abstract class AbstractScreen {
public int mouseY = 0;
/** Weather it should close when you press the escape key */
public boolean shouldCloseOnEsc = true;
/** If set to true, the screen would close on the next tick (warning, it closes after tick gets called for a last time) */
public boolean close = false;
@@ -30,5 +31,5 @@ public abstract class AbstractScreen {
// ---------- Random stuff that might be needed later on ---------- //
/** File dropped into the screen */
public void onFilesDrop(List<Path> list) {}
public void onFilesDrop(List<Path> files) {}
}
@@ -0,0 +1,13 @@
package com.seibel.lod.core.config.gui;
public class JavaFXConfigScreen extends AbstractScreen {
@Override
public void init() {
}
@Override
public void render(float delta) {
}
}
@@ -14,7 +14,7 @@ import java.nio.ByteOrder;
/**
* @author coolGi
*/
public class ConfigScreen extends AbstractScreen {
public class OpenGLConfigScreen extends AbstractScreen {
ShaderProgram basicShader;
GLVertexBuffer sameContextBuffer;
GLVertexBuffer sharedContextBuffer;
@@ -1,8 +1,7 @@
package com.seibel.lod.core.config.gui.standaloneTests;
import com.seibel.lod.core.api.internal.ClientApi;
import com.seibel.lod.core.config.gui.AbstractScreen;
import com.seibel.lod.core.config.gui.ConfigScreen;
import com.seibel.lod.core.config.gui.OpenGLConfigScreen;
import com.seibel.lod.core.pos.Pos2D;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
@@ -34,7 +33,7 @@ public class TestConfigMain {
public void run() {
abstractScreen = new ConfigScreen();
abstractScreen = new OpenGLConfigScreen();
System.out.println("Hello LWJGL version " + Version.getVersion());
// ClientApi.INSTANCE.rendererStartupEvent();
@@ -1,12 +1,9 @@
package com.seibel.lod.core.jar;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.seibel.lod.core.ModInfo;
import com.seibel.lod.core.config.ConfigBase;
import com.seibel.lod.core.jar.DarkModeDetector;
import com.seibel.lod.core.jar.JarUtils;
import com.seibel.lod.core.jar.gui.BaseJFrame;
import com.seibel.lod.core.jar.gui.cusomJObject.JBox;
import com.seibel.lod.core.jar.installer.ModrinthGetter;
import com.seibel.lod.core.jar.installer.WebDownloader;
@@ -41,6 +38,9 @@ public class JarMain extends Application {
public static final boolean isDarkTheme = DarkModeDetector.isDarkMode();
public static boolean isOffline = WebDownloader.netIsAvailable();
// TODO: Rewrite the standalone jar
// Previous version here https://gitlab.com/jeseibel/distant-horizons-core/-/blob/333dc4d0e079777b712c0fff246837104ae9a2b6/core/src/main/java/com/seibel/lod/core/jar/JarMain.java
@Override
public void start(Stage stage) {
logger.debug("JavaFX version "+System.getProperty("javafx.version"));
@@ -1,133 +0,0 @@
package com.seibel.lod.core.jar.gui;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.seibel.lod.core.dependencyInjection.SingletonInjector;
import com.seibel.lod.core.jar.JarUtils;
import com.seibel.lod.core.wrapperInterfaces.config.ILangWrapper;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;
/**
* @author coolGi
*/
// This will be removed later on to make a better ui
// To get colors use https://alvinalexander.com/java/java-uimanager-color-keys-list/
public class BaseJFrame extends JFrame {
public BaseJFrame() {
init();
}
public BaseJFrame(boolean show, boolean resizable) {
init();
setVisible(show);
setResizable(resizable);
}
public void init() {
setTitle(SingletonInjector.INSTANCE.get(ILangWrapper.class).getLang("lod.title"));
try {
setIconImage(new FlatSVGIcon(JarUtils.accessFile("icon.svg")).getImage());
} catch (Exception e) {e.printStackTrace();}
setSize(720, 480);
setLocationRelativeTo(null); // Puts the window at the middle of the screen
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Buttons for language and theme changing
*
* @param themeOnBottom Puts the theme buttons below the language
* @param rootPosOnLeft Where the start for the x is (on the left of the buttons or on the right)
*/
public void addExtraButtons(int x, int y, boolean themeOnBottom, boolean rootPosOnLeft) {
// ========== LANGUAGE ==========
int langBoxHeight = 25;
int langBoxWidth = 100;
// Creates a list with all the options in it
List<String> langsToChoose = new ArrayList<>();
try(
final InputStreamReader isr = new InputStreamReader(JarUtils.accessFile("assets/lod/lang"), StandardCharsets.UTF_8);
final BufferedReader br = new BufferedReader(isr)
) {
List<Object> col = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(br.lines().toArray())));
for (Object obj: col)
langsToChoose.add(((String) obj).replaceAll("\\.json", ""));
} catch (Exception e) {e.printStackTrace();}
// Creates the box
JComboBox<String> languageBox = new JComboBox(new DefaultComboBoxModel(langsToChoose.toArray()));
languageBox.setSelectedIndex(langsToChoose.indexOf(Locale.getDefault().toString().toLowerCase()));
languageBox.addActionListener( e -> {
Locale.setDefault(Locale.forLanguageTag(languageBox.getSelectedItem().toString())); // Change lang on update
} );
// Set where it goes
languageBox.setBounds(rootPosOnLeft? x : x-langBoxWidth, themeOnBottom? y : y+langBoxHeight, langBoxWidth, langBoxHeight);
// And finally add it
add(languageBox);
// ========== THEMING ==========
// TODO: Change the theme to a toggle switch rather than having 2 buttons
int themeButtonSize = 25;
JButton lightMode = null;
JButton darkMode = null;
// Try to set the icons for them
try {
lightMode = new JButton(new ImageIcon(
new FlatSVGIcon(JarUtils.accessFile("assets/lod/textures/jar/themeLight.svg")).getImage() // Get the image
.getScaledInstance(themeButtonSize, themeButtonSize, Image.SCALE_DEFAULT) // Scale it to the correct size
));
darkMode = new JButton(new ImageIcon(
new FlatSVGIcon(JarUtils.accessFile("assets/lod/textures/jar/themeDark.svg")).getImage() // Get the image
.getScaledInstance(themeButtonSize, themeButtonSize, Image.SCALE_DEFAULT) // Scale it to the correct size
));
} catch (Exception e) {e.printStackTrace();}
// Where do the buttons go
lightMode.setBounds(rootPosOnLeft? x : x-(themeButtonSize*2), themeOnBottom? y+langBoxHeight: y, themeButtonSize, themeButtonSize);
darkMode.setBounds(rootPosOnLeft? x+themeButtonSize : x-themeButtonSize, themeOnBottom? y+langBoxHeight: y, themeButtonSize, themeButtonSize);
// Tell buttons what to do
lightMode.addActionListener(e -> {
FlatLightLaf.setup();
FlatLightLaf.updateUI();
});
darkMode.addActionListener(e -> {
FlatDarkLaf.setup();
FlatDarkLaf.updateUI();
});
// Finally add the buttons
add(lightMode);
add(darkMode);
}
public BaseJFrame addLogo() {
int logoHeight = 200;
JPanel logo = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
BufferedImage image = ImageIO.read(JarUtils.accessFile("logo.png"));
int logoWidth = (int) ((double) logoHeight * ((double) image.getWidth() / (double) image.getHeight())); // Calculate the aspect ratio and set the height correctly to not stretch it
g.drawImage(image, (getWidth()/2)-(logoWidth/2), 0, logoWidth, logoHeight,this); // Resize image and draw it
} catch (Exception e) {e.printStackTrace();}
}
};
logo.setBounds(logo.getX(), logo.getY(), logo.getWidth(), logo.getHeight());
add(logo);
return this;
}
}