UI.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package view;
  2. import util.GAMESTATE;
  3. import view.components.Button;
  4. import view.popUpMenu.PopupMenu;
  5. import view.tile.interactive.InteractiveTile;
  6. import view.tile.interactive.ONCLICKTYPE;
  7. import java.awt.*;
  8. import java.util.ArrayList;
  9. public class UI {
  10. private GamePanel gp;
  11. private Graphics2D g2;
  12. private Font arial_40;
  13. private ArrayList<Button> activeButtons = new ArrayList<>();
  14. private ArrayList<InteractiveTile> interactiveTiles = new ArrayList<>();
  15. public UI(GamePanel gp){
  16. this.gp = gp;
  17. arial_40 = new Font("Arial", Font.PLAIN, 80);
  18. }
  19. public void draw(Graphics2D g2){
  20. this.g2 = g2;
  21. g2.setFont(arial_40);
  22. g2.setColor(Color.white);
  23. if(gp.gameState == GAMESTATE.PAUSED){
  24. drawPauseScreen(g2, "Game paused");
  25. }
  26. }
  27. public void handleMenuClick(int screenX, int screenY) {
  28. for (Button button : activeButtons) {
  29. if (button.wasClicked(screenX, screenY)) {
  30. button.click();
  31. break;
  32. }
  33. }
  34. }
  35. public void handleClick(int screenX, int screenY){
  36. for (InteractiveTile tile : gp.tileManager.interactiveTiles) {
  37. if(tile.getClicked() && tile.getOnClickType() == ONCLICKTYPE.POPUP){
  38. PopupMenu popupMenu = tile.getPopupMenu();
  39. Button clickedButton = popupMenu.getClickedButton(screenX,screenY);
  40. if(clickedButton != null){
  41. clickedButton.click();
  42. }
  43. }
  44. if (tile.isClicked(screenX, screenY, gp.camera) && !tile.getClicked()) {
  45. tile.click(screenX, screenY);
  46. }else if(tile.getClicked()){
  47. tile.unClick();
  48. }
  49. }
  50. }
  51. private void drawPauseScreen(Graphics2D g2, String message) {
  52. // Background overlay
  53. g2.setColor(new Color(0, 0, 0, 150));
  54. g2.fillRect(0, 0, gp.screenWidth, gp.screenHeight);
  55. // Dialog box
  56. int boxWidth = gp.screenWidth / 2;
  57. int boxHeight = gp.screenHeight / 2;
  58. int boxX = gp.screenWidth / 2 - boxWidth / 2;
  59. int boxY = gp.screenHeight / 4;
  60. g2.setColor(Color.WHITE);
  61. g2.fillRoundRect(boxX, boxY, boxWidth, boxHeight, 25, 25);
  62. // Message text
  63. g2.setColor(Color.BLACK);
  64. g2.setFont(new Font("Arial", Font.BOLD, 28));
  65. FontMetrics fm = g2.getFontMetrics();
  66. int msgWidth = fm.stringWidth(message);
  67. g2.drawString(message, gp.screenWidth / 2 - msgWidth / 2, boxY + 50);
  68. // Buttons
  69. int buttonWidth = 260;
  70. int buttonHeight = 40;
  71. int spacing = 20;
  72. int buttonX = gp.screenWidth / 2 - buttonWidth / 2;
  73. int resumeY = boxY + 100;
  74. int saveY = resumeY + buttonHeight + spacing;
  75. int exitY = resumeY + buttonHeight + buttonHeight + spacing + spacing;
  76. Button resumeButton = new Button(buttonWidth, buttonHeight, "Continue", () -> {
  77. gp.gameState = GAMESTATE.PLAY;
  78. });
  79. drawAndRegisterButton(resumeButton, buttonX, resumeY);
  80. Button exitButton = new Button(buttonWidth, buttonHeight, "Quit", () -> {
  81. gp.gameState = GAMESTATE.QUIT;
  82. });
  83. drawAndRegisterButton(exitButton, buttonX, exitY);
  84. Button saveButton = new Button(buttonWidth, buttonHeight, "Save", () -> {
  85. gp.gameState = GAMESTATE.SAVE;
  86. });
  87. drawAndRegisterButton(saveButton, buttonX, saveY);
  88. }
  89. private void drawAndRegisterButton(Button button, int buttonX, int buttonY){
  90. button.setScreenCoordinates(buttonX, buttonY);
  91. button.draw(g2);
  92. activeButtons.add(button);
  93. }
  94. public int getXForCenteredText(String text){
  95. int length = (int)g2.getFontMetrics().getStringBounds(text, g2).getWidth();
  96. return gp.screenWidth/2 - length/2;
  97. }
  98. public void closeMenus() {
  99. for (InteractiveTile tile : gp.tileManager.interactiveTiles) {
  100. tile.unClick();
  101. }
  102. }
  103. }