diff --git a/overcooked_simulator/game_content/layouts/large_t.layout b/overcooked_simulator/game_content/layouts/large_t.layout new file mode 100644 index 0000000000000000000000000000000000000000..304e6f7746f4e0c7510f69395b55c7f691de84f6 --- /dev/null +++ b/overcooked_simulator/game_content/layouts/large_t.layout @@ -0,0 +1,45 @@ +#QU#F###O#T#################N###L###B# +#____________________________________# +#____________________________________M +#____________________________________# +#____________________________________# +#____________________________________K +W____________________________________I +#____________________________________# +#____________________________________# +#__A_____A___________________________D +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +C____________________________________E +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +#____________________________________# +C____________________________________G +#____________________________________# +#P#####S+####X#####S+################# \ 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 73b14016f2493953d9ac9f2da842099bcb63e526..f73f821344b606357c63764e51dd816d61310a68 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -28,6 +28,7 @@ from overcooked_simulator.utils import ( url_and_port_arguments, disable_websocket_logging_arguments, add_list_of_manager_ids_arguments, + setup_logging, ) @@ -58,8 +59,11 @@ class PlayerKeySet: Movement keys in the following order: Down, Up, Left, Right Args: - player_name: The name of the player to control. - keys: The keys which control this player in the following order: Down, Up, Left, Right, Interact, Pickup. + move_keys: The keys which control this players movement in the following order: Down, Up, Left, Right. + interact_key: The key to interact with objects in the game. + pickup_key: The key to pick items up or put them down. + switch_key: The key for switching through controllable players. + players: The player indices which this keyset can control. """ self.move_vectors: list[list[int]] = [[-1, 0], [1, 0], [0, -1], [0, 1]] self.key_to_movement: dict[pygame.key, list[int]] = { @@ -98,6 +102,11 @@ class PyGameGUI: port: int, manager_ids: list[str], ): + pygame.init() + pygame.display.set_icon( + pygame.image.load(ROOT_DIR / "gui_2d_vis" / "images" / "fish3.png") + ) + self.game_screen: pygame.Surface = None self.FPS = 60 self.running = True @@ -117,72 +126,73 @@ class PyGameGUI: self.screen_margin = self.visualization_config["GameWindow"]["screen_margin"] self.min_width = self.visualization_config["GameWindow"]["min_width"] self.min_height = self.visualization_config["GameWindow"]["min_height"] - self.buttons_width = self.visualization_config["GameWindow"]["buttons_width"] self.buttons_height = self.visualization_config["GameWindow"]["buttons_height"] - self.order_bar_height = self.visualization_config["GameWindow"][ "order_bar_height" ] - - self.window_width = self.min_width - self.window_height = self.min_height - - self.main_window = pygame.display.set_mode( - (self.window_width, self.window_height) - ) - - # self.game_width, self.game_height = 0, 0 - + ( + self.window_width_fullscreen, + self.window_height_fullscreen, + ) = pygame.display.get_desktop_sizes()[0] + self.window_width_windowed = self.min_width + self.window_height_windowed = self.min_height + self.kitchen_width = 1 + self.kitchen_height = 1 + self.kitchen_aspect_ratio = 1 self.images_path = ROOT_DIR / "pygame_gui" / "images" + self.vis = Visualizer(self.visualization_config) + + self.fullscreen = False self.menu_state = MenuStates.Start self.manager: pygame_gui.UIManager - self.vis = Visualizer(self.visualization_config) - self.sub_processes = [] - def get_window_sizes(self, state: dict): - kitchen_width = state["kitchen"]["width"] - kitchen_height = state["kitchen"]["height"] - if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width": - game_width = self.visualization_config["GameWindow"]["size"] - kitchen_aspect_ratio = kitchen_height / kitchen_width - game_height = int(game_width * kitchen_aspect_ratio) - grid_size = int(game_width / (kitchen_width - 0.1)) - - elif self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_height": - game_height = self.visualization_config["GameWindow"]["size"] - kitchen_aspect_ratio = kitchen_width / kitchen_height - game_width = int(game_height * kitchen_aspect_ratio) - grid_size = int(game_width / (kitchen_width - 0.1)) - - elif self.visualization_config["GameWindow"]["WhatIsFixed"] == "grid": - grid_size = self.visualization_config["GameWindow"]["size"] - game_width, game_height = ( - kitchen_width * grid_size, - kitchen_height * grid_size, - ) + def get_window_sizes_from_state(self, state: dict): + self.kitchen_width = state["kitchen"]["width"] + self.kitchen_height = state["kitchen"]["height"] + self.kitchen_aspect_ratio = self.kitchen_height / self.kitchen_width + game_width = self.visualization_config["GameWindow"]["min_width"] - ( + 2 * self.screen_margin + ) + game_height = self.visualization_config["GameWindow"]["min_height"] - ( + 2 * self.screen_margin + ) + if self.kitchen_width > game_width: + self.game_height = game_width * self.kitchen_aspect_ratio + self.grid_size = game_width / self.kitchen_width else: - game_width, game_height = 0, 0 - grid_size = 0 + self.game_width = game_height / self.kitchen_aspect_ratio + self.grid_size = game_width / self.kitchen_width - window_width, window_height = ( - game_width + (2 * self.screen_margin), - game_height + (2 * self.screen_margin), # bar with orders - ) + self.window_width_windowed = self.min_width + self.window_height_windowed = self.min_height - window_width = max(window_width, self.min_width) - window_height = max(window_height, self.min_height) - return ( - int(window_width), - int(window_height), - int(game_width), - int(game_height), - grid_size, - ) + def recalc_game_size(self): + log.debug("Resizing game screen") + max_width = self.window_width - (2 * self.screen_margin) + max_height = self.window_height - (2 * self.screen_margin) + if max_width < max_height: + self.game_width = max_width + self.game_height = max_width * self.kitchen_aspect_ratio + self.grid_size = int(self.game_height / self.kitchen_height) + + else: + self.game_height = max_height + self.game_width = max_height / self.kitchen_aspect_ratio + self.grid_size = int(self.game_width / self.kitchen_width) + + self.game_width = max(self.game_width, 100) + self.game_height = max(self.game_height, 100) + self.grid_size = max(self.grid_size, 1) + + residual_x = self.game_width - (self.kitchen_width * self.grid_size) + residual_y = self.game_height - (self.kitchen_height * self.grid_size) + self.game_width -= residual_x + self.game_height -= residual_y def setup_player_keys(self, n=1, disjunct=False): if n: @@ -302,17 +312,27 @@ class PyGameGUI: ) self.quit_button.can_hover() + fullscreen_button_rect = pygame.Rect( + (0, 0), (self.buttons_width * 0.7, self.buttons_height) + ) + fullscreen_button_rect.topright = (-self.buttons_width, 0) + self.fullscreen_button = pygame_gui.elements.UIButton( + relative_rect=fullscreen_button_rect, + text="Fullscreen", + manager=self.manager, + object_id="#fullscreen_button", + anchors={"right": "right", "top": "top"}, + ) + self.fullscreen_button.can_hover() + + reset_button_rect = pygame.Rect((0, 0), (self.screen_margin * 0.75, 50)) + reset_button_rect.topright = (0, 2 * self.buttons_height) self.reset_button = pygame_gui.elements.UIButton( - relative_rect=pygame.Rect( - ( - self.window_width - (self.screen_margin * 3 // 4), - self.screen_margin, - ), - (self.screen_margin - (self.screen_margin // 4), 50), - ), + relative_rect=reset_button_rect, text="RESET", manager=self.manager, object_id="#reset_button", + anchors={"right": "right", "top": "top"}, ) self.reset_button.can_hover() @@ -581,8 +601,8 @@ class PyGameGUI: self.vis.draw_orders( screen=self.main_window, state=state, - grid_size=self.grid_size, - width=self.game_width, + grid_size=self.buttons_height, + width=self.window_width - self.buttons_width - (self.buttons_width * 0.7), height=self.game_height, screen_margin=self.screen_margin, config=self.visualization_config, @@ -609,18 +629,28 @@ class PyGameGUI: ( self.game_width, self.game_height, - ), + ) ) + + if self.fullscreen: + flags = pygame.FULLSCREEN + self.window_width = self.window_width_fullscreen + self.window_height = self.window_height_fullscreen + else: + flags = 0 + self.window_width = self.window_width_windowed + self.window_height = self.window_height_windowed + self.main_window = pygame.display.set_mode( ( self.window_width, self.window_height, - ) + ), + flags=flags, + display=0, ) def reset_window_size(self): - self.window_width = self.min_width - self.window_height = self.min_height self.game_width = 0 self.game_height = 0 self.set_window_size() @@ -796,13 +826,7 @@ class PyGameGUI: ) state = json.loads(websocket.recv()) - ( - self.window_width, - self.window_height, - self.game_width, - self.game_height, - self.grid_size, - ) = self.get_window_sizes(state) + self.get_window_sizes_from_state(state) def start_button_press(self): self.menu_state = MenuStates.Game @@ -823,13 +847,11 @@ class PyGameGUI: self.setup_environment() + self.recalc_game_size() self.set_window_size() - self.init_ui_elements() log.debug("Pressed start button") - # self.api.set_sim(self.simulator) - def back_button_press(self): self.menu_state = MenuStates.Start self.reset_window_size() @@ -963,14 +985,13 @@ class PyGameGUI: def start_pygame(self): """Starts pygame and the gui loop. Each frame the game state is visualized and keyboard inputs are read.""" log.debug(f"Starting pygame gui at {self.FPS} fps") - pygame.init() pygame.font.init() - pygame.display.set_caption("Simple Overcooked Simulator") clock = pygame.time.Clock() self.reset_window_size() + self.init_ui_elements() self.manage_button_visibility() @@ -980,13 +1001,22 @@ class PyGameGUI: self.running = True while self.running: try: - self.time_delta = clock.tick(self.FPS) / 1000.0 + self.time_delta = clock.tick(self.FPS) / 1000 + # print(clock.get_time()) for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False - # UI Buttons: + # elif event.type == pygame.VIDEORESIZE: + # # scrsize = event.size + # self.window_width_windowed = event.w + # self.window_height_windowed = event.h + # self.recalc_game_size() + # self.set_window_size() + # self.init_ui_elements() + # self.manage_button_visibility() + if event.type == pygame_gui.UI_BUTTON_PRESSED: match event.ui_element: case self.start_button: @@ -996,6 +1026,7 @@ class PyGameGUI: ): continue self.start_button_press() + case self.back_button: self.back_button_press() self.disconnect_websockets() @@ -1003,9 +1034,11 @@ class PyGameGUI: case self.finished_button: self.finished_button_press() self.disconnect_websockets() + case self.quit_button: self.quit_button_press() self.disconnect_websockets() + case self.reset_button: self.reset_button_press() self.disconnect_websockets() @@ -1036,6 +1069,18 @@ class PyGameGUI: case self.xbox_controller_button: print("xbox_controller_button pressed.") + case self.fullscreen_button: + self.fullscreen = not self.fullscreen + if self.fullscreen: + self.window_width = self.window_width_fullscreen + self.window_height = self.window_height_fullscreen + else: + self.window_width = self.window_width_windowed + self.window_height = self.window_height_windowed + self.recalc_game_size() + self.set_window_size() + self.init_ui_elements() + self.update_selection_elements() self.manage_button_visibility() @@ -1095,6 +1140,7 @@ class PyGameGUI: def main(url: str, port: int, manager_ids: list[str]): + setup_logging() gui = PyGameGUI( url=url, port=port, @@ -1114,4 +1160,4 @@ if __name__ == "__main__": disable_websocket_logging_arguments(parser) add_list_of_manager_ids_arguments(parser) args = parser.parse_args() - main(args.url, args.port, args.manager_ids, args.enable_websocket_logging) + main(args.url, args.port, args.manager_ids) diff --git a/overcooked_simulator/gui_2d_vis/visualization.yaml b/overcooked_simulator/gui_2d_vis/visualization.yaml index 428c76c01bd5567c2b420679671282bda7d74a4c..a0d7989c1c1bf9c2a96fcc28e1baf09bcd96e55e 100644 --- a/overcooked_simulator/gui_2d_vis/visualization.yaml +++ b/overcooked_simulator/gui_2d_vis/visualization.yaml @@ -1,10 +1,8 @@ # colors: https://www.webucator.com/article/python-color-constants-module/ GameWindow: - WhatIsFixed: grid # grid or window_width or window_height - size: 50 screen_margin: 100 - min_width: 700 + min_width: 900 min_height: 600 buttons_width: 180 buttons_height: 60