X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2FSWTDPIUtil.java;h=0fe51a9f2688b0b4957e6b433c8efd9b7bfe0793;hb=b7250834202635a09dd18e25370ed9b4c32b1380;hp=962276432b223fa1138ef3f2a9f6628f02b6ecad;hpb=181013801c831415efe3cfe346546b5ab62c3214;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java index 962276432..0fe51a9f2 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java @@ -105,17 +105,42 @@ public class SWTDPIUtil { return target; } + private static double downscaleSwt0(double x) { + return hasSwtScale ? x * fromSwtInternalScalingFactorD : x; + } + + private static int downscaleToIntegerSwt0(double x) { + return (int)(hasSwtScale ? Math.round((double) x * fromSwtInternalScalingFactorD) : x); + } + + private static int downscaleSwt0(int x) { + return hasSwtScale ? (int) Math.round((double) x * fromSwtInternalScalingFactorD) : x; + } + + private static double upscaleSwt0(double x) { + return hasSwtScale ? x * toSwtInternalScalingFactorD : x; + } + + private static int upscaleToIntegerSwt0(double x) { + return (int)(hasSwtScale ? Math.round((double) x * toSwtInternalScalingFactorD) : x); + } + + private static int upscaleSwt0(int x) { + return hasSwtScale ? (int) Math.round((double) x * toSwtInternalScalingFactorD) : x; + } + // SWT API Coordinates <-> pixels + // Downscaling + public static double downscaleSwt(double x) { initialize(); - return hasSwtScale ? x * fromSwtInternalScalingFactorD : x; + return downscaleSwt0(x); } public static int downscaleSwt(int x) { - if (!hasSwtScale) - return x; - return (int) Math.round(downscaleSwt((double) x)); + initialize(); + return downscaleSwt0(x); } public static Point2D downscaleSwt(double x, double y) { @@ -127,7 +152,8 @@ public class SWTDPIUtil { } public static Point downscaleSwt(int x, int y) { - return new Point(downscaleSwt(x), downscaleSwt(y)); + initialize(); + return new Point(downscaleSwt0(x), downscaleSwt0(y)); } public static Point2D downscaleSwt(Point2D p) { @@ -135,9 +161,8 @@ public class SWTDPIUtil { } public static Point downscaleSwtToInteger(Point2D p) { - return new Point( - (int) Math.round(downscaleSwt(p.getX())), - (int) Math.round(downscaleSwt(p.getY()))); + initialize(); + return new Point(downscaleToIntegerSwt0(p.getX()), downscaleToIntegerSwt0(p.getY())); } public static Rectangle2D downscaleSwt(Rectangle2D r, Rectangle2D target) { @@ -159,26 +184,28 @@ public class SWTDPIUtil { } public static Rectangle downscaleSwtToInteger(Rectangle2D r) { + initialize(); return new Rectangle( - (int) Math.round(downscaleSwt(r.getMinX())), - (int) Math.round(downscaleSwt(r.getMinY())), - (int) Math.round(downscaleSwt(r.getWidth())), - (int) Math.round(downscaleSwt(r.getHeight()))); + downscaleToIntegerSwt0(r.getMinX()), + downscaleToIntegerSwt0(r.getMinY()), + downscaleToIntegerSwt0(r.getWidth()), + downscaleToIntegerSwt0(r.getHeight())); } + // Upscaling + public static double upscaleSwt(double x) { initialize(); - return hasSwtScale ? x * toSwtInternalScalingFactorD : x; + return upscaleSwt0(x); } public static int upscaleSwt(int x) { initialize(); - if (!hasSwtScale) - return x; - return (int) Math.round((double) x * toSwtInternalScalingFactorD); + return upscaleSwt0(x); } public static Point2D upscaleSwt(double x, double y) { + initialize(); if (!hasSwtScale) return new Point2D.Double(x, y); double s = toSwtInternalScalingFactorD; @@ -186,21 +213,23 @@ public class SWTDPIUtil { } public static Point upscaleSwt(int x, int y) { - return new Point(upscaleSwt(x), upscaleSwt(y)); + initialize(); + return new Point(upscaleSwt0(x), upscaleSwt0(y)); } public static Point2D upscaleSwt(Point2D p) { - return upscaleSwt(p.getX(), p.getY()); + initialize(); + return (hasSwtScale && p != null) ? upscaleSwt(p.getX(), p.getY()) : p; } public static Point upscaleSwtToInteger(Point2D p) { - return new Point( - (int) Math.round(upscaleSwt(p.getX())), - (int) Math.round(upscaleSwt(p.getY()))); + initialize(); + return new Point(upscaleToIntegerSwt0(p.getX()), upscaleToIntegerSwt0(p.getY())); } public static Point upscaleSwt(Point p) { - return upscaleSwt(p.x, p.y); + initialize(); + return (hasSwtScale && p != null) ? upscaleSwt(p.x, p.y) : p; } public static Rectangle2D upscaleSwt(Rectangle2D r, Rectangle2D target) { @@ -223,10 +252,10 @@ public class SWTDPIUtil { public static Rectangle upscaleSwtToInteger(Rectangle2D r) { return new Rectangle( - (int) Math.round(upscaleSwt(r.getMinX())), - (int) Math.round(upscaleSwt(r.getMinY())), - (int) Math.round(upscaleSwt(r.getWidth())), - (int) Math.round(upscaleSwt(r.getHeight()))); + upscaleToIntegerSwt0(r.getMinX()), + upscaleToIntegerSwt0(r.getMinY()), + upscaleToIntegerSwt0(r.getWidth()), + upscaleToIntegerSwt0(r.getHeight())); } }