2022-06-15 16:24:45 +01:00
|
|
|
import java.awt.*;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
public class DialogueMenu extends TextBox implements Serializable {
|
|
|
|
public static final int PORTRAIT_WIDTH = 200;
|
|
|
|
public static final int PADDING = 20;
|
2022-06-15 19:55:22 +01:00
|
|
|
public static final int TOP_PADDING = 10;
|
|
|
|
public static final double LINE_SPACING = 1.5;
|
2022-06-16 18:31:44 +01:00
|
|
|
public static final int FREQUENCY = 2;
|
2022-06-15 19:55:22 +01:00
|
|
|
public BufferedImageWrapper PORTRAIT;
|
2022-06-16 18:31:44 +01:00
|
|
|
public int currentFrame = 0;
|
|
|
|
public int frameCounter = 0;
|
2022-06-16 22:57:35 +01:00
|
|
|
public boolean isNarrator;
|
2022-06-15 16:24:45 +01:00
|
|
|
|
2022-06-16 22:57:35 +01:00
|
|
|
public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait, boolean isNarrator) {
|
2022-06-15 19:55:22 +01:00
|
|
|
super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null);
|
|
|
|
PORTRAIT = portrait;
|
2022-06-19 19:31:19 +01:00
|
|
|
checkForNarrator();
|
|
|
|
this.isNarrator = isNarrator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkForNarrator() {
|
2022-06-16 22:57:35 +01:00
|
|
|
if (isNarrator) {
|
|
|
|
newX = PORTRAIT_WIDTH + PADDING*2;
|
|
|
|
} else {
|
|
|
|
newX = PADDING;
|
|
|
|
}
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) {
|
2022-06-16 18:31:44 +01:00
|
|
|
text = text.substring(0, currentFrame);
|
2022-06-15 16:24:45 +01:00
|
|
|
if (backgroundColor != null) {
|
|
|
|
g.setColor(textColor);
|
2022-06-16 22:57:35 +01:00
|
|
|
if (isNarrator) {
|
|
|
|
g.drawImage(PORTRAIT.image, newX - PORTRAIT_WIDTH - PADDING, newY, PORTRAIT_WIDTH, yHeight, null);
|
|
|
|
} else {
|
|
|
|
g.drawImage(PORTRAIT.image, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING, newY, PORTRAIT_WIDTH, yHeight, null);
|
|
|
|
}
|
2022-06-15 16:24:45 +01:00
|
|
|
((Graphics2D)g).setStroke(new BasicStroke(4f));
|
2022-06-16 15:10:37 +01:00
|
|
|
g.drawRect(newX, newY, xWidth, yHeight - 4);
|
2022-06-15 16:24:45 +01:00
|
|
|
g.setColor(backgroundColor);
|
2022-06-16 15:10:37 +01:00
|
|
|
g.fillRect(newX, newY, xWidth, yHeight - 4);
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
g.setColor(textColor);
|
2022-06-15 19:55:22 +01:00
|
|
|
drawCenteredString(g, newY, newX, text);
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void drawCenteredString(Graphics g, int y, int x, String text) {
|
|
|
|
// split text by spaces
|
|
|
|
String[] newText = text.split(" ");
|
|
|
|
ArrayList<String> lines = new ArrayList<String>();
|
|
|
|
lines.add("");
|
|
|
|
// get font size
|
|
|
|
FontMetrics metrics = g.getFontMetrics();
|
|
|
|
int currentLineWidth = 0, lastLineIndex = 0;
|
|
|
|
for (String s: newText) {
|
2022-06-15 19:55:22 +01:00
|
|
|
currentLineWidth += metrics.stringWidth(s + " ");
|
|
|
|
if (currentLineWidth - metrics.stringWidth(" ") < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*5)) {
|
|
|
|
lines.set(lastLineIndex, lines.get(lastLineIndex) + s + " ");
|
2022-06-15 16:24:45 +01:00
|
|
|
} else {
|
|
|
|
currentLineWidth = metrics.stringWidth(s);
|
2022-06-15 19:55:22 +01:00
|
|
|
lines.add(s + " ");
|
|
|
|
lastLineIndex ++;
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 19:55:22 +01:00
|
|
|
y += TOP_PADDING;
|
2022-06-15 16:24:45 +01:00
|
|
|
// center y (half is above y value, half is below y value)
|
|
|
|
for (String s: lines) {
|
2022-06-15 19:55:22 +01:00
|
|
|
y += (metrics.getAscent() - metrics.getDescent()) * LINE_SPACING;
|
2022-06-15 16:24:45 +01:00
|
|
|
// draw string
|
2022-06-15 19:55:22 +01:00
|
|
|
g.drawString(s + "\n", x + PADDING, y);
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 18:31:44 +01:00
|
|
|
public boolean draw(Graphics g, String text, Color backgroundColor, Color textColor) {
|
2022-06-19 19:31:19 +01:00
|
|
|
checkForNarrator();
|
2022-06-16 18:31:44 +01:00
|
|
|
if (frameCounter >= FREQUENCY) {
|
|
|
|
frameCounter -= FREQUENCY;
|
|
|
|
currentFrame += 1;
|
|
|
|
}
|
2022-06-15 16:24:45 +01:00
|
|
|
g.setFont(font);
|
|
|
|
drawCenteredTextBox(g, text, backgroundColor, textColor);
|
2022-06-16 18:31:44 +01:00
|
|
|
frameCounter++;
|
|
|
|
if (currentFrame >= text.length()) {
|
|
|
|
currentFrame = 0;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-15 16:24:45 +01:00
|
|
|
}
|
|
|
|
}
|