| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package view;
- import util.GAMESTATE;
- import view.components.Button;
- import view.popUpMenu.PopupMenu;
- import view.tile.interactive.InteractiveTile;
- import view.tile.interactive.ONCLICKTYPE;
- import java.awt.*;
- import java.util.ArrayList;
- public class UI {
- private GamePanel gp;
- private Graphics2D g2;
- private Font arial_40;
- private ArrayList<Button> activeButtons = new ArrayList<>();
- private ArrayList<InteractiveTile> interactiveTiles = new ArrayList<>();
- public UI(GamePanel gp){
- this.gp = gp;
- arial_40 = new Font("Arial", Font.PLAIN, 80);
- }
- public void draw(Graphics2D g2){
- this.g2 = g2;
- g2.setFont(arial_40);
- g2.setColor(Color.white);
- if(gp.gameState == GAMESTATE.PAUSED){
- drawPauseScreen(g2, "Game paused");
- }
- }
- public void handleMenuClick(int screenX, int screenY) {
- for (Button button : activeButtons) {
- if (button.wasClicked(screenX, screenY)) {
- button.click();
- break;
- }
- }
- }
- public void handleClick(int screenX, int screenY){
- for (InteractiveTile tile : gp.tileManager.interactiveTiles) {
- if(tile.getClicked() && tile.getOnClickType() == ONCLICKTYPE.POPUP){
- PopupMenu popupMenu = tile.getPopupMenu();
- Button clickedButton = popupMenu.getClickedButton(screenX,screenY);
- if(clickedButton != null){
- clickedButton.click();
- }
- }
- if (tile.isClicked(screenX, screenY, gp.camera) && !tile.getClicked()) {
- tile.click(screenX, screenY);
- }else if(tile.getClicked()){
- tile.unClick();
- }
- }
- }
- private void drawPauseScreen(Graphics2D g2, String message) {
- // Background overlay
- g2.setColor(new Color(0, 0, 0, 150));
- g2.fillRect(0, 0, gp.screenWidth, gp.screenHeight);
- // Dialog box
- int boxWidth = gp.screenWidth / 2;
- int boxHeight = gp.screenHeight / 2;
- int boxX = gp.screenWidth / 2 - boxWidth / 2;
- int boxY = gp.screenHeight / 4;
- g2.setColor(Color.WHITE);
- g2.fillRoundRect(boxX, boxY, boxWidth, boxHeight, 25, 25);
- // Message text
- g2.setColor(Color.BLACK);
- g2.setFont(new Font("Arial", Font.BOLD, 28));
- FontMetrics fm = g2.getFontMetrics();
- int msgWidth = fm.stringWidth(message);
- g2.drawString(message, gp.screenWidth / 2 - msgWidth / 2, boxY + 50);
- // Buttons
- int buttonWidth = 260;
- int buttonHeight = 40;
- int spacing = 20;
- int buttonX = gp.screenWidth / 2 - buttonWidth / 2;
- int resumeY = boxY + 100;
- int saveY = resumeY + buttonHeight + spacing;
- int exitY = resumeY + buttonHeight + buttonHeight + spacing + spacing;
- Button resumeButton = new Button(buttonWidth, buttonHeight, "Continue", () -> {
- gp.gameState = GAMESTATE.PLAY;
- });
- drawAndRegisterButton(resumeButton, buttonX, resumeY);
- Button exitButton = new Button(buttonWidth, buttonHeight, "Quit", () -> {
- gp.gameState = GAMESTATE.QUIT;
- });
- drawAndRegisterButton(exitButton, buttonX, exitY);
- Button saveButton = new Button(buttonWidth, buttonHeight, "Save", () -> {
- gp.gameState = GAMESTATE.SAVE;
- });
- drawAndRegisterButton(saveButton, buttonX, saveY);
- }
- private void drawAndRegisterButton(Button button, int buttonX, int buttonY){
- button.setScreenCoordinates(buttonX, buttonY);
- button.draw(g2);
- activeButtons.add(button);
- }
- public int getXForCenteredText(String text){
- int length = (int)g2.getFontMetrics().getStringBounds(text, g2).getWidth();
- return gp.screenWidth/2 - length/2;
- }
- public void closeMenus() {
- for (InteractiveTile tile : gp.tileManager.interactiveTiles) {
- tile.unClick();
- }
- }
- }
|