From 78849daaaf9154bbdae8b3083898fb6726b428ff Mon Sep 17 00:00:00 2001 From: LondonClass <43668206+LondonClass@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:39:08 +0800 Subject: [PATCH 1/4] set_dimensions now support dynamic sizes --- pygame_gui/core/ui_element.py | 49 ++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py index 360a8056..453ec5b8 100644 --- a/pygame_gui/core/ui_element.py +++ b/pygame_gui/core/ui_element.py @@ -187,6 +187,16 @@ def _on_contents_changed(self): def _calc_dynamic_size(self): pass + + def _set_dynamic_width(self, is_dynamic: bool = True): + self.dynamic_width = is_dynamic + if self.drawable_shape is not None: + self.drawable_shape.dynamic_width = is_dynamic + + def _set_dynamic_height(self, is_dynamic: bool = True): + self.dynamic_height = is_dynamic + if self.drawable_shape is not None: + self.drawable_shape.dynamic_height = is_dynamic @staticmethod def _validate_horizontal_anchors(anchors: Dict[str, Union[str, 'UIElement']]): @@ -733,21 +743,40 @@ def set_dimensions(self, dimensions: Union[pygame.math.Vector2, dimensions of the container or not. """ + dynamic_width_original = self.dynamic_width + dynamic_height_original = self.dynamic_height + + is_dynamic = False + if dimensions[0] < 0: + self._set_dynamic_width() + is_dynamic = True + if dimensions[1] < 0: + self._set_dynamic_height() + is_dynamic = True + + if is_dynamic: + self._set_image(self.drawable_shape.get_fresh_surface()) + self.rebuild() + + self._set_dynamic_width(dynamic_width_original) + self._set_dynamic_height(dynamic_height_original) + dimensions = self._get_clamped_to_minimum_dimensions(dimensions, clamp_to_container) - self.relative_rect.width = int(dimensions[0]) - self.relative_rect.height = int(dimensions[1]) + if dimensions[0] >= 0: + self.relative_rect.width = int(dimensions[0]) + if dimensions[1] >= 0: + self.relative_rect.height = int(dimensions[1]) self.rect.size = self.relative_rect.size - if dimensions[0] >= 0 and dimensions[1] >= 0: - self._update_absolute_rect_position_from_anchors(recalculate_margins=True) + self._update_absolute_rect_position_from_anchors(recalculate_margins=True) - if self.drawable_shape is not None: - if self.drawable_shape.set_dimensions(self.relative_rect.size): - # needed to stop resizing 'lag' - self._set_image(self.drawable_shape.get_fresh_surface()) + if self.drawable_shape is not None: + if self.drawable_shape.set_dimensions(self.relative_rect.size): + # needed to stop resizing 'lag' + self._set_image(self.drawable_shape.get_fresh_surface()) - self._update_container_clip() - self.ui_container.on_anchor_target_changed(self) + self._update_container_clip() + self.ui_container.on_anchor_target_changed(self) def update(self, time_delta: float): """ From 65b7ee115fd93b5ebf15907ab25065706c8eab5d Mon Sep 17 00:00:00 2001 From: LondonClass <43668206+LondonClass@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:54:45 +0800 Subject: [PATCH 2/4] debug --- pygame_gui/core/ui_element.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py index 453ec5b8..8c921e70 100644 --- a/pygame_gui/core/ui_element.py +++ b/pygame_gui/core/ui_element.py @@ -755,7 +755,6 @@ def set_dimensions(self, dimensions: Union[pygame.math.Vector2, is_dynamic = True if is_dynamic: - self._set_image(self.drawable_shape.get_fresh_surface()) self.rebuild() self._set_dynamic_width(dynamic_width_original) From 3351e51d045097d7fd5a0fff28431ae7a325ecb4 Mon Sep 17 00:00:00 2001 From: LondonClass <43668206+LondonClass@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:29:06 +0800 Subject: [PATCH 3/4] modify to change whether the element is dynamic --- pygame_gui/core/ui_container.py | 3 +- pygame_gui/core/ui_element.py | 57 ++++++++++++++++++++------------ pygame_gui/elements/ui_button.py | 2 +- pygame_gui/elements/ui_label.py | 2 +- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/pygame_gui/core/ui_container.py b/pygame_gui/core/ui_container.py index f0d1cad2..805529eb 100644 --- a/pygame_gui/core/ui_container.py +++ b/pygame_gui/core/ui_container.py @@ -204,8 +204,7 @@ def set_relative_position(self, position: Union[pygame.math.Vector2, def set_dimensions(self, dimensions: Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]], - clamp_to_container: bool = False - ): + clamp_to_container: bool = False): """ Set the dimension of this container and update the positions of elements within it accordingly. diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py index 8c921e70..aca447d5 100644 --- a/pygame_gui/core/ui_element.py +++ b/pygame_gui/core/ui_element.py @@ -522,7 +522,7 @@ def _update_absolute_rect_position_from_anchors(self, recalculate_margins=False) new_width = new_right - new_left new_width, new_height = self._get_clamped_to_minimum_dimensions((new_width, new_height)) if (new_height != self.relative_rect.height) or (new_width != self.relative_rect.width): - self.set_dimensions((new_width, new_height)) + self._set_dimensions((new_width, new_height)) def _update_relative_rect_position_from_anchors(self, recalculate_margins=False): """ @@ -726,56 +726,69 @@ def set_minimum_dimensions(self, dimensions: Union[pygame.math.Vector2, (self.rect.height < self.minimum_dimensions[1])): new_width = max(self.minimum_dimensions[0], self.rect.width) new_height = max(self.minimum_dimensions[1], self.rect.height) - self.set_dimensions((new_width, new_height)) + self._set_dimensions((new_width, new_height)) def set_dimensions(self, dimensions: Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]], clamp_to_container: bool = False): """ - Method to directly set the dimensions of an element. + Method to directly set the dimensions of an element. And set whether the elements are dynamic. NOTE: Using this on elements inside containers with non-default anchoring arrangements may make a mess of them. - :param dimensions: The new dimensions to set. + :param dimensions: The new dimensions to set.If it is a negative value, the element will become + dynamically sized, otherwise it will become statically sized. :param clamp_to_container: Whether we should clamp the dimensions to the dimensions of the container or not. - """ - dynamic_width_original = self.dynamic_width - dynamic_height_original = self.dynamic_height is_dynamic = False if dimensions[0] < 0: self._set_dynamic_width() is_dynamic = True + else: + self._set_dynamic_width(False) + if dimensions[1] < 0: self._set_dynamic_height() is_dynamic = True + else: + self._set_dynamic_height(False) if is_dynamic: self.rebuild() - - self._set_dynamic_width(dynamic_width_original) - self._set_dynamic_height(dynamic_height_original) - + else: + self._set_dimensions(dimensions, clamp_to_container) + + def _set_dimensions(self, dimensions: Union[pygame.math.Vector2, + Tuple[int, int], + Tuple[float, float]], + clamp_to_container: bool = False): + """ + Method to directly set the dimensions of an element. + Dimensions must be positive values. + + :param dimensions: The new dimensions to set. + :param clamp_to_container: Whether we should clamp the dimensions to the + dimensions of the container or not. + """ dimensions = self._get_clamped_to_minimum_dimensions(dimensions, clamp_to_container) - if dimensions[0] >= 0: - self.relative_rect.width = int(dimensions[0]) - if dimensions[1] >= 0: - self.relative_rect.height = int(dimensions[1]) + self.relative_rect.width = int(dimensions[0]) + self.relative_rect.height = int(dimensions[1]) self.rect.size = self.relative_rect.size - self._update_absolute_rect_position_from_anchors(recalculate_margins=True) + if dimensions[0] >= 0 and dimensions[1] >= 0: + self._update_absolute_rect_position_from_anchors(recalculate_margins=True) - if self.drawable_shape is not None: - if self.drawable_shape.set_dimensions(self.relative_rect.size): - # needed to stop resizing 'lag' - self._set_image(self.drawable_shape.get_fresh_surface()) + if self.drawable_shape is not None: + if self.drawable_shape.set_dimensions(self.relative_rect.size): + # needed to stop resizing 'lag' + self._set_image(self.drawable_shape.get_fresh_surface()) - self._update_container_clip() - self.ui_container.on_anchor_target_changed(self) + self._update_container_clip() + self.ui_container.on_anchor_target_changed(self) def update(self, time_delta: float): """ diff --git a/pygame_gui/elements/ui_button.py b/pygame_gui/elements/ui_button.py index 1553ec07..b2a31854 100644 --- a/pygame_gui/elements/ui_button.py +++ b/pygame_gui/elements/ui_button.py @@ -777,7 +777,7 @@ def rebuild(self): def _calc_dynamic_size(self): if self.dynamic_width or self.dynamic_height: - self.set_dimensions(self.image.get_size()) + self._set_dimensions(self.image.get_size()) # if we have anchored the left side of our button to the right of its container then # changing the width is going to mess up the horiz position as well. diff --git a/pygame_gui/elements/ui_label.py b/pygame_gui/elements/ui_label.py index 96dd837e..f967ae19 100644 --- a/pygame_gui/elements/ui_label.py +++ b/pygame_gui/elements/ui_label.py @@ -165,7 +165,7 @@ def rebuild(self): def _calc_dynamic_size(self): if self.dynamic_width or self.dynamic_height: - self.set_dimensions(self.image.get_size()) + self._set_dimensions(self.image.get_size()) # if we have anchored the left side of our button to the right of its container then # changing the width is going to mess up the horiz position as well. From 02c20ca7d0e636127806fa8bb49df9612d2376f7 Mon Sep 17 00:00:00 2001 From: LondonClass <43668206+LondonClass@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:35:49 +0800 Subject: [PATCH 4/4] typo --- pygame_gui/core/ui_element.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py index aca447d5..bb0bbc81 100644 --- a/pygame_gui/core/ui_element.py +++ b/pygame_gui/core/ui_element.py @@ -738,12 +738,11 @@ def set_dimensions(self, dimensions: Union[pygame.math.Vector2, NOTE: Using this on elements inside containers with non-default anchoring arrangements may make a mess of them. - :param dimensions: The new dimensions to set.If it is a negative value, the element will become + :param dimensions: The new dimensions to set. If it is a negative value, the element will become dynamically sized, otherwise it will become statically sized. :param clamp_to_container: Whether we should clamp the dimensions to the dimensions of the container or not. """ - is_dynamic = False if dimensions[0] < 0: self._set_dynamic_width()