cursorcontrol_buttons_text.py
1 import time 2 import board 3 import displayio 4 from adafruit_button import Button 5 from adafruit_cursorcontrol.cursorcontrol import Cursor 6 from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager 7 from adafruit_display_text import label 8 import terminalio 9 10 # Create the display 11 display = board.DISPLAY 12 13 # Create the display context 14 splash = displayio.Group(max_size=22) 15 16 # Use the built-in system font 17 font = terminalio.FONT 18 19 ########################################################################## 20 # Make a background color fill 21 22 color_bitmap = displayio.Bitmap(display.width, display.height, 1) 23 color_palette = displayio.Palette(1) 24 color_palette[0] = 0x404040 25 bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) 26 splash.append(bg_sprite) 27 28 ########################################################################## 29 30 # Set up button/label properties 31 BUTTON_WIDTH = 80 32 BUTTON_HEIGHT = 40 33 BUTTON_MARGIN = 20 34 LBL_HEADER = [100, 20] 35 LBL_TEXT = [120, 40] 36 37 # Resize buttons for small display (PyGamer) 38 if display.width < 240: 39 BUTTON_WIDTH = int(BUTTON_WIDTH / 2) 40 BUTTON_HEIGHT = int(BUTTON_HEIGHT / 2) 41 BUTTON_MARGIN = int(BUTTON_MARGIN / 2) 42 LBL_HEADER[0] -= 75 43 LBL_HEADER[1] -= 10 44 LBL_TEXT[0] -= 70 45 LBL_TEXT[1] += 55 46 47 # Create the buttons 48 buttons = [] 49 50 button_speed_inc = Button( 51 x=BUTTON_MARGIN, 52 y=BUTTON_MARGIN + BUTTON_HEIGHT, 53 width=BUTTON_WIDTH, 54 height=BUTTON_HEIGHT, 55 label="Speed+", 56 label_font=font, 57 ) 58 buttons.append(button_speed_inc) 59 60 button_speed_dec = Button( 61 x=BUTTON_MARGIN, 62 y=BUTTON_MARGIN * 4 + BUTTON_HEIGHT, 63 width=BUTTON_WIDTH, 64 height=BUTTON_HEIGHT, 65 label="Speed-", 66 label_font=font, 67 ) 68 buttons.append(button_speed_dec) 69 70 button_scale_pos = Button( 71 x=BUTTON_MARGIN * 3 + 2 * BUTTON_WIDTH, 72 y=BUTTON_MARGIN + BUTTON_HEIGHT, 73 width=BUTTON_WIDTH, 74 height=BUTTON_HEIGHT, 75 label="Scale+", 76 label_font=font, 77 style=Button.SHADOWRECT, 78 ) 79 buttons.append(button_scale_pos) 80 81 button_scale_neg = Button( 82 x=BUTTON_MARGIN * 3 + 2 * BUTTON_WIDTH, 83 y=BUTTON_MARGIN * 4 + BUTTON_HEIGHT, 84 width=BUTTON_WIDTH, 85 height=BUTTON_HEIGHT, 86 label="Scale-", 87 label_font=font, 88 style=Button.SHADOWRECT, 89 ) 90 buttons.append(button_scale_neg) 91 92 # Show the button 93 for b in buttons: 94 splash.append(b.group) 95 96 # Create a text label 97 text_label = label.Label( 98 font, text="CircuitPython Cursor!", color=0x00FF00, x=LBL_HEADER[0], y=LBL_HEADER[1] 99 ) 100 splash.append(text_label) 101 102 text_speed = label.Label( 103 font, max_glyphs=15, color=0x00FF00, x=LBL_TEXT[0], y=LBL_TEXT[1] 104 ) 105 splash.append(text_speed) 106 107 text_scale = label.Label( 108 font, max_glyphs=15, color=0x00FF00, x=LBL_TEXT[0], y=LBL_TEXT[1] + 20 109 ) 110 splash.append(text_scale) 111 112 # initialize the mouse cursor object 113 mouse_cursor = Cursor(display, display_group=splash) 114 115 # initialize the cursormanager 116 cursor = CursorManager(mouse_cursor) 117 118 # show displayio group 119 display.show(splash) 120 121 prev_btn = None 122 while True: 123 cursor.update() 124 if cursor.is_clicked is True: 125 for i, b in enumerate(buttons): 126 if b.contains((mouse_cursor.x, mouse_cursor.y)): 127 b.selected = True 128 print("Button %d pressed" % i) 129 if i == 0: # Increase the cursor speed 130 mouse_cursor.speed += 1 131 elif i == 1: # Decrease the cursor speed 132 mouse_cursor.speed -= 1 133 if i == 2: # Increase the cursor scale 134 mouse_cursor.scale += 1 135 elif i == 3: # Decrease the cursor scale 136 mouse_cursor.scale -= 1 137 prev_btn = b 138 elif prev_btn is not None: 139 prev_btn.selected = False 140 text_speed.text = "Speed: {0}px".format(mouse_cursor.speed) 141 text_scale.text = "Scale: {0}px".format(mouse_cursor.scale) 142 time.sleep(0.1)