Example #1
0
 def _create_body(self):
     if (self.outline_color is not None) or (self.fill_color is not None):
         if self.style == Button.RECT:
             self.body = Rect(
                 0,
                 0,
                 self.width,
                 self.height,
                 fill=self._fill_color,
                 outline=self._outline_color,
             )
         elif self.style == Button.ROUNDRECT:
             self.body = RoundRect(
                 0,
                 0,
                 self.width,
                 self.height,
                 r=10,
                 fill=self._fill_color,
                 outline=self._outline_color,
             )
         elif self.style == Button.SHADOWRECT:
             self.shadow = Rect(2,
                                2,
                                self.width - 2,
                                self.height - 2,
                                fill=self.outline_color)
             self.body = Rect(
                 0,
                 0,
                 self.width - 2,
                 self.height - 2,
                 fill=self._fill_color,
                 outline=self._outline_color,
             )
         elif self.style == Button.SHADOWROUNDRECT:
             self.shadow = RoundRect(
                 2,
                 2,
                 self.width - 2,
                 self.height - 2,
                 r=10,
                 fill=self._outline_color,
             )
             self.body = RoundRect(
                 0,
                 0,
                 self.width - 2,
                 self.height - 2,
                 r=10,
                 fill=self._fill_color,
                 outline=self._outline_color,
             )
         if self.shadow:
             self.append(self.shadow)
Example #2
0
    def __init__(self, *, x, y, width, height, name=None, style=RECT,
                 fill_color=0xFFFFFF, outline_color=0x0,
                 label=None, label_font=None, label_color=0x0,
                 selected_fill=None, selected_outline=None,
                 selected_label=None, icon=None):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self._font = label_font
        self._selected = False
        self.group = displayio.Group()
        self.name = name
        self._label = label
        self._icon = icon
        self.body = self.fill = self.shadow = None

        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
        if self.selected_outline is None and outline_color is not None:
            self.selected_outline = (~self.outline_color) & 0xFFFFFF

        if outline_color or fill_color:
            if style == Button.RECT:
                self.body = Rect(x, y, width, height,
                                 fill=self.fill_color, outline=self.outline_color)
            elif style == Button.ROUNDRECT:
                self.body = RoundRect(x, y, width, height, r=10,
                                      fill=self.fill_color, outline=self.outline_color)
            elif style == Button.SHADOWRECT:
                self.shadow = Rect(x + 2, y + 2, width - 2, height - 2,
                                   fill=outline_color)
                self.body = Rect(x, y, width - 2, height - 2,
                                 fill=self.fill_color, outline=self.outline_color)
            elif style == Button.SHADOWROUNDRECT:
                self.shadow = RoundRect(x + 2, y + 2, width - 2, height - 2, r=10,
                                        fill=self.outline_color)
                self.body = RoundRect(x, y, width - 2, height - 2, r=10,
                                      fill=self.fill_color, outline=self.outline_color)
            if self.shadow:
                self.group.append(self.shadow)
            self.group.append(self.body)

        self.icon = icon
        self.label = label
Example #3
0
def set_size(widget: Button, native: Native, width: int, height: int) -> None:
    """
    A tie-in to set the size of the native element based on the size hint and fixed style
    :param widget:
    :param width:
    :param height:
    """

    radius: int = min(
        widget.width // 2,
        widget.height // 2,
        _dimspecify(widget._impl_cache_["radius"], widget),
    )
    label_width: int = widget._impl_cache_["label_width"]
    margin = widget._margin_

    assert len(native) == 1, len(native)
    label: Label = native[0]
    label.x = (width - label_width) // 2
    label.y = height // 2

    rect = RoundRect(
        margin,  # label.x - rectpadding // 2,
        margin,
        width - 2 * margin,
        height - 2 * margin,
        radius,
        stroke=0,
    )

    native.insert(0, rect)
Example #4
0
 def __init__(self, screen, xpos, ypos, sizex, sizey, title, func):
     screen.add(self)
     self.xpos = xpos - sizex // 2
     self.ypos = ypos - sizey // 2
     self.sizex = sizex
     self.sizey = sizey
     self.func = func
     self.clicked = False
     self.xBound = sizex + 4
     self.yBound = sizey + 4
     self.rect = RoundRect(self.xpos,
                           self.ypos,
                           sizex,
                           sizey,
                           r=2,
                           outline=Screen.palette[15],
                           fill=Screen.palette[15])
     screen.group.append(self.rect)
     self.label = Label(terminalio.FONT,
                        text=title,
                        max_glyphs=16,
                        color=0x0)
     bounding_box = self.label.bounding_box
     self.label.x = self.xpos + (sizex - bounding_box[2]) // 2
     self.label.y = self.ypos + sizey // 2 - 2
     screen.group.append(self.label)
