From 9fb7682294bdeb14d01b91a171d6c5d1d588758e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Schr=C3=B6der?= <fschroeder@techfak.uni-bielefeld.de> Date: Mon, 11 Mar 2024 15:07:07 +0100 Subject: [PATCH] Add layout selection dropdown to GUI The commit replaces the previous system file access to layout configuration files with a more user-friendly dropdown menu for layout selection. It ensures that the selected layout is correctly identified in all relevant parts of the code. The new method also includes error handling for cases where no layout files are found and an update to the menu state after level completion. --- cooperative_cuisine/pygame_2d_vis/gui.py | 51 +++++++++++++++++++----- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/cooperative_cuisine/pygame_2d_vis/gui.py b/cooperative_cuisine/pygame_2d_vis/gui.py index 645d5a46..42a539c2 100644 --- a/cooperative_cuisine/pygame_2d_vis/gui.py +++ b/cooperative_cuisine/pygame_2d_vis/gui.py @@ -191,9 +191,10 @@ class PyGameGUI: self.sub_processes = [] - self.layout_file_paths = sorted( - (ROOT_DIR / "configs" / "layouts").rglob("*.layout") - ) + self.layout_file_paths_dict = { + p.name: p for p in (ROOT_DIR / "configs" / "layouts").rglob("*.layout") + } + self.layout_file_paths = sorted(list(self.layout_file_paths_dict.keys())) self.current_layout_idx = 0 self.last_state: StateRepresentation @@ -506,7 +507,23 @@ class PyGameGUI: img_height = img_width * (image_rect.height / image_rect.width) new_dims = (img_width, img_height) self.press_a_image.set_dimensions(new_dims) - + if not self.CONNECT_WITH_STUDY_SERVER: + 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( + ( + 0, + 0, + ), + (dropdown_width, dropdown_height), + ), + manager=self.manager, + options_list=self.layout_file_paths, + starting_option="basic.layout" + if "basic.layout" in self.layout_file_paths + else random.choice(self.layout_file_paths), + ) player_selection_rect = pygame.Rect( (0, 0), ( @@ -951,6 +968,11 @@ class PyGameGUI: self.fullscreen_button, ] + if not self.CONNECT_WITH_STUDY_SERVER: + self.start_screen_elements.append(self.layout_selection) + else: + self.other_elements.append(self.layout_selection) + self.tutorial_screen_elements = [ self.tutorial_image, self.continue_button, @@ -1269,7 +1291,10 @@ class PyGameGUI: environment_config_path = ROOT_DIR / "configs" / "tutorial_env_config.yaml" else: environment_config_path = ROOT_DIR / "configs" / "environment_config.yaml" - layout_path = self.layout_file_paths[self.current_layout_idx] + layout_path = self.layout_file_paths_dict[ + self.layout_selection.selected_option + ] + # layout_path = self.layout_file_paths[self.current_layout_idx] item_info_path = ROOT_DIR / "configs" / "item_info.yaml" with open(item_info_path, "r") as file: @@ -1320,6 +1345,8 @@ class PyGameGUI: number_key_sets=min(self.number_humans_to_be_added, num_key_set), disjunct=self.split_players, ) + self.level_info = env_info + self.level_info["name"] = self.layout_selection.selected_option def update_pregame_screen(self): self.level_name_label.set_text(f"Level: {self.level_info['name']}") @@ -1716,12 +1743,14 @@ class PyGameGUI: if not self.last_level: self.get_game_connection() else: - self.current_layout_idx += 1 - self.create_env_on_game_server() - if self.current_layout_idx == len(self.layout_file_paths) - 1: - self.last_level = True - else: - log.debug(f"LEVEL: {self.layout_file_paths[self.current_layout_idx]}") + # self.current_layout_idx += 1 + self.menu_state = MenuStates.Start + return + # self.create_env_on_game_server(tutorial=False) + # if self.current_layout_idx == len(self.layout_file_paths) - 1: + # self.last_level = True + # else: + # log.debug(f"LEVEL: {self.layout_file_paths[self.current_layout_idx]}") self.menu_state = MenuStates.PreGame -- GitLab