Selaa lähdekoodia

🏷️ Upgrade effects module

🚚 Renamed DepthLevel to ElevationLevel and MFXDepthManager to MFXElevationManager
♻️ Adapt shadows to Material Design 3
✨ ElevationLevel: added method to convert level to a DropShadow, and method to animate a shadow to a given level
Alessadro Parisi 2 vuotta sitten
vanhempi
commit
dae6553473

+ 10 - 10
modules/effects/src/main/java/io/github/palexdev/mfxeffects/MFXDepthManager.java → modules/effects/src/main/java/io/github/palexdev/mfxeffects/MFXElevationManager.java

@@ -18,7 +18,7 @@
 
 package io.github.palexdev.mfxeffects;
 
-import io.github.palexdev.mfxeffects.enums.DepthLevel;
+import io.github.palexdev.mfxeffects.enums.ElevationLevel;
 import javafx.scene.effect.BlurType;
 import javafx.scene.effect.DropShadow;
 import javafx.scene.paint.Color;
@@ -27,9 +27,9 @@ import javafx.scene.paint.Color;
  * Utility class which manages a preset number of {@code DropShadow} effects ordered by {@code DepthLevel}, but
  * it also allows to create custom {@code DropShadow} effects with {@link #shadowOf(Color, double, double, double, double)}.
  * <p></p>
- * {@link DepthLevel}
+ * {@link ElevationLevel}
  */
-public class MFXDepthManager {
+public class MFXElevationManager {
 
 	/**
 	 * Returns a new instance of {@code DropShadow} with the specified characteristics.
@@ -54,7 +54,7 @@ public class MFXDepthManager {
 	 * @param level The desired {@code DepthLevel} between 1 and 5
 	 * @return The desired {@code DropShadow} effect
 	 */
-	public static DropShadow shadowOf(DepthLevel level) {
+	public static DropShadow shadowOf(ElevationLevel level) {
 		return new DropShadow(
 				BlurType.GAUSSIAN,
 				level.getColor(),
@@ -78,10 +78,10 @@ public class MFXDepthManager {
 	 * @param delta The number of levels to shift
 	 * @return The final {@code DropShadow} effect}
 	 * <p></p>
-	 * {@link #nextLevel(DepthLevel)}
+	 * {@link #nextLevel(ElevationLevel)}
 	 */
-	public static DropShadow shadowOf(DepthLevel level, int delta) {
-		DepthLevel endLevel = level;
+	public static DropShadow shadowOf(ElevationLevel level, int delta) {
+		ElevationLevel endLevel = level;
 		for (int i = 0; i < delta; i++) {
 			endLevel = nextLevel(endLevel);
 		}
@@ -93,9 +93,9 @@ public class MFXDepthManager {
 	 *
 	 * @param startLevel The starting {@code DepthLevel}
 	 * @return The {@code DropShadow} effect associated to the next {@code DepthLevel}
-	 * @see DepthLevel
+	 * @see ElevationLevel
 	 */
-	private static DepthLevel nextLevel(DepthLevel startLevel) {
-		return !(startLevel.equals(DepthLevel.LEVEL5)) ? startLevel.next() : DepthLevel.LEVEL5;
+	private static ElevationLevel nextLevel(ElevationLevel startLevel) {
+		return !(startLevel.equals(ElevationLevel.LEVEL5)) ? startLevel.next() : ElevationLevel.LEVEL5;
 	}
 }

+ 49 - 12
modules/effects/src/main/java/io/github/palexdev/mfxeffects/enums/DepthLevel.java → modules/effects/src/main/java/io/github/palexdev/mfxeffects/enums/ElevationLevel.java

@@ -18,6 +18,10 @@
 
 package io.github.palexdev.mfxeffects.enums;
 
+import io.github.palexdev.mfxeffects.animations.Animations.KeyFrames;
+import io.github.palexdev.mfxeffects.animations.Animations.TimelineBuilder;
+import javafx.animation.Interpolator;
+import javafx.scene.effect.BlurType;
 import javafx.scene.effect.DropShadow;
 import javafx.scene.paint.Color;
 
@@ -26,22 +30,37 @@ import java.util.Arrays;
 /**
  * Enumerator which defines 6 levels of {@code DropShadow} effects from {@code LEVEL0} to {@code LEVEL5}.
  */
-public enum DepthLevel {
+public enum ElevationLevel {
+	// 0dp
 	LEVEL0(Color.rgb(0, 0, 0, 0), 0, 0, 0, 0),
-	LEVEL1(Color.rgb(0, 0, 0, 0.20), 10, 0.12, -1, 2),
-	LEVEL2(Color.rgb(0, 0, 0, 0.20), 15, 0.16, 0, 4),
-	LEVEL3(Color.rgb(0, 0, 0, 0.20), 20, 0.19, 0, 6),
-	LEVEL4(Color.rgb(0, 0, 0, 0.20), 25, 0.25, 0, 8),
-	LEVEL5(Color.rgb(0, 0, 0, 0.20), 30, 0.30, 0, 10);
+
+	// 1dp
+	LEVEL1(3, 0.12, 0, 2),
+
+	// 3dp
+	LEVEL2(8, 0.16, 0, 3),
+
+	// 6dp
+	LEVEL3(18, 0.19, 0, 3),
+
+	// 8dp
+	LEVEL4(14, 0.25, 0, 5),
+
+	// 12dp
+	LEVEL5(16, 0.30, 0, 7);
 
 	private final Color color;
 	private final double radius;
 	private final double spread;
 	private final double offsetX;
 	private final double offsetY;
-	private static final DepthLevel[] valuesArr = values();
+	private static final ElevationLevel[] valuesArr = values();
 
-	DepthLevel(Color color, double radius, double spread, double offsetX, double offsetY) {
+	ElevationLevel(double radius, double spread, double offsetX, double offsetY) {
+		this(Color.rgb(0, 0, 0, 0.20), radius, spread, offsetX, offsetY);
+	}
+
+	ElevationLevel(Color color, double radius, double spread, double offsetX, double offsetY) {
 		this.color = color;
 		this.radius = radius;
 		this.spread = spread;
@@ -74,17 +93,35 @@ public enum DepthLevel {
 	 *
 	 * @return The next {@code DepthLevel}
 	 */
-	public DepthLevel next() {
+	public ElevationLevel next() {
 		return valuesArr[(this.ordinal() + 1) % valuesArr.length];
 	}
 
+	public void animateTo(DropShadow current, ElevationLevel next) {
+		Interpolator i = Interpolators.INTERPOLATOR_V2.toInterpolator();
+		TimelineBuilder.build()
+				.add(KeyFrames.of(200, current.radiusProperty(), next.getRadius(), i))
+				.add(KeyFrames.of(200, current.spreadProperty(), next.getSpread(), i))
+				.add(KeyFrames.of(150, current.offsetXProperty(), next.getOffsetX(), i))
+				.add(KeyFrames.of(150, current.offsetYProperty(), next.getOffsetY(), i))
+				.getAnimation().play();
+	}
+
+	public DropShadow toShadow() {
+		return new DropShadow(
+				BlurType.GAUSSIAN, getColor(),
+				getRadius(), getSpread(),
+				getOffsetX(), getOffsetY()
+		);
+	}
+
 	/**
 	 * Attempts to get the corresponding {@code DepthLevel} of the given {@link DropShadow} effect.
 	 * If the given effect is not recognized as being generated by this class the {@code null} is returned.
 	 * <p>
-	 * Uses {@link #levelEqualsShadow(DepthLevel, DropShadow)} to check for equality.
+	 * Uses {@link #levelEqualsShadow(ElevationLevel, DropShadow)} to check for equality.
 	 */
-	public static DepthLevel from(DropShadow shadow) {
+	public static ElevationLevel from(DropShadow shadow) {
 		return Arrays.stream(values()).filter(depthLevel -> levelEqualsShadow(depthLevel, shadow))
 				.findFirst()
 				.orElse(null);
@@ -93,7 +130,7 @@ public enum DepthLevel {
 	/**
 	 * Checks if the given {@link DropShadow} effect is equal to the given {@code DepthLevel}.
 	 */
-	public static boolean levelEqualsShadow(DepthLevel level, DropShadow shadow) {
+	public static boolean levelEqualsShadow(ElevationLevel level, DropShadow shadow) {
 		return level.color.equals(shadow.getColor()) &&
 				level.offsetX == shadow.getOffsetX() &&
 				level.offsetY == shadow.getOffsetY() &&

+ 5 - 5
modules/effects/src/main/java/io/github/palexdev/mfxeffects/ripple/RippleGenerator.java

@@ -18,7 +18,7 @@
 
 package io.github.palexdev.mfxeffects.ripple;
 
-import io.github.palexdev.mfxeffects.enums.DepthLevel;
+import io.github.palexdev.mfxeffects.enums.ElevationLevel;
 import io.github.palexdev.mfxeffects.enums.Interpolators;
 import io.github.palexdev.mfxeffects.builders.RippleClipTypeBuilder;
 import io.github.palexdev.mfxeffects.enums.RippleClipType;
@@ -38,7 +38,7 @@ import javafx.util.Duration;
 
 import java.util.List;
 
-import static io.github.palexdev.mfxeffects.MFXDepthManager.shadowOf;
+import static io.github.palexdev.mfxeffects.MFXElevationManager.shadowOf;
 
 /**
  * Convenience class for creating highly customizable ripple effects.
@@ -56,7 +56,7 @@ public class RippleGenerator extends Group {
 	private final Region region;
 
 	private RippleClipTypeBuilder rippleClipTypeBuilder = new RippleClipTypeBuilder(RippleClipType.RECTANGLE);
-	private DepthLevel level = null;
+	private ElevationLevel level = null;
 	//private final Interpolator rippleInterpolator = Interpolator.SPLINE(0.1, 0.50, 0.3, 0.85);
 	private final StyleableObjectProperty<Paint> rippleColor = new SimpleStyleableObjectProperty<>(
 			StyleableProperties.RIPPLE_COLOR,
@@ -101,7 +101,7 @@ public class RippleGenerator extends Group {
 		});
 	}
 
-	public RippleGenerator(Region region, DepthLevel shadowLevel) {
+	public RippleGenerator(Region region, ElevationLevel shadowLevel) {
 		this(region);
 		this.level = shadowLevel;
 	}
@@ -111,7 +111,7 @@ public class RippleGenerator extends Group {
 		this.rippleClipTypeBuilder = factory;
 	}
 
-	public RippleGenerator(Region region, DepthLevel shadowLevel, RippleClipTypeBuilder factory) {
+	public RippleGenerator(Region region, ElevationLevel shadowLevel, RippleClipTypeBuilder factory) {
 		this(region, shadowLevel);
 		this.rippleClipTypeBuilder = factory;
 	}

+ 1 - 1
modules/effects/src/test/java/app/EffectsLauncher.java

@@ -24,6 +24,6 @@ public class EffectsLauncher {
 
 	public static void main(String[] args) {
 		//System.setProperty("prism.vsync", "false");
-		Application.launch(RippleTest.class, args);
+		Application.launch(ShadowsTest.class, args);
 	}
 }

+ 54 - 0
modules/effects/src/test/java/app/ShadowsTest.java

@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2023 Parisi Alessandro - alessandro.parisi406@gmail.com
+ * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX)
+ *
+ * MaterialFX is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * MaterialFX is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with MaterialFX. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package app;
+
+import io.github.palexdev.mfxeffects.enums.ElevationLevel;
+import javafx.application.Application;
+import javafx.geometry.Pos;
+import javafx.scene.Scene;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.Region;
+import javafx.stage.Stage;
+
+public class ShadowsTest extends Application {
+
+	@Override
+	public void start(Stage primaryStage) {
+		HBox box = new HBox(30);
+		box.setAlignment(Pos.CENTER);
+
+		for (ElevationLevel level : ElevationLevel.values()) {
+			box.getChildren().add(createRegion(level));
+		}
+
+		Scene scene = new Scene(box, 800, 800);
+		primaryStage.setScene(scene);
+		primaryStage.show();
+	}
+
+	private Region createRegion(ElevationLevel elevation) {
+		Region r = new Region();
+		r.setPrefSize(100, 50);
+		r.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
+		r.setPickOnBounds(false);
+		r.setEffect(elevation.toShadow());
+		r.setStyle("-fx-background-color: red");
+		return r;
+	}
+}