Example #5
0
 def main_screen(self, fg_color, bg_color):
     # UI
     self.main_group = displayio.Group(max_size=50)
     # background
     bg_bitmap = displayio.Bitmap(160, 128, 2)
     bg_palette = displayio.Palette(1)
     bg_palette[0] = fg_color  # Bright Green
     bg_sprite = displayio.TileGrid(bg_bitmap,
                                    pixel_shader=bg_palette,
                                    x=0,
                                    y=0)
     self.main_group.append(bg_sprite)
     # Main Screen Round rectangle
     roundrect = RoundRect(2,
                           2,
                           156,
                           124,
                           5,
                           fill=bg_color,
                           outline=bg_color,
                           stroke=1)
     self.main_group.append(roundrect)
     # Show display
     self.display.show(self.main_group)
     self.display.auto_refresh = True
     self._speed_labels(0xffbf00)
     self._bme_labels(0xffbf00, 0x454545)
     self._status_bar(0xffbf00, 0x454545)
    def test_shapes(self):

        expected_images = []
        for i in range(5):
            img = Image.open(
                os.path.join(
                    self.abs_path,
                    CONSTANTS.IMG_DIR_NAME,
                    f"test_image_shapes_{i+1}.bmp",
                ))

            img.putalpha(255)
            expected_images.append(img.load())

        # TAKEN FROM ADAFRUIT'S DISPLAY SHAPES LIBRARY
        # https://github.com/ladyada/Adafruit_CircuitPython_Display_Shapes/blob/master/examples/display_shapes_simpletest.py
        splash = displayio.Group(max_size=10)
        splash._Group__show = self.__send_helper
        board.DISPLAY.show(splash)
        color_bitmap = displayio.Bitmap(320, 240, 1)
        color_palette = displayio.Palette(1)
        color_palette[0] = 0xFFFFFF
        bg_sprite = displayio.TileGrid(color_bitmap,
                                       x=0,
                                       y=0,
                                       pixel_shader=color_palette)
        splash.append(bg_sprite)
        helper._Helper__test_image_equality(self.main_img.load(),
                                            expected_images[0])

        rect = Rect(80, 20, 41, 41, fill=0x00FF00)
        splash.append(rect)
        helper._Helper__test_image_equality(self.main_img.load(),
                                            expected_images[1])
        circle = Circle(100, 100, 20, fill=0x00FF00, outline=0xFF00FF)
        splash.append(circle)

        helper._Helper__test_image_equality(self.main_img.load(),
                                            expected_images[2])

        rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
        splash.append(rect2)

        helper._Helper__test_image_equality(self.main_img.load(),
                                            expected_images[3])

        roundrect = RoundRect(10,
                              10,
                              61,
                              81,
                              10,
                              fill=0x0,
                              outline=0xFF00FF,
                              stroke=6)
        splash.append(roundrect)

        helper._Helper__test_image_equality(self.main_img.load(),
                                            expected_images[4])
Example #7
0
def main():
    # Make the display context
    splash = displayio.Group(max_size=20)
    board.DISPLAY.show(splash)

    # Make a background color fill
    color_bitmap = displayio.Bitmap(320, 240, 1)
    color_palette = displayio.Palette(1)
    color_palette[0] = 0xFFFFFF
    bg_sprite = displayio.TileGrid(color_bitmap,
                                   x=0,
                                   y=0,
                                   pixel_shader=color_palette)
    splash.append(bg_sprite)
    ##########################################################################

    splash.append(Line(220, 130, 270, 210, 0xFF0000))
    splash.append(Line(270, 210, 220, 210, 0xFF0000))
    splash.append(Line(220, 210, 270, 130, 0xFF0000))
    splash.append(Line(270, 130, 220, 130, 0xFF0000))

    #Draw a blue star
    polygon = Polygon([(255, 40), (262, 62), (285, 62), (265, 76), (275, 100),
                       (255, 84), (235, 100), (245, 76), (225, 62), (248, 62)],
                      outline=0x0000FF)
    splash.append(polygon)

    triangle = Triangle(170,
                        50,
                        120,
                        140,
                        210,
                        160,
                        fill=0x00FF00,
                        outline=0xFF00FF)
    splash.append(triangle)

    rect = Rect(80, 20, 41, 41, fill=0x0)
    splash.append(rect)

    circle = Circle(100, 100, 20, fill=0x00FF00, outline=0xFF00FF)
    splash.append(circle)

    rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
    splash.append(rect2)

    roundrect = RoundRect(10,
                          10,
                          61,
                          81,
                          10,
                          fill=0x0,
                          outline=0xFF00FF,
                          stroke=6)
    splash.append(roundrect)

    sleep(4)
    running = False
