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

Fix bugs and inconveniences around blunder thresholds table #801

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
22 changes: 13 additions & 9 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public Config() throws IOException {
uiConfig = config.getJSONObject("ui");
persistedUi = persisted.getJSONObject("ui-persist");

theme = new Theme(uiConfig);
applyTheme();

panelUI = uiConfig.optBoolean("panel-ui", false);
showBorder = uiConfig.optBoolean("show-border", false);
Expand Down Expand Up @@ -231,9 +231,6 @@ public Config() throws IOException {
limitBranchLength = uiConfig.optInt("limit-branch-length", 0);
minPlayoutRatioForStats = uiConfig.optDouble("min-playout-ratio-for-stats", 0.1);

winrateStrokeWidth = theme.winrateStrokeWidth();
minimumBlunderBarWidth = theme.minimumBlunderBarWidth();
shadowSize = theme.shadowSize();
showLcbWinrate = config.getJSONObject("leelaz").optBoolean("show-lcb-winrate");

showKataGoScoreMean = uiConfig.optBoolean("show-katago-scoremean", true);
Expand All @@ -253,6 +250,18 @@ public Config() throws IOException {
toolbarPosition =
uiConfig.optString("toolbar-position", persistedUi.optString("toolbar-position", "South"));

gtpConsoleStyle = uiConfig.optString("gtp-console-style", defaultGtpConsoleStyle);

System.out.println(Locale.getDefault().getLanguage()); // todo add config option for language...
setLanguage(Locale.getDefault().getLanguage());
}

public void applyTheme() {
theme = new Theme(uiConfig);
winrateStrokeWidth = theme.winrateStrokeWidth();
minimumBlunderBarWidth = theme.minimumBlunderBarWidth();
shadowSize = theme.shadowSize();

if (theme.fontName() != null) fontName = theme.fontName();

if (theme.uiFontName() != null) uiFontName = theme.uiFontName();
Expand All @@ -272,11 +281,6 @@ public Config() throws IOException {
blunderWinrateThresholds = theme.blunderWinrateThresholds();
blunderNodeColors = theme.blunderNodeColors();
nodeColorMode = theme.nodeColorMode();

gtpConsoleStyle = uiConfig.optString("gtp-console-style", defaultGtpConsoleStyle);

System.out.println(Locale.getDefault().getLanguage()); // todo add config option for language...
setLanguage(Locale.getDefault().getLanguage());
}

// Modifies config by adding in values from default_config that are missing.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ public void drawEstimateRect(ArrayList<Double> estimateArray, boolean isZen) {
boardRenderer.drawEstimateRect(estimateArray, isZen);
}

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

public void saveImage() {
JSONObject filesystem = Lizzie.config.persisted.getJSONObject("filesystem");
JFileChooser chooser = new JFileChooser(filesystem.getString("last-folder"));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,18 @@ public BufferedImage getWallpaper() {
return cachedWallpaperImage;
}

public void resetImages() {
cachedBackgroundImage = emptyImage;
cachedStonesImage = emptyImage;
cachedBoardImage = emptyImage;
cachedWallpaperImage = emptyImage;
cachedStonesShadowImage = emptyImage;
cachedBlackStoneImage = emptyImage;
cachedWhiteStoneImage = emptyImage;
cachedEstimateLargeRectImage = emptyImage;
cachedEstimateSmallRectImage = emptyImage;
}

/**
* Draw scale smooth image, enhanced display quality (Not use, for future) This function use the
* traditional Image.getScaledInstance() method to provide the nice quality, but the performance
Expand Down
59 changes: 39 additions & 20 deletions src/main/java/featurecat/lizzie/gui/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -235,6 +234,7 @@ public ConfigDialog() {
okButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
finalizeEditedBlunderColors();
setVisible(false);
saveConfig();
applyChange();
Expand Down Expand Up @@ -1541,7 +1541,7 @@ protected DocumentFilter getDocumentFilter() {
btnAdd.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
((BlunderNodeTableModel) tblBlunderNodes.getModel()).addRow("", Color.WHITE);
((BlunderNodeTableModel) tblBlunderNodes.getModel()).addRow(null, Color.WHITE);
}
});
themeTab.add(btnAdd);
Expand Down Expand Up @@ -1815,7 +1815,7 @@ protected void paintComponent(Graphics g) {
@Override
protected void done() {
okButton.setEnabled(true);
pnlBoardPreview.repaint();
tabbedPane.repaint();
}
}

Expand Down Expand Up @@ -1925,6 +1925,7 @@ private void read() {
}

private void applyChange() {
Lizzie.config.applyTheme();
int[] size = getBoardSize();
Lizzie.board.reopen(size[0], size[1]);
if (Lizzie.engineManager == null) {
Expand All @@ -1937,6 +1938,9 @@ private void applyChange() {
} catch (IOException e) {
e.printStackTrace();
}
Lizzie.frame.resetImages();
Lizzie.frame.refreshBackground();
Lizzie.frame.refresh();
}

private Integer txtFieldIntValue(JTextField txt) {
Expand Down Expand Up @@ -2088,7 +2092,7 @@ public BlunderNodeTableModel(
if (blunderWinrateThresholds != null) {
for (Double d : blunderWinrateThresholds) {
Vector<Object> row = new Vector<Object>();
row.add(String.valueOf(d));
row.add(d);
row.add(blunderNodeColors.get(d));
data.add(row);
}
Expand All @@ -2097,7 +2101,7 @@ public BlunderNodeTableModel(

public JSONArray getThresholdArray() {
JSONArray thresholds = new JSONArray("[]");
data.forEach(d -> thresholds.put(new Double((String) d.get(0))));
data.forEach(d -> thresholds.put(toDouble(d.get(0))));
return thresholds;
}

Expand All @@ -2107,7 +2111,7 @@ public JSONArray getColorArray() {
return colors;
}

public void addRow(String threshold, Color color) {
public void addRow(Double threshold, Color color) {
Vector<Object> row = new Vector<Object>();
row.add(threshold);
row.add(color);
Expand All @@ -2118,7 +2122,7 @@ public void addRow(String threshold, Color color) {
public void removeRow(int index) {
if (index >= 0 && index < data.size()) {
data.remove(index);
fireTableRowsDeleted(0, data.size() - 1);
fireTableRowsDeleted(index, index);
}
}

Expand All @@ -2139,7 +2143,7 @@ public Object getValueAt(int row, int col) {
}

public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
return c == 0 ? Double.class : Color.class;
}

public void setValueAt(Object value, int row, int col) {
Expand All @@ -2150,6 +2154,24 @@ public void setValueAt(Object value, int row, int col) {
public boolean isCellEditable(int row, int col) {
return true;
}

private double toDouble(Object x) {
final double invalid = 0.0;
try {
return (Double) x;
} catch (Exception e) {
return invalid;
}
}

public void sortData() {
data.sort(
new Comparator<Vector<Object>>() {
public int compare(Vector<Object> a, Vector<Object> b) {
return Double.compare(toDouble(a.get(0)), toDouble(b.get(0)));
}
});
}
}

public boolean isWindows() {
Expand Down Expand Up @@ -2477,6 +2499,14 @@ private void writeDefaultTheme() {
((BlunderNodeTableModel) tblBlunderNodes.getModel()).getColorArray());
}

private void finalizeEditedBlunderColors() {
if (tblBlunderNodes == null) return;
TableCellEditor editor = tblBlunderNodes.getCellEditor();
BlunderNodeTableModel model = (BlunderNodeTableModel) tblBlunderNodes.getModel();
if (editor != null) editor.stopCellEditing();
if (model != null) model.sortData();
}

private void saveConfig() {
try {
leelazConfig.putOpt("max-analyze-time-minutes", txtFieldIntValue(txtMaxAnalyzeTime));
Expand Down Expand Up @@ -2571,16 +2601,5 @@ private void saveConfig() {

public void switchTab(int index) {
tabbedPane.setSelectedIndex(index);
if (index == 2) {
Timer timer = new Timer();
timer.schedule(
new TimerTask() {
public void run() {
tabbedPane.repaint();
this.cancel();
}
},
100);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ public void refreshBackground() {
redrawBackgroundAnyway = true;
}

public void resetImages() {
boardRenderer.resetImages();
subBoardRenderer.resetImages();
}

private Graphics2D createBackground(int width, int hight) {
cachedBackground = new BufferedImage(width, hight, TYPE_INT_RGB);
cachedBackgroundWidth = cachedBackground.getWidth();
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 @@ -318,6 +318,15 @@ public void refreshBackground() {
redrawBackgroundAnyway = true;
}

public void resetImages() {
cachedBasicInfoContainer = emptyImage;
cachedWinrateContainer = emptyImage;
cachedVariationContainer = emptyImage;
cachedWallpaperImage = emptyImage;
boardPane.resetImages();
subBoardPane.resetImages();
}

public BufferedImage getWallpaper() {
if (cachedWallpaperImage == emptyImage) {
cachedWallpaperImage = Lizzie.config.theme.background();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/featurecat/lizzie/gui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public boolean processCommentMouseWheelMoved(MouseWheelEvent e) {

public abstract void refreshBackground();

public abstract void resetImages();

public abstract void clear();

public abstract boolean isMouseOver(int x, int y);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/featurecat/lizzie/gui/SubBoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ protected void paintComponent(Graphics g0) {
// bs.show();
}

public void resetImages() {
subBoardRenderer.resetImages();
}

/**
* Checks whether or not something was clicked and performs the appropriate action
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/featurecat/lizzie/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public static Color getBlunderNodeColor(BoardHistoryNode node) {
Optional<Double> st =
diffWinrate >= 0
? Lizzie.config.blunderWinrateThresholds.flatMap(
l -> l.stream().filter(t -> (t > 0 && t <= diffWinrate)).reduce((f, s) -> s))
l -> l.stream().filter(t -> (t >= 0 && t <= diffWinrate)).reduce((f, s) -> s))
: Lizzie.config.blunderWinrateThresholds.flatMap(
l -> l.stream().filter(t -> (t < 0 && t >= diffWinrate)).reduce((f, s) -> f));
l -> l.stream().filter(t -> (t <= 0 && t >= diffWinrate)).reduce((f, s) -> f));
if (st.isPresent()) {
return Lizzie.config.blunderNodeColors.map(m -> m.get(st.get())).get();
} else {
Expand Down