Skip to content

Commit

Permalink
Merge pull request #444 from MyreMylar/dynamic-sizing-3
Browse files Browse the repository at this point in the history
Add dynamic dimension attributes to text box layout
  • Loading branch information
MyreMylar authored Apr 16, 2023
2 parents 39f6199 + b4c87ea commit d574502
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
21 changes: 15 additions & 6 deletions pygame_gui/core/drawable_shapes/drawable_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pygame_gui/core/text/text_box_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def __init__(self,
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.view_rect.height == -1:
self.dynamic_height = True
else:
self.dynamic_height = False

self.layout_rect_queue = None
self.finalised_surface = None
self.floating_rects: List[TextLayoutRect] = []
Expand Down Expand Up @@ -142,6 +151,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
Expand Down
12 changes: 6 additions & 6 deletions pygame_gui/core/text/text_line_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d574502

Please sign in to comment.