Skip to content

Commit ec40851

Browse files
committed
Remove button B use and add brushes.
1 parent fced56b commit ec40851

1 file changed

Lines changed: 98 additions & 53 deletions

File tree

CircuitPython_PyPaint/code.py

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def __init__(self, splash, cursor_bmp):
7272
self._y_offset = cursor_bmp.height // 2
7373

7474

75-
7675
def poll(self):
77-
7876
"""Check for input. Returns contact (a bool), False (no button B),
7977
and it's location ((x,y) or None)"""
8078

@@ -94,6 +92,17 @@ def poke(self, location=None):
9492
self._cursor_grp.y = location[1] - self._y_offset
9593
self._display_grp.append(self._cursor_grp)
9694

95+
def set_cursor_bitmap(self, bmp):
96+
"""Update the cursor bitmap.
97+
98+
:param bmp: the new cursor bitmap
99+
"""
100+
self._cursor_grp.remove(self._cur_sprite)
101+
self._cur_sprite = displayio.TileGrid(bmp,
102+
pixel_shader=self._cur_palette)
103+
self._cursor_grp.append(self._cur_sprite)
104+
self.poke()
105+
97106
################################################################################
98107

99108
class CursorPoller(object):
@@ -115,12 +124,11 @@ def poll(self):
115124
and the cursor location ((x,y) or None)"""
116125
location = None
117126
self._cursor.update()
118-
a_button = self._cursor.held('a')
119-
b_button = self._cursor.held('b')
127+
a_button = self._cursor.held
120128
if a_button:
121129
location = (self._mouse_cursor.x + self._x_offset,
122130
self._mouse_cursor.y + self._y_offset)
123-
return a_button, b_button, location
131+
return a_button, location
124132

125133
#pylint:disable=unused-argument
126134
def poke(self, x=None, y=None):
@@ -129,6 +137,14 @@ def poke(self, x=None, y=None):
129137
self._mouse_cursor.show()
130138
#pylint:enable=unused-argument
131139

140+
def set_cursor_bitmap(self, bmp):
141+
"""Update the cursor bitmap.
142+
143+
:param bmp: the new cursor bitmap
144+
"""
145+
self._mouse_cursor.cursor_bitmap = bmp
146+
self.poke()
147+
132148
################################################################################
133149

134150
class Paint(object):
@@ -170,59 +186,105 @@ def __init__(self, display=board.DISPLAY):
170186
x=0, y=0)
171187
self._splash.append(self._fg_sprite)
172188

173-
self._color_palette = self._make_color_palette()
174-
self._splash.append(self._color_palette)
189+
self._number_of_palette_options = len(Color.colors) + 2
190+
self._swatch_height = self._h // self._number_of_palette_options
191+
self._swatch_width = self._w // 10
192+
self._logger.debug('Height: %d', self._h)
193+
self._logger.debug('Swatch height: %d', self._swatch_height)
194+
195+
self._palette = self._make_palette()
196+
self._splash.append(self._palette)
175197

176198
self._display.show(self._splash)
177199
self._display.refresh_soon()
178200
gc.collect()
179201
self._display.wait_for_frame()
180202

203+
self._brush = 0
204+
self._cursor_bitmaps = [self._cursor_bitmap_1(), self._cursor_bitmap_3()]
181205
if hasattr(board, 'TOUCH_XL'):
182-
self._poller = TouchscreenPoller(self._splash, self._cursor_bitmap())
206+
self._poller = TouchscreenPoller(self._splash, self._cursor_bitmaps[0])
183207
elif hasattr(board, 'BUTTON_CLOCK'):
184-
self._poller = CursorPoller(self._splash, self._cursor_bitmap())
208+
self._poller = CursorPoller(self._splash, self._cursor_bitmaps[0])
185209
else:
186-
raise AttributeError('PYOA requires a touchscreen or cursor.')
210+
raise AttributeError('PyPaint requires a touchscreen or cursor.')
187211

188212
self._a_pressed = False
189213
self._last_a_pressed = False
190-
self._b_pressed = False
191-
self._last_b_pressed = False
192214
self._location = None
193215
self._last_location = None
194216

195217
self._pencolor = 7
196218

197-
def _make_color_palette(self):
219+
def _make_palette(self):
198220
self._palette_bitmap = displayio.Bitmap(self._w // 10, self._h, 5)
199221
self._palette_palette = displayio.Palette(len(Color.colors))
200-
swatch_height = self._h // len(Color.colors)
201222
for i, c in enumerate(Color.colors):
202223
self._palette_palette[i] = c
203-
for y in range(swatch_height):
204-
for x in range(self._w // 10):
205-
self._palette_bitmap[x, swatch_height * i + y] = i
206-
self._palette_bitmap[self._w // 10 - 1, swatch_height * i + y] = 7
224+
for y in range(self._swatch_height):
225+
for x in range(self._swatch_width):
226+
self._palette_bitmap[x, self._swatch_height * i + y] = i
227+
228+
swatch_x_offset = (self._swatch_width - 9) // 2
229+
swatch_y_offset = (self._swatch_height - 9) // 2
230+
swatch_y = self._swatch_height * len(Color.colors) + swatch_y_offset
231+
for i in range(9):
232+
self._palette_bitmap[swatch_x_offset + 4, swatch_y + i] = 1
233+
self._palette_bitmap[swatch_x_offset + i, swatch_y + 4] = 1
234+
self._palette_bitmap[swatch_x_offset + 4, swatch_y + 4] = 0
207235

236+
swatch_y += self._swatch_height
237+
for i in range(9):
238+
self._palette_bitmap[swatch_x_offset + 3, swatch_y + i] = 1
239+
self._palette_bitmap[swatch_x_offset + 4, swatch_y + i] = 1
240+
self._palette_bitmap[swatch_x_offset + 5, swatch_y + i] = 1
241+
self._palette_bitmap[swatch_x_offset + i, swatch_y + 3] = 1
242+
self._palette_bitmap[swatch_x_offset + i, swatch_y + 4] = 1
243+
self._palette_bitmap[swatch_x_offset + i, swatch_y + 5] = 1
244+
for i in range(swatch_x_offset + 3, swatch_x_offset + 6):
245+
for j in range(swatch_y + 3, swatch_y + 6):
246+
self._palette_bitmap[i, j] = 0
247+
248+
for i in range(self._h):
249+
self._palette_bitmap[self._swatch_width - 1, i] = 7
208250

209251
return displayio.TileGrid(self._palette_bitmap,
210252
pixel_shader=self._palette_palette,
211253
x=0, y=0)
212254

213-
def _cursor_bitmap(self):
255+
def _cursor_bitmap_1(self):
214256
bmp = displayio.Bitmap(9, 9, 3)
215257
for i in range(9):
216258
bmp[4, i] = 1
217259
bmp[i, 4] = 1
218260
bmp[4, 4] = 0
219261
return bmp
220262

263+
def _cursor_bitmap_3(self):
264+
bmp = displayio.Bitmap(9, 9, 3)
265+
for i in range(9):
266+
bmp[3, i] = 1
267+
bmp[4, i] = 1
268+
bmp[5, i] = 1
269+
bmp[i, 3] = 1
270+
bmp[i, 4] = 1
271+
bmp[i, 5] = 1
272+
for i in range(3, 6):
273+
for j in range(3, 6):
274+
bmp[i, j] = 0
275+
return bmp
276+
221277
def _plot(self, x, y, c):
222-
try:
223-
self._fg_bitmap[int(x), int(y)] = c
224-
except IndexError:
225-
pass
278+
if self._brush == 0:
279+
r = [0]
280+
else:
281+
r = [-1, 0, 1]
282+
for i in r:
283+
for j in r:
284+
try:
285+
self._fg_bitmap[int(x + i), int(y + j)] = c
286+
except IndexError:
287+
pass
226288

227289
#pylint:disable=too-many-branches,too-many-statements
228290

@@ -286,22 +348,25 @@ def _draw_line(self, start, end):
286348

287349
#pylint:enable=too-many-branches,too-many-statements
288350

289-
290-
def _pick_color(self, location):
291-
swatch_height = self._h // len(Color.colors)
292-
picked = location[1] // swatch_height
293-
self._pencolor = picked
351+
def _handle_palette_selection(self, location):
352+
selected = location[1] // self._swatch_height
353+
if selected >= self._number_of_palette_options:
354+
return
355+
self._logger.debug('Palette selection: %d', selected)
356+
if selected < len(Color.colors):
357+
self._pencolor = selected
358+
else:
359+
self._brush = selected - len(Color.colors)
360+
self._poller.set_cursor_bitmap(self._cursor_bitmaps[self._brush])
294361

295362
def _handle_motion(self, start, end):
296363
self._logger.debug('Moved: (%d, %d) -> (%d, %d)', start[0], start[1], end[0], end[1])
297-
# self._plot(end[0], end[1], self._pencolor)
298-
# self._poller.poke()
299364
self._draw_line(start, end)
300365

301366
def _handle_a_press(self, location):
302367
self._logger.debug('A Pressed!')
303368
if location[0] < self._w // 10: # in color picker
304-
self._pick_color(location)
369+
self._handle_palette_selection(location)
305370
else:
306371
self._plot(location[0], location[1], self._pencolor)
307372
self._poller.poke()
@@ -311,14 +376,6 @@ def _handle_a_release(self, location):
311376
self._logger.debug('A Released!')
312377
#pylint:enable=unused-argument
313378

314-
def _handle_b_press(self, location):
315-
self._logger.debug('B Pressed!')
316-
317-
#pylint:disable=unused-argument
318-
def _handle_b_release(self, location):
319-
self._logger.debug('B Released!')
320-
#pylint:enable=unused-argument
321-
322379
@property
323380
def _was_a_just_pressed(self):
324381
return self._a_pressed and not self._last_a_pressed
@@ -327,14 +384,6 @@ def _was_a_just_pressed(self):
327384
def _was_a_just_released(self):
328385
return not self._a_pressed and self._last_a_pressed
329386

330-
@property
331-
def _was_b_just_pressed(self):
332-
return self._b_pressed and not self._last_b_pressed
333-
334-
@property
335-
def _was_b_just_released(self):
336-
return not self._b_pressed and self._last_b_pressed
337-
338387
@property
339388
def _did_move(self):
340389
if self._location is not None and self._last_location is not None:
@@ -345,8 +394,8 @@ def _did_move(self):
345394
return False
346395

347396
def _update(self):
348-
self._last_a_pressed, self._last_b_pressed, self._last_location = self._a_pressed, self._b_pressed, self._location
349-
self._a_pressed, self._b_pressed, self._location = self._poller.poll()
397+
self._last_a_pressed, self._last_location = self._a_pressed, self._location
398+
self._a_pressed, self._location = self._poller.poll()
350399

351400

352401
def run(self):
@@ -357,10 +406,6 @@ def run(self):
357406
self._handle_a_press(self._location)
358407
elif self._was_a_just_released:
359408
self._handle_a_release(self._location)
360-
if self._was_b_just_pressed:
361-
self._handle_b_press(self._location)
362-
elif self._was_b_just_released:
363-
self._handle_b_release(self._location)
364409
if self._did_move and self._a_pressed:
365410
self._handle_motion(self._last_location, self._location)
366411
time.sleep(0.1)

0 commit comments

Comments
 (0)