pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Add support for rounded rects to surfaces

Open bilhox opened this issue 1 year ago • 4 comments
trafficstars

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.

bilhox avatar Jan 27 '24 13:01 bilhox

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 avatar Jan 30 '24 07:01 Starbuck5

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

Starbuck5 avatar Jan 30 '24 07:01 Starbuck5

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

bilhox avatar Jan 30 '24 08:01 bilhox

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?

damusss avatar Jul 11 '24 13:07 damusss