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

next: About display comment of sgf and read/write sgf with variation & Winrate. #352

Closed
wants to merge 19 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ src/main/resources/META-INF
target
leelaz_opencl_tuning
lizzie.sh

.classpath

.project

*.prefs
181 changes: 181 additions & 0 deletions src/main/java/featurecat/lizzie/WrapString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package featurecat.lizzie;

import java.awt.FontMetrics;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
* Globally available utility classes, mostly for string manipulation.
*
* @author Jim Menard, <a href="mailto:[email protected]">[email protected]</a>
*/
public class WrapString {
/**
* Returns an array of strings, one for each line in the string after it has
* been wrapped to fit lines of <var>maxWidth</var>. Lines end with any of cr,
* lf, or cr lf. A line ending at the end of the string will not output a
* further, empty string.
* <p>
* This code assumes <var>str</var> is not <code>null</code>.
*
* @param str
* the string to split
* @param fm
* needed for string width calculations
* @param maxWidth
* the max line width, in points
* @return a non-empty list of strings
*/
public static List wrap(String str, FontMetrics fm, int maxWidth) {
List lines = splitIntoLines(str);
if (lines.size() == 0)
return lines;

ArrayList strings = new ArrayList();
for (Iterator iter = lines.iterator(); iter.hasNext();)
wrapLineInto((String) iter.next(), strings, fm, maxWidth);
return strings;
}

/**
* Given a line of text and font metrics information, wrap the line and add the
* new line(s) to <var>list</var>.
*
* @param line
* a line of text
* @param list
* an output list of strings
* @param fm
* font metrics
* @param maxWidth
* maximum width of the line(s)
*/
public static void wrapLineInto(String line, List list, FontMetrics fm, int maxWidth) {
int len = line.length();
int width;
while (len > 0 && (width = fm.stringWidth(line)) > maxWidth) {
// Guess where to split the line. Look for the next space before
// or after the guess.
int guess = len * maxWidth / width;
String before = line.substring(0, guess).trim();

width = fm.stringWidth(before);
int pos = 0;
if (width > maxWidth) { // Too long
pos = findBreakBefore(line, guess);
// fix too long bug
if (pos <= 0 || (width = fm.stringWidth(line.substring(0, pos).trim())) > maxWidth) {
int diff = width - maxWidth;
int i = 0;
for (; (diff > 0 && i < 4); i++) {
diff = diff - fm.stringWidth(line.substring(guess - i - 1, guess - i));
}
pos = guess - i;
}
}
else { // Too short or possibly just right
pos = findBreakAfter(line, guess);
if (pos != -1) { // Make sure this doesn't make us too long
before = line.substring(0, pos).trim();
if (fm.stringWidth(before) > maxWidth)
pos = findBreakBefore(line, guess);
}
}
// fix the bug for '-'
// if (pos == -1)
if (pos <= 0)
pos = guess; // Split in the middle of the word

list.add(line.substring(0, pos).trim());
line = line.substring(pos).trim();
len = line.length();
}
if (len > 0)
list.add(line);
}

/**
* Returns the index of the first whitespace character or '-' in <var>line</var>
* that is at or before <var>start</var>. Returns -1 if no such character is
* found.
*
* @param line
* a string
* @param start
* where to star looking
*/
public static int findBreakBefore(String line, int start) {
for (int i = start; i >= 0; --i) {
char c = line.charAt(i);
if (Character.isWhitespace(c) || c == '-')
return i;
}
return -1;
}

/**
* Returns the index of the first whitespace character or '-' in <var>line</var>
* that is at or after <var>start</var>. Returns -1 if no such character is
* found.
*
* @param line
* a string
* @param start
* where to star looking
*/
public static int findBreakAfter(String line, int start) {
int len = line.length();
for (int i = start; i < len; ++i) {
char c = line.charAt(i);
if (Character.isWhitespace(c) || c == '-')
return i;
}
return -1;
}

/**
* Returns an array of strings, one for each line in the string. Lines end with
* any of cr, lf, or cr lf. A line ending at the end of the string will not
* output a further, empty string.
* <p>
* This code assumes <var>str</var> is not <code>null</code>.
*
* @param str
* the string to split
* @return a non-empty list of strings
*/
public static List splitIntoLines(String str) {
ArrayList strings = new ArrayList();

int len = str.length();
if (len == 0) {
strings.add("");
return strings;
}

int lineStart = 0;

for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (c == '\r') {
int newlineLength = 1;
if ((i + 1) < len && str.charAt(i + 1) == '\n')
newlineLength = 2;
strings.add(str.substring(lineStart, i));
lineStart = i + newlineLength;
if (newlineLength == 2) // skip \n next time through loop
++i;
} else if (c == '\n') {
strings.add(str.substring(lineStart, i));
lineStart = i + 1;
}
}
if (lineStart < len)
strings.add(str.substring(lineStart));

return strings;
}

}
86 changes: 85 additions & 1 deletion src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.jhlabs.image.GaussianFilter;
import featurecat.lizzie.Lizzie;
import featurecat.lizzie.Util;
import featurecat.lizzie.WrapString;
import featurecat.lizzie.analysis.GameInfo;
import featurecat.lizzie.analysis.Leelaz;
import featurecat.lizzie.rules.Board;
Expand All @@ -21,6 +22,7 @@
import featurecat.lizzie.rules.SGFParser;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
Expand Down Expand Up @@ -414,7 +416,12 @@ public void paint(Graphics g0) {

if (Lizzie.config.showVariationGraph) {
drawVariationTreeContainer(backgroundG, vx, vy, vw, vh);
variationTree.draw(g, treex, treey, treew, treeh);
// Draw the Comment of the Sgf
int cHeight = drawCommnet(g, vx, vy, vw, vh, false);
variationTree.draw(g, treex, treey, treew, treeh - cHeight);
} else {
// Draw the Comment of the Sgf
int cHeight = drawCommnet(g, vx, boardY, vw, vh, true);
}
if (Lizzie.config.showSubBoard) {
try {
Expand Down Expand Up @@ -490,6 +497,16 @@ private void drawVariationTreeContainer(Graphics2D g, int vx, int vy, int vw, in
private void drawPonderingState(Graphics2D g, String text, int x, int y, double size) {
Font font = new Font(systemDefaultFontName, Font.PLAIN, (int)(Math.max(getWidth(), getHeight()) * size));
FontMetrics fm = g.getFontMetrics(font);
// for trim long text
if (Lizzie.leelaz.isLoaded()) {
int mainBoardX = (boardRenderer != null && boardRenderer.getLocation() != null) ? boardRenderer.getLocation().x : 0;
if (mainBoardX > x) {
ArrayList<String> list = (ArrayList<String>) WrapString.wrap(text, fm, mainBoardX - x);
if (list != null && list.size() > 0) {
text = list.get(0);
}
}
}
int stringWidth = fm.stringWidth(text);
int stringHeight = fm.getAscent() - fm.getDescent();
int width = stringWidth;
Expand Down Expand Up @@ -945,4 +962,71 @@ public void pasteSgf() {
public void increaseMaxAlpha(int k) {
boardRenderer.increaseMaxAlpha(k);
}

/**
* Draw the Comment of the Sgf file
*
* @param g
* @param x
* @param y
* @param w
* @param h
* @param full
* @return
*/
private int drawCommnet(Graphics2D g, int x, int y, int w, int h, boolean full) {
int cHeight = 0;
String comment = (Lizzie.board.getHistory().getData() != null && Lizzie.board.getHistory().getData().comment != null) ? Lizzie.board.getHistory().getData().comment : "";
if (comment != null && comment.trim().length() > 0) {
double rate = full ? 1 : 0.1;
cHeight = (int)(h * rate);
// May be need to set up a Chinese Font for display a Chinese Text in the non-Chinese environment
// String systemDefaultFontName = "宋体";
int fontSize = (int)(Math.min(getWidth(), getHeight()) * 0.98 * 0.03);
try {
fontSize = Lizzie.config.uiConfig.getInt("comment-font-size");
} catch (JSONException e) {
if (fontSize < 16) {
fontSize = 16;
} else if (fontSize < 16) {
fontSize = 16;
}
}
Font font = new Font(systemDefaultFontName, Font.PLAIN, fontSize);
FontMetrics fm = g.getFontMetrics(font);
int stringWidth = fm.stringWidth(comment);
int stringHeight = fm.getHeight(); //fm.getAscent() - fm.getDescent();
int width = stringWidth;
int height = stringHeight; //(int)(stringHeight * 1.2);

ArrayList<String> list = (ArrayList<String>) WrapString.wrap(comment, fm, (int)(w - height*0.9));
if (list != null && list.size() > 0) {
if (!full) {
if (list.size() * height > cHeight) {
cHeight = list.size() * height;
if (cHeight > (int)(h * 0.4)) {
cHeight = (int)(h * 0.4);
}
}
}
int ystart = full ? y : h - cHeight;
// Draw background
Color oriColor = g.getColor();
g.setColor(new Color(0, 0, 0, 150));
g.fillRect(x, ystart - height, w, cHeight + height * 2);
g.setColor(Color.white);
g.setFont(font);
int i = 0;
for (String s : list) {
g.drawString(s, x + (int)(height * 0.2), ystart + height * (full?(i+1):i));
i++;
}
g.setColor(oriColor);
cHeight = cHeight + height;
} else {
cHeight = 0;
}
}
return cHeight;
}
}
2 changes: 1 addition & 1 deletion src/main/java/featurecat/lizzie/gui/VariationTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void drawTree(Graphics2D g, int posx, int posy, int startLane, int maxpos
g.setColor(curcolor);

// Draw main line
while (cur.next() != null && posy + YSPACING < maxposy) {
while (cur.next() != null && ((posy + YSPACING + DOT_DIAM) < maxposy)) { // Fix oval cover issue sometimes
posy += YSPACING;
cur = cur.next();
if (cur == curMove) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/featurecat/lizzie/rules/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public static String convertCoordinatesToName(int x, int y) {
public static boolean isValid(int x, int y) {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}

/**
* The comment. Thread safe
* @param comment the comment of stone
*/
public void comment(String comment) {
synchronized (this) {

if (history.getData() != null) {
history.getData().comment = comment;
}
}
}

/**
* The pass. Thread safe
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/featurecat/lizzie/rules/BoardData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class BoardData {

public int blackCaptures;
public int whiteCaptures;

public String comment;

public BoardData(Stone[] stones, int[] lastMove, Stone lastMoveColor, boolean blackToPlay, Zobrist zobrist, int moveNumber, int[] moveNumberList, int blackCaptures, int whiteCaptures, double winrate, int playouts) {
this.moveNumber = moveNumber;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/featurecat/lizzie/rules/BoardHistoryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public BoardData next() {
return head.getData();
}

/**
* moves the pointer to the right, returns the node stored there
*
* @return the next node, null if there is no next node
*/
public BoardHistoryNode nextNode() {
if (head.next() == null)
return null;
else
head = head.next();

return head;
}

/**
* moves the pointer to the variation number idx, returns the data stored there
*
Expand Down Expand Up @@ -185,6 +199,10 @@ public int[] getMoveNumberList() {
public BoardHistoryNode getCurrentHistoryNode() {
return head;
}

public void setCurrentHistoryNode(BoardHistoryNode head) {
this.head = head;
}

/**
* @param data the board position to check against superko
Expand Down
Loading