Skip to content

Commit

Permalink
Allow control the branch when mouse over (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsalch committed Jul 8, 2019
1 parent b32c6c5 commit 4c74cdc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 10 deletions.
54 changes: 50 additions & 4 deletions src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public void mousePressed(MouseEvent e) {
onClicked(e.getX(), e.getY());
}
} else if (e.getButton() == MouseEvent.BUTTON3) { // right click
Input.undo();
if (Lizzie.frame.isMouseOver) {
Lizzie.frame.addSuggestionAsBranch();
} else {
Input.undo();
}
}
}

Expand Down Expand Up @@ -346,20 +350,38 @@ public void onDoubleClicked(int x, int y) {
}
}

public void clearMoved() {
isReplayVariation = false;
if (Lizzie.frame != null && Lizzie.frame.isMouseOver) {
Lizzie.frame.isMouseOver = false;
boardRenderer.startNormalBoard();
}
}

public void onMouseExited(int x, int y) {
mouseOverCoordinate = outOfBoundCoordinate;
clearMoved();
}

public void onMouseMoved(int x, int y) {
mouseOverCoordinate = outOfBoundCoordinate;
Optional<int[]> coords = boardRenderer.convertScreenToCoordinates(x, y);
coords.filter(c -> !isMouseOver(c[0], c[1])).ifPresent(c -> repaint());
coords
.filter(c -> !isMouseOver(c[0], c[1]))
.ifPresent(
c -> {
clearMoved();
repaint();
});
coords.ifPresent(
c -> {
mouseOverCoordinate = c;
isReplayVariation = false;
if (Lizzie.frame != null) {
Lizzie.frame.isMouseOver = boardRenderer.isShowingBranch();
}
});
if (!coords.isPresent() && boardRenderer.isShowingBranch()) {
clearMoved();
repaint();
}
}
Expand Down Expand Up @@ -390,7 +412,7 @@ private void autosaveMaybe() {
}
}

