Skip to content

Custom cursor support, rework 'hidden' property #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 44 additions & 28 deletions adafruit_cursorcontrol/cursorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Cursor.git"

class Cursor:
class Cursor(object):
"""Mouse cursor interaction for CircuitPython.

:param ~displayio.Display display: CircuitPython display object.
Expand All @@ -65,15 +65,18 @@ class Cursor:
# initialize the mouse cursor object
mouse_cursor = Cursor(display, display_group=splash)
"""
# pylint: disable=too-many-arguments
def __init__(self, display=None, display_group=None, is_hidden=False, cursor_speed=5, scale=1):
# pylint: disable=too-many-arguments,line-too-long
def __init__(self, display=None, display_group=None, bmp=None, is_hidden=False, cursor_speed=5, scale=1):
self._display = display
self._scale = scale
self._speed = cursor_speed
self._is_hidden = is_hidden
self._display_grp = display_group
self._disp_sz = display.height - 1, display.width - 1
self.generate_cursor()
if bmp is None:
bmp = self._default_cursor_bitmap()
self.generate_cursor(bmp)
# pylint: enable=too-many-arguments,line-too-long

def __enter__(self):
return self
Expand Down Expand Up @@ -159,12 +162,12 @@ def y(self, y_val):
self._cursor_grp.y = y_val

@property
def hide(self):
def hidden(self):
"""Returns True if the cursor is hidden or visible on the display."""
return self._is_hidden

@hide.setter
def hide(self, is_hidden):
@hidden.setter
def hidden(self, is_hidden):
self._is_deinited()
if is_hidden:
self._is_hidden = True
Expand All @@ -173,34 +176,47 @@ def hide(self, is_hidden):
self._is_hidden = False
self._display_grp.append(self._cursor_grp)

def generate_cursor(self):
"""Generates a cursor icon"""
self._is_deinited()
self._cursor_grp = displayio.Group(max_size=1, scale=self._scale)
self._cur_bmp = displayio.Bitmap(20, 20, 3)
self._cur_palette = displayio.Palette(3)
self._cur_palette.make_transparent(0)
self._cur_palette[1] = 0xFFFFFF
self._cur_palette[2] = 0x0000
def hide(self):
"""Hide the cursor."""
self.hidden = True

def show(self):
"""Show the cursor."""
self.hidden = False

#pylint:disable=no-self-use
def _default_cursor_bitmap(self):
bmp = displayio.Bitmap(20, 20, 3)
# left edge, outline
for i in range(0, self._cur_bmp.height):
self._cur_bmp[0, i] = 2
for i in range(0, bmp.height):
bmp[0, i] = 2
# right diag outline, inside fill
for j in range(1, 15):
self._cur_bmp[j, j] = 2
for i in range(j+1, self._cur_bmp.height - j):
self._cur_bmp[j, i] = 1
bmp[j, j] = 2
for i in range(j+1, bmp.height - j):
bmp[j, i] = 1
# bottom diag., outline
for i in range(1, 5):
self._cur_bmp[i, self._cur_bmp.height-i] = 2
bmp[i, bmp.height-i] = 2
# bottom flat line, right side fill
for i in range(5, 15):
self._cur_bmp[i, 15] = 2
self._cur_bmp[i-1, 14] = 1
self._cur_bmp[i-2, 13] = 1
self._cur_bmp[i-3, 12] = 1
self._cur_bmp[i-4, 11] = 1
self._cur_sprite = displayio.TileGrid(self._cur_bmp,
bmp[i, 15] = 2
bmp[i-1, 14] = 1
bmp[i-2, 13] = 1
bmp[i-3, 12] = 1
bmp[i-4, 11] = 1
return bmp
#pylint:enable=no-self-use

def generate_cursor(self, bmp):
"""Generates a cursor icon"""
self._is_deinited()
self._cursor_grp = displayio.Group(max_size=1, scale=self._scale)
self._cur_palette = displayio.Palette(3)
self._cur_palette.make_transparent(0)
self._cur_palette[1] = 0xFFFFFF
self._cur_palette[2] = 0x0000
self._cur_sprite = displayio.TileGrid(bmp,
pixel_shader=self._cur_palette)
self._cursor_grp.append(self._cur_sprite)
self._display_grp.append(self._cursor_grp)
37 changes: 37 additions & 0 deletions examples/cursorcontrol_custom_cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import time
import board
import displayio
from adafruit_cursorcontrol.cursorcontrol import Cursor
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager

# Create the display
display = board.DISPLAY

# Create the display context
splash = displayio.Group(max_size=5)

# initialize the mouse cursor object
bmp = displayio.Bitmap(20, 20, 3)
for i in range(0, bmp.height):
bmp[0, i] = 1
bmp[bmp.width - 1, i] = 1
for i in range(0, bmp.width):
bmp[i, 0] = 1
bmp[i, bmp.height - 1] = 1

mouse_cursor = Cursor(display, display_group=splash, bmp=bmp)

# initialize the cursormanager
cursor = CursorManager(mouse_cursor)

# show displayio group
display.show(splash)

while True:
cursor.update()
if cursor.is_clicked:
if mouse_cursor.hidden:
mouse_cursor.show()
else:
mouse_cursor.hide()
time.sleep(0.01)
6 changes: 3 additions & 3 deletions examples/cursorcontrol_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
while True:
cursor.update()
if cursor.is_clicked:
if mouse_cursor.hide:
mouse_cursor.hide = False
if mouse_cursor.hidden:
mouse_cursor.show()
else:
mouse_cursor.hide = True
mouse_cursor.hide()
time.sleep(0.01)