Finish DialogueMenu

master
John 2022-06-15 14:55:22 -04:00
parent 35fa4639e1
commit 9911ae1a4d
2 changed files with 18 additions and 11 deletions

BIN
img/dialogue/Gunther.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -7,23 +7,28 @@ import java.util.Collections;
public class DialogueMenu extends TextBox implements Serializable { public class DialogueMenu extends TextBox implements Serializable {
public static final int PORTRAIT_WIDTH = 200; public static final int PORTRAIT_WIDTH = 200;
public static final int PADDING = 20; public static final int PADDING = 20;
public static final int TOP_PADDING = 10;
public static final double LINE_SPACING = 1.5;
public BufferedImageWrapper PORTRAIT;
public DialogueMenu(int y, int yHeight, Font font) { public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait) {
super(y, GamePanel.GAME_WIDTH, yHeight, 0, font, null, null); super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null);
newX = PORTRAIT_WIDTH + PADDING; newX = PORTRAIT_WIDTH + PADDING*2;
PORTRAIT = portrait;
} }
public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) { public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) {
if (backgroundColor != null) { if (backgroundColor != null) {
g.setColor(textColor); g.setColor(textColor);
// TODO: make drawn line widths consistent // TODO: make drawn line widths consistent
g.drawImage(PORTRAIT.image, newX - PORTRAIT_WIDTH - PADDING, newY, PORTRAIT_WIDTH, yHeight, null);
((Graphics2D)g).setStroke(new BasicStroke(4f)); ((Graphics2D)g).setStroke(new BasicStroke(4f));
g.drawRect(newX, newY, xWidth, yHeight); g.drawRect(newX, newY, xWidth, yHeight);
g.setColor(backgroundColor); g.setColor(backgroundColor);
g.fillRect(newX, newY, xWidth, yHeight); g.fillRect(newX, newY, xWidth, yHeight);
} }
g.setColor(textColor); g.setColor(textColor);
drawCenteredString(g, y, newX, xWidth, text); drawCenteredString(g, newY, newX, text);
} }
public static void drawCenteredString(Graphics g, int y, int x, String text) { public static void drawCenteredString(Graphics g, int y, int x, String text) {
@ -35,20 +40,22 @@ public class DialogueMenu extends TextBox implements Serializable {
FontMetrics metrics = g.getFontMetrics(); FontMetrics metrics = g.getFontMetrics();
int currentLineWidth = 0, lastLineIndex = 0; int currentLineWidth = 0, lastLineIndex = 0;
for (String s: newText) { for (String s: newText) {
currentLineWidth += metrics.stringWidth(s); currentLineWidth += metrics.stringWidth(s + " ");
if (currentLineWidth < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*2)) { if (currentLineWidth - metrics.stringWidth(" ") < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*5)) {
lines.set(lastLineIndex, lines.get(lastLineIndex) + s); System.out.println(s + " " + currentLineWidth);
lines.set(lastLineIndex, lines.get(lastLineIndex) + s + " ");
} else { } else {
currentLineWidth = metrics.stringWidth(s); currentLineWidth = metrics.stringWidth(s);
lines.add(s); lines.add(s + " ");
lastLineIndex ++;
} }
} }
y -= PADDING; y += TOP_PADDING;
// center y (half is above y value, half is below y value) // center y (half is above y value, half is below y value)
for (String s: lines) { for (String s: lines) {
y -= (metrics.getAscent() - metrics.getDescent()) / 2; y += (metrics.getAscent() - metrics.getDescent()) * LINE_SPACING;
// draw string // draw string
g.drawString(text, x, y); g.drawString(s + "\n", x + PADDING, y);
} }
} }