Skip to content

Commit

Permalink
Merge pull request #179 from Jaggeroth/ZOrderViolation
Browse files Browse the repository at this point in the history
Z order violation fix
  • Loading branch information
Azhrei authored Mar 20, 2018
2 parents fd98e91 + 074e033 commit bdb0527
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,13 @@ public void renderZone(Graphics2D g2d, PlayerView view) {
}

// if there is fog or vision we may need to re-render figure type tokens
// and figure tokens need sorting via alternative logic.
List<Token> tokens = zone.getFigureTokens();
List<Token> sortedTokens = new ArrayList<Token>(tokens);
Collections.sort(sortedTokens, zone.getFigureZOrderComparator());
if (!tokens.isEmpty()) {
timer.start("tokens - figures");
renderTokens(g2d, tokens, view, true);
renderTokens(g2d, sortedTokens, view, true);
timer.stop("tokens - figures");
}

Expand Down
43 changes: 22 additions & 21 deletions maptool/src/main/java/net/rptools/maptool/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ public void putToken(Token token) {
// LATER: optimize this
tokenOrderedList.remove(token);
tokenOrderedList.add(token);
Collections.sort(tokenOrderedList, getZOrderComparator());
Collections.sort(tokenOrderedList, TOKEN_Z_ORDER_COMPARATOR);

if (newToken) {
fireModelChangeEvent(new ModelChangeEvent(this, Event.TOKEN_ADDED, token));
Expand Down Expand Up @@ -1151,7 +1151,7 @@ public void putTokens(List<Token> tokens) {
}
tokenOrderedList.removeAll(tokens);
tokenOrderedList.addAll(tokens);
Collections.sort(tokenOrderedList, getZOrderComparator());
Collections.sort(tokenOrderedList, TOKEN_Z_ORDER_COMPARATOR);

if (!addedTokens.isEmpty())
fireModelChangeEvent(new ModelChangeEvent(this, Event.TOKEN_ADDED, addedTokens));
Expand Down Expand Up @@ -1536,7 +1536,6 @@ public static interface Filter {
public boolean matchToken(Token t);
}

@Deprecated
public static final Comparator<Token> TOKEN_Z_ORDER_COMPARATOR = new TokenZOrderComparator();

public static class TokenZOrderComparator implements Comparator<Token> {
Expand All @@ -1554,30 +1553,32 @@ public int compare(Token o1, Token o2) {
}

/**
* Need to replace static TOKEN_Z_ORDER_COMPARATOR comparator with instantiated version so that grid is available
* and can access token footprint
* Replaces the static TOKEN_Z_ORDER_COMPARATOR comparator with instantiated version so that grid is available
* and can access token footprint. Only used when Rendering just Figure Tokens.
**/
public Comparator<Token> getZOrderComparator() {
public Comparator<Token> getFigureZOrderComparator() {
return new Comparator<Token>() {
@Override
public int compare(Token o1, Token o2) {
/**
* If either token is a figure, get the footprint and find the lowest point but if the same,
* return the smallest, else use normal z order
* It is an assumption of this comparator that all tokens are being sorted using isometric logic.
*
* Get the footprint (dependent on Grid) and find the centre of the footprint.
* If the same, place tokens above objects.
* Otherwise use height to place the smallest in front.
* If still equal use normal z order.
*/
if (o1.getShape() == Token.TokenShape.FIGURE || o2.getShape() == Token.TokenShape.FIGURE) {
int v1 = getFigureZOrder(o1);
int v2 = getFigureZOrder(o2);
if ((v1 - v2) != 0)
return v1 - v2;
if (o1.isStamp() && o2.isToken())
return -1;
if (o2.isStamp() && o1.isToken())
return +1;
if (o1.getHeight() != o2.getHeight()) {
// Larger tokens at the same position, go behind
return o2.getHeight() - o1.getHeight();
}
int v1 = getFigureZOrder(o1);
int v2 = getFigureZOrder(o2);
if ((v1 - v2) != 0)
return v1 - v2;
if (!o1.isToken() && o2.isToken())
return -1;
if (!o2.isToken() && o1.isToken())
return +1;
if (o1.getHeight() != o2.getHeight()) {
// Larger tokens at the same position, go behind
return o2.getHeight() - o1.getHeight();
}
int lval = o1.getZOrder();
int rval = o2.getZOrder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void bringTokensToFront(GUID zoneGUID, Set<GUID> tokenSet) {
}
}
// Arrange
Collections.sort(tokenList, zone.getZOrderComparator());
Collections.sort(tokenList, Zone.TOKEN_Z_ORDER_COMPARATOR);

// Update
int z = zone.getLargestZOrder() + 1;
Expand Down Expand Up @@ -487,7 +487,7 @@ public void sendTokensToBack(GUID zoneGUID, Set<GUID> tokenSet) {
}
}
// Arrange
Collections.sort(tokenList, zone.getZOrderComparator());
Collections.sort(tokenList, Zone.TOKEN_Z_ORDER_COMPARATOR);

// Update
int z = zone.getSmallestZOrder() - 1;
Expand Down

0 comments on commit bdb0527

Please sign in to comment.