Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_dimensions now support dynamic sizes #514

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pygame_gui/core/ui_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 44 additions & 4 deletions pygame_gui/core/ui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']]):
Expand Down Expand Up @@ -512,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):
"""
Expand Down Expand Up @@ -716,22 +726,52 @@ 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.
"""
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()
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)
self.relative_rect.width = int(dimensions[0])
Expand Down
2 changes: 1 addition & 1 deletion pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pygame_gui/elements/ui_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading