-
Notifications
You must be signed in to change notification settings - Fork 18
Fixed up for release. #1
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
""" | ||
`adafruit_display_button` | ||
`adafruit_button` | ||
================================================================================ | ||
|
||
UI Buttons for displayio | ||
|
@@ -38,21 +38,50 @@ | |
|
||
""" | ||
|
||
from micropython import const | ||
import displayio | ||
from adafruit_display_text.text_area import TextArea | ||
from adafruit_bitmap_font import bitmap_font | ||
from adafruit_display_shapes.rect import Rect | ||
from adafruit_display_shapes.roundrect import RoundRect | ||
|
||
__version__ = "0.0.0-auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git" | ||
|
||
|
||
def _check_color(color): | ||
# if a tuple is supplied, convert it to a RGB number | ||
if isinstance(color, tuple): | ||
r, g, b = color | ||
return int((r << 16) + (g << 8) + (b & 0xff)) | ||
return color | ||
|
||
|
||
class Button(): | ||
# pylint: disable=too-many-instance-attributes, too-many-locals | ||
"""Helper class for creating UI buttons for ``displayio``. | ||
|
||
:param x: The x position of the button. | ||
:param y: The y position of the button. | ||
:param width: The width of the button in pixels. | ||
:param height: The height of the button in pixels. | ||
:param name: The name of the button. | ||
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. | ||
Defaults to RECT. | ||
:param fill_color: The color to fill the button. Defaults to 0xFFFFFF. | ||
:param outline_color: The color of the outline of the button. | ||
:param label: The button label. Defaults to 0x0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. label is the button text that appears inside it, by default nothing is displayed |
||
:param label_font: The button label font. | ||
:param label_color: The color of the button label. Defaults to 0x0. | ||
:param selected_fill: Inverts the fill color. | ||
:param selected_outline: Inverts the outline color. | ||
:param selected_label: Inverts the label color. | ||
|
||
""" | ||
RECT = const(0) | ||
ROUNDRECT = const(1) | ||
SHADOWRECT = const(2) | ||
SHADOWROUNDRECT = const(3) | ||
|
||
def __init__(self, *, x, y, width, height, name=None, style=RECT, | ||
fill_color=0xFFFFFF, outline_color=0x0, | ||
label=None, label_font=None, label_color=0x0, | ||
|
@@ -66,66 +95,66 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, | |
self._selected = False | ||
self.group = displayio.Group() | ||
self.name = name | ||
self.label = label | ||
|
||
self.fill_color = fill_color | ||
self.outline_color = outline_color | ||
self.fill_color = _check_color(fill_color) | ||
self.outline_color = _check_color(outline_color) | ||
self.label_color = label_color | ||
# Selecting inverts the button colors! | ||
self.selected_fill = selected_fill | ||
self.selected_outline = selected_outline | ||
self.selected_label = selected_label | ||
self.selected_fill = _check_color(selected_fill) | ||
self.selected_outline = _check_color(selected_outline) | ||
self.selected_label = _check_color(selected_label) | ||
|
||
if self.selected_fill is None and fill_color is not None: | ||
self.selected_fill = (~fill_color) & 0xFFFFFF | ||
self.selected_fill = (~self.fill_color) & 0xFFFFFF | ||
if self.selected_outline is None and outline_color is not None: | ||
self.selected_outline = (~outline_color) & 0xFFFFFF | ||
self.selected_outline = (~self.outline_color) & 0xFFFFFF | ||
|
||
if outline_color or fill_color: | ||
self.body = self.shadow = None | ||
if style == RECT: | ||
if style == Button.RECT: | ||
self.body = Rect(x, y, width, height, | ||
fill=fill_color, outline=outline_color) | ||
elif style == ROUNDRECT: | ||
fill=self.fill_color, outline=self.outline_color) | ||
elif style == Button.ROUNDRECT: | ||
self.body = RoundRect(x, y, width, height, r=10, | ||
fill=fill_color, outline=outline_color) | ||
elif style == SHADOWRECT: | ||
self.shadow = Rect(x+2, y+2, width-2, height-2, | ||
fill=outline_color) | ||
self.body = Rect(x, y, width-2, height-2, | ||
fill=fill_color, outline=outline_color) | ||
elif style == SHADOWROUNDRECT: | ||
self.shadow = RoundRect(x+2, y+2, width-2, height-2, r=10, | ||
fill=self.fill_color, outline=self.outline_color) | ||
elif style == Button.SHADOWRECT: | ||
self.shadow = Rect(x + 2, y + 2, width - 2, height - 2, | ||
fill=outline_color) | ||
self.body = RoundRect(x, y, width-2, height-2, r=10, | ||
fill=fill_color, outline=outline_color) | ||
self.body = Rect(x, y, width - 2, height - 2, | ||
fill=self.fill_color, outline=self.outline_color) | ||
elif style == Button.SHADOWROUNDRECT: | ||
self.shadow = RoundRect(x + 2, y + 2, width - 2, height - 2, r=10, | ||
fill=self.outline_color) | ||
self.body = RoundRect(x, y, width - 2, height - 2, r=10, | ||
fill=self.fill_color, outline=self.outline_color) | ||
if self.shadow: | ||
self.group.append(self.shadow) | ||
self.group.append(self.body) | ||
|
||
if label and (label_color is not None): # button with text label | ||
if label and (label_color is not None): # button with text label | ||
if not label_font: | ||
raise RuntimeError("Please provide label font") | ||
dims = label_font.text_bounding_box(label) | ||
if dims[2] >= width or dims[3] >= height: | ||
raise RuntimeError("Button not large enough for label") | ||
self.label = TextArea(label_font, text=label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be Label not TextArea, too |
||
self.label.x = x + (width - dims[2])//2 | ||
self.label.x = x + (width - dims[2]) // 2 | ||
self.label.y = y + (height - dims[3]) | ||
self.label.color = label_color | ||
self.group.append(self.label) | ||
|
||
if self.selected_label is None and label_color is not None: | ||
self.selected_label = (~label_color) & 0xFFFFFF | ||
#print(dims) | ||
# print(dims) | ||
|
||
""" | ||
#else: # ok just a bounding box | ||
#self.bodyshape = displayio.Shape(width, height) | ||
#self.group.append(self.bodyshape) | ||
""" | ||
# else: # ok just a bounding box | ||
# self.bodyshape = displayio.Shape(width, height) | ||
# self.group.append(self.bodyshape) | ||
|
||
@property | ||
def selected(self): | ||
"""Selected inverts the colors.""" | ||
return self._selected | ||
|
||
@selected.setter | ||
|
@@ -142,4 +171,9 @@ def selected(self, value): | |
self.label.color = self.label_color | ||
|
||
def contains(self, point): | ||
return (self.x <= point[0] <= self.x+self.width) and (self.y <= point[1] <= self.y+self.height) | ||
"""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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adafruit_display_text should refer to Label, instead of TextArea (https://github.com/adafruit/Adafruit_CircuitPython_Display_Text/releases/tag/2.0.0)