From 7d060d8b859c170cf78de74a2da7463f77409f77 Mon Sep 17 00:00:00 2001 From: coolGi Date: Sun, 26 Mar 2023 17:31:53 +1030 Subject: [PATCH] Fixed up some things to do with the Java screen --- .../lod/core/config/gui/AbstractScreen.java | 4 ++ .../lod/core/config/gui/ConfigScreen.java | 51 ++++++++++++++ .../core/config/gui/EmbeddedFrameUtil.java | 19 ++---- .../core/config/gui/JavaFXConfigScreen.java | 40 ----------- .../config/gui/JavaScreenHandlerScreen.java | 68 +++++++++++++++++++ 5 files changed, 130 insertions(+), 52 deletions(-) create mode 100644 core/src/main/java/com/seibel/lod/core/config/gui/ConfigScreen.java delete mode 100644 core/src/main/java/com/seibel/lod/core/config/gui/JavaFXConfigScreen.java create mode 100644 core/src/main/java/com/seibel/lod/core/config/gui/JavaScreenHandlerScreen.java diff --git a/core/src/main/java/com/seibel/lod/core/config/gui/AbstractScreen.java b/core/src/main/java/com/seibel/lod/core/config/gui/AbstractScreen.java index e81cbfd96..3166ab3f8 100644 --- a/core/src/main/java/com/seibel/lod/core/config/gui/AbstractScreen.java +++ b/core/src/main/java/com/seibel/lod/core/config/gui/AbstractScreen.java @@ -13,6 +13,8 @@ public abstract class AbstractScreen { public long minecraftWindow; public int width; public int height; + public int scaledWidth; + public int scaledHeight; public int mouseX = 0; public int mouseY = 0; /** Weather it should close when you press the escape key */ @@ -27,6 +29,8 @@ public abstract class AbstractScreen { public abstract void render(float delta); public void tick() {} + /** Called every time the window gets re-sized */ + public void onResize() {}; /** What happens when the user closes the screen*/ public void onClose() {} diff --git a/core/src/main/java/com/seibel/lod/core/config/gui/ConfigScreen.java b/core/src/main/java/com/seibel/lod/core/config/gui/ConfigScreen.java new file mode 100644 index 000000000..54eee958d --- /dev/null +++ b/core/src/main/java/com/seibel/lod/core/config/gui/ConfigScreen.java @@ -0,0 +1,51 @@ +package com.seibel.lod.core.config.gui; + +import javax.swing.*; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.embed.swing.JFXPanel; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ConfigScreen extends JFrame { + private final JFXPanel fxPanel = new JFXPanel(); + + public ConfigScreen() { +// super("JavaFX in Swing"); +// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(new BorderLayout()); + add(fxPanel, BorderLayout.CENTER); +// setSize(300, 200); +// setLocationRelativeTo(null); +// setVisible(true); + + Platform.runLater(() -> initFX(fxPanel)); + } + + private void initFX(JFXPanel fxPanel) { + Scene scene = createScene(); + fxPanel.setScene(scene); + } + + private Scene createScene() { + Label label = new Label("Hello from JavaFX!"); + return new Scene(label); + } + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + JFrame frame = new ConfigScreen(); + frame.setSize(300, 200); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + }); + } +} diff --git a/core/src/main/java/com/seibel/lod/core/config/gui/EmbeddedFrameUtil.java b/core/src/main/java/com/seibel/lod/core/config/gui/EmbeddedFrameUtil.java index 9b946321b..7d6898010 100644 --- a/core/src/main/java/com/seibel/lod/core/config/gui/EmbeddedFrameUtil.java +++ b/core/src/main/java/com/seibel/lod/core/config/gui/EmbeddedFrameUtil.java @@ -1,6 +1,7 @@ package com.seibel.lod.core.config.gui; import com.seibel.lod.core.jar.Platform; +import org.jetbrains.annotations.NotNull; import org.lwjgl.system.jawt.JAWT; import org.lwjgl.system.macosx.*; @@ -122,22 +123,16 @@ public final class EmbeddedFrameUtil { } } - // The code below is by Ran - public static void hideFrame(Frame embeddedFrame) { - if (embeddedFrame != null) { - embeddedFrame.setVisible(false); - embeddedFrameSynthesizeWindowActivation(embeddedFrame, false); - } + public static void hideFrame(@NotNull Frame embeddedFrame) { + embeddedFrame.setVisible(false); + embeddedFrameSynthesizeWindowActivation(embeddedFrame, false); } - public static void showFrame(Frame embeddedFrame) { - if (embeddedFrame != null) { - embeddedFrameSynthesizeWindowActivation(embeddedFrame, true); - embeddedFrame.setVisible(true); - } + public static void showFrame(@NotNull Frame embeddedFrame) { + embeddedFrameSynthesizeWindowActivation(embeddedFrame, true); + embeddedFrame.setVisible(true); } - public static void placeAtCenter(Frame embeddedFrame, int windowWidth, int windowHeight, int frameWidth, int frameHeight, float scale) { float scaleFactor = (100.0F - scale) / 100.0F; float newWidth = frameWidth * scaleFactor; diff --git a/core/src/main/java/com/seibel/lod/core/config/gui/JavaFXConfigScreen.java b/core/src/main/java/com/seibel/lod/core/config/gui/JavaFXConfigScreen.java deleted file mode 100644 index 55b02d74f..000000000 --- a/core/src/main/java/com/seibel/lod/core/config/gui/JavaFXConfigScreen.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.seibel.lod.core.config.gui; - -import javax.swing.*; -import java.awt.*; - -public class JavaFXConfigScreen extends AbstractScreen { - static { - System.setProperty("java.awt.headless", "false"); - } - - @Override - public void init() { - Frame frame = EmbeddedFrameUtil.embeddedFrameCreate(this.minecraftWindow); - frame.add(new ExampleScreen()); - EmbeddedFrameUtil.placeAtCenter(frame, this.width, this.height, 100, 100, 1); - } - - public class ExampleScreen extends JComponent { - public ExampleScreen() { - setLayout(new GridBagLayout()); - GridBagConstraints constraints = new GridBagConstraints(); - constraints.fill = GridBagConstraints.HORIZONTAL; - constraints.weightx = 0.5; - constraints.gridx = 0; - constraints.gridy = 0; - add(new JLabel("Hello World!"), constraints); - } - } - - - @Override - public void render(float delta) { - - } - - @Override - public void onClose() { - - } -} diff --git a/core/src/main/java/com/seibel/lod/core/config/gui/JavaScreenHandlerScreen.java b/core/src/main/java/com/seibel/lod/core/config/gui/JavaScreenHandlerScreen.java new file mode 100644 index 000000000..1a94898e8 --- /dev/null +++ b/core/src/main/java/com/seibel/lod/core/config/gui/JavaScreenHandlerScreen.java @@ -0,0 +1,68 @@ +package com.seibel.lod.core.config.gui; + +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.awt.*; + +/** + * + */ +public class JavaScreenHandlerScreen extends AbstractScreen { + public static Frame frame; + public static boolean firstRun = true; + public final Component jComponent; + + static { + // Required to run this + System.setProperty("java.awt.headless", "false"); + } + + public JavaScreenHandlerScreen(@NotNull Component component) { + this.jComponent = component; + } + + @Override + public void init() { + if (firstRun) + frame = EmbeddedFrameUtil.embeddedFrameCreate(this.minecraftWindow); // Don't call this multiple times + + frame.add(jComponent); + + if (firstRun) { + EmbeddedFrameUtil.embeddedFrameSetBounds(frame, 0, 0, this.width, this.height); + firstRun = false; + } else + EmbeddedFrameUtil.showFrame(frame); + } + + /** A testing/debug screen */ + public static class ExampleScreen extends JComponent { + public ExampleScreen() { + setLayout(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.fill = GridBagConstraints.HORIZONTAL; + constraints.weightx = 0.5; + constraints.gridx = 0; + constraints.gridy = 0; + add(new JLabel("Hello World!"), constraints); + } + } + + + @Override + public void render(float delta) { + // TODO: Make screen only update on this being called + } + + @Override + public void onResize() { + EmbeddedFrameUtil.embeddedFrameSetBounds(frame, 0, 0, this.width, this.height); + } + + @Override + public void onClose() { + frame.remove(jComponent); + EmbeddedFrameUtil.hideFrame(frame); + } +}