Skip to content

Commit

Permalink
Fix featurecat#862 (insufficient x-shift in variation tree)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Feb 5, 2021
1 parent ebeb446 commit 1944203
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions src/main/java/featurecat/lizzie/gui/VariationTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class VariationTree {
private BoardHistoryNode curMove;
private Rectangle area;
private Point clickPoint;
private int curMoveLane = 0;

public VariationTree() {
laneUsageList = new ArrayList<Integer>();
Expand Down Expand Up @@ -69,6 +70,8 @@ public Optional<BoardHistoryNode> drawTree(

// At this point, lane contains the lane we should use (the main branch is in lane 0)

if (startNode == curMove) curMoveLane = lane;

BoardHistoryNode cur = startNode;
int curposx = posx + lane * XSPACING;
int dotoffset = DOT_DIAM / 2;
Expand Down Expand Up @@ -156,6 +159,7 @@ public Optional<BoardHistoryNode> drawTree(
if (cur.isEndDummay()) {
continue;
}
if (cur == curMove) curMoveLane = lane;
if (calc) {
if (inNode(curposx + dotoffset, posy + dotoffset)) {
return Optional.of(cur);
Expand Down Expand Up @@ -233,6 +237,16 @@ public Optional<BoardHistoryNode> draw(
if (width <= 0 || height <= 0) {
return Optional.empty(); // we don't have enough space
}
area.setBounds(posx, posy, width, height);

// Get the lane of the current node by a dummy drawing.
if (!calc) {
int DUMMY = Integer.MIN_VALUE / 2;
clickPoint.setLocation(DUMMY, DUMMY);
curMoveLane = 0;
draw(g, posx, posy, width, height, true); // set curMoveLane as a side effect
}
int lane = curMoveLane;

// Use dense tree for saving space if large-subboard
YSPACING = (Lizzie.config.showLargeSubBoard() ? 20 : 30);
Expand All @@ -241,7 +255,6 @@ public Optional<BoardHistoryNode> draw(
int strokeRadius = Lizzie.config.showBorder ? 2 : 0;
if (!calc) {
// Draw background
area.setBounds(posx, posy, width, height);
g.setColor(new Color(0, 0, 0, 60));
g.fillRect(posx, posy, width, height);

Expand Down Expand Up @@ -272,7 +285,6 @@ public Optional<BoardHistoryNode> draw(
node = node.previous().get();
curposy -= YSPACING;
}
int lane = getCurLane(node, curMove, curposy, posy + height, 0, true);
int startx = posx + xoffset;
if (((lane + 1) * XSPACING + xoffset + DOT_DIAM + strokeRadius - width) > 0) {
startx = startx - ((lane + 1) * XSPACING + xoffset + DOT_DIAM + strokeRadius - width);
Expand Down Expand Up @@ -306,37 +318,4 @@ public void onClicked(int x, int y) {
node.ifPresent(n -> Lizzie.board.moveToAnyPosition(n));
}
}

private int getCurLane(
BoardHistoryNode start,
BoardHistoryNode curMove,
int curposy,
int maxy,
int laneCount,
boolean isMain) {
BoardHistoryNode next = start;
int nexty = curposy;
while (next.next().isPresent() && nexty + YSPACING < maxy) {
nexty += YSPACING;
next = next.next().get();
}
while (next.previous().isPresent() && (isMain || next != start)) {
next = next.previous().get();
for (int i = 1; i < next.numberOfChildren(); i++) {
laneCount++;
if (next.findIndexOfNode(curMove, true) == i) {
return laneCount;
}
Optional<BoardHistoryNode> variation = next.getVariation(i);
if (variation.isPresent()) {
int subLane = getCurLane(variation.get(), curMove, nexty, maxy, laneCount, false);
if (subLane > 0) {
return subLane;
}
}
}
nexty -= YSPACING;
}
return 0;
}
}

0 comments on commit 1944203

Please sign in to comment.