Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z order violation #179

Merged
merged 4 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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