From 9911ae1a4d257f1dbe5af6db58c98023237562c2 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 15 Jun 2022 14:55:22 -0400 Subject: [PATCH] Finish DialogueMenu --- img/dialogue/Gunther.png | Bin 0 -> 1127 bytes src/DialogueMenu.java | 29 ++++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 img/dialogue/Gunther.png diff --git a/img/dialogue/Gunther.png b/img/dialogue/Gunther.png new file mode 100644 index 0000000000000000000000000000000000000000..50ce1d61dfe92204fd8a57a59c69ba247b3539b2 GIT binary patch literal 1127 zcmV-t1ep7YP)K<4fz5f3CoHsCQu?Gf`4Ru2rvj~$-YMb0L9tspvM76FN@R$NQo4)>lAc9iN!?%;X(SNPKn}AKEN)gGWpWiq4E8zX3=pJB`N~t4r zT613~Z{EHG*i^bF;nRRRSB!IP)QcEFA z<<^Pj?mgRjcQdnQ?vDaUCLsaQ%)B#VFVW1*%+1W71568QW>RPRjS>4k0Lf@?%IQ9! zltIW$>dnl|J0cE!TeJflyEv42_2U|4T>`9002ovPDHLkV1g{{{WAan literal 0 HcmV?d00001 diff --git a/src/DialogueMenu.java b/src/DialogueMenu.java index 257269f..696743f 100644 --- a/src/DialogueMenu.java +++ b/src/DialogueMenu.java @@ -7,23 +7,28 @@ import java.util.Collections; public class DialogueMenu extends TextBox implements Serializable { public static final int PORTRAIT_WIDTH = 200; 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) { - super(y, GamePanel.GAME_WIDTH, yHeight, 0, font, null, null); - newX = PORTRAIT_WIDTH + PADDING; + public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait) { + super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null); + newX = PORTRAIT_WIDTH + PADDING*2; + PORTRAIT = portrait; } public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) { if (backgroundColor != null) { g.setColor(textColor); // 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)); g.drawRect(newX, newY, xWidth, yHeight); g.setColor(backgroundColor); g.fillRect(newX, newY, xWidth, yHeight); } 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) { @@ -35,20 +40,22 @@ public class DialogueMenu extends TextBox implements Serializable { FontMetrics metrics = g.getFontMetrics(); int currentLineWidth = 0, lastLineIndex = 0; for (String s: newText) { - currentLineWidth += metrics.stringWidth(s); - if (currentLineWidth < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*2)) { - lines.set(lastLineIndex, lines.get(lastLineIndex) + s); + currentLineWidth += metrics.stringWidth(s + " "); + if (currentLineWidth - metrics.stringWidth(" ") < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*5)) { + System.out.println(s + " " + currentLineWidth); + lines.set(lastLineIndex, lines.get(lastLineIndex) + s + " "); } else { 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) for (String s: lines) { - y -= (metrics.getAscent() - metrics.getDescent()) / 2; + y += (metrics.getAscent() - metrics.getDescent()) * LINE_SPACING; // draw string - g.drawString(text, x, y); + g.drawString(s + "\n", x + PADDING, y); } }