2020-05-10 20:13:02 +02:00

352 lines
13 KiB
Java

package ruscz.game.gameOfProd.LevelEditor;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
public class InfoManager {
private LevelEditor editor;
private JPanel info;
private JScrollPane scrollInfo;
private JLayeredPane view;
private LinkedList<JRadioButton> radioButtons;
public InfoManager(LevelEditor editor) {
this.editor = editor;
this.view = editor.getView();
}
protected void displayInfo(JLabel label, String layer, int x, int y) {
if (info != null) {
info.removeAll();
view.remove(info);
info = null;
}
if (scrollInfo != null) {
scrollInfo.removeAll();
view.remove(scrollInfo);
scrollInfo = null;
view.repaint();
}
info = new JPanel();
info.setBounds(20, 200, 260, 730);
info.setLayout(null);
switch (layer) {
case "object":
displayObjectInfo(label, x, y);
break;
case "cell":
displayCellInfo(label, x, y);
break;
}
view.add(info);
view.revalidate();
}
private void displayObjectInfo(JLabel label, int x, int y) {
JLabel infoLabel = new JLabel();
infoLabel.setBounds(0, 0, 50, 50);
infoLabel.setIcon(label.getIcon());
infoLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
JLabel numValue = new JLabel("Inner Value:");
numValue.setBounds(90, 10, 100, 30);
JTextField tileNum = new JTextField(label.getName());
tileNum.setBounds(170, 10, 50, 30);
JButton importTile = new JButton("Import selected tile");
importTile.setVisible(false);
importTile.setBounds(50, 150, 150, 30);
importTile.addActionListener(e -> {
if (!editor.getOverview().getInfo().getText().equals("")) {
infoLabel.setIcon(editor.getOverview().getInfo().getIcon());
tileNum.setText(editor.getOverview().getInfo().getText().replace("<html><span color='black'>", "").replace("</span></html>", ""));
}
else {
editor.getOverview().setState(Frame.NORMAL);
editor.getOverview().toFront();
}
});
JButton selectTile = new JButton("Select tile");
selectTile.setBounds(50, 100, 150, 30);
selectTile.addActionListener(e -> {
if (editor.getOverview() == null) editor.setOverview(new TilesetOverview());
else {
editor.getOverview().setState(Frame.NORMAL);
editor.getOverview().toFront();
}
importTile.setVisible(true);
});
JButton saveInfo = new JButton("Save tile info");
saveInfo.setBounds(50, 200, 150, 30);
saveInfo.addActionListener(e -> {
if (editor.getImagesOfTiles() != null) {
if (Integer.parseInt(tileNum.getText()) > editor.getImagesOfTiles().size() || Integer.parseInt(tileNum.getText()) < 0) {
editor.displayError("Inner value out of bounds. Value has to bee between 0 and " + (editor.getImagesOfTiles().size() - 1) + ".");
return;
}
}
else if (Integer.parseInt(tileNum.getText()) < 0) {
editor.displayError("Inner value out of bounds. Value has to bee higher than 0.");
return;
}
editor.hideError();
editor.getMap().getImageCell().getMapForeground()[x][y] = Integer.parseInt(tileNum.getText());
editor.getMapLayers().repaintObjectset();
info.removeAll();
view.remove(info);
view.revalidate();
view.repaint();
});
info.add(infoLabel);
info.add(numValue);
info.add(tileNum);
info.add(selectTile);
info.add(importTile);
info.add(saveInfo);
}
private void displayCellInfo(JLabel label, int x, int y) {
String[] array = {"EMPTY", "BLOCK", "INTER", "TRIGGER", "MOVETO", "DOOR", "DOORTO", "CHEST"};
radioButtons = new LinkedList<>();
JLabel infoLabel = new JLabel();
infoLabel.setBounds(0, 0, 50, 50);
infoLabel.setIcon(label.getIcon());
infoLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
JLabel valueLabel = new JLabel("Inner Value:");
valueLabel.setBounds(90, 10, 100, 30);
JButton openCellMap = new JButton("Open CellMap Configuration");
JButton saveInfo = new JButton("Save tile info");
JComboBox cellType = new JComboBox(array);
cellType.setBounds(170, 10, 90, 30);
String[] cellname = label.getName().split(",");
cellType.setSelectedItem(cellname[0]);
cellType.addActionListener(e -> {
if (editor.getImagesOfTiles() != null && editor.getImagesOfTiles().size() > 0) {
String value;
if (cellType.getSelectedItem().equals("DOOR") || cellType.getSelectedItem().equals("DOORTO")) value = "DOOR/DOORTO";
else value = String.valueOf(cellType.getSelectedItem());
infoLabel.setIcon(new ImageIcon(editor.getImagesOfTiles().get(editor.getCellMap().get(value)).getScaledInstance(50, 50, Image.SCALE_DEFAULT)));
}
displayDetailedCellInfo(String.valueOf(cellType.getSelectedItem()), openCellMap, saveInfo);
info.requestFocus();
info.revalidate();
});
openCellMap.setBounds(30, 70, 200, 30);
openCellMap.addActionListener(e -> {
info.removeAll();
info.revalidate();
view.remove(info);
view.revalidate();
displayCellSetConfig();
});
saveInfo.setBounds(50, 120, 150, 30);
saveInfo.addActionListener(e -> {
editor.getCellArray()[x][y] = String.valueOf(cellType.getSelectedItem());
editor.getMapLayers().repaintCellSet();
info.removeAll();
info.revalidate();
view.remove(info);
view.revalidate();
view.repaint();
});
info.add(infoLabel);
info.add(valueLabel);
info.add(cellType);
info.add(openCellMap);
info.add(saveInfo);
}
private void displayDetailedCellInfo(String type, JButton open, JButton save) {
if (type.equals("BLOCK")) {
upperLowerInfo(open, save);
}
}
private void upperLowerInfo(JButton open, JButton save) {
JLabel label = new JLabel("<html><div style='text-align: center; width: 260'>Half Block</div></html>");
label.setBounds(0, 120, 260, 20);
JRadioButton up = new JRadioButton("Upper");
up.setBounds(0, 150, 70, 20);
up.setName("up");
radioButtons.add(up);
JRadioButton low = new JRadioButton("Lower");
low.setBounds(100, 150, 70, 20);
low.setName("low");
radioButtons.add(low);
JRadioButton none = new JRadioButton("None");
none.setBounds(200, 150, 70, 20);
none.setSelected(true);
none.setName("none");
radioButtons.add(none);
ButtonGroup group = new ButtonGroup();
group.add(up);
group.add(low);
group.add(none);
open.setLocation(open.getX(), open.getY() + 110);
save.setLocation(save.getX(), save.getY() + 110);
info.add(label);
info.add(up);
info.add(low);
info.add(none);
}
private void displayCellSetConfig() {
int offset = 0;
String[] array = {"BLOCK", "INTER", "TRIGGER", "MOVETO", "DOOR/DOORTO", "CHEST"}; //EMPTY is missing on purpose, DOOR & DOORTO are combined on purpose
LinkedList<JTextField> numFields = new LinkedList<>();
JPanel infoPanel = new JPanel();
infoPanel.setPreferredSize(new Dimension(250, array.length * 160 + 50));
infoPanel.setLayout(null);
if (scrollInfo != null) {
scrollInfo.removeAll();
view.remove(scrollInfo);
scrollInfo = null;
}
scrollInfo = new JScrollPane(infoPanel);
scrollInfo.setBounds(20, 200, 270, 730);
scrollInfo.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollInfo.setWheelScrollingEnabled(true);
scrollInfo.getVerticalScrollBar().setUnitIncrement(10);
for (String name : array) {
JLabel nameLabel = nameLabel(name, offset);
JLabel iconLabel = iconLabel(name, offset);
JLabel valueLabel = valueLabel(offset);
JTextField numField = numField(name, offset);
numFields.add(numField);
JButton importButton = importButton(iconLabel, numField, offset);
JButton selectButton = selectButton(importButton, offset);
JLabel dividerLabel = dividerLabel(offset);
infoPanel.add(nameLabel);
infoPanel.add(iconLabel);
infoPanel.add(valueLabel);
infoPanel.add(numField);
infoPanel.add(importButton);
infoPanel.add(selectButton);
infoPanel.add(dividerLabel);
offset += 160;
}
JButton saveInfo = new JButton("Save CellMap info");
saveInfo.setBounds(50, offset, 150, 30);
saveInfo.addActionListener(e -> {
for (JTextField field : numFields) {
try {
editor.getCellMap().replace(field.getName(), Integer.parseInt(field.getText()));
}
catch (NumberFormatException ex) {
editor.displayError("Inner Value for " + field.getName() + " is not an Integer.");
return;
}
}
editor.hideError();
editor.getMapLayers().repaintCellSet();
scrollInfo.removeAll();
scrollInfo.revalidate();
view.remove(scrollInfo);
view.revalidate();
view.repaint();
});
infoPanel.add(saveInfo);
scrollInfo.revalidate();
scrollInfo.requestFocus();
view.add(scrollInfo);
}
private JLabel nameLabel(String name, int offset) {
JLabel label = new JLabel("<html><div style='width: 250; text-align:center; font-size: 16'>" + name + "</div></html>");
label.setBounds(0, offset, 250, 20);
return label;
}
private JLabel iconLabel(String name, int offset) {
JLabel label = new JLabel();
label.setBounds(0, offset + 40, 50, 50);
if (editor.getImagesOfTiles() != null && editor.getImagesOfTiles().size() > 0)
label.setIcon(new ImageIcon(editor.getImagesOfTiles().get(editor.getCellMap().get(name)).getScaledInstance(50, 50, Image.SCALE_DEFAULT)));
label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
return label;
}
private JLabel valueLabel(int offset) {
JLabel label = new JLabel("Inner Value:");
label.setBounds(80, offset + 40, 100, 30);
return label;
}
private JTextField numField(String name, int offset) {
JTextField field = new JTextField(String.valueOf(editor.getCellMap().get(name)));
field.setName(name);
field.setBounds(160, offset + 40, 50, 30);
return field;
}
private JButton importButton(JLabel iconLabel, JTextField tileNum, int offset) {
JButton button = new JButton("Import selected tile");
button.setVisible(false);
button.setBounds(100, offset + 100, 150, 30);
button.addActionListener(e -> {
if (!editor.getOverview().getInfo().getText().equals("")) {
iconLabel.setIcon(editor.getOverview().getInfo().getIcon());
tileNum.setText(editor.getOverview().getInfo().getText().replace("<html><span color='black'>", "").replace("</span></html>", ""));
}
else {
editor.getOverview().setState(Frame.NORMAL);
editor.getOverview().toFront();
}
});
return button;
}
private JButton selectButton(JButton importButton, int offset) {
JButton button = new JButton("Select tile");
button.setBounds(0, offset + 100, 90, 30);
button.addActionListener(e -> {
if (editor.getOverview() == null) editor.setOverview(new TilesetOverview());
else {
editor.getOverview().setState(Frame.NORMAL);
editor.getOverview().toFront();
}
importButton.setVisible(true);
});
return button;
}
private JLabel dividerLabel(int offset) {
JLabel label = new JLabel();
label.setBounds(0, offset + 140, 255, 1);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
return label;
}
}