Skip to content

Commit 123d348

Browse files
authored
Merge pull request #5 from dastels/cursor
Custom cursor support, rework 'hidden' property
2 parents 0c5cc05 + 1b6a0ac commit 123d348

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

adafruit_cursorcontrol/cursorcontrol.py

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
__version__ = "0.0.0-auto.0"
4343
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Cursor.git"
4444

45-
class Cursor:
45+
class Cursor(object):
4646
"""Mouse cursor interaction for CircuitPython.
4747
4848
:param ~displayio.Display display: CircuitPython display object.
@@ -65,15 +65,18 @@ class Cursor:
6565
# initialize the mouse cursor object
6666
mouse_cursor = Cursor(display, display_group=splash)
6767
"""
68-
# pylint: disable=too-many-arguments
69-
def __init__(self, display=None, display_group=None, is_hidden=False, cursor_speed=5, scale=1):
68+
# pylint: disable=too-many-arguments,line-too-long
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)
79+
# pylint: enable=too-many-arguments,line-too-long
7780

7881
def __enter__(self):
7982
return self
@@ -159,12 +162,12 @@ def y(self, y_val):
159162
self._cursor_grp.y = y_val
160163

161164
@property
162-
def hide(self):
165+
def hidden(self):
163166
"""Returns True if the cursor is hidden or visible on the display."""
164167
return self._is_hidden
165168

166-
@hide.setter
167-
def hide(self, is_hidden):
169+
@hidden.setter
170+
def hidden(self, is_hidden):
168171
self._is_deinited()
169172
if is_hidden:
170173
self._is_hidden = True
@@ -173,34 +176,47 @@ def hide(self, is_hidden):
173176
self._is_hidden = False
174177
self._display_grp.append(self._cursor_grp)
175178

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
179+
def hide(self):
180+
"""Hide the cursor."""
181+
self.hidden = True
182+
183+
def show(self):
184+
"""Show the cursor."""
185+
self.hidden = False
186+
187+
#pylint:disable=no-self-use
188+
def _default_cursor_bitmap(self):
189+
bmp = displayio.Bitmap(20, 20, 3)
185190
# left edge, outline
186-
for i in range(0, self._cur_bmp.height):
187-
self._cur_bmp[0, i] = 2
191+
for i in range(0, bmp.height):
192+
bmp[0, i] = 2
188193
# right diag outline, inside fill
189194
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
195+
bmp[j, j] = 2
196+
for i in range(j+1, bmp.height - j):
197+
bmp[j, i] = 1
193198
# bottom diag., outline
194199
for i in range(1, 5):
195-
self._cur_bmp[i, self._cur_bmp.height-i] = 2
200+
bmp[i, bmp.height-i] = 2
196201
# bottom flat line, right side fill
197202
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,
203+
bmp[i, 15] = 2
204+
bmp[i-1, 14] = 1
205+
bmp[i-2, 13] = 1
206+
bmp[i-3, 12] = 1
207+
bmp[i-4, 11] = 1
208+
return bmp
209+
#pylint:enable=no-self-use
210+
211+
def generate_cursor(self, bmp):
212+
"""Generates a cursor icon"""
213+
self._is_deinited()
214+
self._cursor_grp = displayio.Group(max_size=1, scale=self._scale)
215+
self._cur_palette = displayio.Palette(3)
216+
self._cur_palette.make_transparent(0)
217+
self._cur_palette[1] = 0xFFFFFF
218+
self._cur_palette[2] = 0x0000
219+
self._cur_sprite = displayio.TileGrid(bmp,
204220
pixel_shader=self._cur_palette)
205221
self._cursor_grp.append(self._cur_sprite)
206222
self._display_grp.append(self._cursor_grp)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
import board
3+
import displayio
4+
from adafruit_cursorcontrol.cursorcontrol import Cursor
5+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
6+
7+
# Create the display
8+
display = board.DISPLAY
9+
10+
# Create the display context
11+
splash = displayio.Group(max_size=5)
12+
13+
# initialize the mouse cursor object
14+
bmp = displayio.Bitmap(20, 20, 3)
15+
for i in range(0, bmp.height):
16+
bmp[0, i] = 1
17+
bmp[bmp.width - 1, i] = 1
18+
for i in range(0, bmp.width):
19+
bmp[i, 0] = 1
20+
bmp[i, bmp.height - 1] = 1
21+
22+
mouse_cursor = Cursor(display, display_group=splash, bmp=bmp)
23+
24+
# initialize the cursormanager
25+
cursor = CursorManager(mouse_cursor)
26+
27+
# show displayio group
28+
display.show(splash)
29+
30+
while True:
31+
cursor.update()
32+
if cursor.is_clicked:
33+
if mouse_cursor.hidden:
34+
mouse_cursor.show()
35+
else:
36+
mouse_cursor.hide()
37+
time.sleep(0.01)

examples/cursorcontrol_simpletest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
while True:
2323
cursor.update()
2424
if cursor.is_clicked:
25-
if mouse_cursor.hide:
26-
mouse_cursor.hide = False
25+
if mouse_cursor.hidden:
26+
mouse_cursor.show()
2727
else:
28-
mouse_cursor.hide = True
28+
mouse_cursor.hide()
2929
time.sleep(0.01)

0 commit comments

Comments
 (0)