Coverage Summary for Class: Popups (io.github.unisim.ui)
| Class |
Method, %
|
Branch, %
|
Line, %
|
| Popups |
0%
(0/8)
|
0%
(0/16)
|
0%
(0/137)
|
| Popups$1 |
0%
(0/2)
|
0%
(0/2)
|
0%
(0/3)
|
| Popups$2 |
0%
(0/2)
|
0%
(0/2)
|
0%
(0/6)
|
| Popups$3 |
0%
(0/2)
|
0%
(0/3)
|
| Popups$4 |
0%
(0/2)
|
0%
(0/2)
|
0%
(0/5)
|
| Popups$5 |
0%
(0/2)
|
0%
(0/3)
|
| Popups$6 |
0%
(0/2)
|
0%
(0/3)
|
| Total |
0%
(0/20)
|
0%
(0/22)
|
0%
(0/160)
|
package io.github.unisim.ui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import io.github.unisim.AchievementManager;
import io.github.unisim.GameState;
public class Popups extends JFrame {
private boolean isActive = false;
private int displayWidth = Gdx.graphics.getDisplayMode().width / 2;
private int displayHeight = Gdx.graphics.getDisplayMode().height / 2;
private GameScreen gameScreen;
private ArrayList<String> infoHolder;
public Popups(GameScreen gameScreen) {
this.gameScreen = gameScreen;
}
public boolean getActive() {
return isActive;
}
public void setActive(boolean active) {
this.isActive = active;
}
public void namePopup() {
isActive = true;
final JFrame parent = new JFrame();
JTextField input = new JTextField();
parent.setUndecorated(true);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.GRAY, 5)); // Blue border with 5px thickness
panel.setLayout(new BorderLayout());
JButton playBtn = new JButton("PLAY");
JButton backBtn = new JButton("BACK");
JLabel label = new JLabel("Enter player name:");
parent.setAlwaysOnTop(true); // Makes sure the popup is always above other windows
input.setFont(new Font("Verdana", Font.BOLD, 20));
input.addKeyListener((KeyListener) new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (input.getText().length() >= 18) // limit textfield to 3 characters
e.consume();
}
});
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.NORTH);
panel.add(backBtn, BorderLayout.SOUTH);
centerPanel.add(input, BorderLayout.NORTH);
centerPanel.add(playBtn, BorderLayout.SOUTH);
panel.add(centerPanel, BorderLayout.CENTER);
parent.add(panel);
parent.pack();
int width = 300;
int height = 120;
parent.setBounds(displayWidth - (width / 2), displayHeight - (height / 2), width, height);
parent.setVisible(true);
playBtn.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (input.getText().isEmpty() == false) {
GameState.settings.setPlayerName(input.getText());
GameState.currentScreen = GameState.gameScreen;
AchievementManager.getInstance().resetAchievements();
parent.dispose();
}
}
});
backBtn.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
isActive = false;
parent.dispose();
}
});
parent.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public void event() {
FileHandle file = Gdx.files.internal("Events.txt");
String text = file.readString(); // Read the entire file content
String[] lines = text.split("\r?\n");
int randomNum = (int) (Math.random() * lines.length); // Select a random line
String selectedLine = lines[randomNum].trim();
String[] words = selectedLine.split("@"); // Split the line into components using "@"
ArrayList<String> eventList = new ArrayList<>();
for (String str : words) {
eventList.add(str.trim()); // Add each trimmed component to the event list
}
eventPopup(eventList);
}
private int eventAction(String event) {
if (event.equals("NULL")) { // Use .equals() for string comparison
resultPopup("Invalid Button.", false);
return 0;
}
String[] events = event.split("\\|"); // Split multiple events by "|"
for (String eventData : events) {
String[] words = eventData.trim().split(" ");
int amount = Integer.valueOf(words[words.length - 1]);
String eventName = eventData.replace(" " + amount, "").trim();
switch (eventName) {
case "Add money":
this.gameScreen.addMoneyRep(amount, 0);
resultPopup("You've received $" + amount, true);
break;
case "Remove money":
if (!this.gameScreen.checkMoney(amount)) { // Insufficient money
resultPopup("Not enough money to proceed!", false);
eventPopup(infoHolder); // Redisplay the event
return 0;
}
this.gameScreen.addMoneyRep(-amount, 0);
resultPopup("You've lost $" + amount, true);
break;
case "Add reputation":
this.gameScreen.addMoneyRep(0, amount);
resultPopup("You've received %" + amount, true);
break;
case "Remove reputation":
if (!this.gameScreen.checkRep(amount)) { // Insufficient reputation
resultPopup("Not enough reputation to proceed! You lost!", false);
GameState.gameOver = true;
return 0;
}
this.gameScreen.addMoneyRep(0, -amount);
resultPopup("You've lost %" + amount, true);
break;
default:
System.out.println("Unknown event: " + eventName);
break;
}
}
isActive = false;
return 0;
}
public void resultPopup(String result, boolean unpauseOnClose) {
isActive = true;
GameState.paused = true;
final JFrame parent = new JFrame();
JButton button = new JButton("<html>" + result + "<br>Click the popup to continue!<html>");
parent.setUndecorated(true);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.GRAY, 3));
panel.setLayout(new BorderLayout());
parent.setAlwaysOnTop(true); // Makes sure the popup is always above other windows
panel.add(button);
parent.add(panel);
parent.pack();
int width = 300;
int height = 150;
parent.setBounds(displayWidth - (width / 2), displayHeight - (height / 2), width, height);
parent.setVisible(true);
parent.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
isActive = false;
if (unpauseOnClose == true) {
GameState.paused = false;
}
parent.dispose();
}
});
}
private void eventPopup(ArrayList<String> info) {
infoHolder = new ArrayList<>(info);
isActive = true;
GameState.paused = true;
while (infoHolder.size() <= 6) {
infoHolder.add("NULL");
}
final JFrame parent = new JFrame();
parent.setAlwaysOnTop(true); // Popup always above other windows
parent.setUndecorated(true);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.GRAY, 3)); // Gray border with 3px thickness
panel.setLayout(new BorderLayout(10, 10)); // Add spacing between components
// Title Panel
JPanel titlePanel = new JPanel();
JLabel title = new JLabel(infoHolder.get(0), SwingConstants.CENTER);
title.setFont(new Font("Verdana", Font.BOLD, 30));
titlePanel.add(title);
// Event Panel (Scrollable for long descriptions)
JPanel eventPanel = new JPanel(new BorderLayout());
JTextArea desc = new JTextArea(infoHolder.get(1));
desc.setFont(new Font("Verdana", Font.PLAIN, 15));
desc.setLineWrap(true);
desc.setWrapStyleWord(true);
desc.setEditable(false);
JScrollPane scrollPane = new JScrollPane(desc);
scrollPane.setPreferredSize(new Dimension(550, 150));
eventPanel.add(scrollPane, BorderLayout.CENTER);
// Button Panel
JPanel panelBtn = new JPanel(new GridLayout(1, 2, 10, 0)); // Space between buttons
JButton button1 = new JButton("<html>" + infoHolder.get(2) + "</html>");
button1.setPreferredSize(new Dimension(250, 60));
button1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
eventAction(infoHolder.get(3));
parent.dispose();
}
});
panelBtn.add(button1);
JButton button2 = new JButton("<html>" + infoHolder.get(4) + "</html>");
button2.setPreferredSize(new Dimension(250, 30));
button2.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
eventAction(infoHolder.get(5));
parent.dispose();
}
});
panelBtn.add(button2);
// Main Frame Layout
parent.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
parent.setLayout(new BorderLayout());
panel.add(titlePanel, BorderLayout.NORTH);
panel.add(eventPanel, BorderLayout.CENTER);
panel.add(panelBtn, BorderLayout.SOUTH);
parent.add(panel);
parent.pack();
parent.setResizable(false);
// Adjust position and size
int width = 600;
int height = 400;
parent.setBounds(displayWidth / 2 - width / 2, displayHeight / 2 - height / 2, width, height);
parent.setVisible(true);
}
}