Skip to content

Commit ff77ecb

Browse files
committed
Refactored CoordinateSystemMapper and added tests
1 parent 055ec84 commit ff77ecb

File tree

5 files changed

+507
-303
lines changed

5 files changed

+507
-303
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
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+
void translateRectangleInGapBackAndForthShouldBeTheSame() {
76+
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
77+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
78+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
79+
}
80+
81+
@Test
82+
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame() {
83+
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
84+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
85+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
86+
}
87+
88+
@Test
89+
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame() {
90+
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
91+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
92+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
93+
}
94+
95+
@Test
96+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame() {
97+
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
98+
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
99+
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
100+
}
101+
@Test
102+
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
103+
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
104+
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, 0);
105+
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width/2) - 200;
106+
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height/2) - 200;
107+
expectedSmallRectInPxs.width = 400;
108+
expectedSmallRectInPxs.height = 400;
109+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
110+
Rectangle smallRectInPts = new Rectangle(0, 0, 0, 0);
111+
smallRectInPts.x = rectInPts.x + (rectInPts.width/2) - 200;
112+
smallRectInPts.y = rectInPts.y + (rectInPts.height/2) - 200;
113+
smallRectInPts.width = 400;
114+
smallRectInPts.height = 400;
115+
Rectangle smallRectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(smallRectInPts, 0);
116+
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
117+
}
118+
119+
@Test
120+
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame() {
121+
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
122+
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
123+
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
124+
}
125+
126+
}
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)