From aa85b1598cb40550e362a0699fb8a66ae0968174 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Thu, 4 Nov 2021 08:11:21 -0400
Subject: [PATCH] Update gui text to display active counters as program
 executes

---
 main.py                      | 10 ++++++----
 src/entities/gui_entities.py | 12 +++++++++---
 src/misc.py                  |  6 ++++++
 src/systems.py               | 10 ++++++++++
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/main.py b/main.py
index b9cb26c..c695e6a 100644
--- a/main.py
+++ b/main.py
@@ -23,10 +23,10 @@ RESOURCES = sdl2.ext.Resources(__file__, './src/images/')
 # Initialize window width/height.
 WINDOW_WIDTH = 640
 WINDOW_HEIGHT = 480
-# WINDOW_WIDTH = 400
-# WINDOW_HEIGHT = 200
-WINDOW_WIDTH_MIN = 400
-WINDOW_HEIGHT_MIN = 200
+# WINDOW_WIDTH = 500
+# WINDOW_HEIGHT = 450
+WINDOW_WIDTH_MIN = 500
+WINDOW_HEIGHT_MIN = 450
 
 
 def main():
@@ -168,6 +168,8 @@ def initialize_data():
         'gui_h_end': gui_h_end,
         'gui_center_w': gui_center_w,
         'gui_center_h': gui_center_h,
+        'optimal_counter': 999999,
+        'total_move_counter': -1,
     }
     tile_data = {
         'tile_w_start': tile_w_start,
diff --git a/src/entities/gui_entities.py b/src/entities/gui_entities.py
index 0ea5811..f1d3417 100644
--- a/src/entities/gui_entities.py
+++ b/src/entities/gui_entities.py
@@ -49,6 +49,12 @@ class GuiCore:
             5,
             data_manager.gui_data['gui_h_start'] + 5,
         )
+        self.total_move_counter_text = GuiText(
+            data_manager,
+            'Moves:',
+            data_manager.gui_data['gui_w_start'] - 80,
+            data_manager.gui_data['gui_h_start'] + 5,
+        )
         self.settings_header_text = GuiText(
             data_manager,
             'Settings:',
@@ -253,9 +259,9 @@ class GuiText:
         self.font = fcmatch('monospace').file
 
         # Initialize text sprite.
-        self.update_text(text)
+        self.update(text)
 
-    def update_text(self, text):
+    def update(self, text):
         """
         Update's text entity to display new text value.
         :param text: Text string to display.
@@ -267,7 +273,7 @@ class GuiText:
         # Initialize button text.
         text_color = sdl2.SDL_Color(250, 250, 250)  # White text.
         font_manager = sdl2.ext.FontManager(self.font, size=12, color=text_color)
-        text_sprite = self.data_manager.sprite_factory.from_text(text, fontmanager=font_manager)
+        text_sprite = self.data_manager.sprite_factory.from_text(str(text), fontmanager=font_manager)
 
         # Create sprite entity to display text value.
         self.text_entity = self.Text(self.data_manager.world, text_sprite, self.data_manager, self.pos_x, self.pos_y)
diff --git a/src/misc.py b/src/misc.py
index 5b0beb4..0c09333 100644
--- a/src/misc.py
+++ b/src/misc.py
@@ -62,6 +62,9 @@ class DataManager:
             'inactive': 0,
         }
 
+        # Also make the Sprite Renderer aware of current Data Manager object.
+        sprite_renderer.data_manager = self
+
 
 # endregion Data Structures
 
@@ -822,9 +825,12 @@ def calc_traveling_salesman(data_manager, debug=False):
         if swapped_total_dist < curr_total_dist:
             calculated_path['ordering'][conn_1_index_1] = conn_2_id_1
             calculated_path['ordering'][conn_2_index_1] = conn_1_id_1
+            calculated_path['total_cost'] = swapped_total_dist
 
     # Save found path.
     data_manager.ideal_overall_path = calculated_path
+    data_manager.gui_data['optimal_counter'] = calculated_path['total_cost']
+    data_manager.gui_data['total_move_counter'] += 1
 
     # Optionally display debug tile sprites.
     if debug:
diff --git a/src/systems.py b/src/systems.py
index da571e1..a182fcb 100644
--- a/src/systems.py
+++ b/src/systems.py
@@ -23,12 +23,22 @@ class SoftwareRendererSystem(sdl2.ext.SoftwareSpriteRenderSystem):
     System that handles displaying sprites to renderer window.
     """
     def __init__(self, window):
+        self.data_manager = None
         super(SoftwareRendererSystem, self).__init__(window)
 
     def render(self, components):
+        # Run tick to update general interface.
         sdl2.ext.fill(self.surface, sdl2.ext.Color(0, 0, 0))
         super(SoftwareRendererSystem, self).render(components)
 
+        # Also update dynamic GUI text elements.
+        self.data_manager.gui.optimal_counter_text.update(
+            'Optimal Solution Cost: {0}'.format(self.data_manager.gui_data['optimal_counter']),
+        )
+        self.data_manager.gui.total_move_counter_text.update(
+            'Moves: {0}'.format(self.data_manager.gui_data['total_move_counter']),
+        )
+
 
 class AbstractMovementSystem(ABC):
     """
-- 
GitLab