Skip to content

Commit fced56b

Browse files
committed
Add button B support as a start on multiple brush size support
1 parent cc1d646 commit fced56b

1 file changed

Lines changed: 54 additions & 26 deletions

File tree

CircuitPython_PyPaint/code.py

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,17 @@ def __init__(self, splash, cursor_bmp):
7474

7575

7676
def poll(self):
77-
"""Check for input. Returns contact (a bool) and it's location ((x,y) or None)"""
77+
78+
"""Check for input. Returns contact (a bool), False (no button B),
79+
and it's location ((x,y) or None)"""
7880

7981
p = self._touchscreen.touch_point
8082
if p is not None:
8183
self._cursor_grp.x = p[0] - self._x_offset
8284
self._cursor_grp.y = p[1] - self._y_offset
83-
return True, p
85+
return True, False, p
8486
else:
85-
return False, None
87+
return False, False, None
8688

8789
def poke(self, location=None):
8890
"""Force a bitmap refresh."""
@@ -109,14 +111,16 @@ def __init__(self, splash, cursor_bmp):
109111
self._logger = logging.getLogger('Paint')
110112

111113
def poll(self):
112-
"""Check for input. Returns press (a bool) and it's location ((x,y) or None)"""
114+
"""Check for input. Returns press of A (a bool), B,
115+
and the cursor location ((x,y) or None)"""
113116
location = None
114117
self._cursor.update()
115-
button = self._cursor.held
116-
if button:
118+
a_button = self._cursor.held('a')
119+
b_button = self._cursor.held('b')
120+
if a_button:
117121
location = (self._mouse_cursor.x + self._x_offset,
118122
self._mouse_cursor.y + self._y_offset)
119-
return button, location
123+
return a_button, b_button, location
120124

121125
#pylint:disable=unused-argument
122126
def poke(self, x=None, y=None):
@@ -181,8 +185,10 @@ def __init__(self, display=board.DISPLAY):
181185
else:
182186
raise AttributeError('PYOA requires a touchscreen or cursor.')
183187

184-
self._pressed = False
185-
self._last_pressed = False
188+
self._a_pressed = False
189+
self._last_a_pressed = False
190+
self._b_pressed = False
191+
self._last_b_pressed = False
186192
self._location = None
187193
self._last_location = None
188194

@@ -220,7 +226,7 @@ def _plot(self, x, y, c):
220226

221227
#pylint:disable=too-many-branches,too-many-statements
222228

223-
def _goto(self, start, end):
229+
def _draw_line(self, start, end):
224230
"""Draw a line from the previous position to the current one.
225231
226232
:param start: a tuple of (x, y) coordinatess to fram from
@@ -288,28 +294,46 @@ def _pick_color(self, location):
288294

289295
def _handle_motion(self, start, end):
290296
self._logger.debug('Moved: (%d, %d) -> (%d, %d)', start[0], start[1], end[0], end[1])
291-
self._goto(start, end)
297+
# self._plot(end[0], end[1], self._pencolor)
298+
# self._poller.poke()
299+
self._draw_line(start, end)
292300

293-
def _handle_press(self, location):
294-
self._logger.debug('Pressed!')
301+
def _handle_a_press(self, location):
302+
self._logger.debug('A Pressed!')
295303
if location[0] < self._w // 10: # in color picker
296304
self._pick_color(location)
297305
else:
298306
self._plot(location[0], location[1], self._pencolor)
299307
self._poller.poke()
300308

301309
#pylint:disable=unused-argument
302-
def _handle_release(self, location):
303-
self._logger.debug('Released!')
310+
def _handle_a_release(self, location):
311+
self._logger.debug('A Released!')
312+
#pylint:enable=unused-argument
313+
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!')
304320
#pylint:enable=unused-argument
305321

306322
@property
307-
def _was_just_pressed(self):
308-
return self._pressed and not self._last_pressed
323+
def _was_a_just_pressed(self):
324+
return self._a_pressed and not self._last_a_pressed
325+
326+
@property
327+
def _was_a_just_released(self):
328+
return not self._a_pressed and self._last_a_pressed
329+
330+
@property
331+
def _was_b_just_pressed(self):
332+
return self._b_pressed and not self._last_b_pressed
309333

310334
@property
311-
def _was_just_released(self):
312-
return not self._pressed and self._last_pressed
335+
def _was_b_just_released(self):
336+
return not self._b_pressed and self._last_b_pressed
313337

314338
@property
315339
def _did_move(self):
@@ -321,19 +345,23 @@ def _did_move(self):
321345
return False
322346

323347
def _update(self):
324-
self._last_pressed, self._last_location = self._pressed, self._location
325-
self._pressed, self._location = self._poller.poll()
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()
326350

327351

328352
def run(self):
329353
"""Run the painting program."""
330354
while True:
331355
self._update()
332-
if self._was_just_pressed:
333-
self._handle_press(self._location)
334-
elif self._was_just_released:
335-
self._handle_release(self._location)
336-
if self._did_move and self._pressed:
356+
if self._was_a_just_pressed:
357+
self._handle_a_press(self._location)
358+
elif self._was_a_just_released:
359+
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)
364+
if self._did_move and self._a_pressed:
337365
self._handle_motion(self._last_location, self._location)
338366
time.sleep(0.1)
339367

0 commit comments

Comments
 (0)