UI.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package view.ui;
  2. import controller.InteractiveController;
  3. import controller.entity.EntityController;
  4. import controller.tiles.interactive.InteractiveTileController;
  5. import util.Translator;
  6. import view.GamePanel;
  7. import view.components.Button;
  8. import view.popUpMenu.PopupMenu;
  9. import view.tile.ONCLICKTYPE;
  10. import view.ui.menu.PauseMenu;
  11. import java.awt.*;
  12. import java.util.ArrayList;
  13. public class UI {
  14. private GamePanel gp;
  15. private Graphics2D g2;
  16. private Font arial_40;
  17. private static ArrayList<Button> activeButtons = new ArrayList<>();
  18. public UI(GamePanel gp){
  19. this.gp = gp;
  20. arial_40 = new Font("Arial", Font.PLAIN, 80);
  21. }
  22. public void draw(Graphics2D g2){
  23. this.g2 = g2;
  24. g2.setFont(arial_40);
  25. g2.setColor(Color.white);
  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 (InteractiveTileController tile : gp.gameController.interactiveTileControllers) {
  37. handleClickForController(tile, screenX, screenY);
  38. }
  39. for (EntityController controller : gp.gameController.entityControllers) {
  40. handleClickForController(controller, screenX, screenY);
  41. }
  42. }
  43. private void handleClickForController(InteractiveController interactiveController, int screenX, int screenY){
  44. if(interactiveController.getClicked() && interactiveController.getOnClickType() == ONCLICKTYPE.POPUP){
  45. PopupMenu popupMenu = interactiveController.getPopupMenu();
  46. Button clickedButton = popupMenu.getClickedButton(screenX,screenY);
  47. if(clickedButton != null){
  48. clickedButton.click();
  49. interactiveController.unClick();
  50. return;
  51. }
  52. }
  53. if (interactiveController.isClicked(screenX, screenY, gp.camera) && !interactiveController.getClicked()) {
  54. interactiveController.click(screenX, screenY);
  55. }else if(interactiveController.getClicked()){
  56. interactiveController.unClick();
  57. }
  58. }
  59. public static void drawAndRegisterButton(Graphics2D g2, Button button, int buttonX, int buttonY){
  60. button.setScreenCoordinates(buttonX, buttonY);
  61. button.draw(g2);
  62. activeButtons.add(button);
  63. }
  64. public int getXForCenteredText(String text){
  65. int length = (int)g2.getFontMetrics().getStringBounds(text, g2).getWidth();
  66. return gp.screenWidth/2 - length/2;
  67. }
  68. public void closeMenus() {
  69. for (InteractiveTileController tile : gp.gameController.interactiveTileControllers) {
  70. tile.unClick();
  71. }
  72. for(EntityController e : gp.gameController.entityControllers){
  73. e.unClick();
  74. }
  75. }
  76. }