11package de .doubleslash .keeptime .common ;
22
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+
6+ import javafx .geometry .Rectangle2D ;
37import javafx .stage .Screen ;
48
9+ /**
10+ * Helper to handle screen coordinates. Converts between absolute coordinates to screen+proportional coordinates. E.g.
11+ * you have two screens, each with 1000px width. Absolute position gives you 1500px and this class will get you hash for
12+ * screen #2 and proportional value 0.5. <br>
13+ * Reference #38
14+ */
515public class ScreenPosHelper {
616
7- private int screenhash ;
8- private double absolutX ;
9- private double absolutY ;
10- private double xProportion ;
11- private double yProportion ;
17+ private static final Logger LOG = LoggerFactory .getLogger (ScreenPosHelper .class );
1218
13- public ScreenPosHelper (final double absolutX , final double absolutY ) {
14- this .absolutX = absolutX ;
15- this .absolutY = absolutY ;
16- calcRelativScreenpos ();
17- }
19+ private int screenHash ;
1820
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- }
21+ private double absoluteX ;
22+ private double absoluteY ;
2523
26- public double getAbsolutX () {
27- return absolutX ;
28- }
24+ private double proportionalX ;
25+ private double proportionalY ;
2926
30- public void setAbsolutX (final double absolutX ) {
31- this .absolutX = absolutX ;
32- calcRelativScreenpos ();
27+ public ScreenPosHelper (final int screenhash , final double proportionalX , final double proportionalY ) {
28+ this .screenHash = screenhash ;
29+ this .proportionalX = proportionalX ;
30+ this .proportionalY = proportionalY ;
31+ calcAbsolutePosition ();
3332 }
3433
35- public double getAbsolutY () {
36- return absolutY ;
34+ public double getAbsoluteX () {
35+ return absoluteX ;
3736 }
3837
39- public void setAbsolutY (final double absolutY ) {
40- this .absolutY = absolutY ;
41- calcRelativScreenpos ();
38+ public void setAbsoluteX (final double absolutX ) {
39+ this .absoluteX = absolutX ;
40+ calcProportionalPosition ();
4241 }
4342
44- public int getScreenhash () {
45- return screenhash ;
43+ public double getAbsoluteY () {
44+ return absoluteY ;
4645 }
4746
48- public void setScreenhash (final int screenhash ) {
49- this .screenhash = screenhash ;
50- calcRelativScreenpos ();
47+ public void setAbsoluteY (final double absolutY ) {
48+ this .absoluteY = absolutY ;
49+ calcProportionalPosition ();
5150 }
5251
53- public double getxProportion () {
54- return xProportion ;
52+ public int getScreenHash () {
53+ return screenHash ;
5554 }
5655
57- public void setxProportion (final double xProportion ) {
58- this .xProportion = xProportion ;
59- calcAbsolutScreenpos ();
56+ public double getProportionalX () {
57+ return proportionalX ;
6058 }
6159
62- public double getyProportion () {
63- return yProportion ;
60+ public double getProportionalY () {
61+ return proportionalY ;
6462 }
6563
66- public void setyProportion (final double yProportion ) {
67- this .yProportion = yProportion ;
68- calcAbsolutScreenpos ();
64+ public void resetPositionIfInvalid () {
65+ if (!isPositionValid ()) {
66+ final Screen screen = getScreenWithHashOrPrimary (this .screenHash );
67+ final Rectangle2D screenBounds = screen .getBounds ();
68+ LOG .warn (
69+ "Position is not in the range of the screen. Screen (hash '{}') range '{}'. Calculated positions '{}'/'{}'. Setting to '0'/'0'." ,
70+ screen .hashCode (), screenBounds , absoluteX , absoluteY );
71+ proportionalX = 0 ;
72+ proportionalY = 0 ;
73+ absoluteX = 0 ;
74+ absoluteY = 0 ;
75+ }
6976 }
7077
71- private void calcRelativScreenpos () {
78+ private void calcProportionalPosition () {
7279 Screen screen = Screen .getPrimary ();
7380 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 ()) {
81+ if (s .getBounds ().contains (this .absoluteX , this .absoluteY )) {
7682 screen = s ;
7783 break ;
7884 }
7985 }
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 ();
86+ this .screenHash = screen .hashCode ();
87+ final Rectangle2D bounds = screen .getBounds ();
88+ final double xInScreen = absoluteX - bounds .getMinX ();
89+ this .proportionalX = xInScreen / bounds .getWidth ();
90+ final double yInScreen = absoluteY - bounds .getMinY ();
91+ this .proportionalY = yInScreen / bounds .getHeight ();
92+
93+ }
94+
95+ private void calcAbsolutePosition () {
96+ final Screen screen = getScreenWithHashOrPrimary (this .screenHash );
97+ final Rectangle2D screenBounds = screen .getBounds ();
8598
99+ this .absoluteX = screenBounds .getMinX () + (screenBounds .getWidth () * this .proportionalX );
100+ this .absoluteY = screenBounds .getMinY () + (screenBounds .getHeight () * this .proportionalY );
86101 }
87102
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 );
103+ private boolean isPositionValid () {
104+ final Screen screen = getScreenWithHashOrPrimary (this .screenHash );
105+ final Rectangle2D screenBounds = screen .getBounds ();
106+ return screenBounds . contains ( this . absoluteX , this .absoluteY );
92107 }
93108
94109 private Screen getScreenWithHashOrPrimary (final int screenHash ) {
@@ -97,6 +112,7 @@ private Screen getScreenWithHashOrPrimary(final int screenHash) {
97112 return s ;
98113 }
99114 }
115+ LOG .warn ("Could not find wanted screen with hash '{}'. Using primary." , screenHash );
100116 return Screen .getPrimary ();
101117 }
102118
0 commit comments