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..7e430b6 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], 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: