Skip to content

Commit

Permalink
Merge pull request #513 from zsalch/n_newui
Browse files Browse the repository at this point in the history
next: New UI - Multiple independent panels that can be dragged out
  • Loading branch information
zsalch authored Jun 25, 2019
2 parents d0e7040 + bb16715 commit b8210bc
Show file tree
Hide file tree
Showing 33 changed files with 5,190 additions and 328 deletions.
54 changes: 27 additions & 27 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package featurecat.lizzie;

import featurecat.lizzie.theme.Theme;
import featurecat.lizzie.util.WindowPosition;
import java.awt.Color;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import javax.swing.*;
import org.json.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class Config {
public String language = "en";

public boolean panelUI = false;
public boolean showBorder = false;
public boolean showMoveNumber = false;
public int onlyLastMoveNumber = 0;
Expand Down Expand Up @@ -160,6 +174,7 @@ public Config() throws IOException {

theme = new Theme(uiConfig);

panelUI = uiConfig.optBoolean("panel-ui", false);
showBorder = uiConfig.optBoolean("show-border", false);
showMoveNumber = uiConfig.getBoolean("show-move-number");
onlyLastMoveNumber = uiConfig.optInt("only-last-move-number");
Expand All @@ -185,7 +200,7 @@ public Config() throws IOException {
showCoordinates = uiConfig.optBoolean("show-coordinates");
replayBranchIntervalSeconds = uiConfig.optDouble("replay-branch-interval-seconds", 1.0);
colorByWinrateInsteadOfVisits = uiConfig.optBoolean("color-by-winrate-instead-of-visits");
boardPositionProportion = uiConfig.optInt("board-postion-proportion", 4);
boardPositionProportion = uiConfig.optInt("board-position-proportion", 4);
winrateStrokeWidth = theme.winrateStrokeWidth();
minimumBlunderBarWidth = theme.minimumBlunderBarWidth();
shadowSize = theme.shadowSize();
Expand Down Expand Up @@ -414,6 +429,7 @@ private JSONObject createDefaultConfig() {
ui.put("append-winrate-to-comment", false);
ui.put("replay-branch-interval-seconds", 1.0);
ui.put("gtp-console-style", defaultGtpConsoleStyle);
ui.put("panel-ui", false);
config.put("ui", ui);
return config;
}
Expand All @@ -437,10 +453,8 @@ private JSONObject createPersistConfig() {
// ui.put("window-width", 687);
// ui.put("max-alpha", 240);

// Main Window Position & Size
ui.put("main-window-position", new JSONArray("[]"));
ui.put("gtp-console-position", new JSONArray("[]"));
ui.put("window-maximized", false);
// Window Position & Size
ui = WindowPosition.create(ui);

config.put("filesystem", filesys);

Expand All @@ -464,24 +478,10 @@ private void writeConfig(JSONObject config, File file) throws IOException, JSONE
}

public void persist() throws IOException {
boolean windowIsMaximized = Lizzie.frame.getExtendedState() == JFrame.MAXIMIZED_BOTH;

JSONArray mainPos = new JSONArray();
if (!windowIsMaximized) {
mainPos.put(Lizzie.frame.getX());
mainPos.put(Lizzie.frame.getY());
mainPos.put(Lizzie.frame.getWidth());
mainPos.put(Lizzie.frame.getHeight());
}
persistedUi.put("main-window-position", mainPos);
JSONArray gtpPos = new JSONArray();
gtpPos.put(Lizzie.gtpConsole.getX());
gtpPos.put(Lizzie.gtpConsole.getY());
gtpPos.put(Lizzie.gtpConsole.getWidth());
gtpPos.put(Lizzie.gtpConsole.getHeight());
persistedUi.put("gtp-console-position", gtpPos);
persistedUi.put("board-postion-propotion", Lizzie.frame.BoardPositionProportion);
persistedUi.put("window-maximized", windowIsMaximized);

// Save the window position
persistedUi = WindowPosition.save(persistedUi);

writeConfig(this.persisted, new File(persistFilename));
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/featurecat/lizzie/Lizzie.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import featurecat.lizzie.analysis.Leelaz;
import featurecat.lizzie.gui.GtpConsolePane;
import featurecat.lizzie.gui.LizzieFrame;
import featurecat.lizzie.gui.LizzieMain;
import featurecat.lizzie.gui.MainFrame;
import featurecat.lizzie.rules.Board;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.json.JSONArray;

/** Main class. */
public class Lizzie {
public static Config config;
public static MainFrame frame;
public static GtpConsolePane gtpConsole;
public static LizzieFrame frame;
public static Board board;
public static Leelaz leelaz;
public static String lizzieVersion = "0.7";
Expand All @@ -26,7 +30,7 @@ public static void main(String[] args) throws IOException {
mainArgs = args;
config = new Config();
board = new Board();
frame = new LizzieFrame();
frame = config.panelUI ? new LizzieMain() : new LizzieFrame();
gtpConsole = new GtpConsolePane(frame);
gtpConsole.setVisible(config.leelazConfig.optBoolean("print-comms", false));
try {
Expand Down Expand Up @@ -67,7 +71,7 @@ public static void shutdown() {
JOptionPane.showConfirmDialog(
null, "Do you want to save this SGF?", "Save SGF?", JOptionPane.OK_CANCEL_OPTION);
if (ret == JOptionPane.OK_OPTION) {
LizzieFrame.saveFile();
frame.saveFile();
}
}
board.autosaveToMemory();
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void startEngine(String engineCommand) throws IOException {
return;
}

isLoaded = false;
commands = splitCommand(engineCommand);

// Get weight name
Expand Down Expand Up @@ -247,6 +248,9 @@ private void parseLine(String line) {
} else if (line.equals("\n")) {
// End of response
} else if (line.startsWith("info")) {
if (!isLoaded) {
Lizzie.frame.refresh();
}
isLoaded = true;
// Clear switching prompt
switching = false;
Expand All @@ -256,21 +260,24 @@ private void parseLine(String line) {
// This should not be stale data when the command number match
this.bestMoves = parseInfo(line.substring(5));
notifyBestMoveListeners();
Lizzie.frame.repaint();
Lizzie.frame.refresh(1);
// don't follow the maxAnalyzeTime rule if we are in analysis mode
if (System.currentTimeMillis() - startPonderTime > maxAnalyzeTimeMillis
&& !Lizzie.board.inAnalysisMode()) {
togglePonder();
}
}
} else if (line.contains(" -> ")) {
if (!isLoaded) {
Lizzie.frame.refresh();
}
isLoaded = true;
if (isResponseUpToDate()
|| isThinking
&& (!isPondering && Lizzie.frame.isPlayingAgainstLeelaz || isInputCommand)) {
bestMoves.add(MoveData.fromSummary(line));
notifyBestMoveListeners();
Lizzie.frame.repaint();
Lizzie.frame.refresh(1);
}
} else if (line.startsWith("play")) {
// In lz-genmove_analyze
Expand Down Expand Up @@ -302,7 +309,8 @@ private void parseLine(String line) {
} else if (isThinking && !isPondering) {
if (Lizzie.frame.isPlayingAgainstLeelaz || isInputCommand) {
Lizzie.board.place(params[1]);
togglePonder();
// TODO Do not ponder when playing against Leela Zero
// togglePonder();
if (!isInputCommand) {
isPondering = false;
}
Expand Down Expand Up @@ -528,6 +536,7 @@ public void togglePonder() {
} else {
sendCommand("name"); // ends pondering
}
Lizzie.frame.updateBasicInfo();
}

/** End the process */
Expand Down
166 changes: 166 additions & 0 deletions src/main/java/featurecat/lizzie/gui/BasicInfoPane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package featurecat.lizzie.gui;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

import featurecat.lizzie.Lizzie;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

/** The window used to display the game. */
public class BasicInfoPane extends LizziePane {

private LizzieMain owner;

public BasicInfoPane(LizzieMain owner) {
super(owner);
this.owner = owner;
setVisible(true);
}

private BufferedImage cachedImage;

/**
* Draws the game board and interface
*
* @param g0 not used
*/
@Override
protected void paintComponent(Graphics g0) {
super.paintComponent(g0);

int x = 0; // getX();
int y = 0; // getY();
int width = getWidth();
int height = getHeight();

// initialize

cachedImage = new BufferedImage(width, height, TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) cachedImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if (Lizzie.config.showCaptured) {
if (owner == null) {
g.drawImage(owner.getBasicInfoContainer(this), x, y, null);
} else {
g.drawImage(owner.getBasicInfoContainer(this), x, y, null);
}
drawCaptured(g, x, y, width, height);
}

// cleanup
g.dispose();

// draw the image
Graphics2D bsGraphics = (Graphics2D) g0; // bs.getDrawGraphics();
bsGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
bsGraphics.drawImage(cachedImage, 0, 0, null);

// cleanup
bsGraphics.dispose();
// bs.show();
}

private void drawCaptured(Graphics2D g, int posX, int posY, int width, int height) {
// Draw border
g.setColor(new Color(0, 0, 0, 130));
g.fillRect(posX, posY, width, height);

// border. does not include bottom edge
int strokeRadius = Lizzie.config.showBorder ? 3 : 1;
g.setStroke(new BasicStroke(strokeRadius == 1 ? strokeRadius : 2 * strokeRadius));
if (Lizzie.config.showBorder) {
g.drawLine(
posX + strokeRadius,
posY + strokeRadius,
posX - strokeRadius + width,
posY + strokeRadius);
g.drawLine(
posX + strokeRadius,
posY + 3 * strokeRadius,
posX + strokeRadius,
posY - strokeRadius + height);
g.drawLine(
posX - strokeRadius + width,
posY + 3 * strokeRadius,
posX - strokeRadius + width,
posY - strokeRadius + height);
}

// Draw middle line
g.drawLine(
posX - strokeRadius + width / 2,
posY + 3 * strokeRadius,
posX - strokeRadius + width / 2,
posY - strokeRadius + height);
g.setColor(Color.white);

// Draw black and white "stone"
int diam = height / 3;
int smallDiam = diam / 2;
int bdiam = diam, wdiam = diam;
if (Lizzie.board != null) {
if (Lizzie.board.inScoreMode()) {
// do nothing
} else if (Lizzie.board.getHistory().isBlacksTurn()) {
wdiam = smallDiam;
} else {
bdiam = smallDiam;
}
} else {
bdiam = smallDiam;
}
g.setColor(Color.black);
g.fillOval(
posX + width / 4 - bdiam / 2, posY + height * 3 / 8 + (diam - bdiam) / 2, bdiam, bdiam);

g.setColor(Color.WHITE);
g.fillOval(
posX + width * 3 / 4 - wdiam / 2, posY + height * 3 / 8 + (diam - wdiam) / 2, wdiam, wdiam);

// Draw captures
String bval, wval;
setPanelFont(g, (float) (height * 0.18));
if (Lizzie.board == null) {
return;
}
if (Lizzie.board.inScoreMode()) {
double score[] = Lizzie.board.getScore(Lizzie.board.scoreStones());
bval = String.format("%.0f", score[0]);
wval = String.format("%.1f", score[1]);
} else {
bval = String.format("%d", Lizzie.board.getData().blackCaptures);
wval = String.format("%d", Lizzie.board.getData().whiteCaptures);
}

g.setColor(Color.WHITE);
int bw = g.getFontMetrics().stringWidth(bval);
int ww = g.getFontMetrics().stringWidth(wval);
boolean largeSubBoard = Lizzie.config.showLargeSubBoard();
int bx = (largeSubBoard ? diam : -bw / 2);
int wx = (largeSubBoard ? bx : -ww / 2);

g.drawString(bval, posX + width / 4 + bx, posY + height * 7 / 8);
g.drawString(wval, posX + width * 3 / 4 + wx, posY + height * 7 / 8);

// Status Indicator
int statusDiam = height / 8;
g.setColor((Lizzie.leelaz != null && Lizzie.leelaz.isPondering()) ? Color.GREEN : Color.RED);
g.fillOval(
posX - strokeRadius + width / 2 - statusDiam / 2,
posY + height * 3 / 8 + (diam - statusDiam) / 2,
statusDiam,
statusDiam);
}

private void setPanelFont(Graphics2D g, float size) {
Font font = new Font(Lizzie.config.fontName, Font.PLAIN, (int) size);
g.setFont(font);
}
}
Loading

0 comments on commit b8210bc

Please sign in to comment.