diff --git a/src/main/java/featurecat/lizzie/gui/BoardPane.java b/src/main/java/featurecat/lizzie/gui/BoardPane.java index 0b4fd0582..126ac19bf 100644 --- a/src/main/java/featurecat/lizzie/gui/BoardPane.java +++ b/src/main/java/featurecat/lizzie/gui/BoardPane.java @@ -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(); + } } } @@ -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 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(); } } @@ -390,7 +412,7 @@ private void autosaveMaybe() { } } - private void setDisplayedBranchLength(int n) { + public void setDisplayedBranchLength(int n) { boardRenderer.setDisplayedBranchLength(n); } @@ -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 diff --git a/src/main/java/featurecat/lizzie/gui/BoardRenderer.java b/src/main/java/featurecat/lizzie/gui/BoardRenderer.java index e36d9e286..6fd3ee426 100644 --- a/src/main/java/featurecat/lizzie/gui/BoardRenderer.java +++ b/src/main/java/featurecat/lizzie/gui/BoardRenderer.java @@ -476,7 +476,7 @@ private void drawBranch() { gShadow.dispose(); } - private Optional mouseOveredMove() { + public Optional mouseOveredMove() { return bestMoves .stream() .filter( @@ -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: @@ -1468,6 +1472,10 @@ public boolean isShowingBranch() { return showingBranch; } + public void startNormalBoard() { + setDisplayedBranchLength(SHOW_NORMAL_BOARD); + } + public void setDisplayedBranchLength(int n) { displayedBranchLength = n; } @@ -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 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: diff --git a/src/main/java/featurecat/lizzie/gui/Input.java b/src/main/java/featurecat/lizzie/gui/Input.java index eaa74447c..1817be9b3 100644 --- a/src/main/java/featurecat/lizzie/gui/Input.java +++ b/src/main/java/featurecat/lizzie/gui/Input.java @@ -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; @@ -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; @@ -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(); } diff --git a/src/main/java/featurecat/lizzie/gui/LizzieMain.java b/src/main/java/featurecat/lizzie/gui/LizzieMain.java index 90b9c4d4b..b66551304 100644 --- a/src/main/java/featurecat/lizzie/gui/LizzieMain.java +++ b/src/main/java/featurecat/lizzie/gui/LizzieMain.java @@ -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); diff --git a/src/main/java/featurecat/lizzie/gui/MainFrame.java b/src/main/java/featurecat/lizzie/gui/MainFrame.java index ec227a3d3..6f310c90a 100644 --- a/src/main/java/featurecat/lizzie/gui/MainFrame.java +++ b/src/main/java/featurecat/lizzie/gui/MainFrame.java @@ -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); @@ -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();