Skip to content

Commit 1984725

Browse files
committed
start roundrect
1 parent e6e96c4 commit 1984725

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import displayio
2+
3+
class RoundRect(displayio.TileGrid):
4+
def __init__(self, x, y, width, height, r, *, fill=None, outline=None):
5+
self._bitmap = displayio.Bitmap(width, height, 3)
6+
self._palette = displayio.Palette(3)
7+
self._palette.make_transparent(0)
8+
9+
"""
10+
if fill is not None:
11+
#for i in range(x0-r, x0+r): # draw the center line
12+
# self._bitmap[i, r] = 2
13+
# ask our helper to fill in the circle
14+
self._helper(x0, y0, r, 0xF, 2, fill=True)
15+
self._palette[2] = fill
16+
else:
17+
self._palette.make_transparent(2)
18+
"""
19+
20+
if outline is not None:
21+
self._palette[1] = outline
22+
# draw flat sides
23+
for w in range(r, width-r):
24+
self._bitmap[w, 0] = 1
25+
self._bitmap[w, height-1] = 1
26+
for h in range(r, height-r):
27+
self._bitmap[0, h] = 1
28+
self._bitmap[width-1, h] = 1
29+
# draw round corners
30+
self._helper(r, r, r, x_offset=width-2*r-1, y_offset=height-2*r-1, cornerflags=0xF)
31+
32+
super().__init__(self._bitmap, pixel_shader=self._palette, position=(x, y))
33+
34+
def _helper(self, x0, y0, r, *, x_offset=0, y_offset=0, cornerflags=0xF, color=1, fill=False):
35+
f = 1 - r
36+
ddF_x = 1
37+
ddF_y = -2 * r
38+
x = 0
39+
y = r
40+
41+
while x < y:
42+
if f >= 0:
43+
y -= 1
44+
ddF_y += 2
45+
f += ddF_y
46+
x += 1
47+
ddF_x += 2
48+
f += ddF_x
49+
if cornerflags & 0x8:
50+
if fill:
51+
for w in range(x0-y, x0+y):
52+
self._bitmap[w, y0+x] = color
53+
for w in range(x0-x, x0+x):
54+
self._bitmap[w, y0+y] = color
55+
else:
56+
self._bitmap[x0-y, y0+x+y_offset] = color
57+
self._bitmap[x0-x, y0+y+y_offset] = color
58+
if cornerflags & 0x1:
59+
if fill:
60+
for w in range(x0-y, x0+y):
61+
self._bitmap[w, y0-x] = color
62+
for w in range(x0-x, x0+x):
63+
self._bitmap[w, y0-y] = color
64+
else:
65+
self._bitmap[x0-y, y0-x] = color
66+
self._bitmap[x0-x, y0-y] = color
67+
if cornerflags & 0x4:
68+
self._bitmap[x0+x+x_offset, y0+y+y_offset] = color
69+
self._bitmap[x0+y+x_offset, y0+x+y_offset] = color
70+
if cornerflags & 0x2:
71+
self._bitmap[x0+x+x_offset, y0-y] = color
72+
self._bitmap[x0+y+x_offset, y0-x] = color
73+
74+
@property
75+
def x(self):
76+
return self.position[0]
77+
78+
@x.setter
79+
def x(self, x):
80+
self.position = (x, self.position[1])
81+
82+
@property
83+
def y(self):
84+
return self.position[1]
85+
86+
@x.setter
87+
def y(self, y):
88+
self.position = (self.position[0], y)

0 commit comments

Comments
 (0)