From 68760d1e06c93845e4e8ddcf33726464d66ffd40 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 5 Dec 2024 10:04:00 -0600 Subject: [PATCH 1/2] move contains() to base and add support for FocalTouch --- adafruit_button/button.py | 9 --------- adafruit_button/button_base.py | 26 +++++++++++++++++++++++++- adafruit_button/sprite_button.py | 9 --------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 2e2f3ec..d81e994 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -211,15 +211,6 @@ def group(self) -> Group: ) return self - def contains(self, point: tuple[int, int]) -> bool: - """Used to determine if a point is contained within a button. For example, - ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for - determining that a button has been touched. - """ - return (self.x <= point[0] <= self.x + self.width) and ( - self.y <= point[1] <= self.y + self.height - ) - @property def fill_color(self) -> Optional[int]: """The fill color of the button body""" diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index f4adaf9..820bc9d 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -27,7 +27,7 @@ from displayio import Group try: - from typing import Optional, Tuple, Union + from typing import Dict, List, Optional, Tuple, Union from fontio import FontProtocol except ImportError: @@ -177,3 +177,27 @@ def name(self) -> str: @name.setter def name(self, new_name: str) -> None: self._name = new_name + + def contains(self, point: Union[tuple[int, int], List[int, int], List[Dict[str, int]]]) -> bool: + """Used to determine if a point is contained within a button. For example, + ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for + determining that a button has been touched. + """ + if isinstance(point, tuple) or (isinstance(point, list) and isinstance(point[0], int)): + return (self.x <= point[0] <= self.x + self.width) and ( + self.y <= point[1] <= self.y + self.height + ) + elif isinstance(point, list): + touch_points = point + if len(touch_points) == 0: + return False + for touch_point in touch_points: + if ( + isinstance(touch_point, dict) + and "x" in touch_point.keys() + and "y" in touch_point.keys() + ): + if self.contains((touch_point["x"], touch_point["y"])): + return True + + return False diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 4ca220f..e8f2f44 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -130,15 +130,6 @@ def height(self) -> int: """The height of the button. Read-Only""" return self._height - def contains(self, point: Tuple[int, int]) -> bool: - """Used to determine if a point is contained within a button. For example, - ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for - determining that a button has been touched. - """ - return (self.x <= point[0] <= self.x + self.width) and ( - self.y <= point[1] <= self.y + self.height - ) - def _subclass_selected_behavior(self, value: bool) -> None: if self._selected: if self._selected_bmp is not None: From ca0f247c4e35f0e099a728fcd40079bb661b8aef Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 5 Dec 2024 10:51:10 -0600 Subject: [PATCH 2/2] fix List annotation --- adafruit_button/button_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 820bc9d..7e430b6 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -178,7 +178,7 @@ def name(self) -> str: def name(self, new_name: str) -> None: self._name = new_name - def contains(self, point: Union[tuple[int, int], List[int, int], List[Dict[str, int]]]) -> bool: + def contains(self, point: Union[tuple[int, int], List[int], List[Dict[str, int]]]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy