Skip to content

Commit 5742a52

Browse files
committed
Geometry With Float on Scaling #62
This commit introduces FloatAwareRectangle and FloatAwarePoint as an extension of Rectangle and Point respectively to improve scaling precision in the DPIUtil by using residuals obtained from rounding. Contributes to #62 and #128
1 parent e40ad29 commit 5742a52

File tree

9 files changed

+273
-173
lines changed

9 files changed

+273
-173
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS
137137
void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() {
138138
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
139139
setupMonitors(mapper);
140-
Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]);
140+
Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]);
141141
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
142142
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
143143
}
@@ -223,15 +223,15 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M
223223
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
224224
return new Point(x, y);
225225
} else {
226-
return new MonitorAwarePoint(x, y, monitor);
226+
return new Point.WithMonitor(x, y, monitor);
227227
}
228228
}
229229

230230
private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) {
231231
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
232232
return new Rectangle(x, y, width, height);
233233
} else {
234-
return new MonitorAwareRectangle(x, y, width, height, monitor);
234+
return new Rectangle.WithMonitor(x, y, width, height, monitor);
235235
}
236236
}
237237

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.io.*;
1818

19+
import org.eclipse.swt.widgets.*;
20+
1921
/**
2022
* Instances of this class represent places on the (x, y)
2123
* coordinate plane.
@@ -41,7 +43,7 @@
4143
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4244
*/
4345

44-
public sealed class Point implements Serializable permits MonitorAwarePoint {
46+
public sealed class Point implements Serializable permits Point.OfFloat {
4547

4648
/**
4749
* the x coordinate of the point
@@ -66,6 +68,8 @@ public Point (int x, int y) {
6668
this.y = y;
6769
}
6870

71+
private Point() {}
72+
6973
/**
7074
* Compares the argument to the receiver, and returns true
7175
* if they represent the <em>same</em> object using a class
@@ -116,5 +120,85 @@ public String toString () {
116120
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117121
}
118122

123+
/**
124+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
125+
* objects with the fields capable of storing more precise value in float.
126+
*
127+
* @since 3.130
128+
* @noreference This class is not intended to be referenced by clients
129+
*/
130+
public static sealed class OfFloat extends Point permits Point.WithMonitor {
131+
132+
private static final long serialVersionUID = -1862062276431597053L;
133+
134+
public float residualX, residualY;
135+
136+
public OfFloat(int x, int y) {
137+
super(x, y);
138+
}
139+
140+
public OfFloat(float x, float y) {
141+
super();
142+
this.x = Math.round(x);
143+
this.y = Math.round(y);
144+
this.residualX = this.x - x;
145+
this.residualY = this.y - y;
146+
}
147+
148+
public float getX() {
149+
return x + residualX;
150+
}
151+
152+
public float getY() {
153+
return y + residualY;
154+
}
155+
156+
public void setX(float x) {
157+
this.x = Math.round(x);
158+
this.residualX = this.x - x;
159+
}
160+
161+
public void setY(float y) {
162+
this.y = Math.round(y);
163+
this.residualY = this.y - y;
164+
}
165+
}
166+
167+
/**
168+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point.OfFloat}
169+
* objects along with the context of the monitor in relation to which they are
170+
* placed on the display. The monitor awareness makes it easy to scale and
171+
* translate the points between pixels and points.
172+
*
173+
* @since 3.130
174+
* @noreference This class is not intended to be referenced by clients
175+
*/
176+
public static final class WithMonitor extends Point.OfFloat {
177+
178+
private static final long serialVersionUID = 6077427420686999194L;
179+
180+
private final Monitor monitor;
181+
182+
/**
183+
* Constructs a new Point.WithMonitor
184+
*
185+
* @param x the x coordinate of the point
186+
* @param y the y coordinate of the point
187+
* @param monitor the monitor with whose context the point is created
188+
*/
189+
public WithMonitor(int x, int y, Monitor monitor) {
190+
super(x, y);
191+
this.monitor = monitor;
192+
}
193+
194+
/**
195+
* {@return the monitor with whose context the instance is created}
196+
*/
197+
public Monitor getMonitor() {
198+
return monitor;
199+
}
200+
201+
}
202+
119203
}
120204

0 commit comments

Comments
 (0)