From c431703989855538d7863e8194632c7b48374824 Mon Sep 17 00:00:00 2001 From: fheinrich <fheinrich@techfak.uni-bielefeld.de> Date: Tue, 16 Jan 2024 13:45:11 +0100 Subject: [PATCH] Better management of window sizes --- .../gui_2d_vis/overcooked_gui.py | 73 +++++++++++-------- .../gui_2d_vis/visualization.yaml | 4 +- .../overcooked_environment.py | 2 +- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index f50124a1..89a3acc8 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -115,8 +115,11 @@ class PyGameGUI: self.visualization_config = yaml.safe_load(file) 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.init_width = self.visualization_config["GameWindow"]["init_width"] + self.init_height = self.visualization_config["GameWindow"]["init_height"] + + self.window_width = self.init_width + self.window_height = self.init_height self.main_window = pygame.display.set_mode( ( @@ -125,7 +128,7 @@ class PyGameGUI: ) ) - self.game_width, self.game_height = 0, 0 + # self.game_width, self.game_height = 0, 0 self.images_path = ROOT_DIR / "pygame_gui" / "images" @@ -134,26 +137,25 @@ class PyGameGUI: self.menu_state = MenuStates.Start self.manager: pygame_gui.UIManager - def init_window_sizes(self): + def get_window_sizes(self, state: dir): + counter_positions = np.array([c.pos for c in state["counters"]]) + kitchen_width = counter_positions[:, 0].max() + 0.5 + kitchen_height = counter_positions[:, 1].max() + 0.5 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 - ) + kitchen_aspect_ratio = kitchen_height / 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"] == "window_height": game_height = self.visualization_config["GameWindow"]["size"] - kitchen_aspect_ratio = ( - self.simulator.env.kitchen_width / self.simulator.env.kitchen_height - ) + kitchen_aspect_ratio = kitchen_width / kitchen_height game_width = int(game_height * 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, + kitchen_width * grid_size, + kitchen_height * grid_size, ) else: game_width, game_height = 0, 0 @@ -507,7 +509,7 @@ class PyGameGUI: self.draw_counters(state) self.draw_players(state) self.manager.draw_ui(self.main_window) - self.draw_time(state["passed_time"]) + self.draw_time(state["remaining_time"]) def init_ui_elements(self): self.manager = pygame_gui.UIManager((self.window_width, self.window_height)) @@ -610,29 +612,27 @@ class PyGameGUI: object_id="#timer_label", ) - def setup_windows(self): - ( - self.window_width, - self.window_height, - self.game_width, - self.game_height, - self.grid_size, - ) = self.init_window_sizes() - + def set_window_size(self, window_width, window_height, game_width, game_height): self.game_screen = pygame.Surface( ( - self.game_width, - self.game_height, + game_width, + game_height, ), ) - self.main_window = pygame.display.set_mode( ( - self.window_width, - self.window_height, + window_width, + window_height, ) ) - self.player_colors = self.create_player_colors() + + def reset_window_size(self): + self.window_width = self.init_width + self.window_height = self.init_height + self.game_width = 0 + self.game_height = 0 + self.set_window_size(self.init_width, self.init_height, 0, 0) + self.init_ui_elements() def setup_simulation(self, config_path, layout_path): self.simulator = Simulator(config_path, layout_path, 600) @@ -641,6 +641,14 @@ class PyGameGUI: player_name = f"p{i}" self.simulator.register_player(player_name) self.simulator.start() + ( + self.window_width, + self.window_height, + self.game_width, + self.game_height, + self.grid_size, + ) = self.get_window_sizes(self.simulator.get_state()) + self.player_colors = self.create_player_colors() def manage_button_visibility(self): match self.menu_state: @@ -679,11 +687,15 @@ class PyGameGUI: config_path = ROOT_DIR / "game_content" / "environment_config.yaml" self.setup_simulation(config_path, layout_path) - self.setup_windows() + + self.set_window_size(*(self.get_window_sizes(self.simulator.get_state()))[:-1]) + self.init_ui_elements() log.debug("Pressed start button") def back_button_press(self): + self.reset_window_size() + self.simulator.stop() self.menu_state = MenuStates.Start log.debug("Pressed back button") @@ -694,6 +706,7 @@ class PyGameGUI: log.debug("Pressed quit button") def finished_button_press(self): + self.reset_window_size() self.simulator.stop() self.menu_state = MenuStates.End log.debug("Pressed finished button") @@ -704,7 +717,7 @@ class PyGameGUI: pygame.init() pygame.font.init() - self.setup_windows() + # self.setup_windows() self.init_ui_elements() pygame.display.set_caption("Simple Overcooked Simulator") diff --git a/overcooked_simulator/gui_2d_vis/visualization.yaml b/overcooked_simulator/gui_2d_vis/visualization.yaml index a1381661..6dd5bc91 100644 --- a/overcooked_simulator/gui_2d_vis/visualization.yaml +++ b/overcooked_simulator/gui_2d_vis/visualization.yaml @@ -4,8 +4,8 @@ GameWindow: WhatIsFixed: grid # grid or window_width or window_height size: 40 screen_margin: 100 - start_width: 600 - start_height: 600 + init_width: 600 + init_height: 600 Kitchen: ground_tiles_color: sgigray76 diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index 59ed6bfd..56f9f899 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -463,7 +463,7 @@ class Environment: "counters": self.counters, "score": self.score, "ended": self.game_ended, - "passed_time": self.env_time_end - self.env_time, + "remaining_time": max(self.env_time_end - self.env_time, timedelta(0)), } def get_state_json(self): -- GitLab