From 7e65f738901899ce8d76069e32b4ad18f3dad0c8 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 16 Apr 2023 19:24:29 +0100 Subject: [PATCH 1/2] add dynamic dimension attributes to text box layout --- pygame_gui/core/text/text_box_layout.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pygame_gui/core/text/text_box_layout.py b/pygame_gui/core/text/text_box_layout.py index 01958afd..9afec5ab 100644 --- a/pygame_gui/core/text/text_box_layout.py +++ b/pygame_gui/core/text/text_box_layout.py @@ -50,10 +50,17 @@ def __init__(self, self.expand_width = False if self.layout_rect.width == -1: + self.dynamic_width = True self.layout_rect.width = 0 self.expand_width = True for rect in self.input_data_rect_queue: self.layout_rect.width += rect.width + else: + self.dynamic_width = False + if self.layout_rect.height == -1: + self.dynamic_height = True + else: + self.dynamic_height = False self.layout_rect_queue = None self.finalised_surface = None @@ -142,6 +149,10 @@ def _process_layout_queue(self, input_queue, current_row): current_row = self._handle_regular_rect(current_row, text_layout_rect, input_queue) # make sure we add the last row to the layout self._add_row_to_layout(current_row, last_row=True) + if self.dynamic_width: + self.view_rect.width = self.layout_rect.width + if self.dynamic_height: + self.view_rect.height = self.layout_rect.height def _add_row_to_layout(self, current_row: TextBoxLayoutRow, last_row=False): # handle an empty row being added to layout From b4c87eab468e208e9cc4ea19a18263dff629fa85 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 16 Apr 2023 19:46:43 +0100 Subject: [PATCH 2/2] More dynamic resizing fixes --- .../core/drawable_shapes/drawable_shape.py | 21 +++++++++++++------ pygame_gui/core/text/text_box_layout.py | 6 ++++-- pygame_gui/core/text/text_line_chunk.py | 12 +++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pygame_gui/core/drawable_shapes/drawable_shape.py b/pygame_gui/core/drawable_shapes/drawable_shape.py index 2a9b1472..4bdc17f9 100644 --- a/pygame_gui/core/drawable_shapes/drawable_shape.py +++ b/pygame_gui/core/drawable_shapes/drawable_shape.py @@ -148,6 +148,8 @@ def __init__(self, self.theming = theming_parameters self.containing_rect = containing_rect.copy() + self.dynamic_width = True if self.containing_rect.width == -1 else False + self.dynamic_height = True if self.containing_rect.height == -1 else False self.text_view_rect: Optional[pygame.Rect] = None self.shadow_width = 0 @@ -207,7 +209,7 @@ def __init__(self, self.only_text_changed = False def _evaluate_contents_for_containing_rect(self): - if self.containing_rect.width == -1: + if self.dynamic_width: # check to see if we have text and a font, this won't work with HTML # text - throw a warning? # What we really need to to is process the html text layout by this @@ -230,7 +232,7 @@ def _evaluate_contents_for_containing_rect(self): self.text_view_rect.width = text_width self.text_box_layout.view_rect.width = self.text_view_rect.width self.containing_rect.width = final_width - if self.containing_rect.height == -1: + if self.dynamic_height: if self.text_box_layout is not None: text_height = self.text_box_layout.layout_rect.height @@ -490,10 +492,17 @@ def build_text_layout(self): self.text_view_rect = self.containing_rect.copy() self.text_view_rect.x = 0 self.text_view_rect.y = 0 - if self.text_view_rect.width != -1: - self.text_view_rect.width -= ((total_text_buffer * 2) + (2 * horiz_padding)) - if self.text_view_rect.height != -1: - self.text_view_rect.height -= ((total_text_buffer * 2) + (2 * vert_padding)) + if self.dynamic_width: + self.text_view_rect.width = -1 + else: + self.text_view_rect.width = max(0, self.text_view_rect.width - + ((total_text_buffer * 2) + (2 * horiz_padding))) + + if self.dynamic_height: + self.text_view_rect.height = -1 + else: + self.text_view_rect.height = max(0, self.text_view_rect.height - + ((total_text_buffer * 2) + (2 * vert_padding))) text_actual_area_rect = self.text_view_rect.copy() text_actual_area_rect.x = total_text_buffer + horiz_padding diff --git a/pygame_gui/core/text/text_box_layout.py b/pygame_gui/core/text/text_box_layout.py index 9afec5ab..877bfa52 100644 --- a/pygame_gui/core/text/text_box_layout.py +++ b/pygame_gui/core/text/text_box_layout.py @@ -50,14 +50,16 @@ def __init__(self, self.expand_width = False if self.layout_rect.width == -1: - self.dynamic_width = True self.layout_rect.width = 0 self.expand_width = True for rect in self.input_data_rect_queue: self.layout_rect.width += rect.width + + if self.view_rect.width == -1: + self.dynamic_width = True else: self.dynamic_width = False - if self.layout_rect.height == -1: + if self.view_rect.height == -1: self.dynamic_height = True else: self.dynamic_height = False diff --git a/pygame_gui/core/text/text_line_chunk.py b/pygame_gui/core/text/text_line_chunk.py index 108ba440..3de8f0c2 100644 --- a/pygame_gui/core/text/text_line_chunk.py +++ b/pygame_gui/core/text/text_line_chunk.py @@ -109,9 +109,9 @@ def _handle_dimensions(self, font, max_dimensions, text): text_width = self._text_render_width(text, font) + (2 * text_shadow_width) text_height = text_rect.height if max_dimensions is not None: - if max_dimensions[0] != -1: + if not (max_dimensions[0] == -1 or max_dimensions[1] == -1): + # we don't have dynamic maximum dimensions so use maximum dimensions for both text_width = min(max_dimensions[0], text_width) - if max_dimensions[1] != -1: text_height = min(max_dimensions[1], text_height) return text_height, text_rect, text_width @@ -567,9 +567,9 @@ def insert_text(self, input_text: str, index: int): text_width = self._text_render_width(self.text, self.font) text_height = text_rect.height if self.max_dimensions is not None: - if self.max_dimensions[0] != -1: + if not (self.max_dimensions[0] == -1 or self.max_dimensions[1] == -1): + # we don't have dynamic maximum dimensions so use maximum dimensions for both text_width = min(self.max_dimensions[0], text_width) - if self.max_dimensions[1] != -1: text_height = min(self.max_dimensions[1], text_height) self.size = (text_width, text_height) # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused @@ -592,9 +592,9 @@ def delete_letter_at_index(self, index): text_width = self._text_render_width(self.text, self.font) text_height = text_rect.height if self.max_dimensions is not None: - if self.max_dimensions[0] != -1: + if not (self.max_dimensions[0] == -1 or self.max_dimensions[1] == -1): + # we don't have dynamic maximum dimensions so use maximum dimensions for both text_width = min(self.max_dimensions[0], text_width) - if self.max_dimensions[1] != -1: text_height = min(self.max_dimensions[1], text_height) self.size = (text_width, text_height) # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused