From 4f8453f16708128b983e1d5ca9e035de3e885dfc Mon Sep 17 00:00:00 2001 From: weisj Date: Fri, 1 May 2020 19:01:35 +0200 Subject: [PATCH] Generalised concept of derivable icons. Added arbitrary rotations for rotatable icon. --- .../weisj/darklaf/icons/DarkSVGIcon.java | 3 +- .../weisj/darklaf/icons/DerivableIcon.java | 39 ++++++++++++++++++ .../weisj/darklaf/icons/IconLoader.java | 4 +- .../weisj/darklaf/icons/RotatableIcon.java | 40 +++++-------------- 4 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableIcon.java diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java index 1a921581..f8114679 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java @@ -41,7 +41,7 @@ import com.kitfox.svg.app.beans.SVGIcon; * @author Jannis Weis * @since 2019 */ -public class DarkSVGIcon implements Icon, Serializable { +public class DarkSVGIcon implements DerivableIcon, Serializable { private static final Logger LOGGER = LogUtil.getLogger(DarkSVGIcon.class); private final Dimension size; @@ -72,6 +72,7 @@ public class DarkSVGIcon implements Icon, Serializable { this.loaded = icon.loaded; } + @Override public DarkSVGIcon derive(final int width, final int height) { return new DarkSVGIcon(width, height, this); } diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableIcon.java new file mode 100644 index 00000000..6d9e39a0 --- /dev/null +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableIcon.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ +package com.github.weisj.darklaf.icons; + +import javax.swing.*; + +public interface DerivableIcon extends Icon { + + /** + * Derive a new icon with the specified size. + * + * @param width the new width + * @param height the new height. + * @return the derived icon. + */ + T derive(final int width, final int height); +} diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java index 6fa7f390..2355fcac 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java @@ -214,10 +214,10 @@ public final class IconLoader { key.w = -1; // Enable wild card search. Find any icon that matches path. if (iconMap.containsKey(key)) { Icon icon = iconMap.get(key); - if (icon instanceof DarkSVGIcon) { + if (icon instanceof DerivableIcon) { // If the desired icon is an DarkSVGIcon we can create a view that shares the underlying svg with // the existing icon. - Icon derived = ((DarkSVGIcon) icon).derive(w, h); + Icon derived = ((DerivableIcon) icon).derive(w, h); key.w = w; iconMap.put(key, derived); return derived; diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java index 7d98907b..d4d68fe9 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java @@ -35,6 +35,7 @@ public class RotatableIcon implements Icon { private Icon icon; private Alignment alignment; + private double angle; public RotatableIcon() { this(null); @@ -42,7 +43,6 @@ public class RotatableIcon implements Icon { public RotatableIcon(final Icon icon) { setIcon(icon); - setOrientation(null); } public void setIcon(final Icon icon) { @@ -62,36 +62,8 @@ public class RotatableIcon implements Icon { } } - private double getAngle() { - double angle = 0.0; - switch (alignment) { - case NORTH : - case CENTER : - angle = 0.0; - break; - case SOUTH : - angle = 180.0; - break; - case EAST : - angle = 90.0; - break; - case WEST : - angle = 270.0; - break; - case NORTH_EAST : - angle = 45.0; - break; - case NORTH_WEST : - angle = 315.0; - break; - case SOUTH_EAST : - angle = 135.0; - break; - case SOUTH_WEST : - angle = 225.0; - break; - } - return Math.toRadians(angle); + public double getAngle() { + return angle; } @Override @@ -110,5 +82,11 @@ public class RotatableIcon implements Icon { public void setOrientation(final Alignment alignment) { this.alignment = alignment != null ? alignment : Alignment.NORTH; + this.angle = this.alignment.getAngle(); + } + + public void setRotation(final double angle) { + this.alignment = null; + this.angle = angle; } }