Skip to content

Commit

Permalink
Allow the user to specify a scaling function
Browse files Browse the repository at this point in the history
`smoothscale` doesn't always give the desired aesthetic results.
We should give the option to inject our own scaling function of a
surface in.
  • Loading branch information
joereynolds committed Oct 18, 2024
1 parent cc1c41d commit e95ef56
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pygame_gui/elements/ui_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(self,
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
visible: int = 1,
*,
starting_height: int = 1,):
starting_height: int = 1,
scale_func=pygame.transform.smoothscale):

super().__init__(relative_rect, manager, container,
starting_height=starting_height,
Expand All @@ -50,7 +51,7 @@ def __init__(self,

self.original_image = None

self.set_image(image_surface, image_is_alpha_premultiplied)
self.set_image(image_surface, image_is_alpha_premultiplied, scale_func)
self.rebuild_from_changed_theme_data()

def rebuild_from_changed_theme_data(self):
Expand Down Expand Up @@ -79,7 +80,8 @@ def set_dimensions(self, dimensions: Coordinate, clamp_to_container: bool = Fals

def set_image(self,
new_image: Union[pygame.surface.Surface, None],
image_is_alpha_premultiplied: bool = False) -> None:
image_is_alpha_premultiplied: bool = False,
scale_func = pygame.transform.smoothscale) -> None:
"""
Allows users to change the image displayed on a UIImage element during run time, without recreating
the element.
Expand All @@ -97,6 +99,6 @@ def set_image(self,
if (image_surface.get_width() != self.rect.width or
image_surface.get_height() != self.rect.height):
self.original_image = image_surface
self._set_image(pygame.transform.smoothscale(self.original_image, self.rect.size))
self._set_image(scale_func(self.original_image, self.rect.size))
else:
self._set_image(image_surface)

0 comments on commit e95ef56

Please sign in to comment.