Skip to content

Commit

Permalink
Improve our alignment to physical pixel boundaries.
Browse files Browse the repository at this point in the history
Previously we aligned elements to integral logical pixel boundaries, and relied
on the ratio between logical and physical pixels to be integral. This is no
longer the case in the wild world of zillions of screen sizes and densities.

Our new approach is to leave logical position/size alone until validation, at
which point we ensure that our boundaries (top, left, bottom, right) fall on
integral physical pixels and then translate those back to an updated logical
position and size which are used when laying out deeper children.
  • Loading branch information
samskivert committed Sep 20, 2018
1 parent a0a9ebc commit 45701dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 17 additions & 1 deletion core/src/main/java/tripleplay/ui/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import react.UnitSlot;
import react.ValueView;

import playn.core.Scale;
import playn.scene.GroupLayer;
import playn.scene.Layer;

Expand Down Expand Up @@ -417,6 +418,21 @@ protected UnitSlot invalidateSlot (final boolean styles) {
*/
protected void validate () {
if (!isSet(Flag.VALID)) {
// prior to laying ourselves out, ensure that our visual boundaries fall on physical
// pixels; this avoids rendering artifacts on devices where the scale factor between
// virtual and physical pixels is non-integral
Root root = root();
if (root != null) {
Scale scale = root.iface.plat.graphics().scale();
float x = layer.tx(), y = layer.ty();
float rx = scale.roundToNearestPixel(x), ry = scale.roundToNearestPixel(y);
float rr = scale.roundToNearestPixel(x + _size.width);
float rb = scale.roundToNearestPixel(y + _size.height);
layer.setTranslation(rx, ry);
_size.setSize(rr-rx, rb-ry);
}

// now that our boundaries are adjusted, we can layout our children (if any)
layout();
set(Flag.VALID, true);
wasValidated();
Expand Down Expand Up @@ -473,7 +489,7 @@ protected IDimension preferredSize (float hintX, float hintY) {
* Configures the location of this element, relative to its parent.
*/
protected void setLocation (float x, float y) {
layer.setTranslation(MathUtil.round(x), MathUtil.round(y));
layer.setTranslation(x, y);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.playn</groupId>
<artifactId>playn-project</artifactId>
<version>2.0.4</version>
<version>2.1-SNAPSHOT</version>
</parent>

<groupId>com.threerings</groupId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<playn.version>2.0.3</playn.version>
<playn.version>2.1-SNAPSHOT</playn.version>
</properties>

<modules>
Expand Down

0 comments on commit 45701dd

Please sign in to comment.