Skip to content

Commit c530c62

Browse files
SamCarlbergJLLeitschuh
authored andcommitted
Make the preloader have a fancy background (#843)
* Make the preloader have a fancy background Remove the progress bar from the preloader as well * Make colors stand out a bit more * Change to dark color theme. Should probably depend on #832 Small improvements to hex grid placement, no more weird boders
1 parent e12fcc7 commit c530c62

File tree

4 files changed

+140
-34
lines changed

4 files changed

+140
-34
lines changed
Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
package edu.wpi.grip.preloader;
22

33
import java.io.IOException;
4+
import java.util.Random;
5+
6+
import javafx.animation.Animation;
7+
import javafx.animation.FillTransition;
8+
import javafx.application.Platform;
49
import javafx.application.Preloader;
510
import javafx.fxml.FXMLLoader;
6-
import javafx.scene.Parent;
711
import javafx.scene.Scene;
8-
import javafx.scene.control.ProgressBar;
12+
import javafx.scene.layout.Pane;
13+
import javafx.scene.layout.StackPane;
14+
import javafx.scene.paint.Color;
915
import javafx.stage.Stage;
1016
import javafx.stage.StageStyle;
17+
import javafx.util.Duration;
1118

1219
public final class GripPreloader extends Preloader {
1320

14-
private ProgressBar progressBar;
21+
private static final Color primaryColor = Color.gray(0.075);
22+
private static final Color secondaryColor = Color.gray(0.1);
23+
24+
// Animation timings
25+
private static final double minTime = 1 / 3.0;
26+
private static final double maxTime = 2 / 3.0;
27+
28+
private static final double HEXAGON_RADIUS = 12;
29+
1530
private Stage preloaderStage;
1631

1732
public static void main(String[] args) {
@@ -20,16 +35,41 @@ public static void main(String[] args) {
2035

2136
@Override
2237
public void start(Stage preloaderStage) throws IOException {
23-
Parent root = FXMLLoader.load(GripPreloader.class.getResource("Preloader.fxml"));
24-
Scene scene = new Scene(root);
38+
final StackPane root = FXMLLoader.load(GripPreloader.class.getResource("Preloader.fxml"));
39+
40+
// Animated hexagon grid background
41+
// wrap in runLater so we can get the size of the scene
42+
Platform.runLater(() -> {
43+
Random random = new Random(System.currentTimeMillis() ^ (System.currentTimeMillis() >> 16));
44+
HexagonGrid hexagonGrid = new HexagonGrid(
45+
(int) (preloaderStage.getScene().getWidth() / HEXAGON_RADIUS),
46+
(int) (preloaderStage.getScene().getHeight() / HEXAGON_RADIUS),
47+
HEXAGON_RADIUS,
48+
0);
49+
// animate the hexagons
50+
hexagonGrid.hexagons()
51+
.stream()
52+
.map(h -> new FillTransition(
53+
Duration.seconds(
54+
clamp(random.nextGaussian() + 1, 0, 2) * (maxTime - minTime) + minTime),
55+
h, primaryColor, secondaryColor))
56+
.peek(t -> t.setCycleCount(Animation.INDEFINITE))
57+
.peek(t -> t.setAutoReverse(true))
58+
.forEach(t -> t.playFrom(Duration.seconds(random.nextDouble() * maxTime * 16)));
59+
Pane backgroundContainer = new Pane(hexagonGrid);
2560

26-
progressBar = (ProgressBar) root.getChildrenUnmodifiable().filtered(
27-
p -> p instanceof ProgressBar).get(0);
61+
// bring the hexagons to the top edge to avoid weird blank spots
62+
backgroundContainer.setTranslateY(-HEXAGON_RADIUS);
63+
64+
root.getChildren().add(0, backgroundContainer);
65+
});
66+
67+
Scene scene = new Scene(root);
2868

2969
System.setProperty("prism.lcdtext", "false");
3070

3171
if (getParameters().getRaw().contains("windowed")) {
32-
preloaderStage.initStyle(StageStyle.UTILITY);
72+
preloaderStage.initStyle(StageStyle.UNDECORATED);
3373
} else {
3474
preloaderStage.initStyle(StageStyle.TRANSPARENT);
3575
}
@@ -43,11 +83,14 @@ public void start(Stage preloaderStage) throws IOException {
4383

4484
@Override
4585
public void handleApplicationNotification(PreloaderNotification pn) {
46-
if (pn instanceof ProgressNotification) {
47-
progressBar.setProgress(((ProgressNotification) pn).getProgress());
48-
} else if (pn instanceof StateChangeNotification
86+
if (pn instanceof StateChangeNotification
4987
&& ((StateChangeNotification) pn).getType() == StateChangeNotification.Type.BEFORE_START) {
5088
preloaderStage.hide();
5189
}
5290
}
91+
92+
private static double clamp(double n, double min, double max) {
93+
return (n < min) ? min : ((n > max) ? max : n);
94+
}
95+
5396
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package edu.wpi.grip.preloader;
2+
3+
import java.util.Collection;
4+
import java.util.stream.Collectors;
5+
6+
import javafx.scene.Group;
7+
import javafx.scene.shape.Polygon;
8+
9+
public class HexagonGrid extends Group {
10+
11+
private static final double ang30 = Math.toRadians(30);
12+
13+
/**
14+
* Creates a new hexagon grid with the given number of rows and columns.
15+
*
16+
* @param cols the number of columns in the grid
17+
* @param rows the number of rows in the grid
18+
* @param radius the radius of the hexagons in the grid
19+
* @param padding the padding between each hexagon
20+
*/
21+
public HexagonGrid(int cols, int rows, double radius, double padding) {
22+
double xOffset = Math.cos(ang30) * (radius + padding);
23+
double yOffset = Math.sin(ang30) * (radius + padding) * 3;
24+
for (int row = 0; row < rows; row++) {
25+
for (int col = 0; col < cols; col++) {
26+
double x = xOffset * (col * 2 + row % 2);
27+
double y = radius + yOffset * row;
28+
Polygon hexagon = new Hexagon(radius);
29+
hexagon.setRotate(90);
30+
hexagon.setTranslateX(x);
31+
hexagon.setTranslateY(y);
32+
getChildren().add(hexagon);
33+
}
34+
}
35+
}
36+
37+
/**
38+
* Gets the hexagons in the grid. Do not apply any transforms to the hexagons; they are already
39+
* in the correct locations and orientations.
40+
*/
41+
public Collection<Polygon> hexagons() {
42+
return getChildren().stream()
43+
.filter(n -> n instanceof Hexagon)
44+
.map(n -> (Hexagon) n)
45+
.collect(Collectors.toList());
46+
}
47+
48+
private static class Hexagon extends Polygon {
49+
50+
private static final double ROOT_THREE_OVER_2 = Math.sqrt(3) / 2;
51+
52+
public Hexagon(double radius) {
53+
super(radius, 0,
54+
radius / 2, ROOT_THREE_OVER_2 * radius,
55+
-radius / 2, ROOT_THREE_OVER_2 * radius,
56+
-radius, 0,
57+
-radius / 2, -ROOT_THREE_OVER_2 * radius,
58+
radius / 2, -ROOT_THREE_OVER_2 * radius);
59+
}
60+
61+
}
62+
63+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
.root {
2-
-fx-padding: 25;
3-
}
4-
5-
.progress-bar {
6-
-fx-padding: 25 0 0 0;
7-
-fx-accent: #6C8058;
2+
-fx-background-color: #111111;
83
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<?import java.net.URL?>
4-
<?import javafx.scene.control.ProgressBar?>
54
<?import javafx.scene.image.Image?>
65
<?import javafx.scene.image.ImageView?>
76
<?import javafx.scene.layout.VBox?>
87
<?import javafx.stage.Screen?>
9-
<VBox id="root" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
10-
<children>
11-
<ImageView fitWidth="${screen.visualBounds.width / 4}" pickOnBounds="true" preserveRatio="true">
12-
<image>
13-
<Image url="@grip.png" />
14-
</image>
15-
<fx:define>
16-
<Screen fx:factory="getPrimary" fx:id="screen"/>
17-
</fx:define>
18-
</ImageView>
19-
<ProgressBar id="progress-bar" maxWidth="1.7976931348623157E308" />
20-
</children>
21-
<stylesheets>
22-
<URL value="@Preloader.css" />
23-
</stylesheets>
24-
</VBox>
8+
<?import javafx.scene.layout.StackPane?>
9+
<?import javafx.geometry.Insets?>
10+
<StackPane id="root" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
11+
<children>
12+
<VBox alignment="CENTER">
13+
<ImageView fitWidth="${screen.visualBounds.width / 4}" pickOnBounds="true" preserveRatio="true">
14+
<image>
15+
<Image url="@grip.png"/>
16+
</image>
17+
<fx:define>
18+
<Screen fx:factory="getPrimary" fx:id="screen"/>
19+
</fx:define>
20+
</ImageView>
21+
<padding>
22+
<Insets topRightBottomLeft="25"/>
23+
</padding>
24+
</VBox>
25+
</children>
26+
<stylesheets>
27+
<URL value="@Preloader.css"/>
28+
</stylesheets>
29+
</StackPane>

0 commit comments

Comments
 (0)