-
Notifications
You must be signed in to change notification settings - Fork 18
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
adding sprite_button and refactoring to package instead of single file #36
Conversation
After a bit more thought I'm marking this as Draft because it not only relies on the other PR in imageload mentioned above, but also this PR from the core: #6270 |
The requisite PRs have been merged now. This could use an example still though. I'll come back and add that with a new commit. |
# Conflicts: # .pylintrc
Is this still waiting on an example before it's reviewed? |
I had created it locally but forgot to commit and push it 🤦 I've just added a commit with the example and I believe this is ready for review now @makermelissa |
Merged main again because I had push changes causing a merge conflict! |
@makermelissa do you want to review this or were just querying about its status? |
Hello :), let me know if you folks want me to review. Thanks :) |
@jposada202020 I'd be happy to have anyone take a look. If you end up having the time please do :) |
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.
Hello, I start reviewing this, I have started with the sprite_button. General comments
- We need to add the example to
examples.rst
- We need to add the new modules in
api.rst
for RTD to generate the docs - It would be a good idea now that we are Adding annotations #37 to do the type annotations for sprite_button
- We would need to include
adafruit_imageload
inrequirements.txt
because sprtie_button will need that library - I have tested the functionality with a modified script (below) works as expected. One thing, when the bitmaps are not declared, the user will not be aware as that is the reason for the sprite_button is not showing. If you do not declare the
bmp_path
, it will raiseAttributeError: __enter__
not very helpful, however if you do not declare theselected_bmp_path
the button will not load the bitmap - Others are mentioned inline.
- Need to modify the example to correct the import to reflect the new structure
Test code
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import displayio
import pygame
import terminalio
from adafruit_bitmap_font import bitmap_font
from adafruit_button.button import Button
from adafruit_button.sprite_button import SpriteButton
from blinka_displayio_pygamedisplay import PyGameDisplay
# Make the display context
splash = displayio.Group(scale=2)
display = PyGameDisplay(width=480, height=320)
font2 = bitmap_font.load_font("Helvetica-Bold-16.bdf")
main_group = displayio.Group()
display.show(main_group)
BUTTON_WIDTH = 10 * 16
BUTTON_HEIGHT = 3 * 16
BUTTON_MARGIN = 20
font = terminalio.FONT
buttons = []
button_0 = SpriteButton(
x=BUTTON_MARGIN,
y=BUTTON_MARGIN,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="button0",
label_font=font,
label_color=0xFF0000,
bmp_path="bmps/gradient_button_0.bmp",
selected_bmp_path="bmps/gradient_button_1.bmp",
transparent_index=0,
)
# font2 = "font/Arial-italicMT-17.bdf"
button_1 = SpriteButton(
x=BUTTON_MARGIN+180,
y=BUTTON_MARGIN,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="button1",
label_font=font2,
label_color=0xFF00FF,
bmp_path="bmps/gradient_button_0.bmp",
# selected_bmp_path="bmps/gradient_button_1.bmp",
transparent_index=0,
)
BUTTON_X = 110
BUTTON_Y = 95
BUTTON_WIDTH = 100
BUTTON_HEIGHT = 50
BUTTON_STYLE = Button.ROUNDRECT
BUTTON_FILL_COLOR = 0x00FFFF
BUTTON_OUTLINE_COLOR = 0xFF00FF
BUTTON_LABEL = "HELLO WORLD"
BUTTON_LABEL_COLOR = 0x000000
button2 = Button(
x=BUTTON_X,
y=BUTTON_Y,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
style=BUTTON_STYLE,
fill_color=BUTTON_FILL_COLOR,
outline_color=BUTTON_OUTLINE_COLOR,
label=BUTTON_LABEL,
label_font=terminalio.FONT,
label_color=BUTTON_LABEL_COLOR,
)
buttons.append(button_0)
buttons.append(button_1)
buttons.append(button2)
for b in buttons:
main_group.append(b)
while True:
# get mouse down events
ev = pygame.event.get(eventtype=pygame.MOUSEBUTTONDOWN)
for event in ev:
pos = pygame.mouse.get_pos()
print(pos)
for i, b in enumerate(buttons):
if b.contains(pos):
print("Button %d pressed" % i)
b.selected = True
b.label = "pressed"
else:
b.selected = False
b.label = "button0"
Thank you @jposada202020 I've added a new commit with the following:
I will work on a follow-up PR after this one to add typing annotations for sprite_button, and get everything from #37 incorporated in the new class files, as well as handle the requested changes from there. |
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.
Thanks for the changes. Retested with the code above. Very Nice work. Love the buttons' colors :)
Thank you for trying it out @jposada202020! |
Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Button to 1.7.0 from 1.6.9: > Merge pull request adafruit/Adafruit_CircuitPython_Display_Button#36 from FoamyGuy/sprite_button Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA: > Updated download stats for the libraries
This is a somewhat large change, but should be "un-breaking" of existing code.
I've refactored the library into a package with multiple files and added a new type of button the SpriteButton which uses 3x3 spriteshates inflated with the change from: #58 to create fully customized bitmap buttons.
THIS SHOULD NOT GET MERGED UNTIL AFTER #58
The original Button (display_shapes based Button) still exists and works the same as it used to.
I factored the common parts into a new ButtonBase class and then made each of the subclasses contain only their own unique behavior.
Pylint is complaining about duplicated lines in the init() signature, and in the calls to
super.__init__()
in order to resolve it for now I raised the minimum duplicate lines to trigger that error. It could possibly be done by usingkwargs
instead of all of the actual argument names, but then the sphinx documentation isn't as good because it doesn't list the relavent function arguments inside of the parens of the init function. I believe the tradoff of unhappy pylint is worth it for better docs, but I'm open to other thoughts or opinions.