private void setDisplayedBranchLength(int n) {
public void setDisplayedBranchLength(int n) {
boardRenderer.setDisplayedBranchLength(n);
}

Expand All @@ -408,6 +430,30 @@ public boolean incrementDisplayedBranchLength(int n) {
return boardRenderer.incrementDisplayedBranchLength(n);
}

public void doBranch(int moveTo) {
if (moveTo > 0) {
if (boardRenderer.isShowingNormalBoard()) {
setDisplayedBranchLength(2);
} else {
if (boardRenderer.getReplayBranch() > boardRenderer.getDisplayedBranchLength()) {
boardRenderer.incrementDisplayedBranchLength(1);
}
}
} else {
if (boardRenderer.isShowingNormalBoard()) {
setDisplayedBranchLength(boardRenderer.getReplayBranch());
} else {
if (boardRenderer.getDisplayedBranchLength() > 1) {
boardRenderer.incrementDisplayedBranchLength(-1);
}
}
}
}

public void addSuggestionAsBranch() {
boardRenderer.addSuggestionAsBranch();
}

public void copySgf() {
try {
// Get sgf content from game
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private void drawBranch() {
gShadow.dispose();
}

private Optional<MoveData> mouseOveredMove() {
public Optional<MoveData> mouseOveredMove() {
return bestMoves
.stream()
.filter(
Expand Down Expand Up @@ -1449,10 +1449,14 @@ private static int calculateSquareHeight(int availableHeight) {
return availableHeight / (Board.boardHeight - 1);
}

private boolean isShowingRawBoard() {
public boolean isShowingRawBoard() {
return (displayedBranchLength == SHOW_RAW_BOARD || displayedBranchLength == 0);
}

public boolean isShowingNormalBoard() {
return displayedBranchLength == SHOW_NORMAL_BOARD;
}

private int maxBranchMoves() {
switch (displayedBranchLength) {
case SHOW_NORMAL_BOARD:
Expand All @@ -1468,6 +1472,10 @@ public boolean isShowingBranch() {
return showingBranch;
}

public void startNormalBoard() {
setDisplayedBranchLength(SHOW_NORMAL_BOARD);
}

public void setDisplayedBranchLength(int n) {
displayedBranchLength = n;
}
Expand All @@ -1480,6 +1488,38 @@ public int getReplayBranch() {
return mouseOveredMove().isPresent() ? mouseOveredMove().get().variation.size() : 0;
}

public void addSuggestionAsBranch() {
mouseOveredMove()
.ifPresent(
m -> {
if (m.variation.size() > 0) {
if (Lizzie.board.getHistory().getCurrentHistoryNode().numberOfChildren() == 0) {
Stone color =
Lizzie.board.getHistory().getLastMoveColor() == Stone.WHITE
? Stone.BLACK
: Stone.WHITE;
Lizzie.board.getHistory().pass(color, false, true);
Lizzie.board.getHistory().previous();
}
for (int i = 0; i < m.variation.size(); i++) {
Stone color =
Lizzie.board.getHistory().getLastMoveColor() == Stone.WHITE
? Stone.BLACK
: Stone.WHITE;
Optional<int[]> coordOpt = Board.asCoordinates(m.variation.get(i));
if (!coordOpt.isPresent()
|| !Board.isValid(coordOpt.get()[0], coordOpt.get()[1])) {
break;
}
int[] coord = coordOpt.get();
Lizzie.board.getHistory().place(coord[0], coord[1], color, i == 0);
}
Lizzie.board.getHistory().toBranchTop();
Lizzie.frame.refresh(2);
}
});
}

public boolean incrementDisplayedBranchLength(int n) {
switch (displayedBranchLength) {
case SHOW_NORMAL_BOARD:
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ public void keyPressed(KeyEvent e) {
} else if (controlIsPressed(e)) {
undo(10);
} else {
undo();
if (Lizzie.frame.isMouseOver) {
Lizzie.frame.doBranch(-1);
} else {
undo();
}
}
break;

Expand All @@ -230,7 +234,11 @@ public void keyPressed(KeyEvent e) {
} else if (controlIsPressed(e)) {
redo(10);
} else {
redo();
if (Lizzie.frame.isMouseOver) {
Lizzie.frame.doBranch(1);
} else {
redo();
}
}
break;

Expand Down Expand Up @@ -531,9 +539,17 @@ public void mouseWheelMoved(MouseWheelEvent e) {
wheelWhen = e.getWhen();
if (Lizzie.board.inAnalysisMode()) Lizzie.board.toggleAnalysis();
if (e.getWheelRotation() > 0) {
redo();
if (Lizzie.frame.isMouseOver) {
Lizzie.frame.doBranch(1);
} else {
redo();
}
} else if (e.getWheelRotation() < 0) {
undo();
if (Lizzie.frame.isMouseOver) {
Lizzie.frame.doBranch(-1);
} else {
undo();
}
}
Lizzie.frame.refresh();
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/featurecat/lizzie/gui/LizzieMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,15 @@ public boolean incrementDisplayedBranchLength(int n) {
return boardPane.incrementDisplayedBranchLength(n);
}

@Override
public void doBranch(int moveTo) {
boardPane.doBranch(moveTo);
}

public void addSuggestionAsBranch() {
boardPane.addSuggestionAsBranch();
}

@Override
public void increaseMaxAlpha(int k) {
boardPane.increaseMaxAlpha(k);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/featurecat/lizzie/gui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class MainFrame extends JFrame {
protected String visitsString = "";
// Force refresh board
private boolean forceRefresh;
public boolean isMouseOver = false;

public MainFrame() throws HeadlessException {
super(DEFAULT_TITLE);
Expand Down Expand Up @@ -123,6 +124,10 @@ public boolean processCommentMouseWheelMoved(MouseWheelEvent e) {

public abstract boolean incrementDisplayedBranchLength(int n);

public void doBranch(int moveTo) {}

public void addSuggestionAsBranch() {}

public abstract void increaseMaxAlpha(int k);

public abstract void copySgf();
Expand Down

0 comments on commit 4c74cdc

Please sign in to comment.