From 91d285ec5b95208971ecffdd5b422d064a52f014 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 23 Apr 2022 12:38:54 -0500 Subject: [PATCH 1/7] adding sprite_button and refactoring to package instead of single file --- .pylintrc | 2 +- adafruit_button/__init__.py | 23 +++ .../button.py | 108 ++----------- adafruit_button/button_base.py | 152 ++++++++++++++++++ adafruit_button/sprite_button.py | 126 +++++++++++++++ setup.py | 2 +- 6 files changed, 320 insertions(+), 93 deletions(-) create mode 100644 adafruit_button/__init__.py rename adafruit_button.py => adafruit_button/button.py (73%) create mode 100644 adafruit_button/button_base.py create mode 100644 adafruit_button/sprite_button.py diff --git a/.pylintrc b/.pylintrc index cfd1c41..4abf2dc 100644 --- a/.pylintrc +++ b/.pylintrc @@ -252,7 +252,7 @@ ignore-docstrings=yes ignore-imports=yes # Minimum lines number of a similarity. -min-similarity-lines=4 +min-similarity-lines=10 [BASIC] diff --git a/adafruit_button/__init__.py b/adafruit_button/__init__.py new file mode 100644 index 0000000..cd66773 --- /dev/null +++ b/adafruit_button/__init__.py @@ -0,0 +1,23 @@ +# 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_button.button import Button diff --git a/adafruit_button.py b/adafruit_button/button.py similarity index 73% rename from adafruit_button.py rename to adafruit_button/button.py index caf554b..dc9fd7a 100644 --- a/adafruit_button.py +++ b/adafruit_button/button.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT """ -`adafruit_button` +`adafruit_button.button` ================================================================================ UI Buttons for displayio @@ -22,24 +22,15 @@ """ from micropython import const -import displayio -from adafruit_display_text.label import Label from adafruit_display_shapes.rect import Rect from adafruit_display_shapes.roundrect import RoundRect +from adafruit_button.button_base import ButtonBase, _check_color __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(displayio.Group): +class Button(ButtonBase): # pylint: disable=too-many-instance-attributes, too-many-locals """Helper class for creating UI buttons for ``displayio``. @@ -141,26 +132,27 @@ def __init__( selected_outline=None, 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 + super().__init__( + x=x, + y=y, + width=width, + height=height, + name=name, + label=label, + label_font=label_font, + label_color=label_color, + selected_label=selected_label, + ) + self.body = self.fill = self.shadow = None self.style = style self._fill_color = _check_color(fill_color) self._outline_color = _check_color(outline_color) - self._label_color = label_color - self._label_font = label_font + # Selecting inverts the button colors! 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 = (~self._fill_color) & 0xFFFFFF @@ -173,64 +165,17 @@ def __init__( self.label = 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 - self.append(self._label) - - if (self.selected_label is None) and (self._label_color is not None): - self.selected_label = (~self._label_color) & 0xFFFFFF - - @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 + def _subclass_selected_behavior(self, value): if self._selected: new_fill = self.selected_fill new_out = self.selected_outline - new_label = self.selected_label else: new_fill = self._fill_color new_out = self._outline_color - new_label = self._label_color # update all relevant colors! if self.body is not None: self.body.fill = new_fill self.body.outline = new_out - if self._label is not None: - self._label.color = new_label @property def group(self): @@ -295,25 +240,6 @@ def selected_outline(self, new_color): if self.selected: self.body.outline = self._selected_outline - @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 - @property def width(self): """The width of the button""" diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py new file mode 100644 index 0000000..29d33a9 --- /dev/null +++ b/adafruit_button/button_base.py @@ -0,0 +1,152 @@ +# 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 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. + """ + + 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 diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py new file mode 100644 index 0000000..c9ed66b --- /dev/null +++ b/adafruit_button/sprite_button.py @@ -0,0 +1,126 @@ +# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_button.button` +================================================================================ + +Bitmap 3x3 Spritesheet based UI Button for displayio + + +* Author(s): Tim Cocks + +Implementation Notes +-------------------- + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" +from adafruit_imageload.tilegrid_inflator import inflate_tilegrid +from adafruit_imageload import load +from adafruit_button.button_base import ButtonBase + + +class SpriteButton(ButtonBase): + """Helper class for creating 3x3 Bitmap Spritesheet 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 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 string bmp_path: The path of the 3x3 spritesheet Bitmap file + :param string selected_bmp_path: The path of the 3x3 spritesheet Bitmap file to use when pressed + """ + + def __init__( + self, + *, + x, + y, + width, + height, + name=None, + label=None, + label_font=None, + label_color=0x0, + selected_label=None, + bmp_path=None, + selected_bmp_path=None, + transparent_index=None + ): + super().__init__( + x=x, + y=y, + width=width, + height=height, + name=name, + label=label, + label_font=label_font, + label_color=label_color, + selected_label=selected_label, + ) + + self._bmp, self._bmp_palette = load(bmp_path) + + self._selected_bmp = None + self._selected_bmp_palette = None + self._selected = False + + if selected_bmp_path is not None: + self._selected_bmp, self._selected_bmp_palette = load(selected_bmp_path) + if transparent_index is not None: + if isinstance(transparent_index, tuple): + for _index in transparent_index: + self._selected_bmp_palette.make_transparent(_index) + elif isinstance(transparent_index, int): + self._selected_bmp_palette.make_transparent(0) + + print((width // (self._bmp.width // 3), height // (self._bmp.height // 3))) + self._btn_tilegrid = inflate_tilegrid( + bmp_obj=self._bmp, + bmp_palette=self._bmp_palette, + target_size=( + width // (self._bmp.width // 3), + height // (self._bmp.height // 3), + ), + transparent_index=transparent_index, + ) + self.append(self._btn_tilegrid) + + print("setting label") + self.label = label + + @property + def width(self): + """The width of the button""" + return self._width + + @property + def height(self): + """The height of the button""" + return self._height + + def contains(self, point): + """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 + ) + + def _subclass_selected_behavior(self, value): + if self._selected: + if self._selected_bmp is not None: + self._btn_tilegrid.bitmap = self._selected_bmp + self._btn_tilegrid.pixel_shader = self._selected_bmp_palette + else: + self._btn_tilegrid.bitmap = self._bmp + self._btn_tilegrid.pixel_shader = self._bmp_palette diff --git a/setup.py b/setup.py index 8beeb54..3aa2af5 100644 --- a/setup.py +++ b/setup.py @@ -56,5 +56,5 @@ # simple. Or you can use find_packages(). # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, # CHANGE `py_modules=['...']` TO `packages=['...']` - py_modules=["adafruit_button"], + packages=["adafruit_button"], ) From 17d1b2d15c57b199a268ba0763987fa2e3bdc4e1 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 23 Apr 2022 15:10:12 -0500 Subject: [PATCH 2/7] fix docs build --- adafruit_button/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button/__init__.py b/adafruit_button/__init__.py index cd66773..2fa5ba8 100644 --- a/adafruit_button/__init__.py +++ b/adafruit_button/__init__.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT """ -`adafruit_button.button` +`adafruit_button` ================================================================================ UI Buttons for displayio From ddd7c13b3256d90a6e329a9e773226ed31a7d27a Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 6 Jun 2022 10:24:45 -0500 Subject: [PATCH 3/7] adding sprite button example --- examples/bmps/gradient_button_0.bmp | Bin 0 -> 3206 bytes examples/bmps/gradient_button_0.bmp.license | 2 + examples/bmps/gradient_button_1.bmp | Bin 0 -> 2822 bytes examples/bmps/gradient_button_1.bmp.license | 2 + .../display_button_spritebutton_simpletest.py | 68 ++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 examples/bmps/gradient_button_0.bmp create mode 100644 examples/bmps/gradient_button_0.bmp.license create mode 100644 examples/bmps/gradient_button_1.bmp create mode 100644 examples/bmps/gradient_button_1.bmp.license create mode 100644 examples/display_button_spritebutton_simpletest.py diff --git a/examples/bmps/gradient_button_0.bmp b/examples/bmps/gradient_button_0.bmp new file mode 100644 index 0000000000000000000000000000000000000000..bfa8cfa18c0c192577fc329f79b4b9f870a59ccc GIT binary patch literal 3206 zcmb7_Sx{7G5QUpXK@gEa9Yl=m1O{Xq7dAziQ4t3aR8T=hMZg7IB1Aw17f=+9;J)vg zxWqN?7=4IIv?@tudDHU6hg4Zfm3c_=)H(hCcQEmRBz^Dc?{xp?-my@b@s5_hq^iY@ zs1Yu~kPCV0gFZxI!?hk_BO+qwt)}r43sx!fKbq@1S;jlr^$v8G*bQ_8J#G?vLVd45 zSBZT0%+Y1wG)p3M}R3?psmEIU@ACfBeaQlEI1ZS-$9(U9%>@C zfi`ebJ=8#)4Q7LmTBwdV7t933&BFLXc<&ZTnrY2C6!PWaVb~|mMtWnFDfTq zG#8pjTme>qOJ+kQ#7n`Y;PM$z5pn+K#4F%ez?a}>vbq2&B(4Q(!IBHa^$vV)7nn=j z0N((v&n9jXO(x!CgY3kc!Oh^-OyV}tIO1~r{q0DD(usG1JHgIT#JfeKi4P<}$;3y8 zL-E8Xqlr(6Vu+6;b3Ou!B)$|3g%Dp0f`$@b2XC370OI@p&;a5GVCy3gzU2w=qkfPt z@ni5Ycxfgyi}(vK$eZ{H_yl~~3+he$415ND>jJqFe+M3VNPG`Jvwh!V9zy(eInIjU zp&#;ze}uloznQO~Z(1?85dQ?d+=q9*gkBN9dP@A_XX59-K)({d#{BvP@o&$G-~0~! zLHy?%V#fa^XLa>h&XkiqhSi}nDg5`eB)Xr_@w-<)IeYy0iQ`fyRafoA*$Y?i>V)xt z8g74l`0$ZqM-Cr3auf|#njV4kw^c|zAnw7H{reB@Kd@ibfdhxMG5SH_rw=424@mr6 z=bp~aj*d>0YQo(ehVQ^ZN9Q?tK*zf-Z#Sy_Bd6Ogwq2$j>fYXdS$_cfjkdP7ZMr}V zGPbEpZS`W?4fz0h=ydCr)>fsrfNO2ts<(2AIxSh$ofgz)x#eit837g)zmD5Uka~FwkWLN5YqW2i=~!SX{cVc zSTC_wv$U!ui*>%TvZAuGa$!YC)v!lc89gY5+K+sCY(^&QF~- ztx)c%=u(AXK|!I^RJ8>3fzIdU<>lt(qYLwL^I<_}b=&;Be4TgXI2==^I3`Qa$$?Xq zqiD=IICMBB>-;3tgzW4|6DEK%025VrE+%3x(s{ehKHipP&$8LkZCN%OtaST$yFJTp zmu;5NZrAxtRbw(U$3Wx8jsam%Mjxj=CY?{u$iSVEktRsjQyPZU)U@=p^mN#?R5{N` z)A`XUDWj!EjU1^uTFNN2k+?>yT?(vZbl#erm~6EsCM8)>Ny*lvL~D{YF&SerT%tA6 zD!b&wB&*KH$0fwYOU0oC>K+~klaQdiK8=sl`4Q2vF|je0*jS4t+7e@d(PfE=vBbpU z40m)ib|Z9tPL!?)1Cjq6qoN|-kBW$jh>Vz{^QXc?Lc+sCL&L(u!@@)1LPEnZ;R;cU zFqtH9O6P9|1rG}vh8rz7SPbgDVZmx=ASejtrp|XQ7(6&I&`Hii7 z*Jo#D4|Q{KcXM@jadmfhb#;N<-Q8S<%sz8VC)C%k@s+AKJ@)Rn@vYip*!gGae&|1@ F{{k9?7Q+Al literal 0 HcmV?d00001 diff --git a/examples/bmps/gradient_button_0.bmp.license b/examples/bmps/gradient_button_0.bmp.license new file mode 100644 index 0000000..8f7990c --- /dev/null +++ b/examples/bmps/gradient_button_0.bmp.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT diff --git a/examples/bmps/gradient_button_1.bmp b/examples/bmps/gradient_button_1.bmp new file mode 100644 index 0000000000000000000000000000000000000000..ba6d75c0bfd0b47efb2f89f4cd576e865f03c9ba GIT binary patch literal 2822 zcmb7FT}+#06n&1(WGw=5!jT z-l!SbTrw}zU}8+v7-!~CJWK80>>~Z{&`xAcOGl7U<3JduYm?@aX;(xJdg9kb2#(pEj;_! z5=Nd_#FI}g;_;`W7(PvWdI19?Q4Bt_jKMRDIQ3i?_r+1^}5{o8A>U5dg%TsOJ``{*(%FUN4?@@-UISwq8> z7^<$WpyBE=%CD`V`dSR;_hKmi;0}t$R+0PBI&wZ zpf6s>(6-Q1I&T|ROFhZFbD9(J8xl`rq^iE9rIIHJ?SdN8)6&ujY5cI1!?*UFMSUrUK&S6uyDa8kWKSCB)*B|V3vUSj5O zo-gfA;I4$+y;&=pvxL1{u!kltY5GbzdnLbb-`!jAk5SvPEBU{Tqo&yPJI&SGgSn`EM3WbG5>S5+Xjlm?FqC&|R6Ar3} z6>A-=cq2GiEcsHSv6NsmmMCUfq6}hkNYh+WYAiKM{!r48d^w}6ysWH@iI19fwTUHf zt}s=YE2vR7nJP?XYGyLXGRuMthFS9WCJo6~R#qP7qZ$n?4~s*juh^D+l@3dlMWn?- zlC_$pahJT+T5GMLr`Bq<*3?wjuqL4fvUp9XuC>-mzP`S$j*coKZAdt*#FhM!q#^l+ zq#^m{=H@0UK~r;MQ*#r~y2pd2CGT+9?R4xmyUk%^!3Jr&!$!<-*ljf0?2^APX-MAX zYH4x0Tu!DICsT`ygp0s}i?+;?KNiPPU5Pk)jQDe;(Wo3{vEAk- zDfy1$^mH7jB5qgQ-qB7I%{TtuixkQ`Ivluzu)Wa^7*|UkKaSm z$Cj^)lwb1QjDR{sCjx;$FhFB>cP|mEolvu%&ZNo*Tb%gyPXKO>(08zzl}6#xJL literal 0 HcmV?d00001 diff --git a/examples/bmps/gradient_button_1.bmp.license b/examples/bmps/gradient_button_1.bmp.license new file mode 100644 index 0000000..8f7990c --- /dev/null +++ b/examples/bmps/gradient_button_1.bmp.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT diff --git a/examples/display_button_spritebutton_simpletest.py b/examples/display_button_spritebutton_simpletest.py new file mode 100644 index 0000000..8f78c48 --- /dev/null +++ b/examples/display_button_spritebutton_simpletest.py @@ -0,0 +1,68 @@ +# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +import time +import board +import displayio +import adafruit_touchscreen +import terminalio +from adafruit_button.sprite_button import SpriteButton + +# These pins are used as both analog and digital! XL, XR and YU must be analog +# and digital capable. YD just need to be digital +ts = adafruit_touchscreen.Touchscreen( + board.TOUCH_XL, + board.TOUCH_XR, + board.TOUCH_YD, + board.TOUCH_YU, + calibration=((5200, 59000), (5800, 57000)), + size=(board.DISPLAY.width, board.DISPLAY.height), +) + +# Make the display context +main_group = displayio.Group() +board.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, + bmp_path="bmps/gradient_button_0.bmp", + selected_bmp_path="bmps/gradient_button_1.bmp", + transparent_index=0, +) + +buttons.append(button_0) + +for b in buttons: + main_group.append(b) +while True: + p = ts.touch_point + if p: + print(p) + for i, b in enumerate(buttons): + if b.contains(p): + print("Button %d pressed" % i) + b.selected = True + b.label = "pressed" + else: + b.selected = False + b.label = "button0" + + else: + for i, b in enumerate(buttons): + if b.selected: + b.selected = False + b.label = "button0" + time.sleep(0.01) From f1c449acef499a5b67d8dd1c004b8c77f053e549 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 15 Aug 2022 10:32:34 -0500 Subject: [PATCH 4/7] merge main, update pyproject.toml for package instead of single file --- pyproject.toml | 2 +- setup.py | 60 -------------------------------------------------- 2 files changed, 1 insertion(+), 61 deletions(-) delete mode 100644 setup.py diff --git a/pyproject.toml b/pyproject.toml index c34c497..1d845d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,7 @@ classifiers = [ dynamic = ["dependencies", "optional-dependencies"] [tool.setuptools] -py-modules = ["adafruit_button"] +packages = ["adafruit_button"] [tool.setuptools.dynamic] dependencies = {file = ["requirements.txt"]} diff --git a/setup.py b/setup.py deleted file mode 100644 index 3aa2af5..0000000 --- a/setup.py +++ /dev/null @@ -1,60 +0,0 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries -# -# SPDX-License-Identifier: MIT - -"""A setuptools based setup module. - -See: -https://packaging.python.org/en/latest/distributing.html -https://github.com/pypa/sampleproject -""" - -from setuptools import setup, find_packages - -# To use a consistent encoding -from codecs import open -from os import path - -here = path.abspath(path.dirname(__file__)) - -# Get the long description from the README file -with open(path.join(here, "README.rst"), encoding="utf-8") as f: - long_description = f.read() - -setup( - name="adafruit-circuitpython-display_button", - use_scm_version=True, - setup_requires=["setuptools_scm"], - description="UI Buttons for displayio", - long_description=long_description, - long_description_content_type="text/x-rst", - # The project's main homepage. - url="https://github.com/adafruit/Adafruit_CircuitPython_Display_Button", - # Author details - author="Adafruit Industries", - author_email="circuitpython@adafruit.com", - install_requires=[ - "Adafruit-Blinka", - "adafruit-blinka-displayio", - "adafruit-circuitpython-display-text", - "adafruit-circuitpython-display-shapes", - ], - # Choose your license - license="MIT", - # See https://pypi.python.org/pypi?%3Aaction=list_classifiers - classifiers=[ - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Topic :: Software Development :: Libraries", - "Topic :: System :: Hardware", - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3", - ], - # What does your project relate to? - keywords="adafruit blinka circuitpython micropython display_button buttons UI", - # You can just specify the packages manually here if your project is - # simple. Or you can use find_packages(). - # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, - # CHANGE `py_modules=['...']` TO `packages=['...']` - packages=["adafruit_button"], -) From 8d7daa65eaa38478afecdd7ddb4064d76b6be989 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 29 Jun 2023 20:13:24 -0500 Subject: [PATCH 5/7] add arg docs, add examples to rst, add modules to api.rst. --- adafruit_button/button_base.py | 2 ++ adafruit_button/sprite_button.py | 5 +++-- docs/api.rst | 9 +++++++++ docs/examples.rst | 9 +++++++++ optional_requirements.txt | 2 ++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 29d33a9..41a2286 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -40,9 +40,11 @@ class ButtonBase(Group): :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__( diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index c9ed66b..75dc70a 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -32,11 +32,14 @@ class SpriteButton(ButtonBase): :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 :param string bmp_path: The path of the 3x3 spritesheet Bitmap file :param string selected_bmp_path: The path of the 3x3 spritesheet Bitmap file to use when pressed + :param int or tuple transparent_index: Index(s) that will be made transparent on the Palette """ def __init__( @@ -82,7 +85,6 @@ def __init__( elif isinstance(transparent_index, int): self._selected_bmp_palette.make_transparent(0) - print((width // (self._bmp.width // 3), height // (self._bmp.height // 3))) self._btn_tilegrid = inflate_tilegrid( bmp_obj=self._bmp, bmp_palette=self._bmp_palette, @@ -94,7 +96,6 @@ def __init__( ) self.append(self._btn_tilegrid) - print("setting label") self.label = label @property diff --git a/docs/api.rst b/docs/api.rst index 49c1de1..b604b89 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -6,3 +6,12 @@ .. automodule:: adafruit_button :members: + +.. automodule:: adafruit_button.button_base + :members: + +.. automodule:: adafruit_button.button + :members: + +.. automodule:: adafruit_button.sprite_button + :members: diff --git a/docs/examples.rst b/docs/examples.rst index 3960d39..30c5250 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -33,3 +33,12 @@ A soundboard made with buttons .. literalinclude:: ../examples/display_button_soundboard.py :caption: examples/display_button_soundboard.py :linenos: + +Sprite Button +------------- + +Custom sprite button + +.. literalinclude:: ../examples/display_button_spritebutton_simpletest.py + :caption: examples/display_button_spritebutton_simpletest.py + :linenos: diff --git a/optional_requirements.txt b/optional_requirements.txt index d4e27c4..9c5fce5 100644 --- a/optional_requirements.txt +++ b/optional_requirements.txt @@ -1,3 +1,5 @@ # SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries # # SPDX-License-Identifier: Unlicense + +adafruit-circuitpython-ImageLoad From 95bb72a2f190e9571b36067be419b51a05458a1e Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 29 Jun 2023 20:21:04 -0500 Subject: [PATCH 6/7] format --- adafruit_button/button_base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 41a2286..d33cfc4 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -80,7 +80,6 @@ def label(self): @label.setter def label(self, newtext): - if self._label and self and (self[-1] == self._label): self.pop() From ea1769a83b0b70377569b93573b3d7807da3fa6a Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 29 Jun 2023 20:30:15 -0500 Subject: [PATCH 7/7] raise helpful error if missing bmp_path --- adafruit_button/sprite_button.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 75dc70a..c2f02ba 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -58,6 +58,9 @@ def __init__( selected_bmp_path=None, transparent_index=None ): + if bmp_path is None: + raise ValueError("Please supply bmp_path. It cannot be None.") + super().__init__( x=x, y=y,