Skip to content

Commit 13df60f

Browse files
committed
Refactored CoordinateSystemMapper and added tests
This commit refactors the Display to separate single zoom and multi zoom coordinate system mappers in separate classes. Additionally, it adds some tests for validating the multi zoom coordinate system mapper. A few tests are disabled because of the current state of multi zoom coordinate system limitations. contributes to #62 and #127
1 parent d7febc4 commit 13df60f

File tree

5 files changed

+516
-303
lines changed

5 files changed

+516
-303
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.eclipse.swt.widgets;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.function.*;
6+
7+
import org.eclipse.swt.graphics.*;
8+
import org.junit.jupiter.api.*;
9+
10+
public class CoordinateSystemMapperTests {
11+
12+
Supplier<Monitor[]> getMonitorConsumer;
13+
MultiZoomCoordinateSystemMapper multiZoomCoordinateSystemMapper;
14+
15+
private Monitor getMonitorLeft200() {
16+
Monitor monitor = new Monitor();
17+
monitor.x = 0;
18+
monitor.y = 0;
19+
monitor.width = 1000;
20+
monitor.height = 1000;
21+
monitor.clientHeight = 1000;
22+
monitor.clientWidth = 1000;
23+
monitor.clientX = 0;
24+
monitor.clientY = 0;
25+
monitor.zoom = 200;
26+
monitor.handle = 1;
27+
return monitor;
28+
}
29+
30+
private Monitor getMonitorRight100() {
31+
Monitor monitor = new Monitor();
32+
monitor.x = 2000;
33+
monitor.y = 0;
34+
monitor.width = 2000;
35+
monitor.height = 2000;
36+
monitor.clientHeight = 2000;
37+
monitor.clientWidth = 2000;
38+
monitor.clientX = 2000;
39+
monitor.clientY = 0;
40+
monitor.zoom = 100;
41+
monitor.handle = 2;
42+
return monitor;
43+
}
44+
45+
@BeforeEach
46+
void setup() {
47+
Monitor [] monitors = new Monitor [] {getMonitorLeft200(), getMonitorRight100()};
48+
getMonitorConsumer = () -> monitors;
49+
multiZoomCoordinateSystemMapper = new MultiZoomCoordinateSystemMapper(null, getMonitorConsumer);
50+
}
51+
52+
@Test
53+
void translatePointInNoMonitorBackAndForthShouldBeTheSame() {
54+
Point pt = new Point(5000, -400);
55+
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
56+
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
57+
}
58+
59+
@Test
60+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
61+
void translatePointInGapBackAndForthShouldBeTheSame() {
62+
Point pt = new Point(1900, 400);
63+
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
64+
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
65+
}
66+
67+
@Test
68+
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame() {
69+
Rectangle rectInPts = new Rectangle(5000, -400, 200, 200);
70+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
71+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
72+
}
73+
74+
@Test
75+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
76+
void translateRectangleInGapBackAndForthShouldBeTheSame() {
77+
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
78+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
79+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
80+
}
81+
82+
@Test
83+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
84+
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame() {
85+
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
86+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
87+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
88+
}
89+
90+
@Test
91+
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame() {
92+
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
93+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
94+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
95+
}
96+
97+
@Test
98+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame() {
99+
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
100+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
101+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
102+
}
103+
104+
@Test
105+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
106+
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
107+
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
108+
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, 0);
109+
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width/2) - 200;
110+
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height/2) - 200;
111+
expectedSmallRectInPxs.width = 400;
112+
expectedSmallRectInPxs.height = 400;
113+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
114+
Rectangle smallRectInPts = new Rectangle(0, 0, 0, 0);
115+
smallRectInPts.x = rectInPts.x + (rectInPts.width/2) - 200;
116+
smallRectInPts.y = rectInPts.y + (rectInPts.height/2) - 200;
117+
smallRectInPts.width = 400;
118+
smallRectInPts.height = 400;
119+
Rectangle smallRectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(smallRectInPts, 0);
120+
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
121+
}
122+
123+
@Test
124+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
125+
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame() {
126+
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
127+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
128+
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
129+
}
130+
131+
@Test
132+
void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame() {
133+
Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500);
134+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
135+
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
136+
}
137+
138+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.eclipse.swt.widgets;
2+
3+
import org.eclipse.swt.graphics.*;
4+
5+
abstract class CoordinateSystemMapper {
6+
7+
Display display;
8+
9+
CoordinateSystemMapper(Display display) {
10+
super();
11+
this.display = display;
12+
}
13+
14+
abstract Rectangle map(Control from, Control to, Rectangle rectangle);
15+
16+
abstract Rectangle map(Control from, Control to, int x, int y, int width, int height);
17+
18+
abstract Point map(Control from, Control to, Point point);
19+
20+
abstract Point map(Control from, Control to, int x, int y);
21+
22+
abstract Rectangle mapMonitorBounds(Rectangle rectangle, int zoom);
23+
24+
abstract Point translateFromDisplayCoordinates(Point point, int zoom);
25+
26+
abstract Point translateToDisplayCoordinates(Point point, int zoom);
27+
28+
abstract Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom);
29+
30+
abstract Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom);
31+
32+
abstract void setCursorLocation(int x, int y);
33+
34+
abstract Point getCursorLocation();
35+
}

0 commit comments

Comments
 (0)