From c2bd61384135b7a4c7638b47041beaaa9d1b87cd Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Wed, 13 Mar 2024 09:22:04 +0100
Subject: [PATCH] Better management of drawing orders in game

---
 cooperative_cuisine/pygame_2d_vis/drawing.py | 26 ++++-------
 cooperative_cuisine/pygame_2d_vis/gui.py     | 45 +++++++++++++++++---
 2 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/cooperative_cuisine/pygame_2d_vis/drawing.py b/cooperative_cuisine/pygame_2d_vis/drawing.py
index 0c67eaf1..2e0ef10a 100644
--- a/cooperative_cuisine/pygame_2d_vis/drawing.py
+++ b/cooperative_cuisine/pygame_2d_vis/drawing.py
@@ -783,7 +783,6 @@ class Visualizer:
         grid_size: int,
         width: int,
         height: int,
-        screen_margin: int,
         config: dict,
     ):
         """Visualization of the current orders.
@@ -799,13 +798,13 @@ class Visualizer:
 
         """
         orders_width = width - 100
-        orders_height = screen_margin
-        order_screen = pygame.Surface(
-            (orders_width, orders_height),
-        )
+        orders_height = height
+        # order_screen = pygame.Surface(
+        #     (orders_width, orders_height),
+        # )
 
         bg_color = colors[config["GameWindow"]["background_color"]]
-        pygame.draw.rect(order_screen, bg_color, order_screen.get_rect())
+        pygame.draw.rect(screen, bg_color, screen.get_rect())
 
         order_rects_start = (orders_height // 2) - (grid_size // 2)
         for idx, order in enumerate(state["orders"]):
@@ -814,7 +813,7 @@ class Visualizer:
                 order_rects_start,
             ]
             pygame.draw.rect(
-                order_screen,
+                screen,
                 colors["red"],
                 pygame.Rect(
                     order_upper_left[0],
@@ -828,14 +827,14 @@ class Visualizer:
             self.draw_thing(
                 pos=center + (grid_size / 2),
                 parts=config["Plate"]["parts"],
-                screen=order_screen,
+                screen=screen,
                 grid_size=grid_size,
             )
             self.draw_item(
                 pos=center + (grid_size / 2),
                 item={"type": order["meal"]},
                 plate=True,
-                screen=order_screen,
+                screen=screen,
                 grid_size=grid_size,
             )
             order_done_seconds = (
@@ -850,18 +849,11 @@ class Visualizer:
             self.draw_progress_bar(
                 pos=center + (grid_size / 2),
                 percent=percentage,
-                screen=order_screen,
+                screen=screen,
                 grid_size=grid_size,
                 attention=percentage < 0.25,
             )
 
-        orders_rect = order_screen.get_rect()
-        orders_rect.center = [
-            screen_margin + (orders_width // 2),
-            orders_height // 2,
-        ]
-        screen.blit(order_screen, orders_rect)
-
     def save_state_image(
         self, grid_size: int, state: dict, filename: str | Path
     ) -> None:
diff --git a/cooperative_cuisine/pygame_2d_vis/gui.py b/cooperative_cuisine/pygame_2d_vis/gui.py
index f0cb5e17..02ea6b3c 100644
--- a/cooperative_cuisine/pygame_2d_vis/gui.py
+++ b/cooperative_cuisine/pygame_2d_vis/gui.py
@@ -478,7 +478,6 @@ class PyGameGUI:
         self.manager.get_theme().load_theme(
             ROOT_DIR / "pygame_2d_vis" / "gui_theme.json"
         )
-
         self.elements_margin = self.window_height * 0.02
 
         ########################################################################
@@ -813,11 +812,43 @@ class PyGameGUI:
 
         self.orders_label = pygame_gui.elements.UILabel(
             text="translations.orders",
-            relative_rect=pygame.Rect(0, 0, self.screen_margin, self.screen_margin),
+            relative_rect=pygame.Rect(0, 0, self.buttons_width, self.screen_margin),
             manager=self.manager,
             object_id="#orders_label",
         )
 
+        self.orders_container_width = (
+            self.window_width - (2 * self.buttons_width) - (self.buttons_width * 0.7)
+        )
+        # rect = pygame.Rect(
+        #     0,
+        #     0,
+        #     self.orders_container_width,
+        #     self.screen_margin,
+        # )
+        # self.orders_container = pygame_gui.elements.UIPanel(
+        #     relative_rect=rect,
+        #     manager=self.manager,
+        #     object_id="#graph_container",
+        #     anchors={
+        #         "top": "top",
+        #         "left": "left",
+        #         "left_target": self.orders_label,
+        #     },
+        # )
+
+        self.orders_image = pygame_gui.elements.UIImage(
+            relative_rect=pygame.Rect(
+                0, 0, self.orders_container_width, self.screen_margin
+            ),
+            image_surface=pygame.Surface(
+                (self.orders_container_width, self.screen_margin)
+            ),
+            manager=self.manager,
+            object_id="#recipe_graph",
+            anchors={"top": "top", "left_target": self.orders_label},
+        )
+
         rect = pygame.Rect(
             (0, 0),
             (self.window_width * 0.2, self.buttons_height),
@@ -1002,6 +1033,7 @@ class PyGameGUI:
 
         self.game_screen_elements = [
             self.orders_label,
+            self.orders_image,
             self.score_label,
             self.timer_label,
             self.wait_players_label,
@@ -1249,15 +1281,16 @@ class PyGameGUI:
             [int(k.current_player) for k in self.key_sets],
         )
 
+        # orders_surface = pygame.Surface((self.orders_container_width, self.screen_margin))
         self.vis.draw_orders(
-            screen=self.main_window,
+            screen=self.orders_image.image,
             state=state,
             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,
+            width=self.orders_container_width,
+            height=self.screen_margin,
             config=self.visualization_config,
         )
+        # self.orders_image.set_image(orders_surface)
 
         border = self.visualization_config["GameWindow"]["game_border_size"]
         border_rect = pygame.Rect(
-- 
GitLab