From 70317322718f2650065f5627d7362c0d764c8d99 Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Thu, 4 Jan 2024 10:23:31 +0100
Subject: [PATCH] Correct management of gui window sizes, cleaner button press
 management.

---
 .../game_content/layouts/basic.layout         |  18 +-
 .../game_content/layouts/empty.layout         |   7 +-
 .../gui_2d_vis/overcooked_gui.py              | 194 +++++++++++-------
 .../gui_2d_vis/visualization.yaml             |   4 +-
 .../overcooked_environment.py                 |   7 +-
 5 files changed, 137 insertions(+), 93 deletions(-)

diff --git a/overcooked_simulator/game_content/layouts/basic.layout b/overcooked_simulator/game_content/layouts/basic.layout
index 36384db9..2e7395aa 100644
--- a/overcooked_simulator/game_content/layouts/basic.layout
+++ b/overcooked_simulator/game_content/layouts/basic.layout
@@ -1,9 +1,9 @@
-#QU#TNLB#
-#_______M
-#_______#
-W________
-#__A__A__
-C________
-C_______#
-#_______X
-#P#S+#S+#
\ No newline at end of file
+#QU#T###NLB#
+#__________M
+#__________#
+W___________
+#__A_____A__
+C___________
+C__________#
+#__________X
+#P#S+####S+#
\ No newline at end of file
diff --git a/overcooked_simulator/game_content/layouts/empty.layout b/overcooked_simulator/game_content/layouts/empty.layout
index 3c68f9d5..2e7dfab8 100644
--- a/overcooked_simulator/game_content/layouts/empty.layout
+++ b/overcooked_simulator/game_content/layouts/empty.layout
@@ -1,4 +1,3 @@
-_____
-_____
-____P
-
+____
+APA_
+____
\ No newline at end of file
diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
index 47edef81..7c910c07 100644
--- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py
+++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
@@ -97,6 +97,7 @@ class PyGameGUI:
         self.game_screen = None
         self.FPS = 60
         self.simulator: Simulator = simulator
+        self.running = True
 
         self.player_names = player_names
         self.player_keys = player_keys
@@ -112,40 +113,51 @@ class PyGameGUI:
         with open(ROOT_DIR / "gui_2d_vis" / "visualization.yaml", "r") as file:
             self.visualization_config = yaml.safe_load(file)
 
-        if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width":
-            self.world_width = self.visualization_config["GameWindow"]["size"]
-            kitchen_aspect_ratio = (
-                simulator.env.kitchen_height / simulator.env.kitchen_width
-            )
-            self.world_height = int(self.world_width * kitchen_aspect_ratio)
-            self.grid_size = int(self.world_width / simulator.env.kitchen_width)
-        elif self.visualization_config["GameWindow"]["WhatIsFixed"] == "grid":
-            self.grid_size = self.visualization_config["GameWindow"]["size"]
-            self.world_width, self.world_height = (
-                simulator.env.kitchen_width * self.grid_size,
-                simulator.env.kitchen_height * self.grid_size,
-            )
-
         self.screen_margin = self.visualization_config["GameWindow"]["screen_margin"]
+        self.window_width = self.visualization_config["GameWindow"]["start_width"]
+        self.window_height = self.visualization_config["GameWindow"]["start_height"]
 
