Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move contains() to base and add support for FocalTouch #52

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions adafruit_button/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
26 changes: 25 additions & 1 deletion adafruit_button/button_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
9 changes: 0 additions & 9 deletions adafruit_button/sprite_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading