|
| 1 | +package de.doubleslash.keeptime.common; |
| 2 | + |
| 3 | +import javafx.stage.Screen; |
| 4 | + |
| 5 | +public class ScreenPosHelper { |
| 6 | + |
| 7 | + private int screenhash; |
| 8 | + private double absolutX; |
| 9 | + private double absolutY; |
| 10 | + private double xProportion; |
| 11 | + private double yProportion; |
| 12 | + |
| 13 | + public ScreenPosHelper(final double absolutX, final double absolutY) { |
| 14 | + this.absolutX = absolutX; |
| 15 | + this.absolutY = absolutY; |
| 16 | + calcRelativScreenpos(); |
| 17 | + } |
| 18 | + |
| 19 | + public ScreenPosHelper(final int screenhash, final double xProportion, final double yProportion) { |
| 20 | + this.screenhash = screenhash; |
| 21 | + this.xProportion = xProportion; |
| 22 | + this.yProportion = yProportion; |
| 23 | + calcAbsolutScreenpos(); |
| 24 | + } |
| 25 | + |
| 26 | + public double getAbsolutX() { |
| 27 | + return absolutX; |
| 28 | + } |
| 29 | + |
| 30 | + public void setAbsolutX(final double absolutX) { |
| 31 | + this.absolutX = absolutX; |
| 32 | + calcRelativScreenpos(); |
| 33 | + } |
| 34 | + |
| 35 | + public double getAbsolutY() { |
| 36 | + return absolutY; |
| 37 | + } |
| 38 | + |
| 39 | + public void setAbsolutY(final double absolutY) { |
| 40 | + this.absolutY = absolutY; |
| 41 | + calcRelativScreenpos(); |
| 42 | + } |
| 43 | + |
| 44 | + public int getScreenhash() { |
| 45 | + return screenhash; |
| 46 | + } |
| 47 | + |
| 48 | + public void setScreenhash(final int screenhash) { |
| 49 | + this.screenhash = screenhash; |
| 50 | + calcRelativScreenpos(); |
| 51 | + } |
| 52 | + |
| 53 | + public double getxProportion() { |
| 54 | + return xProportion; |
| 55 | + } |
| 56 | + |
| 57 | + public void setxProportion(final double xProportion) { |
| 58 | + this.xProportion = xProportion; |
| 59 | + calcAbsolutScreenpos(); |
| 60 | + } |
| 61 | + |
| 62 | + public double getyProportion() { |
| 63 | + return yProportion; |
| 64 | + } |
| 65 | + |
| 66 | + public void setyProportion(final double yProportion) { |
| 67 | + this.yProportion = yProportion; |
| 68 | + calcAbsolutScreenpos(); |
| 69 | + } |
| 70 | + |
| 71 | + private void calcRelativScreenpos() { |
| 72 | + Screen screen = Screen.getPrimary(); |
| 73 | + for (final Screen s : Screen.getScreens()) { |
| 74 | + if (s.getBounds().getMinX() <= this.absolutX && this.absolutX <= s.getBounds().getMaxX() |
| 75 | + && s.getBounds().getMinY() <= this.absolutY && this.absolutY <= s.getBounds().getMaxY()) { |
| 76 | + screen = s; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + this.screenhash = screen.hashCode(); |
| 81 | + final double xInScreen = absolutX - screen.getBounds().getMinX(); |
| 82 | + this.xProportion = xInScreen / screen.getBounds().getWidth(); |
| 83 | + final double yInScreen = absolutY - screen.getBounds().getMinY(); |
| 84 | + this.yProportion = yInScreen / screen.getBounds().getHeight(); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private void calcAbsolutScreenpos() { |
| 89 | + final Screen screen = getScreenWithHashOrPrimary(this.screenhash); |
| 90 | + this.absolutX = screen.getBounds().getMinX() + (screen.getBounds().getWidth() * this.xProportion); |
| 91 | + this.absolutY = screen.getBounds().getMinY() + (screen.getBounds().getHeight() * this.yProportion); |
| 92 | + } |
| 93 | + |
| 94 | + private Screen getScreenWithHashOrPrimary(final int screenHash) { |
| 95 | + for (final Screen s : Screen.getScreens()) { |
| 96 | + if (s.hashCode() == screenHash) { |
| 97 | + return s; |
| 98 | + } |
| 99 | + } |
| 100 | + return Screen.getPrimary(); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments