|
27 | 27 | from displayio import Group
|
28 | 28 |
|
29 | 29 | try:
|
30 |
| - from typing import Optional, Tuple, Union |
| 30 | + from typing import Dict, List, Optional, Tuple, Union |
31 | 31 |
|
32 | 32 | from fontio import FontProtocol
|
33 | 33 | except ImportError:
|
@@ -177,3 +177,27 @@ def name(self) -> str:
|
177 | 177 | @name.setter
|
178 | 178 | def name(self, new_name: str) -> None:
|
179 | 179 | 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 |
0 commit comments