forked from ladyada/Adafruit_CircuitPython_Display_Button
-
Notifications
You must be signed in to change notification settings - Fork 18
adding sprite_button and refactoring to package instead of single file #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
91d285e
adding sprite_button and refactoring to package instead of single file
FoamyGuy 17d1b2d
fix docs build
FoamyGuy 96e272e
Merge branch 'main' into sprite_button
FoamyGuy ddd7c13
adding sprite button example
FoamyGuy d8a6f0b
Merge branch 'main' into sprite_button
FoamyGuy f1c449a
merge main, update pyproject.toml for package instead of single file
FoamyGuy ea58138
Merge branch 'main' of https://github.com/adafruit/Adafruit_CircuitPy…
8d7daa6
add arg docs, add examples to rst, add modules to api.rst.
FoamyGuy 95bb72a
format
FoamyGuy ea1769a
raise helpful error if missing bmp_path
FoamyGuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
`adafruit_button` | ||
================================================================================ | ||
|
||
UI Buttons for displayio | ||
|
||
|
||
* Author(s): Limor Fried | ||
|
||
Implementation Notes | ||
-------------------- | ||
|
||
**Software and Dependencies:** | ||
|
||
* Adafruit CircuitPython firmware for the supported boards: | ||
https://github.com/adafruit/circuitpython/releases | ||
|
||
""" | ||
from adafruit_button.button import Button |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
`adafruit_button.button` | ||
================================================================================ | ||
|
||
UI Buttons for displayio | ||
|
||
|
||
* Author(s): Limor Fried | ||
|
||
Implementation Notes | ||
-------------------- | ||
|
||
**Software and Dependencies:** | ||
|
||
* Adafruit CircuitPython firmware for the supported boards: | ||
https://github.com/adafruit/circuitpython/releases | ||
|
||
""" | ||
from adafruit_display_text.bitmap_label import Label | ||
from displayio import Group | ||
|
||
|
||
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 ButtonBase(Group): | ||
# pylint: disable=too-many-instance-attributes | ||
"""Superclass 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 tiles. | ||
:param height: The height of the button in tiles. | ||
:param name: A name, or miscellaneous string that is stored on the button. | ||
:param label: The text that appears inside the button. Defaults to not displaying the label. | ||
:param label_font: The button label font. | ||
:param label_color: The color of the button label text. Defaults to 0x0. | ||
:param selected_label: Text that appears when selected | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
x, | ||
y, | ||
width, | ||
height, | ||
name=None, | ||
label=None, | ||
label_font=None, | ||
label_color=0x0, | ||
selected_label=None | ||
): | ||
super().__init__(x=x, y=y) | ||
self.x = x | ||
self.y = y | ||
self._width = width | ||
self._height = height | ||
self._font = label_font | ||
self._selected = False | ||
self.name = name | ||
self._label = label | ||
self._label_color = label_color | ||
self._label_font = label_font | ||
self._selected_label = _check_color(selected_label) | ||
|
||
@property | ||
def label(self): | ||
"""The text label of the button""" | ||
return self._label.text | ||
|
||
@label.setter | ||
def label(self, newtext): | ||
if self._label and self and (self[-1] == self._label): | ||
self.pop() | ||
|
||
self._label = None | ||
if not newtext or (self._label_color is None): # no new text | ||
return # nothing to do! | ||
|
||
if not self._label_font: | ||
raise RuntimeError("Please provide label font") | ||
self._label = Label(self._label_font, text=newtext) | ||
dims = self._label.bounding_box | ||
if dims[2] >= self.width or dims[3] >= self.height: | ||
while len(self._label.text) > 1 and ( | ||
dims[2] >= self.width or dims[3] >= self.height | ||
): | ||
self._label.text = "{}.".format(self._label.text[:-2]) | ||
dims = self._label.bounding_box | ||
if len(self._label.text) <= 1: | ||
raise RuntimeError("Button not large enough for label") | ||
self._label.x = (self.width - dims[2]) // 2 | ||
self._label.y = self.height // 2 | ||
self._label.color = ( | ||
self._label_color if not self.selected else self._selected_label | ||
) | ||
self.append(self._label) | ||
|
||
if (self.selected_label is None) and (self._label_color is not None): | ||
self.selected_label = (~self._label_color) & 0xFFFFFF | ||
|
||
def _subclass_selected_behavior(self, value): | ||
# Subclasses should overide this! | ||
pass | ||
|
||
@property | ||
def selected(self): | ||
"""Selected inverts the colors.""" | ||
return self._selected | ||
|
||
@selected.setter | ||
def selected(self, value): | ||
if value == self._selected: | ||
return # bail now, nothing more to do | ||
self._selected = value | ||
|
||
if self._selected: | ||
new_label = self.selected_label | ||
else: | ||
new_label = self._label_color | ||
if self._label is not None: | ||
self._label.color = new_label | ||
|
||
self._subclass_selected_behavior(value) | ||
|
||
@property | ||
def selected_label(self): | ||
"""The font color of the button when selected""" | ||
return self._selected_label | ||
|
||
@selected_label.setter | ||
def selected_label(self, new_color): | ||
self._selected_label = _check_color(new_color) | ||
|
||
@property | ||
def label_color(self): | ||
"""The font color of the button""" | ||
return self._label_color | ||
|
||
@label_color.setter | ||
def label_color(self, new_color): | ||
self._label_color = _check_color(new_color) | ||
self._label.color = self._label_color |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.