Skip to content

Commit 5a4099e

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 5a4099e

File tree

5 files changed

+520
-303
lines changed

5 files changed

+520
-303
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
// Display display = new Display();
48+
Monitor [] monitors = new Monitor [] {getMonitorLeft200(), getMonitorRight100()};
49+
getMonitorConsumer = () -> monitors;
50+
multiZoomCoordinateSystemMapper = new MultiZoomCoordinateSystemMapper(null, getMonitorConsumer);
51+
}
52+
53+
@Test
54+
void translatePointInNoMonitorBackAndForthShouldBeTheSame() {
55+
Point pt = new Point(5000, -400);
56+
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
57+
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
58+
}
59+
60+
@Test
61+
@Disabled
62+
void translatePointInGapBackAndForthShouldBeTheSame() {
63+
Point pt = new Point(1900, 400);
64+
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
65+
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
66+
}
67+
68+
@Test
69+
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame() {
70+
Rectangle rectInPts = new Rectangle(5000, -400, 200, 200);
71+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
72+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
73+
}
74+
75+
@Test
76+
@Disabled
77+
void translateRectangleInGapBackAndForthShouldBeTheSame() {
78+
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
79+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
80+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
81+
}
82+
83+
@Test
84+
@Disabled
85+
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame() {
86+
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
87+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
88+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
89+
}
90+
91+
@Test
92+
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame() {
93+
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
94+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
95+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
96+
}
97+
98+
@Test
99+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame() {
100+
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
101+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
102+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
103+
}
104+
105+
@Test
106+
@Disabled
107+
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
108+
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
109+
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, 0);
110+
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width/2) - 200;
111+
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height/2) - 200;
112+
expectedSmallRectInPxs.width = 400;
113+
expectedSmallRectInPxs.height = 400;
114+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
115+
Rectangle smallRectInPts = new Rectangle(0, 0, 0, 0);
116+
smallRectInPts.x = rectInPts.x + (rectInPts.width/2) - 200;
117+
smallRectInPts.y = rectInPts.y + (rectInPts.height/2) - 200;
118+
smallRectInPts.width = 400;
119+
smallRectInPts.height = 400;
120+
Rectangle smallRectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(smallRectInPts, 0);
121+
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
122+
}
123+
124+
@Test
125+
@Disabled
126+
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame() {
127+
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
128+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
129+
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
130+
}
131+
132+
@Test
133+
void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame() {
134+
Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500);
135+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
136+
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
137+
}
138+
139+
}
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)