Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Mar 12, 2021
2 parents 9d2ce94 + 007c5e9 commit a988cc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 84 deletions.
60 changes: 11 additions & 49 deletions src/main/java/featurecat/lizzie/gui/CommentPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@

import featurecat.lizzie.Lizzie;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
import javax.swing.JTextArea;

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

// private final BufferStrategy bs;

// Display Comment
private HTMLDocument htmlDoc;
private LizziePane.HtmlKit htmlKit;
private StyleSheet htmlStyle;
public JScrollPane scrollPane;
private JTextPane commentPane;
private JTextArea commentPane;
private JLabel dragPane = new JLabel("Drag out");
private MouseMotionListener[] mouseMotionListeners;
private MouseMotionAdapter mouseMotionAdapter;
Expand All @@ -38,38 +32,16 @@ public CommentPane(LizzieMain owner) {
super(owner);
setLayout(new BorderLayout(0, 0));

htmlKit = new LizziePane.HtmlKit();
htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlStyle = htmlKit.getStyleSheet();
String style =
"body {background:#"
+ String.format(
"%02x%02x%02x",
Lizzie.config.commentBackgroundColor.getRed(),
Lizzie.config.commentBackgroundColor.getGreen(),
Lizzie.config.commentBackgroundColor.getBlue())
+ "; color:#"
+ String.format(
"%02x%02x%02x",
Lizzie.config.commentFontColor.getRed(),
Lizzie.config.commentFontColor.getGreen(),
Lizzie.config.commentFontColor.getBlue())
+ "; font-family:"
+ Lizzie.config.fontName
+ ", Consolas, Menlo, Monaco, 'Ubuntu Mono', monospace;"
+ (Lizzie.config.commentFontSize > 0
? "font-size:" + Lizzie.config.commentFontSize
: "")
+ "}";
htmlStyle.addRule(style);

commentPane = new JTextPane();
commentPane = new JTextArea();
commentPane.setBorder(BorderFactory.createEmptyBorder());
commentPane.setEditorKit(htmlKit);
commentPane.setDocument(htmlDoc);
commentPane.setText("");
commentPane.setEditable(false);
commentPane.setFocusable(false);
commentPane.setWrapStyleWord(true);
commentPane.setLineWrap(true);
// drop alpha for backward compatibility with Lizze 0.7.4
commentPane.setBackground(new Color(Lizzie.config.commentBackgroundColor.getRGB()));
commentPane.setForeground(new Color(Lizzie.config.commentFontColor.getRGB()));
commentPane.addMouseListener(
new MouseAdapter() {
@Override
Expand Down Expand Up @@ -124,22 +96,12 @@ public void drawComment() {
}
Font font = new Font(Lizzie.config.fontName, Font.PLAIN, fontSize);
commentPane.setFont(font);
comment = comment.replaceAll("(\r\n)|(\n)", "<br />").replaceAll(" ", "&nbsp;");
addText("<span class=\"comment\">" + comment + "</span>");
commentPane.setText(comment);
commentPane.setCaretPosition(0);
}
}
}

private void addText(String text) {
try {
htmlDoc.remove(0, htmlDoc.getLength());
htmlKit.insertHTML(htmlDoc, htmlDoc.getLength(), text, 0, 0, null);
commentPane.setCaretPosition(htmlDoc.getLength());
} catch (BadLocationException | IOException e) {
e.printStackTrace();
}
}

public void setDesignMode(boolean mode) {
// if (mode) {
// mouseMotionListeners = commentPane.getMouseMotionListeners();
Expand Down
44 changes: 9 additions & 35 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
import java.util.function.Consumer;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
import org.json.JSONArray;

/** The window used to display the game. */
Expand Down Expand Up @@ -104,11 +102,8 @@ public class LizzieFrame extends MainFrame {
private boolean isPonderingBeforeReplayVariation = false;

// Display Comment
private HTMLDocument htmlDoc;
private LizziePane.HtmlKit htmlKit;
private StyleSheet htmlStyle;
private JScrollPane scrollPane;
private JTextPane commentPane;
private JTextArea commentPane;
private BufferedImage cachedCommentImage = new BufferedImage(1, 1, TYPE_INT_ARGB);
private String cachedComment;
private Rectangle commentRect;
Expand Down Expand Up @@ -175,35 +170,14 @@ protected void paintComponent(Graphics g) {
setExtendedState(Frame.MAXIMIZED_BOTH);
}

htmlKit = new LizziePane.HtmlKit();
htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlStyle = htmlKit.getStyleSheet();
String style =
"body {background:#"
+ String.format(
"%02x%02x%02x",
Lizzie.config.commentBackgroundColor.getRed(),
Lizzie.config.commentBackgroundColor.getGreen(),
Lizzie.config.commentBackgroundColor.getBlue())
+ "; color:#"
+ String.format(
"%02x%02x%02x",
Lizzie.config.commentFontColor.getRed(),
Lizzie.config.commentFontColor.getGreen(),
Lizzie.config.commentFontColor.getBlue())
+ "; font-family:"
+ Lizzie.config.fontName
+ ", Consolas, Menlo, Monaco, 'Ubuntu Mono', monospace;"
+ (Lizzie.config.commentFontSize > 0
? "font-size:" + Lizzie.config.commentFontSize
: "")
+ "}";
htmlStyle.addRule(style);
commentPane = new JTextPane();
commentPane = new JTextArea();
commentPane.setBorder(BorderFactory.createEmptyBorder());
commentPane.setEditorKit(htmlKit);
commentPane.setDocument(htmlDoc);
commentPane.setEditable(false);
commentPane.setWrapStyleWord(true);
commentPane.setLineWrap(true);
// drop alpha for backward compatibility with Lizze 0.7.4
commentPane.setBackground(new Color(Lizzie.config.commentBackgroundColor.getRGB()));
commentPane.setForeground(new Color(Lizzie.config.commentFontColor.getRGB()));
scrollPane = new JScrollPane();
scrollPane.setViewportView(commentPane);
scrollPane.setBorder(null);
Expand Down Expand Up @@ -1314,7 +1288,8 @@ public boolean processCommentMouseWheelMoved(MouseWheelEvent e) {
if (Lizzie.config.showComment && commentRect.contains(e.getX(), e.getY())) {
scrollPane.dispatchEvent(e);
createCommentImage(true, commentRect.width, commentRect.height);
getGraphics()
mainPanel
.getGraphics()
.drawImage(
cachedCommentImage,
commentRect.x,
Expand Down Expand Up @@ -1445,7 +1420,6 @@ private void drawComment(Graphics2D g, int x, int y, int w, int h) {
}
Font font = new Font(Lizzie.config.fontName, Font.PLAIN, fontSize);
commentPane.setFont(font);
comment = comment.replaceAll("(\r\n)|(\n)", "<br />").replaceAll(" ", "&nbsp;");
commentPane.setText(comment);
commentPane.setSize(w, h);
createCommentImage(!comment.equals(this.cachedComment), w, h);
Expand Down

0 comments on commit a988cc4

Please sign in to comment.