Browse Source

Generalised concept of derivable icons.

Added arbitrary rotations for rotatable icon.
pull/170/head
weisj 5 years ago
parent
commit
4f8453f167
  1. 3
      property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java
  2. 39
      property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableIcon.java
  3. 4
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java
  4. 40
      property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java

3
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 * @author Jannis Weis
* @since 2019 * @since 2019
*/ */
public class DarkSVGIcon implements Icon, Serializable { public class DarkSVGIcon implements DerivableIcon<DarkSVGIcon>, Serializable {
private static final Logger LOGGER = LogUtil.getLogger(DarkSVGIcon.class); private static final Logger LOGGER = LogUtil.getLogger(DarkSVGIcon.class);
private final Dimension size; private final Dimension size;
@ -72,6 +72,7 @@ public class DarkSVGIcon implements Icon, Serializable {
this.loaded = icon.loaded; this.loaded = icon.loaded;
} }
@Override
public DarkSVGIcon derive(final int width, final int height) { public DarkSVGIcon derive(final int width, final int height) {
return new DarkSVGIcon(width, height, this); return new DarkSVGIcon(width, height, this);
} }

39
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<T extends Icon> 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);
}

4
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. key.w = -1; // Enable wild card search. Find any icon that matches path.
if (iconMap.containsKey(key)) { if (iconMap.containsKey(key)) {
Icon icon = iconMap.get(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 // If the desired icon is an DarkSVGIcon we can create a view that shares the underlying svg with
// the existing icon. // the existing icon.
Icon derived = ((DarkSVGIcon) icon).derive(w, h); Icon derived = ((DerivableIcon<Icon>) icon).derive(w, h);
key.w = w; key.w = w;
iconMap.put(key, derived); iconMap.put(key, derived);
return derived; return derived;

40
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 Icon icon;
private Alignment alignment; private Alignment alignment;
private double angle;
public RotatableIcon() { public RotatableIcon() {
this(null); this(null);
@ -42,7 +43,6 @@ public class RotatableIcon implements Icon {
public RotatableIcon(final Icon icon) { public RotatableIcon(final Icon icon) {
setIcon(icon); setIcon(icon);
setOrientation(null);
} }
public void setIcon(final Icon icon) { public void setIcon(final Icon icon) {
@ -62,36 +62,8 @@ public class RotatableIcon implements Icon {
} }
} }
private double getAngle() { public double getAngle() {
double angle = 0.0; return angle;
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);
} }
@Override @Override
@ -110,5 +82,11 @@ public class RotatableIcon implements Icon {
public void setOrientation(final Alignment alignment) { public void setOrientation(final Alignment alignment) {
this.alignment = alignment != null ? alignment : Alignment.NORTH; this.alignment = alignment != null ? alignment : Alignment.NORTH;
this.angle = this.alignment.getAngle();
}
public void setRotation(final double angle) {
this.alignment = null;
this.angle = angle;
} }
} }

Loading…
Cancel
Save