Skip to content

Commit 403047b

Browse files
optimize touch_indicator
1 parent d3cd013 commit 403047b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

examples/_uix/touch_indicator.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__all__ = ('touch_indicator', )
22

33
from functools import partial
4-
from typing import Unpack, Self
4+
from typing import Unpack
55

66
import pygame
77
from pygame import Color, Event
@@ -12,14 +12,22 @@
1212
class Ring:
1313
__slots__ = ('draw', 'pos', )
1414

15-
def __init__(self, draw_target, color, pos, radius, line_width):
16-
self.draw = partial(self._draw, draw_target, color, radius, line_width, self)
17-
self.pos = pos
15+
def __init__(self, draw_target: pygame.Surface, color, initial_pos, radius, line_width):
16+
ring_img = pygame.Surface((radius * 2, radius * 2)).convert(draw_target)
17+
color = Color(color)
18+
bgcolor = Color("black")
19+
if color == bgcolor:
20+
bgcolor = Color("white")
21+
ring_img.fill(bgcolor)
22+
ring_img.set_colorkey(bgcolor)
23+
pygame.draw.circle(ring_img, color, (radius, radius), radius, line_width)
1824

19-
def _draw(pygame_draw_circle, draw_target, color, radius, line_width, self: Self):
20-
pygame_draw_circle(draw_target, color, self.pos, radius, line_width)
25+
self.draw = partial(self.__class__._draw, self, draw_target.blit, ring_img, ring_img.get_rect())
26+
self.pos = initial_pos
2127

22-
_draw = partial(_draw, pygame.draw.circle)
28+
def _draw(self, blit, ring_img, ring_dest):
29+
ring_dest.center = self.pos
30+
blit(ring_img, ring_dest)
2331

2432

2533
async def touch_indicator(*, color="white", radius=60, line_width=4, priority, **kwargs: Unpack[apg.CommonParams]):

0 commit comments

Comments
 (0)