Example #8
0
def draw_sprit_rect(centerx, centery, xlength, ylength):
    rect = RoundRect(centerx - (xlength // 2),
                     centery - (ylength // 2),
                     xlength,
                     ylength,
                     20,
                     fill=0xFFFF44,
                     outline=0x0)
    return rect
Example #9
0
def show(up, down, left, right, robot, line1, line1highlight, line2,
         line2highlight):
    screen = displayio.Group(max_size=25)

    # define the controls
    rect = RoundRect(30, 30, 100, 68, 5, fill=0x0, outline=0xFFFFFF, stroke=1)
    screen.append(rect)

    if up:
        uptriangle = Triangle(80, 3, 44, 24, 116, 24, fill=0xADFF2F)
    else:
        uptriangle = Triangle(80, 3, 44, 24, 116, 24, outline=0x2F4F4F)
    screen.append(uptriangle)

    if down:
        downtriangle = Triangle(80, 125, 44, 103, 116, 103, fill=0xADFF2F)
    else:
        downtriangle = Triangle(80, 125, 44, 103, 116, 103, outline=0x2F4F4F)
    screen.append(downtriangle)

    if left:
        lefttriangle = Triangle(3, 64, 24, 40, 24, 86, fill=0xADFF2F)
    else:
        lefttriangle = Triangle(3, 64, 24, 40, 24, 86, outline=0x2F4F4F)
    screen.append(lefttriangle)

    if right:
        righttriangle = Triangle(157, 64, 134, 40, 134, 86, fill=0xADFF2F)
    else:
        righttriangle = Triangle(157, 64, 134, 40, 134, 86, outline=0x2F4F4F)
    screen.append(righttriangle)

    robot_name_text = label.Label(terminalio.FONT, text=robot, color=0xDC143C)
    robot_name_text.x = 65
    robot_name_text.y = 45
    screen.append(robot_name_text)

    if line1highlight:
        newline1 = "[{0}]".format(line1)
    else:
        newline1 = line1
    mode1_text = label.Label(terminalio.FONT, text=newline1, color=0xDCDCDC)
    mode1_text.x = 60
    mode1_text.y = 66
    screen.append(mode1_text)

    if line2highlight:
        newline2 = "[{0}]".format(line2)
    else:
        newline2 = line2
    mode2_text = label.Label(terminalio.FONT, text=newline2, color=0xC0C0C0)
    mode2_text.x = 65
    mode2_text.y = 83
    screen.append(mode2_text)

    display.show(screen)
Example #10
0
 def _status_bar(self, fill_color, outline_color):
     # Shapes
     frame = RoundRect(3,
                       96,
                       154,
                       28,
                       5,
                       fill=fill_color,
                       outline=outline_color,
                       stroke=1)
     self.main_group.append(frame)
     line = Rect(8, 110, 144, 1, fill=0x0)
     self.main_group.append(line)
     # Free resources
     self.sys_stat_label = label.Label(self.font_mini,
                                       text='Wait for status ...',
                                       color=outline_color)
     self.sys_stat_label.x = 92
     self.sys_stat_label.y = 116
     self.main_group.append(self.sys_stat_label)
     # Coordinates
     self.lat_label = label.Label(self.font_mini,
                                  text='LAT: wait            ',
                                  color=outline_color)
     self.lat_label.x = 7
     self.lat_label.y = 103
     self.main_group.append(self.lat_label)
     self.lon_label = label.Label(self.font_mini,
                                  text='LON: wait            ',
                                  color=outline_color)
     self.lon_label.x = 7
     self.lon_label.y = 115
     self.main_group.append(self.lon_label)
     # Satellites
     self.sat_label = label.Label(self.font_mini,
                                  text='SAT:0',
                                  color=fill_color)
     self.sat_label.x = 122
     self.sat_label.y = 25
     self.main_group.append(self.sat_label)
     # Fix Quality
     self.fq_label = label.Label(self.font_mini,
                                 text='FQU:0',
                                 color=fill_color)
     self.fq_label.x = 122
     self.fq_label.y = 35
     self.main_group.append(self.fq_label)
     # Altitude
     self.alt_label = label.Label(self.font_mini,
                                  text='ALT:0      ',
                                  color=outline_color)
     self.alt_label.x = 92
     self.alt_label.y = 103
     self.main_group.append(self.alt_label)
Example #11
0
 def __init__(self, pin, my_pyportal):
     self.switch = digitalio.DigitalInOut(pin)
     self.switch.direction = digitalio.Direction.OUTPUT
     rect = RoundRect(SWITCHX,
                      SWITCHY,
                      31,
                      60,
                      16,
                      fill=0x0,
                      outline=DISPLAY_COLOR,
                      stroke=3)
     my_pyportal.splash.append(rect)
     self.circle_on = Circle(SWITCHX + 15, SWITCHY + 16, 10, fill=0x0)
     my_pyportal.splash.append(self.circle_on)
     self.circle_off = Circle(SWITCHX + 15,
                              SWITCHY + 42,
                              10,
                              fill=DISPLAY_COLOR)
     my_pyportal.splash.append(self.circle_off)
Example #12
0
 def __init__(self, screen, xpos, ypos):
     screen.add(self)
     self.screen = screen
     self.xpos = xpos
     self.ypos = ypos
     self.val = 0
     self.count1 = 0
     self.count2 = 0
     self.blinkCount = 100
     self.blinkDirection = -2
     self.transparent = False
     self.xBound = 20
     self.yBound = 35
     self.rect = RoundRect(xpos - 6,
                           ypos - 8,
                           13,
                           16,
                           r=2,
                           outline=0xFFFFFF,
                           fill=None)
     screen.group.append(self.rect)
     self.tri1 = Triangle(self.xpos - 5,
                          self.ypos - 11,
                          self.xpos,
                          self.ypos - 16,
                          self.xpos + 6,
                          self.ypos - 11,
                          fill=0xFFFFFF)
     screen.group.append(self.tri1)
     self.tri2 = Triangle(self.xpos - 5,
                          self.ypos + 11,
                          self.xpos,
                          self.ypos + 16,
                          self.xpos + 6,
                          self.ypos + 10,
                          fill=0xFFFFFF)
     screen.group.append(self.tri2)
     self.label = Label(terminalio.FONT, text=self.val, max_glyphs=1)
     screen.group.append(self.label)
     bounding_box = self.label.bounding_box
     self.label.x = xpos - bounding_box[2] // 2 + 1
     self.label.y = ypos - 1
Example #13
0
    def __init__(self, setCurrent=False):
        Screen.palette = displayio.Palette(16)
        for i in range(16):
            Screen.palette[i] = i * 1118481
        self.group = displayio.Group(max_size=24)
        self.selector = RoundRect(0,
                                  0,
                                  20,
                                  20,
                                  r=2,
                                  outline=0xFFFFFF,
                                  fill=None)
        self.group.append(self.selector)
        if setCurrent:
            Screen.current = self
        self.components = []
        self.selectable = []

        self.index = 0
        self.selecting = True
Example #14
0
 def _status_bar(self, fill_color, outline_color):
     # Shapes
     frame = RoundRect(3,
                       96,
                       154,
                       28,
                       5,
                       fill=fill_color,
                       outline=outline_color,
                       stroke=1)
     self.main_group.append(frame)
     line = Rect(8, 110, 144, 1, fill=0x0)
     self.main_group.append(line)
     #Free resources
     sys_stat_text = 'System Status check ... '
     self.sys_stat_label = label.Label(self.font_mini,
                                       text=sys_stat_text,
                                       color=outline_color)
     self.sys_stat_label.x = 7
     self.sys_stat_label.y = 116
     self.main_group.append(self.sys_stat_label)
        (248, 62),
    ],
    outline=0x0000FF,
)
splash.append(polygon)

