Skip to content

Commit

Permalink
Merge pull request #4847 from kwvanderlinde/bugfix/4819-splash-screen…
Browse files Browse the repository at this point in the history
…-transparency-on-linux

Fix splash screen transparency on Linux
  • Loading branch information
cwisniew authored Jun 28, 2024
2 parents 41988c1 + f41fee6 commit 84072c9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 219 deletions.
19 changes: 17 additions & 2 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.SecondaryLoop;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.WindowAdapter;
Expand All @@ -42,6 +43,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.*;
import javafx.application.Platform;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.imageio.ImageIO;
Expand Down Expand Up @@ -1472,6 +1474,15 @@ public static String getLoggerFileName() {
return "NOT_CONFIGURED";
}

private static void initJavaFX() {
var eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
Platform.startup(secondaryLoop::exit);
secondaryLoop.enter();

Platform.setImplicitExit(false); // necessary to use JavaFX later
}

public static void main(String[] args) {
log.info("********************************************************************************");
log.info("** **");
Expand Down Expand Up @@ -1609,7 +1620,10 @@ public static void main(String[] args) {
// System properties
System.setProperty("swing.aatext", "true");

final SplashScreen splash = new SplashScreen((isDevelopment()) ? getVersion() : getVersion());
initJavaFX();

final SplashScreen splash = new SplashScreen(getVersion());
splash.setVisible(true);

try {
ThemeSupport.loadTheme();
Expand Down Expand Up @@ -1721,7 +1735,8 @@ public static void main(String[] args) {
EventQueue.invokeLater(
() -> {
clientFrame.setVisible(true);
splash.hideSplashScreen();
splash.setVisible(false);
splash.dispose();
EventQueue.invokeLater(MapTool::postInitialize);
});
});
Expand Down
141 changes: 104 additions & 37 deletions src/main/java/net/rptools/maptool/client/swing/SplashScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,58 @@
package net.rptools.maptool.client.swing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.SnapshotParameters;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javax.swing.JFrame;
import net.rptools.maptool.util.CreateVersionedInstallSplash;
import javax.swing.JPanel;
import net.rptools.maptool.client.ui.theme.Images;
import net.rptools.maptool.client.ui.theme.RessourceManager;
import org.javatuples.Pair;

public class SplashScreen extends JFrame {
private static final String FONT_RESOURCE = "/net/rptools/maptool/client/fonts/Horta.ttf";

private static int imgWidth = 490;
private static int imgHeight = 290;
private static final int imgWidth = 490;
private static final int imgHeight = 290;
private static final int versionTextX = 48;
private static final int versionTextY = 37;

public SplashScreen(final String versionText) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final JFXPanel fxPanel = new JFXPanel();
public SplashScreen(String versionText) {
versionText = Character.isDigit(versionText.charAt(0)) ? "v" + versionText : versionText;

setUndecorated(true);
setType(Type.UTILITY);

add(fxPanel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = imgWidth;
int h = imgHeight;
int x = (screenSize.width - w) / 2;
int y = (screenSize.height - h) / 2;
setBounds(x, y, imgWidth, imgHeight);

setLocationRelativeTo(null);
setLayout(new GridBagLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try {
Expand All @@ -50,41 +77,81 @@ public SplashScreen(final String versionText) {
setBackground(new java.awt.Color(0, 0, 0));
}

Platform.setImplicitExit(false); // necessary to use JavaFX later
Platform.runLater(
() -> {
initFX(fxPanel, versionText);
int w = imgWidth;
int h = imgHeight;
int x = (screenSize.width - w) / 2;
int y = (screenSize.height - h) / 2;
setBounds(x, y, imgWidth, imgHeight);
setVisible(true);
var image = createLaunchSplash("Launching... " + versionText);

setContentPane(
new JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, imgWidth, imgHeight, null);
}
});
}

private static void initFX(JFXPanel fxPanel, String versionText) {
// This method is invoked on the JavaFX thread
Group root = new Group();
Scene scene = new Scene(root, Color.TRANSPARENT);
private static BufferedImage createLaunchSplash(String versionText) {
Image splashIcon = RessourceManager.getImage(Images.MAPTOOL_SPLASH);
final Color versionColor = Color.rgb(3, 78, 149, 1); // Color.rgb(27, 85, 139, 1)

InputStream is = SplashScreen.class.getResourceAsStream(FONT_RESOURCE);
var versionFont = Font.loadFont(is, 28);

BufferedImage buffImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffImage.createGraphics();
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHints(rh);
g2d.drawImage(splashIcon, 0, 0, null);

var future =
CompletableFuture.supplyAsync(
() -> {
// Adding glow twice to make it more pronounced...
var glowingText = textToImage(versionText, Color.WHITESMOKE, versionFont, true);
var regularText = textToImage(versionText, versionColor, versionFont, false);
return new Pair<>(glowingText, regularText);
},
Platform::runLater);

if (Character.isDigit(versionText.charAt(0))) {
versionText = "v" + versionText;
try {
var result = future.get();
// Adding glow twice to make it more pronounced...
g2d.drawImage(result.getValue0(), versionTextX, versionTextY, null);
g2d.drawImage(result.getValue0(), versionTextX, versionTextY, null);
g2d.drawImage(result.getValue1(), versionTextX, versionTextY, null);

} catch (InterruptedException | ExecutionException e) {
// Oh no... we can't show the version. Oh, well.
}

Image splashImage =
SwingFXUtils.toFXImage(
CreateVersionedInstallSplash.createLaunchSplash("Launching... " + versionText), null);
ImageView splashView = new ImageView(splashImage);
imgWidth = (int) splashImage.getWidth();
imgHeight = (int) splashImage.getHeight();
root.getChildren().add(splashView);
g2d.dispose();

fxPanel.setScene(scene);
return buffImage;
}

public void hideSplashScreen() {
setVisible(false);
dispose();
private static BufferedImage textToImage(
String text, Color fontColor, Font versionFont, boolean addGlow) {
Text versionText = new Text(0, 0, text);
versionText.setFill(fontColor);
versionText.setFont(versionFont);

if (addGlow) {
Effect glow = new Glow(1.0);
versionText.setEffect(glow);
}

Stage stage = new Stage(StageStyle.TRANSPARENT);
Group root = new Group();
Scene scene = new Scene(root);
SnapshotParameters sp = new SnapshotParameters();

sp.setFill(Color.TRANSPARENT);
stage.setScene(scene);
root.getChildren().add(versionText);

WritableImage img = root.snapshot(sp, null);

return SwingFXUtils.fromFXImage(img, null);
}
}
Loading

0 comments on commit 84072c9

Please sign in to comment.