Skip to content

Commit 9ef52d7

Browse files
committed
Add support for custom cursor bitmap
1 parent ab67ed0 commit 9ef52d7

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

adafruit_cursorcontrol/cursorcontrol.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ class Cursor:
6666
mouse_cursor = Cursor(display, display_group=splash)
6767
"""
6868
# pylint: disable=too-many-arguments
69-
def __init__(self, display=None, display_group=None, is_hidden=False, cursor_speed=5, scale=1):
69+
def __init__(self, display=None, display_group=None, bmp=None, is_hidden=False, cursor_speed=5, scale=1):
7070
self._display = display
7171
self._scale = scale
7272
self._speed = cursor_speed
7373
self._is_hidden = is_hidden
7474
self._display_grp = display_group
7575
self._disp_sz = display.height - 1, display.width - 1
76-
self.generate_cursor()
76+
if bmp is None:
77+
bmp = self._default_cursor_bitmap()
78+
self.generate_cursor(bmp)
7779

7880
def __enter__(self):
7981
return self
@@ -173,34 +175,37 @@ def hide(self, is_hidden):
173175
self._is_hidden = False
174176
self._display_grp.append(self._cursor_grp)
175177

176-
def generate_cursor(self):
177-
"""Generates a cursor icon"""
178-
self._is_deinited()
179-
self._cursor_grp = displayio.Group(max_size=1, scale=self._scale)
180-
self._cur_bmp = displayio.Bitmap(20, 20, 3)
181-
self._cur_palette = displayio.Palette(3)
182-
self._cur_palette.make_transparent(0)
183-
self._cur_palette[1] = 0xFFFFFF
184-
self._cur_palette[2] = 0x0000
178+
def _default_cursor_bitmap(self):
179+
bmp = displayio.Bitmap(20, 20, 3)
185180
# left edge, outline
186-
for i in range(0, self._cur_bmp.height):
187-
self._cur_bmp[0, i] = 2
181+
for i in range(0, bmp.height):
182+
bmp[0, i] = 2
188183
# right diag outline, inside fill
189184
for j in range(1, 15):
190-
self._cur_bmp[j, j] = 2
191-
for i in range(j+1, self._cur_bmp.height - j):
192-
self._cur_bmp[j, i] = 1
185+
bmp[j, j] = 2
186+
for i in range(j+1, bmp.height - j):
187+
bmp[j, i] = 1
193188
# bottom diag., outline
194189
for i in range(1, 5):
195-
self._cur_bmp[i, self._cur_bmp.height-i] = 2
190+
bmp[i, bmp.height-i] = 2
196191
# bottom flat line, right side fill
197192
for i in range(5, 15):
198-
self._cur_bmp[i, 15] = 2
199-
self._cur_bmp[i-1, 14] = 1
200-
self._cur_bmp[i-2, 13] = 1
201-
self._cur_bmp[i-3, 12] = 1
202-
self._cur_bmp[i-4, 11] = 1
203-
self._cur_sprite = displayio.TileGrid(self._cur_bmp,
193+
bmp[i, 15] = 2
194+
bmp[i-1, 14] = 1
195+
bmp[i-2, 13] = 1
196+
bmp[i-3, 12] = 1
197+
bmp[i-4, 11] = 1
198+
return bmp
199+
200+
def generate_cursor(self, bmp):
201+
"""Generates a cursor icon"""
202+
self._is_deinited()
203+
self._cursor_grp = displayio.Group(max_size=1, scale=self._scale)
204+
self._cur_palette = displayio.Palette(3)
205+
self._cur_palette.make_transparent(0)
206+
self._cur_palette[1] = 0xFFFFFF
207+
self._cur_palette[2] = 0x0000
208+
self._cur_sprite = displayio.TileGrid(bmp,
204209
pixel_shader=self._cur_palette)
205210
self._cursor_grp.append(self._cur_sprite)
206211
self._display_grp.append(self._cursor_grp)

0 commit comments

Comments
 (0)