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

Rescale the image for the rect in UIButton #278

Open
HantianZheng opened this issue Jul 14, 2022 · 0 comments
Open

Rescale the image for the rect in UIButton #278

HantianZheng opened this issue Jul 14, 2022 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@HantianZheng
Copy link

Is your feature request related to a problem? Please describe.
I think it will be very handy if the image(e.g. normal image, hover image) are auto-rescaled to the size of the rect in the UI Button.

Describe the solution you'd like

I have modified the code myself and I think this could be a good example.

in pygame_gui\elements\ui_button.py

`def _set_any_images_from_theme(self):

    changed = False
    normal_image = None
    try:
        normal_image = self.ui_theme.get_image('normal_image', self.combined_element_ids)
        normal_image = pygame.transform.scale(normal_image, self.relative_rect.size)  # auto-rescale the image
    except LookupError:
        normal_image = None
    finally:
        if normal_image != self.normal_image:
            self.normal_image = normal_image
            self.hovered_image = normal_image
            self.selected_image = normal_image
            self.disabled_image = normal_image
            changed = True

    hovered_image = None
    try:
        hovered_image = self.ui_theme.get_image('hovered_image', self.combined_element_ids)
        hovered_image = pygame.transform.scale(hovered_image, self.relative_rect.size)  # auto-rescale the image
    except LookupError:
        hovered_image = self.normal_image
    finally:
        if hovered_image != self.hovered_image:
            self.hovered_image = hovered_image
            changed = True

    selected_image = None
    try:
        selected_image = self.ui_theme.get_image('selected_image', self.combined_element_ids)
        selected_image = pygame.transform.scale(selected_image, self.relative_rect.size)  # auto-rescale the image
    except LookupError:
        selected_image = self.normal_image
    finally:
        if selected_image != self.selected_image:
            self.selected_image = selected_image
            changed = True

    disabled_image = None
    try:
        disabled_image = self.ui_theme.get_image('disabled_image', self.combined_element_ids)
        disabled_image = pygame.transform.scale(disabled_image, self.relative_rect.size)  # auto-rescale the image
    except LookupError:
        disabled_image = self.normal_image
    finally:
        if disabled_image != self.disabled_image:
            self.disabled_image = disabled_image
            changed = True

    return changed`
@HantianZheng HantianZheng added the enhancement New feature or request label Jul 14, 2022
@MyreMylar MyreMylar added this to the Version 0.7.0 milestone Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants