Skip to content

Commit

Permalink
Application: Fix splash scaling; add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jul 3, 2024
1 parent 0427478 commit 0041323
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,21 @@ public void start(@NotNull String[] args) {
Splash.getInstance().show();
}

Splash.getInstance().set("Loading services");
Splash.getInstance().update("Loading services and configuration", 0.0f);

this.preferences = Preferences.userRoot().node("decima-explorer");
this.serviceManager = new ServiceManager(getConfigPath());

if (args.length > 0) {
ApplicationCLI.execute(args);
return;
}

Splash.getInstance().show();

connection = MessageBus.getInstance().connect();

Splash.getInstance().set("Configuring UI");
Splash.getInstance().update("Configuring user interface", 0.5f);

configureUI();
frame = new JFrame();
Expand Down Expand Up @@ -159,7 +160,7 @@ public void selectionCleared() {
panel.add(ViewManager.getInstance().getComponent(), BorderLayout.CENTER);
panel.add(statusBar, BorderLayout.SOUTH);

Splash.getInstance().set("Done");
Splash.getInstance().update("Done", 1.0f);

frame.setTitle(getApplicationTitle());
frame.setIconImages(FlatSVGUtils.createWindowIconImages("/icons/application.svg"));
Expand Down
58 changes: 37 additions & 21 deletions modules/decima-ui/src/main/java/com/shade/decima/ui/Splash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shade.decima.ui;

import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;
import com.shade.decima.BuildConfig;
import com.shade.platform.ui.UIColor;
import com.shade.util.NotNull;
Expand All @@ -10,8 +11,8 @@
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.util.Objects;
import java.util.Random;

public class Splash {
Expand Down Expand Up @@ -41,9 +42,9 @@ public void hide() {
}
}

public void set(@Nullable String status) {
public void update(@Nullable String status, float progress) {
if (frame != null) {
frame.component.setStatus(status);
frame.component.update(status, progress);
}
}

Expand All @@ -68,6 +69,7 @@ private static class SplashComponent extends JComponent {

private BufferedImage splash;
private String status;
private float progress = -1.0f;

public SplashComponent() {
setFont(createFont());
Expand All @@ -76,34 +78,49 @@ public SplashComponent() {
@Override
protected void paintComponent(Graphics g) {
if (splash == null) {
splash = createSplashTexture(getFont(), getWidth(), getHeight());
splash = createSplashTexture(
getFont(),
getWidth(),
getHeight(),
UIScale.getSystemScaleFactor(getGraphicsConfiguration())
);
}

g.drawImage(splash, 0, 0, null);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(splash, 0, 0, getWidth(), getHeight(), null);

if (status != null) {
final Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.setFont(getFont());
g2.drawString(status, 30, getHeight() - g2.getFontMetrics().getDescent() - 30);
g2.dispose();
g2.drawString(status, 30, getHeight() - g2.getFontMetrics().getDescent() - 40);
}
}

public void setStatus(@Nullable String status) {
if (Objects.equals(status, this.status)) {
return;
if (progress >= 0.0f) {
g2.setColor(COLOR_2);
g2.fillRect(30, getHeight() - 30, getWidth() - 60, 5);
g2.setColor(COLOR_1);
g2.fillRect(31, getHeight() - 29, (int) ((getWidth() - 62) * progress), 3);
}

g2.dispose();
}

public void update(@Nullable String status, float progress) {
this.status = status;
this.progress = progress;
repaint();
}

@NotNull
private static BufferedImage createSplashTexture(@NotNull Font font, int width, int height) {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
private static BufferedImage createSplashTexture(@NotNull Font font, int width, int height, double scale) {
int scaledWidth = (int) (width * scale);
int scaledHeight = (int) (height * scale);

final BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.scale(scale, scale);

// Rounded mask
g2.setColor(Color.WHITE);
Expand All @@ -117,7 +134,7 @@ private static BufferedImage createSplashTexture(@NotNull Font font, int width,

// Noise overlay
g2.setComposite(AlphaComposite.SrcAtop.derive(0.05f));
g2.drawImage(createNoiseTexture(width, height), 0, 0, null);
g2.drawImage(createNoiseTexture(scaledWidth, scaledHeight), 0, 0, width, height, null);

// Text
final Font font1 = font.deriveFont(36f);
Expand All @@ -139,12 +156,11 @@ private static BufferedImage createSplashTexture(@NotNull Font font, int width,

@NotNull
private static BufferedImage createNoiseTexture(int width, int height) {
final byte[] data = new byte[width * height];
new Random(0xDEC13A).nextBytes(data);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();

final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
final WritableRaster raster = image.getRaster();
raster.setDataElements(0, 0, width, height, data);
new Random(0xDEC13A).nextBytes(buffer.getData());

return image;
}
Expand Down

0 comments on commit 0041323

Please sign in to comment.