Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the preloader have a fancy background #843

Merged
merged 3 commits into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package edu.wpi.grip.preloader;

import java.io.IOException;
import java.util.Random;

import javafx.animation.Animation;
import javafx.animation.FillTransition;
import javafx.application.Platform;
import javafx.application.Preloader;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public final class GripPreloader extends Preloader {

private ProgressBar progressBar;
private static final Color primaryColor = Color.gray(0.075);
private static final Color secondaryColor = Color.gray(0.1);

// Animation timings
private static final double minTime = 1 / 3.0;
private static final double maxTime = 2 / 3.0;

private static final double HEXAGON_RADIUS = 12;

private Stage preloaderStage;

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

@Override
public void start(Stage preloaderStage) throws IOException {
Parent root = FXMLLoader.load(GripPreloader.class.getResource("Preloader.fxml"));
Scene scene = new Scene(root);
final StackPane root = FXMLLoader.load(GripPreloader.class.getResource("Preloader.fxml"));

// Animated hexagon grid background
// wrap in runLater so we can get the size of the scene
Platform.runLater(() -> {
Random random = new Random(System.currentTimeMillis() ^ (System.currentTimeMillis() >> 16));
HexagonGrid hexagonGrid = new HexagonGrid(
(int) (preloaderStage.getScene().getWidth() / HEXAGON_RADIUS),
(int) (preloaderStage.getScene().getHeight() / HEXAGON_RADIUS),
HEXAGON_RADIUS,
0);
// animate the hexagons
hexagonGrid.hexagons()
.stream()
.map(h -> new FillTransition(
Duration.seconds(
clamp(random.nextGaussian() + 1, 0, 2) * (maxTime - minTime) + minTime),
h, primaryColor, secondaryColor))
.peek(t -> t.setCycleCount(Animation.INDEFINITE))
.peek(t -> t.setAutoReverse(true))
.forEach(t -> t.playFrom(Duration.seconds(random.nextDouble() * maxTime * 16)));
Pane backgroundContainer = new Pane(hexagonGrid);

progressBar = (ProgressBar) root.getChildrenUnmodifiable().filtered(
p -> p instanceof ProgressBar).get(0);
// bring the hexagons to the top edge to avoid weird blank spots
backgroundContainer.setTranslateY(-HEXAGON_RADIUS);

root.getChildren().add(0, backgroundContainer);
});

Scene scene = new Scene(root);

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

if (getParameters().getRaw().contains("windowed")) {
preloaderStage.initStyle(StageStyle.UTILITY);
preloaderStage.initStyle(StageStyle.UNDECORATED);
} else {
preloaderStage.initStyle(StageStyle.TRANSPARENT);
}
Expand All @@ -43,11 +83,14 @@ public void start(Stage preloaderStage) throws IOException {

@Override
public void handleApplicationNotification(PreloaderNotification pn) {
if (pn instanceof ProgressNotification) {
progressBar.setProgress(((ProgressNotification) pn).getProgress());
} else if (pn instanceof StateChangeNotification
if (pn instanceof StateChangeNotification
&& ((StateChangeNotification) pn).getType() == StateChangeNotification.Type.BEFORE_START) {
preloaderStage.hide();
}
}

private static double clamp(double n, double min, double max) {
return (n < min) ? min : ((n > max) ? max : n);
}

}
63 changes: 63 additions & 0 deletions ui/preloader/src/main/java/edu/wpi/grip/preloader/HexagonGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package edu.wpi.grip.preloader;

import java.util.Collection;
import java.util.stream.Collectors;

import javafx.scene.Group;
import javafx.scene.shape.Polygon;

public class HexagonGrid extends Group {

private static final double ang30 = Math.toRadians(30);

/**
* Creates a new hexagon grid with the given number of rows and columns.
*
* @param cols the number of columns in the grid
* @param rows the number of rows in the grid
* @param radius the radius of the hexagons in the grid
* @param padding the padding between each hexagon
*/
public HexagonGrid(int cols, int rows, double radius, double padding) {
double xOffset = Math.cos(ang30) * (radius + padding);
double yOffset = Math.sin(ang30) * (radius + padding) * 3;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
double x = xOffset * (col * 2 + row % 2);
double y = radius + yOffset * row;
Polygon hexagon = new Hexagon(radius);
hexagon.setRotate(90);
hexagon.setTranslateX(x);
hexagon.setTranslateY(y);
getChildren().add(hexagon);
}
}
}

/**
* Gets the hexagons in the grid. Do not apply any transforms to the hexagons; they are already
* in the correct locations and orientations.
*/
public Collection<Polygon> hexagons() {
return getChildren().stream()
.filter(n -> n instanceof Hexagon)
.map(n -> (Hexagon) n)
.collect(Collectors.toList());
}

private static class Hexagon extends Polygon {

private static final double ROOT_THREE_OVER_2 = Math.sqrt(3) / 2;

public Hexagon(double radius) {
super(radius, 0,
radius / 2, ROOT_THREE_OVER_2 * radius,
-radius / 2, ROOT_THREE_OVER_2 * radius,
-radius, 0,
-radius / 2, -ROOT_THREE_OVER_2 * radius,
radius / 2, -ROOT_THREE_OVER_2 * radius);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
.root {
-fx-padding: 25;
}

.progress-bar {
-fx-padding: 25 0 0 0;
-fx-accent: #6C8058;
-fx-background-color: #111111;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.URL?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.VBox?>
<?import javafx.stage.Screen?>
<VBox id="root" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitWidth="${screen.visualBounds.width / 4}" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@grip.png" />
</image>
<fx:define>
<Screen fx:factory="getPrimary" fx:id="screen"/>
</fx:define>
</ImageView>
<ProgressBar id="progress-bar" maxWidth="1.7976931348623157E308" />
</children>
<stylesheets>
<URL value="@Preloader.css" />
</stylesheets>
</VBox>
<?import javafx.scene.layout.StackPane?>
<?import javafx.geometry.Insets?>
<StackPane id="root" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox alignment="CENTER">
<ImageView fitWidth="${screen.visualBounds.width / 4}" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@grip.png"/>
</image>
<fx:define>
<Screen fx:factory="getPrimary" fx:id="screen"/>
</fx:define>
</ImageView>
<padding>
<Insets topRightBottomLeft="25"/>
</padding>
</VBox>
</children>
<stylesheets>
<URL value="@Preloader.css"/>
</stylesheets>
</StackPane>