From 6a99c899a125287cef7b210f68cdf1c10dd4de7b Mon Sep 17 00:00:00 2001 From: zsalch Date: Fri, 5 Jul 2019 14:38:50 +0800 Subject: [PATCH] Allow control the branch when mouse over --- .../java/featurecat/lizzie/gui/BoardPane.java | 53 +++++++++++++++++-- .../featurecat/lizzie/gui/BoardRenderer.java | 44 ++++++++++++++- .../java/featurecat/lizzie/gui/Input.java | 24 +++++++-- .../featurecat/lizzie/gui/LizzieMain.java | 9 ++++ .../java/featurecat/lizzie/gui/MainFrame.java | 5 ++ 5 files changed, 125 insertions(+), 10 deletions(-) diff --git a/src/main/java/featurecat/lizzie/gui/BoardPane.java b/src/main/java/featurecat/lizzie/gui/BoardPane.java index 530afa18c..033fe937d 100644 --- a/src/main/java/featurecat/lizzie/gui/BoardPane.java +++ b/src/main/java/featurecat/lizzie/gui/BoardPane.java @@ -109,7 +109,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(); + } } } }); @@ -342,16 +346,33 @@ 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 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(); } } @@ -382,7 +403,7 @@ private void autosaveMaybe() { } } - private void setDisplayedBranchLength(int n) { + public void setDisplayedBranchLength(int n) { boardRenderer.setDisplayedBranchLength(n); } @@ -400,6 +421,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 8feba25fb..a61606526 100644 --- a/src/main/java/featurecat/lizzie/gui/BoardRenderer.java +++ b/src/main/java/featurecat/lizzie/gui/BoardRenderer.java @@ -475,7 +475,7 @@ private void drawBranch() { gShadow.dispose(); } - private Optional mouseOveredMove() { + public Optional mouseOveredMove() { return bestMoves .stream() .filter( @@ -1448,10 +1448,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: @@ -1467,6 +1471,10 @@ public boolean isShowingBranch() { return showingBranch; } + public void startNormalBoard() { + setDisplayedBranchLength(SHOW_NORMAL_BOARD); + } + public void setDisplayedBranchLength(int n) { displayedBranchLength = n; } @@ -1479,6 +1487,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 8ceea4d56..1fb3b0a0e 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; @@ -527,9 +535,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 b46e2f22e..be91bb2de 100644 --- a/src/main/java/featurecat/lizzie/gui/LizzieMain.java +++ b/src/main/java/featurecat/lizzie/gui/LizzieMain.java @@ -715,6 +715,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 e0fb74c83..e6aca7f2d 100644 --- a/src/main/java/featurecat/lizzie/gui/MainFrame.java +++ b/src/main/java/featurecat/lizzie/gui/MainFrame.java @@ -20,6 +20,7 @@ public abstract class MainFrame extends JFrame { public static Font winrateFont; // Force refresh board private boolean forceRefresh; + public boolean isMouseOver = false; public MainFrame(String title) throws HeadlessException { super(title); @@ -92,6 +93,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 loadFile(File file);