Skip to content

Commit 83c3ee7

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 83c3ee7

File tree

5 files changed

+568
-303
lines changed

5 files changed

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

0 commit comments

Comments
 (0)