22
22
"""
23
23
import displayio
24
24
25
+ try :
26
+ from typing import Optional , Type
27
+ from types import TracebackType
28
+ except ImportError :
29
+ pass
30
+
25
31
__version__ = "0.0.0-auto.0"
26
32
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git"
27
33
@@ -31,9 +37,10 @@ class Cursor:
31
37
32
38
:param ~displayio.Display display: CircuitPython display object.
33
39
:param ~displayio.Group display_group: CircuitPython group object to append the cursor to.
40
+ :param ~displayio.Bitmap bmp: CircuitPython bitmap object to use as the cursor
41
+ :param bool is_hidden: Cursor is hidden on init.
34
42
:param int cursor_speed: Speed of the cursor, in pixels.
35
43
:param int scale: Scale amount for the cursor in both directions.
36
- :param bool is_hidden: Cursor is hidden on init.
37
44
38
45
Example for creating a cursor layer
39
46
@@ -53,12 +60,12 @@ class Cursor:
53
60
# pylint: disable=too-many-arguments,line-too-long
54
61
def __init__ (
55
62
self ,
56
- display = None ,
57
- display_group = None ,
58
- bmp = None ,
59
- is_hidden = False ,
60
- cursor_speed = 5 ,
61
- scale = 1 ,
63
+ display : Optional [ displayio . Display ] = None ,
64
+ display_group : Optional [ displayio . Group ] = None ,
65
+ bmp : Optional [ displayio . Bitmap ] = None ,
66
+ is_hidden : bool = False ,
67
+ cursor_speed : int = 5 ,
68
+ scale : int = 1 ,
62
69
):
63
70
self ._display = display
64
71
self ._scale = scale
@@ -75,19 +82,24 @@ def __init__(
75
82
76
83
# pylint: enable=too-many-arguments,line-too-long
77
84
78
- def __enter__ (self ):
85
+ def __enter__ (self ) -> "Cursor" :
79
86
return self
80
87
81
- def __exit__ (self , exception_type , exception_value , traceback ):
88
+ def __exit__ (
89
+ self ,
90
+ exception_type : Optional [Type [type ]],
91
+ exception_value : Optional [BaseException ],
92
+ traceback : Optional [TracebackType ],
93
+ ) -> None :
82
94
self .deinit ()
83
95
84
- def deinit (self ):
96
+ def deinit (self ) -> None :
85
97
"""deinitializes the cursor object."""
86
98
self ._is_deinited ()
87
99
self ._scale = None
88
100
self ._display_grp .remove (self ._cursor_grp )
89
101
90
- def _is_deinited (self ):
102
+ def _is_deinited (self ) -> None :
91
103
"""checks cursor deinitialization"""
92
104
if self ._scale is None :
93
105
raise ValueError (
@@ -96,12 +108,12 @@ def _is_deinited(self):
96
108
)
97
109
98
110
@property
99
- def scale (self ):
111
+ def scale (self ) -> int :
100
112
"""Returns the cursor's scale amount as an integer."""
101
113
return self ._scale
102
114
103
115
@scale .setter
104
- def scale (self , scale_value ) :
116
+ def scale (self , scale_value : int ) -> None :
105
117
"""Scales the cursor by scale_value in both directions.
106
118
:param int scale_value: Amount to scale the cursor by.
107
119
"""
@@ -111,12 +123,12 @@ def scale(self, scale_value):
111
123
self ._cursor_grp .scale = scale_value
112
124
113
125
@property
114
- def speed (self ):
126
+ def speed (self ) -> int :
115
127
"""Returns the cursor's speed, in pixels."""
116
128
return self ._speed
117
129
118
130
@speed .setter
119
- def speed (self , speed ) :
131
+ def speed (self , speed : int ) -> None :
120
132
"""Sets the speed of the cursor.
121
133
:param int speed: Cursor movement speed, in pixels.
122
134
"""
@@ -125,12 +137,12 @@ def speed(self, speed):
125
137
self ._speed = speed
126
138
127
139
@property
128
- def x (self ):
140
+ def x (self ) -> int :
129
141
"""Returns the cursor's x-coordinate."""
130
142
return self ._cursor_grp .x
131
143
132
144
@x .setter
133
- def x (self , x_val ) :
145
+ def x (self , x_val : int ) -> None :
134
146
"""Sets the x-value of the cursor.
135
147
:param int x_val: cursor x-position, in pixels.
136
148
"""
@@ -143,12 +155,12 @@ def x(self, x_val):
143
155
self ._cursor_grp .x = x_val
144
156
145
157
@property
146
- def y (self ):
158
+ def y (self ) -> int :
147
159
"""Returns the cursor's y-coordinate."""
148
160
return self ._cursor_grp .y
149
161
150
162
@y .setter
151
- def y (self , y_val ) :
163
+ def y (self , y_val : int ) -> None :
152
164
"""Sets the y-value of the cursor.
153
165
:param int y_val: cursor y-position, in pixels.
154
166
"""
@@ -161,12 +173,12 @@ def y(self, y_val):
161
173
self ._cursor_grp .y = y_val
162
174
163
175
@property
164
- def hidden (self ):
176
+ def hidden (self ) -> bool :
165
177
"""Returns True if the cursor is hidden or visible on the display."""
166
178
return self ._is_hidden
167
179
168
180
@hidden .setter
169
- def hidden (self , is_hidden ) :
181
+ def hidden (self , is_hidden : bool ) -> None :
170
182
self ._is_deinited ()
171
183
if is_hidden :
172
184
self ._is_hidden = True
@@ -175,16 +187,16 @@ def hidden(self, is_hidden):
175
187
self ._is_hidden = False
176
188
self ._display_grp .append (self ._cursor_grp )
177
189
178
- def hide (self ):
190
+ def hide (self ) -> None :
179
191
"""Hide the cursor."""
180
192
self .hidden = True
181
193
182
- def show (self ):
194
+ def show (self ) -> None :
183
195
"""Show the cursor."""
184
196
self .hidden = False
185
197
186
198
# pylint:disable=no-self-use
187
- def _default_cursor_bitmap (self ):
199
+ def _default_cursor_bitmap (self ) -> displayio . Bitmap :
188
200
bmp = displayio .Bitmap (20 , 20 , 3 )
189
201
# left edge, outline
190
202
for i in range (0 , bmp .height ):
@@ -209,23 +221,26 @@ def _default_cursor_bitmap(self):
209
221
# pylint:enable=no-self-use
210
222
211
223
@property
212
- def cursor_bitmap (self ):
224
+ def cursor_bitmap (self ) -> displayio . Bitmap :
213
225
"""Return the cursor bitmap."""
214
226
return self ._cursor_bitmap
215
227
216
228
@cursor_bitmap .setter
217
- def cursor_bitmap (self , bmp ) :
229
+ def cursor_bitmap (self , bmp : displayio . Bitmap ) -> None :
218
230
"""Set a new cursor bitmap.
219
231
220
- :param bmp: A Bitmap to use for the cursor
232
+ :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor
221
233
"""
222
234
self ._cursor_bitmap = bmp
223
235
self ._cursor_grp .remove (self ._cur_sprite )
224
236
self ._cur_sprite = displayio .TileGrid (bmp , pixel_shader = self ._cur_palette )
225
237
self ._cursor_grp .append (self ._cur_sprite )
226
238
227
- def generate_cursor (self , bmp ):
228
- """Generates a cursor icon"""
239
+ def generate_cursor (self , bmp : displayio .Bitmap ) -> None :
240
+ """Generates a cursor icon
241
+
242
+ :param ~displayio.Bitmap bmp: A Bitmap to use for the cursor
243
+ """
229
244
self ._is_deinited ()
230
245
self ._cursor_grp = displayio .Group (scale = self ._scale )
231
246
self ._cur_palette = displayio .Palette (3 )
0 commit comments