pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
838 stars 140 forks source link

Add support for rounded rects to surfaces #2695

Open bilhox opened 8 months ago

bilhox commented 8 months ago

Hello,

It would be great to add a way to round surface corners. I believe it wouldn't be difficult to implement it, since we do it manually in pygame.draw.rect. What do you think about adding a new function in transform sub module like pygame.transform.round_corners ?

I'm pretty surprised no one is talking about it, maybe there is already a pretty efficient way to do that ? (Maybe there is even an issue already opened ?) I mean with blendmodes it's possible, but it isn't intuitive at all for beginners for example, and I believe we can optimise the process.

This feature would be helpful for everyone creating gui with buttons with rounded corners + texture.

Starbuck5 commented 8 months ago

For the purposes of GUI, with scalability in mind, I one technique people often use is 9-patching. https://en.wikipedia.org/wiki/9-slice_scaling.

Starbuck5 commented 8 months ago

Also I slightly misread your report, so I ended up whipping up an example of how to do this, which I might as well share.

import pygame

surface = pygame.image.load("cool.jpg")

# Poor man's convert alpha without a display, I want this and the
# mock_surface to have the same pixelformat.
standardized = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
standardized.blit(surface, (0,0))
surface = standardized

mock_surface = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
mock_surface.fill((0,0,0,0))
pygame.draw.rect(mock_surface, "white", mock_surface.get_rect(), border_radius=100)

surface.blit(mock_surface, (0,0), special_flags=pygame.BLEND_RGBA_MULT)

pygame.image.save(surface, "out.png")

cool2 out

bilhox commented 8 months ago

I thought about the same solution, I know it works, but I believe we can make it more intuitive for users.

damusss commented 2 months ago

For the purposes of GUI, with scalability in mind, I one technique people often use is 9-patching. https://en.wikipedia.org/wiki/9-slice_scaling.

@Starbuck5 do you think pygame should export 9 patching or do you think users should do it themselves?