-        self.window_width, self.window_height = (
-            self.world_width + (2 * self.screen_margin),
-            self.world_height + (2 * self.screen_margin),
-        )
-        self.game_width, self.game_height = (
-            self.world_width,
-            self.world_height,
+        self.main_window = pygame.display.set_mode(
+            (
+                self.window_width,
+                self.window_height,
+            )
         )
 
-        self.images_path = ROOT_DIR / "pygame_gui" / "images"
+        self.game_width, self.game_height = 0, 0
 
-        self.player_colors = self.create_player_colors()
+        self.images_path = ROOT_DIR / "pygame_gui" / "images"
 
         self.image_cache_dict = {}
 
         self.menu_state = MenuStates.Start
         self.manager: pygame_gui.UIManager
 
+    def init_window_sizes(self):
+        if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width":
+            game_width = self.visualization_config["GameWindow"]["size"]
+            kitchen_aspect_ratio = (
+                self.simulator.env.kitchen_height / self.simulator.env.kitchen_width
+            )
+            game_height = int(game_width * kitchen_aspect_ratio)
+            grid_size = int(game_width / self.simulator.env.kitchen_width)
+        elif self.visualization_config["GameWindow"]["WhatIsFixed"] == "grid":
+            grid_size = self.visualization_config["GameWindow"]["size"]
+            game_width, game_height = (
+                self.simulator.env.kitchen_width * grid_size,
+                self.simulator.env.kitchen_height * grid_size,
+            )
+        else:
+            game_width, game_height = 0, 0
+            grid_size = 0
+
+        window_width, window_height = (
+            game_width + (2 * self.screen_margin),
+            game_height + (2 * self.screen_margin),
+        )
+
+        return window_width, window_height, game_width, game_height, grid_size
+
     def create_player_colors(self) -> list[Color]:
         number_player = len(self.simulator.env.players)
         hue_values = np.linspace(0, 1, number_player + 1)
@@ -569,32 +581,66 @@ class PyGameGUI:
             starting_option=layout_file_paths[-1],
         )
 
+    def setup_windows(self):
+        (
+            self.window_width,
+            self.window_height,
+            self.game_width,
+            self.game_height,
+            self.grid_size,
+        ) = self.init_window_sizes()
+
+        self.game_screen = pygame.Surface(
+            (
+                self.game_width,
+                self.game_height,
+            ),
+        )
+
+        self.main_window = pygame.display.set_mode(
+            (
+                self.window_width,
+                self.window_height,
+            )
+        )
+        self.player_colors = self.create_player_colors()
+
     def setup_simulation(self, config_path, layout_path):
         self.simulator = Simulator(config_path, layout_path, 600)
-
         number_player = len(self.player_names)
         for i in range(number_player):
             player_name = f"p{i}"
             self.simulator.register_player(player_name)
         self.simulator.start()
 
-    def change_to_start_window(self):
-        self.simulator.stop()
-        self.menu_state = MenuStates.Start
-        self.back_button.hide()
-        self.quit_button.show()
-        self.start_button.show()
-        self.score_label.hide()
-        self.finished_button.hide()
-        self.layout_selection.show()
-
-    def change_to_game_window(self):
+    def manage_button_visibility(self):
+        match self.menu_state:
+            case MenuStates.Start:
+                self.back_button.hide()
+                self.quit_button.show()
+                self.start_button.show()
+                self.score_label.hide()
+                self.finished_button.hide()
+                self.layout_selection.show()
+            case MenuStates.Game:
+                self.start_button.hide()
+                self.back_button.show()
+                self.score_label.hide()
+                self.finished_button.show()
+                self.layout_selection.hide()
+            case MenuStates.End:
+                self.start_button.hide()
+                self.back_button.show()
+                self.score_label.show()
+                self.score_label.set_text(
+                    f"Your Score is {self.simulator.env.game_score.score}"
+                )
+                self.finished_button.hide()
+                self.layout_selection.hide()
+
+    def start_button_press(self):
         self.menu_state = MenuStates.Game
-        self.start_button.hide()
-        self.back_button.show()
-        self.score_label.hide()
-        self.finished_button.show()
-        self.layout_selection.hide()
+
         layout_path = (
             ROOT_DIR
             / "game_content"
@@ -602,19 +648,26 @@ class PyGameGUI:
             / self.layout_selection.selected_option
         )
         config_path = ROOT_DIR / "game_content" / "environment_config.yaml"
+
         self.setup_simulation(config_path, layout_path)
+        self.setup_windows()
+        self.init_ui_elements()
+        log.debug("Pressed start button")
+
+    def back_button_press(self):
+        self.simulator.stop()
+        self.menu_state = MenuStates.Start
+        log.debug("Pressed back button")
+
+    def quit_button_press(self):
+        self.simulator.stop()
+        self.running = False
+        log.debug("Pressed quit button")
 
-    def change_to_end_window(self):
+    def finished_button_press(self):
         self.simulator.stop()
         self.menu_state = MenuStates.End
-        self.start_button.hide()
-        self.back_button.show()
-        self.score_label.show()
-        self.score_label.set_text(
-            f"Your Score is {self.simulator.env.game_score.score}"
-        )
-        self.finished_button.hide()
-        self.layout_selection.hide()
+        log.debug("Pressed finished button")
 
     def start_pygame(self):
         """Starts pygame and the gui loop. Each frame the game state is visualized and keyboard inputs are read."""
@@ -622,45 +675,36 @@ class PyGameGUI:
         pygame.init()
         pygame.font.init()
 
+        self.setup_windows()
         self.init_ui_elements()
 
-        self.screen_margin = 100
-        self.main_window = pygame.display.set_mode(
-            (
-                self.window_width,
-                self.window_height,
-            )
-        )
-        self.game_screen = pygame.Surface(
-            (
-                self.game_width,
-                self.game_height,
-            ),
-        )
         pygame.display.set_caption("Simple Overcooked Simulator")
 
         clock = pygame.time.Clock()
 
-        self.change_to_start_window()
+        self.manage_button_visibility()
+
         # Game loop
-        running = True
-        while running:
+        self.running = True
+        while self.running:
             try:
                 time_delta = clock.tick(self.FPS) / 1000.0
 
                 for event in pygame.event.get():
                     if event.type == pygame.QUIT:
-                        running = False
+                        self.running = False
                     if event.type == pygame_gui.UI_BUTTON_PRESSED:
-                        if event.ui_element == self.start_button:
-                            self.change_to_game_window()
-                        if event.ui_element == self.back_button:
-                            self.change_to_start_window()
-                        if event.ui_element == self.finished_button:
-                            self.change_to_end_window()
-                        if event.ui_element == self.quit_button:
-                            running = False
-                            log.debug("Quitting game")
+                        match event.ui_element:
+                            case self.start_button:
+                                self.start_button_press()
+                            case self.back_button:
+                                self.back_button_press()
+                            case self.finished_button:
+                                self.finished_button_press()
+                            case self.quit_button:
+                                self.quit_button_press()
+
+                        self.manage_button_visibility()
 
                     if (
                         event.type in [pygame.KEYDOWN, pygame.KEYUP]
diff --git a/overcooked_simulator/gui_2d_vis/visualization.yaml b/overcooked_simulator/gui_2d_vis/visualization.yaml
index 081f6b0b..02176142 100644
--- a/overcooked_simulator/gui_2d_vis/visualization.yaml
+++ b/overcooked_simulator/gui_2d_vis/visualization.yaml
@@ -2,8 +2,10 @@
 
 GameWindow:
   WhatIsFixed: window_width  # entweder grid oder window_width
-  size: 500
+  size: 600
   screen_margin: 100
+  start_width: 600
+  start_height: 600
 
 Kitchen:
   ground_tiles_color: sgigray76
diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py
index 9851aaba..6a3dce14 100644
--- a/overcooked_simulator/overcooked_environment.py
+++ b/overcooked_simulator/overcooked_environment.py
@@ -156,7 +156,7 @@ class Environment:
         Args:
             layout_file: Path to the layout file.
         """
-        current_y: float = 1.5
+        current_y: float = 0.5
         counters: list[Counter] = []
         designated_player_positions: list[npt.NDArray] = []
         free_positions: list[npt.NDArray] = []
@@ -169,7 +169,7 @@ class Environment:
 
         for line in lines:
             line = line.replace("\n", "").replace(" ", "")  # remove newline char
-            current_x = 1.5
+            current_x = 0.5
             for character in line:
                 character = character.capitalize()
                 pos = np.array([current_x, current_y])
@@ -189,8 +189,7 @@ class Environment:
                     self.kitchen_width = current_x
             current_y += 1
 
-        self.kitchen_height = int(self.kitchen_height + 2.5)
-        self.kitchen_width = int(self.kitchen_width + 0.5)
+        self.kitchen_width -= 0.5
 
         return counters, designated_player_positions, free_positions
 
-- 
GitLab