Skip to content

Commit

Permalink
Refactored CoordinateSystemMapper and added tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
amartya4256 committed Jan 13, 2025
1 parent d7febc4 commit 5a4099e
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 303 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package org.eclipse.swt.widgets;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.function.*;

import org.eclipse.swt.graphics.*;
import org.junit.jupiter.api.*;

public class CoordinateSystemMapperTests {

Supplier<Monitor[]> getMonitorConsumer;
MultiZoomCoordinateSystemMapper multiZoomCoordinateSystemMapper;

private Monitor getMonitorLeft200() {
Monitor monitor = new Monitor();
monitor.x = 0;
monitor.y = 0;
monitor.width = 1000;
monitor.height = 1000;
monitor.clientHeight = 1000;
monitor.clientWidth = 1000;
monitor.clientX = 0;
monitor.clientY = 0;
monitor.zoom = 200;
monitor.handle = 1;
return monitor;
}

private Monitor getMonitorRight100() {
Monitor monitor = new Monitor();
monitor.x = 2000;
monitor.y = 0;
monitor.width = 2000;
monitor.height = 2000;
monitor.clientHeight = 2000;
monitor.clientWidth = 2000;
monitor.clientX = 2000;
monitor.clientY = 0;
monitor.zoom = 100;
monitor.handle = 2;
return monitor;
}

@BeforeEach
void setup() {
// Display display = new Display();
Monitor [] monitors = new Monitor [] {getMonitorLeft200(), getMonitorRight100()};
getMonitorConsumer = () -> monitors;
multiZoomCoordinateSystemMapper = new MultiZoomCoordinateSystemMapper(null, getMonitorConsumer);
}

@Test
void translatePointInNoMonitorBackAndForthShouldBeTheSame() {
Point pt = new Point(5000, -400);
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
}

@Test
@Disabled
void translatePointInGapBackAndForthShouldBeTheSame() {
Point pt = new Point(1900, 400);
Point px = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(pt, 0);
assertEquals(pt, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(px, 0));
}

@Test
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame() {
Rectangle rectInPts = new Rectangle(5000, -400, 200, 200);
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
}

@Test
@Disabled
void translateRectangleInGapBackAndForthShouldBeTheSame() {
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
}

@Test
@Disabled
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame() {
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
}

@Test
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame() {
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
}

@Test
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame() {
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
Rectangle rectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0);
assertEquals(rectInPts, multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0));
}

@Test
@Disabled
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, 0);
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width/2) - 200;
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height/2) - 200;
expectedSmallRectInPxs.width = 400;
expectedSmallRectInPxs.height = 400;
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
Rectangle smallRectInPts = new Rectangle(0, 0, 0, 0);
smallRectInPts.x = rectInPts.x + (rectInPts.width/2) - 200;
smallRectInPts.y = rectInPts.y + (rectInPts.height/2) - 200;
smallRectInPts.width = 400;
smallRectInPts.height = 400;
Rectangle smallRectInPxs = multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(smallRectInPts, 0);
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
}

@Test
@Disabled
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame() {
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
}

@Test
void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame() {
Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500);
Rectangle rectInPts = multiZoomCoordinateSystemMapper.translateFromDisplayCoordinates(rectInPxs, 0);
assertEquals(rectInPxs, multiZoomCoordinateSystemMapper.translateToDisplayCoordinates(rectInPts, 0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.eclipse.swt.widgets;

import org.eclipse.swt.graphics.*;

abstract class CoordinateSystemMapper {

Display display;

CoordinateSystemMapper(Display display) {
super();
this.display = display;
}

abstract Rectangle map(Control from, Control to, Rectangle rectangle);

abstract Rectangle map(Control from, Control to, int x, int y, int width, int height);

abstract Point map(Control from, Control to, Point point);

abstract Point map(Control from, Control to, int x, int y);

abstract Rectangle mapMonitorBounds(Rectangle rectangle, int zoom);

abstract Point translateFromDisplayCoordinates(Point point, int zoom);

abstract Point translateToDisplayCoordinates(Point point, int zoom);

abstract Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom);

abstract Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom);

abstract void setCursorLocation(int x, int y);

abstract Point getCursorLocation();
}
Loading

0 comments on commit 5a4099e

Please sign in to comment.