2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121# THE SOFTWARE.
2222"""
23- `adafruit_display_button `
23+ `adafruit_button `
2424================================================================================
2525
2626UI Buttons for displayio
3838
3939"""
4040
41+ from micropython import const
4142import displayio
42- from adafruit_display_text .text_area import TextArea
43- from adafruit_bitmap_font import bitmap_font
43+ from adafruit_display_text .label import Label
4444from adafruit_display_shapes .rect import Rect
4545from adafruit_display_shapes .roundrect import RoundRect
4646
4747__version__ = "0.0.0-auto.0"
4848__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
4949
5050
51+ def _check_color (color ):
52+ # if a tuple is supplied, convert it to a RGB number
53+ if isinstance (color , tuple ):
54+ r , g , b = color
55+ return int ((r << 16 ) + (g << 8 ) + (b & 0xff ))
56+ return color
57+
58+
5159class Button ():
60+ # pylint: disable=too-many-instance-attributes, too-many-locals
61+ """Helper class for creating UI buttons for ``displayio``.
62+
63+ :param x: The x position of the button.
64+ :param y: The y position of the button.
65+ :param width: The width of the button in pixels.
66+ :param height: The height of the button in pixels.
67+ :param name: The name of the button.
68+ :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
69+ Defaults to RECT.
70+ :param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
71+ :param outline_color: The color of the outline of the button.
72+ :param label: The text that appears inside the button. Defaults to not displaying the label.
73+ :param label_font: The button label font.
74+ :param label_color: The color of the button label text. Defaults to 0x0.
75+ :param selected_fill: Inverts the fill color.
76+ :param selected_outline: Inverts the outline color.
77+ :param selected_label: Inverts the label color.
78+
79+ """
5280 RECT = const (0 )
5381 ROUNDRECT = const (1 )
5482 SHADOWRECT = const (2 )
5583 SHADOWROUNDRECT = const (3 )
84+
5685 def __init__ (self , * , x , y , width , height , name = None , style = RECT ,
5786 fill_color = 0xFFFFFF , outline_color = 0x0 ,
5887 label = None , label_font = None , label_color = 0x0 ,
@@ -66,66 +95,66 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
6695 self ._selected = False
6796 self .group = displayio .Group ()
6897 self .name = name
98+ self .label = label
6999
70- self .fill_color = fill_color
71- self .outline_color = outline_color
100+ self .fill_color = _check_color ( fill_color )
101+ self .outline_color = _check_color ( outline_color )
72102 self .label_color = label_color
73103 # Selecting inverts the button colors!
74- self .selected_fill = selected_fill
75- self .selected_outline = selected_outline
76- self .selected_label = selected_label
104+ self .selected_fill = _check_color ( selected_fill )
105+ self .selected_outline = _check_color ( selected_outline )
106+ self .selected_label = _check_color ( selected_label )
77107
78108 if self .selected_fill is None and fill_color is not None :
79- self .selected_fill = (~ fill_color ) & 0xFFFFFF
109+ self .selected_fill = (~ self . fill_color ) & 0xFFFFFF
80110 if self .selected_outline is None and outline_color is not None :
81- self .selected_outline = (~ outline_color ) & 0xFFFFFF
111+ self .selected_outline = (~ self . outline_color ) & 0xFFFFFF
82112
83113 if outline_color or fill_color :
84114 self .body = self .shadow = None
85- if style == RECT :
115+ if style == Button . RECT :
86116 self .body = Rect (x , y , width , height ,
87- fill = fill_color , outline = outline_color )
88- elif style == ROUNDRECT :
117+ fill = self . fill_color , outline = self . outline_color )
118+ elif style == Button . ROUNDRECT :
89119 self .body = RoundRect (x , y , width , height , r = 10 ,
90- fill = fill_color , outline = outline_color )
91- elif style == SHADOWRECT :
92- self .shadow = Rect (x + 2 , y + 2 , width - 2 , height - 2 ,
93- fill = outline_color )
94- self .body = Rect (x , y , width - 2 , height - 2 ,
95- fill = fill_color , outline = outline_color )
96- elif style == SHADOWROUNDRECT :
97- self .shadow = RoundRect (x + 2 , y + 2 , width - 2 , height - 2 , r = 10 ,
120+ fill = self .fill_color , outline = self .outline_color )
121+ elif style == Button .SHADOWRECT :
122+ self .shadow = Rect (x + 2 , y + 2 , width - 2 , height - 2 ,
98123 fill = outline_color )
99- self .body = RoundRect (x , y , width - 2 , height - 2 , r = 10 ,
100- fill = fill_color , outline = outline_color )
124+ self .body = Rect (x , y , width - 2 , height - 2 ,
125+ fill = self .fill_color , outline = self .outline_color )
126+ elif style == Button .SHADOWROUNDRECT :
127+ self .shadow = RoundRect (x + 2 , y + 2 , width - 2 , height - 2 , r = 10 ,
128+ fill = self .outline_color )
129+ self .body = RoundRect (x , y , width - 2 , height - 2 , r = 10 ,
130+ fill = self .fill_color , outline = self .outline_color )
101131 if self .shadow :
102132 self .group .append (self .shadow )
103133 self .group .append (self .body )
104134
105- if label and (label_color is not None ): # button with text label
135+ if label and (label_color is not None ): # button with text label
106136 if not label_font :
107137 raise RuntimeError ("Please provide label font" )
108138 dims = label_font .text_bounding_box (label )
109139 if dims [2 ] >= width or dims [3 ] >= height :
110140 raise RuntimeError ("Button not large enough for label" )
111- self .label = TextArea (label_font , text = label )
112- self .label .x = x + (width - dims [2 ])// 2
141+ self .label = Label (label_font , text = label )
142+ self .label .x = x + (width - dims [2 ]) // 2
113143 self .label .y = y + (height - dims [3 ])
114144 self .label .color = label_color
115145 self .group .append (self .label )
116146
117147 if self .selected_label is None and label_color is not None :
118148 self .selected_label = (~ label_color ) & 0xFFFFFF
119- #print(dims)
149+ # print(dims)
120150
121- """
122- #else: # ok just a bounding box
123- #self.bodyshape = displayio.Shape(width, height)
124- #self.group.append(self.bodyshape)
125- """
151+ # else: # ok just a bounding box
152+ # self.bodyshape = displayio.Shape(width, height)
153+ # self.group.append(self.bodyshape)
126154
127155 @property
128156 def selected (self ):
157+ """Selected inverts the colors."""
129158 return self ._selected
130159
131160 @selected .setter
@@ -142,4 +171,9 @@ def selected(self, value):
142171 self .label .color = self .label_color
143172
144173 def contains (self , point ):
145- return (self .x <= point [0 ] <= self .x + self .width ) and (self .y <= point [1 ] <= self .y + self .height )
174+ """Used to determine if a point is contained within a button. For example,
175+ ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
176+ determining that a button has been touched.
177+ """
178+ return (self .x <= point [0 ] <= self .x + self .width ) and (self .y <= point [1 ] <=
179+ self .y + self .height )
0 commit comments