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

Support the use of an image in UIButton from python code without a theme entry #622

Open
aelydam opened this issue Aug 3, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@aelydam
Copy link

aelydam commented Aug 3, 2024

Is your feature request related to a problem? Please describe.
No.

Describe the solution you'd like
Let us to make a button to use an image from code instead of setting it up in the theme json.
In the game I'm working, I'm using buttons to create a hotbar where the player can select spells. Each spell has its own icon, and there are some 50+ possible icons.
Setting each possible icon in the theme file and a ObjectID for each spell would be really annoying.
Using code to retrieve each image from the spell database would be much more useful.

Describe alternatives you've considered
As a workaround, I'm setting button.normal_image = my_surface, button.hovered_image = .... and running button.rebuild() and replacing _set_any_images_from_theme in a subclass.
This seems to work, but I'm not sure it would have undesired consequences.

I think implementing this, would open the way for a future implementation of icons for UISelectionList, since behind the scenes each item of a selection list is an UIButton.

@aelydam aelydam added the enhancement New feature or request label Aug 3, 2024
@rbaltrusch
Copy link
Contributor

For me, the approach of adding a custom UIImage on top of an empty UIButton widget has worked without needing to specify anything in a theme file, at the expense of needing some additional manual layout work:

manager: UIManager
button = UIButton(
    relative_rect=pygame.Rect(...),
    text="",
    manager=manager,
)
UIImage(
    relative_rect=pygame.Rect(...),
    image_surface=pygame.image.load("image.png"),
    parent_element=button,
    manager=manager,
)

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