mirror of
https://github.com/clockworkpi/launcher.git
synced 2025-12-12 17:58:50 +01:00
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""
|
|
Rounded rectangles in both non-antialiased and antialiased varieties.
|
|
"""
|
|
|
|
import pygame as pg
|
|
|
|
from pygame import gfxdraw
|
|
|
|
|
|
def round_rect(surface, rect, color, rad=20, border=0, inside=(0,0,0,0)):
|
|
"""
|
|
Draw a rect with rounded corners to surface. Argument rad can be specified
|
|
to adjust curvature of edges (given in pixels). An optional border
|
|
width can also be supplied; if not provided the rect will be filled.
|
|
Both the color and optional interior color (the inside argument) support
|
|
alpha.
|
|
"""
|
|
rect = pg.Rect(rect)
|
|
zeroed_rect = rect.copy()
|
|
zeroed_rect.topleft = 0,0
|
|
image = pg.Surface(rect.size).convert_alpha()
|
|
image.fill((0,0,0,0))
|
|
_render_region(image, zeroed_rect, color, rad)
|
|
if border:
|
|
zeroed_rect.inflate_ip(-2*border, -2*border)
|
|
_render_region(image, zeroed_rect, inside, rad)
|
|
surface.blit(image, rect)
|
|
|
|
|
|
def _render_region(image, rect, color, rad):
|
|
"""Helper function for round_rect."""
|
|
corners = rect.inflate(-2*rad, -2*rad)
|
|
for attribute in ("topleft", "topright", "bottomleft", "bottomright"):
|
|
pg.draw.circle(image, color, getattr(corners,attribute), rad)
|
|
image.fill(color, rect.inflate(-2*rad,0))
|
|
image.fill(color, rect.inflate(0,-2*rad))
|
|
|
|
|
|
def aa_round_rect(surface, rect, color, rad=20, border=0, inside=(0,0,0)):
|
|
"""
|
|
Draw an antialiased rounded rect on the target surface. Alpha is not
|
|
supported in this implementation but other than that usage is identical to
|
|
round_rect.
|
|
"""
|
|
rect = pg.Rect(rect)
|
|
_aa_render_region(surface, rect, color, rad)
|
|
if border:
|
|
rect.inflate_ip(-2*border, -2*border)
|
|
_aa_render_region(surface, rect, inside, rad)
|
|
|
|
|
|
def _aa_render_region(image, rect, color, rad):
|
|
"""Helper function for aa_round_rect."""
|
|
corners = rect.inflate(-2*rad-1, -2*rad-1)
|
|
for attribute in ("topleft", "topright", "bottomleft", "bottomright"):
|
|
x, y = getattr(corners, attribute)
|
|
gfxdraw.aacircle(image, x, y, rad, color)
|
|
gfxdraw.filled_circle(image, x, y, rad, color)
|
|
image.fill(color, rect.inflate(-2*rad,0))
|
|
image.fill(color, rect.inflate(0,-2*rad))
|