triangle = Triangle(170,
                    50,
                    120,
                    140,
                    210,
                    160,
                    fill=0x00FF00,
                    outline=0xFF00FF)
splash.append(triangle)

rect = Rect(80, 20, 41, 41, fill=0x0)
splash.append(rect)

circle = Circle(100, 100, 20, fill=0x00FF00, outline=0xFF00FF)
splash.append(circle)

rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
splash.append(rect2)

roundrect = RoundRect(10, 10, 61, 81, 10, fill=0x0, outline=0xFF00FF, stroke=6)
splash.append(roundrect)

while True:
    pass
Example #16
0
    def __init__(self,
                 *,
                 x,
                 y,
                 width,
                 height,
                 name=None,
                 style=RECT,
                 fill_color=0xFFFFFF,
                 outline_color=0x0,
                 label=None,
                 label_font=None,
                 label_color=0x0,
                 selected_fill=None,
                 selected_outline=None,
                 selected_label=None):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self._font = label_font
        self._selected = False
        self.group = displayio.Group()
        self.name = name

        self.fill_color = fill_color
        self.outline_color = outline_color
        self.label_color = label_color
        # Selecting inverts the button colors!
        self.selected_fill = selected_fill
        self.selected_outline = selected_outline
        self.selected_label = selected_label

        if self.selected_fill is None and fill_color is not None:
            self.selected_fill = (~fill_color) & 0xFFFFFF
        if self.selected_outline is None and outline_color is not None:
            self.selected_outline = (~outline_color) & 0xFFFFFF

        if outline_color or fill_color:
            self.body = self.shadow = None
            if style == RECT:
                self.body = Rect(x,
                                 y,
                                 width,
                                 height,
                                 fill=fill_color,
                                 outline=outline_color)
            elif style == ROUNDRECT:
                self.body = RoundRect(x,
                                      y,
                                      width,
                                      height,
                                      r=10,
                                      fill=fill_color,
                                      outline=outline_color)
            elif style == SHADOWRECT:
                self.shadow = Rect(x + 2,
                                   y + 2,
                                   width - 2,
                                   height - 2,
                                   fill=outline_color)
                self.body = Rect(x,
                                 y,
                                 width - 2,
                                 height - 2,
                                 fill=fill_color,
                                 outline=outline_color)
            elif style == SHADOWROUNDRECT:
                self.shadow = RoundRect(x + 2,
                                        y + 2,
                                        width - 2,
                                        height - 2,
                                        r=10,
                                        fill=outline_color)
                self.body = RoundRect(x,
                                      y,
                                      width - 2,
                                      height - 2,
                                      r=10,
                                      fill=fill_color,
                                      outline=outline_color)
            if self.shadow:
                self.group.append(self.shadow)
            self.group.append(self.body)

        if label and (label_color is not None):  # button with text label
            if not label_font:
                raise RuntimeError("Please provide label font")
            dims = label_font.text_bounding_box(label)
            if dims[2] >= width or dims[3] >= height:
                raise RuntimeError("Button not large enough for label")
            self.label = TextArea(label_font, text=label)
            self.label.x = x + (width - dims[2]) // 2
            self.label.y = y + (height - dims[3])
            self.label.color = label_color
            self.group.append(self.label)

            if self.selected_label is None and label_color is not None:
                self.selected_label = (~label_color) & 0xFFFFFF
            #print(dims)
        """
Example #17
0
# simpleio.tone(buzzer_pin, TONE_FREQ[0],duration=0.3)
# simpleio.tone(buzzer_pin, TONE_FREQ[1],duration=0.3)
# simpleio.tone(buzzer_pin, TONE_FREQ[2],duration=0.3)
# simpleio.tone(buzzer_pin, TONE_FREQ[3],duration=0.3)

from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.roundrect import RoundRect
from adafruit_display_shapes.triangle import Triangle

my_display_group = displayio.Group(max_size=25)
display.show(my_display_group)

WHITE = 0xFFFFFF

roundrect = RoundRect(50, 100, 40, 80, 10, fill=WHITE, stroke=3)
my_display_group.append(roundrect)

label = Label(
    FONT,
    text=
    "My Label Text asd lkasjd alksjd laksdj lksa \njdlksajdlsakjd lkas jdlksa jdalsk \njdalskjd alsjdalskdj lkjsad lakjsd hello",
    x=50,
    y=20,
    color=0xFFFFFF)
my_display_group.append(label)

while True:

    time.sleep(0.1)
Example #18
0
                    140,
                    90,
                    210,
                    100,
                    fill=0x999999,
                    outline=0x000000)
splash.append(triangle)

rect = Rect(80, 20, 41, 41, fill=0x999999, outline=0x666666)
splash.append(rect)

circle = Circle(100, 100, 20, fill=0xFFFFFF, outline=0x000000)
splash.append(circle)

rect2 = Rect(70, 85, 61, 30, outline=0x0, stroke=3)
splash.append(rect2)

roundrect = RoundRect(10,
                      10,
                      61,
                      51,
                      10,
                      fill=0x666666,
                      outline=0x000000,
                      stroke=6)
splash.append(roundrect)

display.refresh()
while True:
    pass
Example #19
0
#color_palette[0] = 0x000000
#bg_sprite = displayio.TileGrid(color_bitmap, x=50, y=50,
#                              pixel_shader=color_palette)
#splash.append(bg_sprite)
##########################################################################
#customwait(2)
# add my sprite


def customwait(wait_time):
    start = time.monotonic()
    while time.monotonic() < (start + wait_time):
        pass


roundrect = RoundRect(40, 40, 90, 30, 10, fill=0x0, outline=0xAFAF00, stroke=6)
splash.append(roundrect)
splash.append(text_area)
# insert play startup sound here ######
customwait(1)

# Here are my screens to move through
song = displayio.Group(max_size=64)
mixgrid = displayio.Group(max_size=64)
instrument_screen = displayio.Group(max_size=64)
settings = displayio.Group(max_size=64)

# Song screen
lbl_song = label.Label(font, text='Song', color=0xff9F00, x=10, y=10)
song.append(lbl_song)
    def __init__(self, display, layout_json):
        self.json = layout_json
        if "view_type" not in layout_json:
            raise MissingTypeError
        if layout_json["view_type"] != "RoundRect":
            raise IncorrectTypeError(
                "view_type '{}' does not match Layout Class 'RoundRect'".
                format(layout_json["view_type"]))
        self._display = display
        if "attributes" in layout_json:
            _missing_attrs = []
            for attribute in REQUIRED_ATTRIBUTES:
                if attribute not in layout_json['attributes']:
                    _missing_attrs.append(attribute)
            if len(_missing_attrs) > 0:
                raise MissingRequiredAttributesError(
                    "Missing required attributes: {}".format(_missing_attrs))

            _outline = 0xFFFFFF
            if "outline" in layout_json["attributes"]:
                _outline = int(layout_json["attributes"]["outline"], 16)

            _fill = 0x000000
            if "fill" in layout_json["attributes"]:
                _fill = int(layout_json["attributes"]["fill"], 16)

            _width = 0
            if "width" in layout_json["attributes"]:
                _width = self.keyword_compiler(
                    layout_json["attributes"]["width"])
            self.width = 0

            _height = 0
            if "height" in layout_json["attributes"]:
                _height = self.keyword_compiler(
                    layout_json["attributes"]["height"])
            self.height = 0

            _stroke = 0
            if "stroke" in layout_json["attributes"]:
                _stroke = self.keyword_compiler(
                    layout_json["attributes"]["stroke"])

            _r = 0
            if "radius" in layout_json["attributes"]:
                _r = self.keyword_compiler(layout_json["attributes"]["radius"],
                                           {
                                               "WIDTH": _width,
                                               "HEIGHT": _height
                                           })

            _x = 0
            if "x" in layout_json["attributes"]:
                _x = self.keyword_compiler(layout_json["attributes"]["x"], {
                    "WIDTH": _width,
                    "HEIGHT": _height
                })

            _y = 0
            if "y" in layout_json["attributes"]:
                _y = self.keyword_compiler(layout_json["attributes"]["y"], {
                    "WIDTH": _width,
                    "HEIGHT": _height
                })

            self.roundrect = RoundRect(_x,
                                       _y,
                                       _width,
                                       _height,
                                       _r,
                                       fill=_fill,
                                       outline=_outline,
                                       stroke=_stroke)
            self.view = self.roundrect
        else:
            raise MissingAttributesError()
Example #21
0
font_spd = bitmap_font.load_font("fonts/dseg-48.bdf")

# UI
print(const("Loading fonts..."))
main_group = displayio.Group(max_size=50)
#background
bg_bitmap = displayio.Bitmap(160, 128, 2)
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xffbf00  # Bright Green
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
main_group.append(bg_sprite)
# Main Screen Round rectangle
roundrect = RoundRect(2,
                      2,
                      156,
                      124,
                      5,
                      fill=0x454545,
                      outline=0x454545,
                      stroke=2)
main_group.append(roundrect)
# Status Bar Down
status_text = "              "
status = label.Label(font10, text=status_text, color=0xffffff)
status.x = 120
status.y = 25
main_group.append(status)
# Status Bar Time
caption_text = "                           "
caption = label.Label(font10, text=caption_text, color=0xffffff)
caption.x = 78
caption.y = 119
Example #22
0
 def _bme_labels(self, fill_color, outline_color):
     # Shapes
     frame_1 = RoundRect(3,
                         3,
                         44,
                         30,
                         5,
                         fill=fill_color,
                         outline=outline_color,
                         stroke=1)
     self.main_group.append(frame_1)
     frame_2 = RoundRect(3,
                         34,
                         44,
                         30,
                         5,
                         fill=fill_color,
                         outline=outline_color,
                         stroke=1)
     self.main_group.append(frame_2)
     frame_3 = RoundRect(3,
                         65,
                         44,
                         30,
                         5,
                         fill=fill_color,
                         outline=outline_color,
                         stroke=1)
     self.main_group.append(frame_3)
     # Temperature Label
     temp_label_text = "TEMP"
     temp_label = label.Label(self.font_mini,
                              text=temp_label_text,
                              color=outline_color)
     temp_label.x = 12
     temp_label.y = 8
     self.main_group.append(temp_label)
     # Humidity Label
     humi_label_text = "HUMI"
     humi_label = label.Label(self.font_mini,
                              text=humi_label_text,
                              color=outline_color)
     humi_label.x = 12
     humi_label.y = 39
     self.main_group.append(humi_label)
     # Pressure label
     press_label_text = "P.mmHg"
     press_label = label.Label(self.font_mini,
                               text=press_label_text,
                               color=outline_color)
     press_label.x = 6
     press_label.y = 71
     self.main_group.append(press_label)
     # Values Labels
     # Temp Value
     temp_value_text = '00' + chr(0176)
     self.temp_value = label.Label(self.font_middle,
                                   text=temp_value_text,
                                   color=outline_color)
     self.temp_value.x = 5
     self.temp_value.y = 24
     self.temp_value.scale = 1
     self.main_group.append(self.temp_value)
     # Humidity Value
     humi_text = "00" + chr(0x25)
     self.humi_value = label.Label(self.font_middle,
                                   text=humi_text,
                                   color=outline_color)
     self.humi_value.x = 5
     self.humi_value.y = 55
     self.main_group.append(self.humi_value)
     # Pressure Value
     press_value_text = "000"
     self.press_value = label.Label(self.font_middle,
                                    text=press_value_text,
                                    color=outline_color)
     self.press_value.x = 5
     self.press_value.y = 86
     self.press_value.scale = 1
     self.main_group.append(self.press_value)
    def __init__(self,
                 *,
                 x,
                 y,
                 width,
                 height,
                 name=None,
                 style=RECT,
                 fill_color=0xFFFFFF,
                 outline_color=0x0,
                 label=None,
                 label_font=None,
                 label_color=0x0,
                 selected_fill=None,
                 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
        self.body = self.fill = self.shadow = None

        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
        if self.selected_outline is None and outline_color is not None:
            self.selected_outline = (~self._outline_color) & 0xFFFFFF

        if (outline_color is not None) or (fill_color is not None):
            if style == Button.RECT:
                self.body = Rect(
                    0,
                    0,
                    width,
                    height,
                    fill=self._fill_color,
                    outline=self._outline_color,
                )
            elif style == Button.ROUNDRECT:
                self.body = RoundRect(
                    0,
                    0,
                    width,
                    height,
                    r=10,
                    fill=self._fill_color,
                    outline=self._outline_color,
                )
            elif style == Button.SHADOWRECT:
                self.shadow = Rect(2,
                                   2,
                                   width - 2,
                                   height - 2,
                                   fill=outline_color)
                self.body = Rect(
                    0,
                    0,
                    width - 2,
                    height - 2,
                    fill=self._fill_color,
                    outline=self._outline_color,
                )
            elif style == Button.SHADOWROUNDRECT:
                self.shadow = RoundRect(2,
                                        2,
                                        width - 2,
                                        height - 2,
                                        r=10,
                                        fill=self._outline_color)
                self.body = RoundRect(
                    0,
                    0,
                    width - 2,
                    height - 2,
                    r=10,
                    fill=self._fill_color,
                    outline=self._outline_color,
                )
            if self.shadow:
                self.append(self.shadow)
            self.append(self.body)

        self.label = label
Example #24
0
    def _create_switch(self):
        # The main function that creates the switch display elements

        switch_x = self._radius
        switch_y = self._radius

        # Define the motion "keyframes" that define the switch movement
        if self._horizontal:  # horizontal switch orientation
            self._x_motion = self._width - 2 * self._radius - 1
            self._y_motion = 0

        else:  # vertical orientation
            self._x_motion = 0
            self._y_motion = self._width - 2 * self._radius - 1

        self._angle_motion = 0

        if self._flip:
            self._x_motion = -1 * self._x_motion
            self._y_motion = -1 * self._y_motion
            self._angle_motion = -1 * self._angle_motion

        # Initialize the display elements - These should depend upon the
        # orientation (`horizontal` and `flip`)
        #
        # Initialize the Circle

        circle_x0 = switch_x
        circle_y0 = switch_y

        if self._flip:
            circle_x0 = circle_x0 - self._x_motion
            circle_y0 = circle_y0 - self._y_motion

        self._switch_circle = Circle(
            x0=circle_x0,
            y0=circle_y0,
            r=self._radius,
            fill=self._fill_color_off,
            outline=self._outline_color_off,
            stroke=self._switch_stroke,
        )

        # Initialize the RoundRect for the background
        if self._horizontal:  # Horizontal orientation
            self._switch_roundrect = RoundRect(
                x=switch_x - self._radius,
                y=switch_y - self._radius,
                r=self._radius,
                width=self._width,
                height=2 * self._radius + 1,
                fill=self._background_color_off,
                outline=self._background_outline_color_off,
                stroke=self._switch_stroke,
            )
        else:  # Vertical orientation
            self._switch_roundrect = RoundRect(
                x=switch_x - self._radius,
                y=switch_y - self._radius,
                r=self._radius,
                width=2 * self._radius + 1,
                height=self._width,
                fill=self._background_color_off,
                outline=self._background_outline_color_off,
                stroke=self._switch_stroke,
            )

        # The "0" text circle shape
        self._text_0 = Circle(
            x0=circle_x0,
            y0=circle_y0,
            r=self._radius // 2,
            fill=self._fill_color_off,
            outline=self._outline_color_off,
            stroke=self._text_stroke,
        )

        # The "1" text rectangle shape
        text1_x_offset = (-1 * self._switch_stroke) + 1
        text1_y_offset = -self._radius // 2

        self._text_1 = Rect(
            x=circle_x0 + text1_x_offset,
            y=circle_y0 + text1_y_offset,
            height=self._radius,
            width=self._text_stroke,
            fill=self._fill_color_off,
            outline=self._outline_color_off,
            stroke=self._text_stroke,
        )

        # bounding_box defines the "local" x and y.
        # Must be offset by self.x and self.y to get the raw display coordinates
        #
        if self._horizontal:  # Horizontal orientation
            self._bounding_box = [
                0,
                0,
                self._width,
                2 * self._radius + 1,
            ]
        else:  # Vertical orientation
            self._bounding_box = [
                0,
                0,
                2 * self._radius + 1,
                self._width,
            ]

        self.touch_boundary = [
            self._bounding_box[0] - self._touch_padding,
            self._bounding_box[1] - self._touch_padding,
            self._bounding_box[2] + 2 * self._touch_padding,
            self._bounding_box[3] + 2 * self._touch_padding,
        ]

        # Store initial positions of moving elements to be used in _draw_function
        self._switch_initial_x = self._switch_circle.x
        self._switch_initial_y = self._switch_circle.y

        self._text_0_initial_x = self._text_0.x
        self._text_0_initial_y = self._text_0.y
        self._text_1_initial_x = self._text_1.x
        self._text_1_initial_y = self._text_1.y

        # Set the initial switch position based on the starting value
        if self._value:
            self._draw_position(1)
        else:
            self._draw_position(0)

        # pop any items off the current self group, in case this is updating
        # an existing switch
        for _ in range(len(self)):
            self.pop()

        # Add the display elements to the self group
        self.append(self._switch_roundrect)
        self.append(self._switch_circle)

        # If display_button_text is True, append the correct text element (0 or 1)
        if self._display_button_text:
            self.append(self._text_0)
            self.append(self._text_1)
            if self._value:
                self._text_0.hidden = True
                self._text_1.hidden = False
            else:
                self._text_0.hidden = False
                self._text_1.hidden = True

        # update the anchor position, if required
        # this calls the parent Widget class to update the anchored_position
        # due to any changes that might have occurred in the bounding_box
        self._update_position()
    def __init__(self, *, x, y, width, height, name=None, style=RECT,
                 fill_color=0xFFFFFF, outline_color=0x0,
                 label=None, label_font=None, label_color=0x0,
                 label_x=-1, label_y=-1, id=-1,                 # PaddedButton
                 selected_fill=None, selected_outline=None,
                 selected_label=None, margin=None, padding=None): # PaddedButton

        # PaddedButton
        # Define padding around the button boundaries to constrain
        # calculation for the contains() method
        if padding is not None:
            self._padding = padding
        else:
            self._padding = (0, 0)  # (x,y) respectively
            
        # Margin provides space around the outside of the button
        self._margin = margin
        
        # PaddedButton
        if margin is not None:
            self.x = x + margin[0]
            self.y = y + margin[1]
            self.width = width - (2 * self.margin[0])
            self.height = height - (2 * self.margin[1])
        else: 
            self.x = x
            self.y = y
            self.width = width
            self.height = height

        self._font = label_font
        self._selected = False
        self.group = displayio.Group()
        self.name = name
        self._label = label
        self._id = id         # PaddedButton
        self.body = self.fill = self.shadow = None

        self.fill_color = _check_color(fill_color)
        self.outline_color = _check_color(outline_color)
        self._label_color = label_color
        self._label_font = label_font
        self._label_x = label_x         # PaddedButton
        self._label_y = label_y         # PaddedButton

        # 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
        if self.selected_outline is None and outline_color is not None:
            self.selected_outline = (~self.outline_color) & 0xFFFFFF

        # print("id: {} selected_fill: {}".format(self._id,self.selected_fill))

        if (outline_color is not None) or (fill_color is not None):
            if style == PaddedButton.RECT:
                self.body = Rect(self.x, self.y, width, height,
                                    fill=self.fill_color, outline=self.outline_color)
            elif style == PaddedButton.ROUNDRECT:
                self.body = RoundRect(self.x, self.y, self.width, self.height, r=10,
                                        fill=self.fill_color, outline=self.outline_color)
            elif style == PaddedButton.SHADOWRECT:
                self.shadow = Rect(x + 2, y + 2, width - 2, height - 2,
                                    fill=outline_color)
                self.body = Rect(x, y, width - 2, height - 2,
                                    fill=self.fill_color, outline=self.outline_color)
            elif style == PaddedButton.SHADOWROUNDRECT:
                self.shadow = RoundRect(x + 2, y + 2, width - 2, height - 2, r=10,
                                        fill=self.outline_color)
                self.body = RoundRect(x, y, width - 2, height - 2, r=10,
                                        fill=self.fill_color, outline=self.outline_color)
            if self.shadow:
                self.group.append(self.shadow)

            self.group.append(self.body)

        self.label = label
BLACK = 0x000000

# This will control how the initial shape is created.
# fill = True  -> fill=WHITE
# fill = False -> fill=None
fill = False

# Initialize PyPortal with black background
pyportal = PyPortal(default_bg=BLACK)

# Add a square
if fill is True:
    roundrect = RoundRect(100,
                          60,
                          120,
                          120,
                          10,
                          fill=WHITE,
                          outline=WHITE,
                          stroke=5)
else:
    roundrect = RoundRect(100,
                          60,
                          120,
                          120,
                          10,
                          fill=None,
                          outline=WHITE,
                          stroke=5)

pyportal.splash.append(roundrect)
time.sleep(2.0)