Skip to content

Commit 1ec53fc

Browse files
authored
Merge pull request #52 from FoamyGuy/focal_touch_support
move contains() to base and add support for FocalTouch
2 parents da2744d + ca0f247 commit 1ec53fc

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

Diff for: adafruit_button/button.py

-9
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,6 @@ def group(self) -> Group:
211211
)
212212
return self
213213

214-
def contains(self, point: tuple[int, int]) -> bool:
215-
"""Used to determine if a point is contained within a button. For example,
216-
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
217-
determining that a button has been touched.
218-
"""
219-
return (self.x <= point[0] <= self.x + self.width) and (
220-
self.y <= point[1] <= self.y + self.height
221-
)
222-
223214
@property
224215
def fill_color(self) -> Optional[int]:
225216
"""The fill color of the button body"""

Diff for: adafruit_button/button_base.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from displayio import Group
2828

2929
try:
30-
from typing import Optional, Tuple, Union
30+
from typing import Dict, List, Optional, Tuple, Union
3131

3232
from fontio import FontProtocol
3333
except ImportError:
@@ -177,3 +177,27 @@ def name(self) -> str:
177177
@name.setter
178178
def name(self, new_name: str) -> None:
179179
self._name = new_name
180+
181+
def contains(self, point: Union[tuple[int, int], List[int], List[Dict[str, int]]]) -> bool:
182+
"""Used to determine if a point is contained within a button. For example,
183+
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
184+
determining that a button has been touched.
185+
"""
186+
if isinstance(point, tuple) or (isinstance(point, list) and isinstance(point[0], int)):
187+
return (self.x <= point[0] <= self.x + self.width) and (
188+
self.y <= point[1] <= self.y + self.height
189+
)
190+
elif isinstance(point, list):
191+
touch_points = point
192+
if len(touch_points) == 0:
193+
return False
194+
for touch_point in touch_points:
195+
if (
196+
isinstance(touch_point, dict)
197+
and "x" in touch_point.keys()
198+
and "y" in touch_point.keys()
199+
):
200+
if self.contains((touch_point["x"], touch_point["y"])):
201+
return True
202+
203+
return False

Diff for: adafruit_button/sprite_button.py

-9
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,6 @@ def height(self) -> int:
130130
"""The height of the button. Read-Only"""
131131
return self._height
132132

133-
def contains(self, point: Tuple[int, int]) -> bool:
134-
"""Used to determine if a point is contained within a button. For example,
135-
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
136-
determining that a button has been touched.
137-
"""
138-
return (self.x <= point[0] <= self.x + self.width) and (
139-
self.y <= point[1] <= self.y + self.height
140-
)
141-
142133
def _subclass_selected_behavior(self, value: bool) -> None:
143134
if self._selected:
144135
if self._selected_bmp is not None:

0 commit comments

Comments
 (0)