From 542a4e99c40bb7743dfce320d2acd7faf7beb549 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20Schr=C3=B6der?=
 <fschroeder@techfak.uni-bielefeld.de>
Date: Mon, 29 Jan 2024 11:02:06 +0100
Subject: [PATCH] Update layout file handling in Overcooked GUI

Refactored the way layout file paths are stored and accessed within the Overcooked simulator GUI. Now, layout file paths are stored in a dictionary which allows for more efficient and flexible usage when setting up the environment. The drop-down menu now initializes with 'basic.layout' as the default option, if it exists, otherwise a random layout is chosen.
---
 .../gui_2d_vis/overcooked_gui.py                 | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
index b6843fc6..6a466292 100644
--- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py
+++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
@@ -298,11 +298,11 @@ class PyGameGUI:
             object_id="#score_label",
         )
 
-        layout_file_paths = [
-            str(p.name)
+        self.layout_file_paths = {
+            str(p.name): p
             for p in (ROOT_DIR / "game_content" / "layouts").glob("*.layout")
-        ]
-        assert len(layout_file_paths) != 0, "No layout files."
+        }
+        assert len(self.layout_file_paths) != 0, "No layout files."
         dropdown_width, dropdown_height = 200, 40
         self.layout_selection = pygame_gui.elements.UIDropDownMenu(
             relative_rect=pygame.Rect(
@@ -313,8 +313,10 @@ class PyGameGUI:
                 (dropdown_width, dropdown_height),
             ),
             manager=self.manager,
-            options_list=layout_file_paths,
-            starting_option=layout_file_paths[-1],
+            options_list=list(self.layout_file_paths.keys()),
+            starting_option="basic.layout"
+            if "basic.layout" in self.layout_file_paths
+            else random.choice(list(self.layout_file_paths.keys())),
         )
         self.timer_label = pygame_gui.elements.UILabel(
             text="GAMETIME",
@@ -439,7 +441,7 @@ class PyGameGUI:
 
     def setup_environment(self):
         environment_config_path = ROOT_DIR / "game_content" / "environment_config.yaml"
-        layout_path = ROOT_DIR / "game_content" / "layouts" / "basic.layout"
+        layout_path = self.layout_file_paths[self.layout_selection.selected_option]
         item_info_path = ROOT_DIR / "game_content" / "item_info_debug.yaml"
         with open(item_info_path, "r") as file:
             item_info = file.read()
-- 
GitLab