|
28 | 28 | import javax.swing.JTextArea; |
29 | 29 | import javax.swing.JTextField; |
30 | 30 | import javax.swing.WindowConstants; |
| 31 | +import javax.swing.event.DocumentEvent; |
| 32 | +import javax.swing.event.DocumentListener; |
31 | 33 |
|
32 | 34 | import com.bobsgame.EditorMain; |
33 | 35 |
|
34 | 36 |
|
35 | 37 |
|
36 | 38 | //=============================================================================================== |
37 | | -public class DialogueEditor extends JDialog implements ActionListener, TextListener, ItemListener, ImageObserver, KeyListener |
| 39 | +public class DialogueEditor extends JDialog implements ActionListener, TextListener, ItemListener, ImageObserver, KeyListener, DocumentListener |
38 | 40 | {//=============================================================================================== |
39 | 41 |
|
40 | 42 |
|
41 | 43 |
|
42 | 44 |
|
43 | 45 | public JTextArea textArea; |
| 46 | + public DialoguePreviewPanel previewPanel; |
44 | 47 |
|
45 | 48 | public JTextField commentTextField, captionTextField; |
46 | 49 |
|
@@ -210,10 +213,14 @@ public DialogueEditor(Frame f) |
210 | 213 | textArea.setFont(new Font("Tahoma", Font.PLAIN, 14)); |
211 | 214 | textArea.setCaretColor(Color.white); |
212 | 215 | textArea.getCaret().setBlinkRate(100); |
| 216 | + textArea.getDocument().addDocumentListener(this); |
213 | 217 |
|
214 | 218 |
|
215 | 219 | everythingPanel.add(textArea,BorderLayout.CENTER); |
216 | 220 |
|
| 221 | + previewPanel = new DialoguePreviewPanel(); |
| 222 | + everythingPanel.add(previewPanel, BorderLayout.SOUTH); |
| 223 | + |
217 | 224 |
|
218 | 225 |
|
219 | 226 |
|
@@ -987,4 +994,123 @@ public boolean imageUpdate(Image img, int infoflags, int x, int y, |
987 | 994 | return false; |
988 | 995 | } |
989 | 996 |
|
| 997 | + @Override |
| 998 | + public void insertUpdate(DocumentEvent e) { |
| 999 | + previewPanel.updatePreview(textArea.getText()); |
| 1000 | + } |
| 1001 | + |
| 1002 | + @Override |
| 1003 | + public void removeUpdate(DocumentEvent e) { |
| 1004 | + previewPanel.updatePreview(textArea.getText()); |
| 1005 | + } |
| 1006 | + |
| 1007 | + @Override |
| 1008 | + public void changedUpdate(DocumentEvent e) { |
| 1009 | + previewPanel.updatePreview(textArea.getText()); |
| 1010 | + } |
| 1011 | + |
| 1012 | + //=============================================================================================== |
| 1013 | + public class DialoguePreviewPanel extends JPanel |
| 1014 | + {//=============================================================================================== |
| 1015 | + private String text = ""; |
| 1016 | + |
| 1017 | + public DialoguePreviewPanel() { |
| 1018 | + setPreferredSize(new java.awt.Dimension(600, 150)); |
| 1019 | + setBackground(Color.BLACK); |
| 1020 | + setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE)); |
| 1021 | + } |
| 1022 | + |
| 1023 | + public void updatePreview(String text) { |
| 1024 | + this.text = text; |
| 1025 | + repaint(); |
| 1026 | + } |
| 1027 | + |
| 1028 | + @Override |
| 1029 | + protected void paintComponent(java.awt.Graphics g) { |
| 1030 | + super.paintComponent(g); |
| 1031 | + |
| 1032 | + g.setColor(Color.WHITE); |
| 1033 | + g.setFont(new Font("Monospaced", Font.PLAIN, 12)); // Simulating game font |
| 1034 | + |
| 1035 | + String[] pages = text.split("<\\.>"); // Split by pages |
| 1036 | + String lastPage = pages.length > 0 ? pages[pages.length - 1] : ""; |
| 1037 | + |
| 1038 | + // Replace newlines with a split token |
| 1039 | + String cleanText = lastPage.replace("<NEWLINE>", "\n"); |
| 1040 | + String[] lines = cleanText.split("\n"); |
| 1041 | + |
| 1042 | + int startY = 20; |
| 1043 | + int startX = 10; |
| 1044 | + int lineHeight = 15; |
| 1045 | + |
| 1046 | + int currentY = startY; |
| 1047 | + |
| 1048 | + for(String line : lines) { |
| 1049 | + int currentX = startX; |
| 1050 | + |
| 1051 | + // Parse color tags per line (simple approach) |
| 1052 | + // Supports <RED>, <BLUE>, <GREEN>, <WHITE>, <BLACK>, <GRAY>, <ORANGE>, <YELLOW>, <PURPLE>, <PINK> |
| 1053 | + |
| 1054 | + // Tokenize by '<' and '>' |
| 1055 | + // But we need to keep text between tags. |
| 1056 | + // Regex split keeping delimiters is hard in java split |
| 1057 | + |
| 1058 | + // Manual scan |
| 1059 | + Color currentColor = g.getColor(); // Keep previous color across lines? usually resets per box in game logic, but tags persist. |
| 1060 | + // Let's assume it persists. |
| 1061 | + |
| 1062 | + int lastIndex = 0; |
| 1063 | + while(lastIndex < line.length()) { |
| 1064 | + int tagStart = line.indexOf("<", lastIndex); |
| 1065 | + if(tagStart != -1) { |
| 1066 | + // Draw text before tag |
| 1067 | + if(tagStart > lastIndex) { |
| 1068 | + String segment = line.substring(lastIndex, tagStart); |
| 1069 | + g.setColor(currentColor); |
| 1070 | + g.drawString(segment, currentX, currentY); |
| 1071 | + currentX += g.getFontMetrics().stringWidth(segment); |
| 1072 | + } |
| 1073 | + |
| 1074 | + int tagEnd = line.indexOf(">", tagStart); |
| 1075 | + if(tagEnd != -1) { |
| 1076 | + String tag = line.substring(tagStart, tagEnd + 1); |
| 1077 | + // Check if color tag |
| 1078 | + if(tag.equals("<RED>")) currentColor = Color.RED; |
| 1079 | + else if(tag.equals("<BLUE>")) currentColor = Color.BLUE; |
| 1080 | + else if(tag.equals("<GREEN>")) currentColor = Color.GREEN; |
| 1081 | + else if(tag.equals("<WHITE>")) currentColor = Color.WHITE; |
| 1082 | + else if(tag.equals("<BLACK>")) currentColor = Color.DARK_GRAY; // Black on black bg is bad, use dark gray or fix bg |
| 1083 | + else if(tag.equals("<GRAY>")) currentColor = Color.GRAY; |
| 1084 | + else if(tag.equals("<ORANGE>")) currentColor = Color.ORANGE; |
| 1085 | + else if(tag.equals("<YELLOW>")) currentColor = Color.YELLOW; |
| 1086 | + else if(tag.equals("<PURPLE>")) currentColor = new Color(150,0,255); |
| 1087 | + else if(tag.equals("<PINK>")) currentColor = Color.PINK; |
| 1088 | + // Else ignore (control tag) |
| 1089 | + |
| 1090 | + lastIndex = tagEnd + 1; |
| 1091 | + } else { |
| 1092 | + // Malformed tag, just print rest |
| 1093 | + String segment = line.substring(lastIndex); |
| 1094 | + g.setColor(currentColor); |
| 1095 | + g.drawString(segment, currentX, currentY); |
| 1096 | + break; |
| 1097 | + } |
| 1098 | + } else { |
| 1099 | + // No more tags |
| 1100 | + String segment = line.substring(lastIndex); |
| 1101 | + g.setColor(currentColor); |
| 1102 | + g.drawString(segment, currentX, currentY); |
| 1103 | + break; |
| 1104 | + } |
| 1105 | + } |
| 1106 | + |
| 1107 | + currentY += lineHeight; |
| 1108 | + } |
| 1109 | + |
| 1110 | + g.setColor(Color.GRAY); |
| 1111 | + g.drawRect(0, 0, getWidth()-1, getHeight()-1); |
| 1112 | + g.drawString("Preview (Last Page)", getWidth() - 150, getHeight() - 5); |
| 1113 | + } |
| 1114 | + } |
| 1115 | + |
990 | 1116 | } |
0 commit comments