Skip to content
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

Merged
merged 10 commits into from
Jul 9, 2023

Conversation

FoamyGuy
Copy link

@FoamyGuy FoamyGuy commented Apr 23, 2022

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 using kwargs 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.

@FoamyGuy FoamyGuy marked this pull request as draft April 23, 2022 17:56
@FoamyGuy
Copy link
Author

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
This should also not get merged before #6270 in the core.

@FoamyGuy FoamyGuy marked this pull request as ready for review May 13, 2022 22:52
@FoamyGuy
Copy link
Author

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
@FoamyGuy FoamyGuy requested a review from a team May 28, 2022 15:51
@makermelissa
Copy link
Collaborator

Is this still waiting on an example before it's reviewed?

@FoamyGuy
Copy link
Author

FoamyGuy commented Jun 6, 2022

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

@tekktrik
Copy link
Member

Merged main again because I had push changes causing a merge conflict!

@dhalbert
Copy link

@makermelissa do you want to review this or were just querying about its status?

@tekktrik tekktrik requested review from tekktrik and removed request for a team January 6, 2023 17:29
@FoamyGuy FoamyGuy requested a review from a team April 25, 2023 12:21
@jposada202020
Copy link

Hello :), let me know if you folks want me to review. Thanks :)

@FoamyGuy
Copy link
Author

FoamyGuy commented May 6, 2023

@jposada202020 I'd be happy to have anyone take a look. If you end up having the time please do :)

Copy link

@jposada202020 jposada202020 left a 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

  1. We need to add the example to examples.rst
  2. We need to add the new modules in api.rst for RTD to generate the docs
  3. It would be a good idea now that we are Adding annotations #37 to do the type annotations for sprite_button
  4. We would need to include adafruit_imageload in requirements.txt because sprtie_button will need that library
  5. 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 raise AttributeError: __enter__ not very helpful, however if you do not declare the selected_bmp_path the button will not load the bitmap
  6. Others are mentioned inline.
  7. 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"

@FoamyGuy
Copy link
Author

Thank you @jposada202020

I've added a new commit with the following:

  • add examples to examples.rst
  • add modules to api.rst
  • add imageload to optional_requirements.txt this is only needed for sprite_button so people using the original one can skip having this library.
  • added the missing argument docstrings
  • removed the leftover prints.
  • raise a more helpful error if bmp_path is not supplied

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.

Copy link

@jposada202020 jposada202020 left a 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 :)

@FoamyGuy
Copy link
Author

FoamyGuy commented Jul 9, 2023

Thank you for trying it out @jposada202020!

@FoamyGuy FoamyGuy merged commit ea69caf into adafruit:main Jul 9, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants