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 a936c7cdff506a99e663e42ca359ca86c65bee69..ed272fc23543e0cf713fede80492de6abdced57a 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -28,10 +28,9 @@ from overcooked_simulator.utils import ( url_and_port_arguments, disable_websocket_logging_arguments, add_list_of_manager_ids_arguments, + setup_logging, ) -from pygame._sdl2 import Window, Texture, Image, Renderer, get_drivers, messagebox - class MenuStates(Enum): Start = "Start" @@ -100,6 +99,8 @@ class PyGameGUI: port: int, manager_ids: list[str], ): + pygame.init() + self.game_screen: pygame.Surface = None self.FPS = 60 self.running = True @@ -127,15 +128,6 @@ class PyGameGUI: "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.images_path = ROOT_DIR / "pygame_gui" / "images" self.menu_state = MenuStates.Start @@ -145,46 +137,60 @@ class PyGameGUI: self.sub_processes = [] + self.fullscreen = False + ( + 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 + 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, - ) + 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) + + redisual_x = self.game_width - (self.kitchen_width * self.grid_size) + residual_y = self.game_height - (self.kitchen_height * self.grid_size) + self.game_width -= redisual_x + self.game_height -= residual_y def setup_player_keys(self, n=1, disjunct=False): if n: @@ -304,6 +310,19 @@ class PyGameGUI: ) self.quit_button.can_hover() + fullscreen_button_rect = pygame.Rect( + (0, 0), (self.buttons_width, self.buttons_height) + ) + fullscreen_button_rect.topright = (0, self.buttons_height) + self.fullscreen_button_rect = 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_rect.can_hover() + self.reset_button = pygame_gui.elements.UIButton( relative_rect=pygame.Rect( ( @@ -583,7 +602,7 @@ class PyGameGUI: self.vis.draw_orders( screen=self.main_window, state=state, - grid_size=self.grid_size, + grid_size=self.buttons_height, width=self.game_width, height=self.game_height, screen_margin=self.screen_margin, @@ -611,18 +630,27 @@ 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 | pygame.RESIZABLE, ) 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() @@ -798,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(state) def start_button_press(self): self.menu_state = MenuStates.Game @@ -825,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() @@ -965,22 +985,18 @@ 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() self.update_selection_elements() - win = Window("asdf", size=(self.window_width, self.window_height), resizable=True) - renderer = Renderer(win) - # Game loop self.running = True while self.running: @@ -992,6 +1008,16 @@ class PyGameGUI: self.running = False # UI Buttons: + elif event.type == pygame.VIDEORESIZE: + # scrsize = event.size + self.window_width_windowed = event.w + self.windowed_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: @@ -1001,6 +1027,7 @@ class PyGameGUI: ): continue self.start_button_press() + case self.back_button: self.back_button_press() self.disconnect_websockets() @@ -1008,9 +1035,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() @@ -1041,6 +1070,15 @@ class PyGameGUI: case self.xbox_controller_button: print("xbox_controller_button pressed.") + case self.fullscreen_button_rect: + self.fullscreen = not self.fullscreen + if not self.fullscreen: + self.window_width_windowed = self.min_width + self.windowed_height_windowed = self.min_height + self.recalc_game_size() + self.set_window_size() + self.init_ui_elements() + self.update_selection_elements() self.manage_button_visibility() @@ -1085,10 +1123,6 @@ class PyGameGUI: self.main_window.blit(self.game_screen, game_screen_rect) - texture = Texture.from_surface(renderer, self.main_window) - renderer.blit(texture, pygame.Rect(0, 0, self.window_width, self.window_height)) - renderer.present() - case MenuStates.End: self.update_conclusion_label(state) @@ -1104,6 +1138,7 @@ class PyGameGUI: def main(url: str, port: int, manager_ids: list[str]): + setup_logging() gui = PyGameGUI( url=url, port=port, @@ -1123,4 +1158,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..641108fbd045f556626f5de9151e67e5c0147a68 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: 750 min_height: 600 buttons_width: 180 buttons_height: 60