def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ global wait_for_interrupt, button_pushed ugfx.set_default_font("Roboto_Regular12") window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, ugfxBLACK) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) window.text(5, 30, text, ugfx.BLACK) list_y = 50 else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 180 - list_y, parent = window) for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 140 if none_text else ugfx.width() - 25, 30 , select_text, parent=window) button_none = ugfx.Button(ugfx.width() - 160, ugfx.height() - 50, 140, 30 , none_text, parent=window) if none_text else None try: ugfx.input_init() wait_for_interrupt = True while wait_for_interrupt: if button_pushed == "A": return options[options_list.selected_index()] if button_pushed == "B": return None if button_none and button_pushed == "START": return None time.sleep(0.2) finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def screen(state): window = ugfx.Container(0, 0, 240, 320) window.show() top_left_logo() ugfx.set_default_font(ugfx.FONT_TITLE) l1 = ugfx.Label(5, 90, 230, 50, "You've swiped everybody!", parent=window, justification=ugfx.Label.CENTERTOP) ugfx.set_default_font(ugfx.FONT_SMALL) l2 = ugfx.Label(5, 160, 230, 20, "Soz " + state["profile"]["username"], parent=window, justification=ugfx.Label.CENTERTOP) l3 = ugfx.Label(5, 180, 230, 20, "Come back later ;)", parent=window, justification=ugfx.Label.CENTERTOP) b2 = ugfx.Button(0, 280, 120, 40, "< Edit profile", parent=window, shape=ugfx.Button.RECT, style=None) b1 = ugfx.Button(120, 280, 120, 40, "Try again >", parent=window, shape=ugfx.Button.RECT, style=None) state['ui'].append(window) state['ui'].append(l1) state['ui'].append(l2) state['ui'].append(l3) state['ui'].append(b1) state['ui'].append(b2)
def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(ugfx.FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) window.text(5, 30, text, ugfx.BLACK) list_y = 50 else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 180 - list_y, parent = window) for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 140 if none_text else ugfx.width() - 25, 30 , select_text, parent=window) button_none = ugfx.Button(ugfx.width() - 160, ugfx.height() - 50, 140, 30 , none_text, parent=window) if none_text else None try: buttons.init() while True: pyb.wfi() ugfx.poll() if buttons.is_triggered("BTN_A"): return options[options_list.selected_index()] if button_none and buttons.is_triggered("BTN_B"): return None if button_none and buttons.is_triggered("BTN_MENU"): return None finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def prompt_text(description, init_text="", true_text="OK", false_text="Back", font=FONT_MEDIUM_BOLD, style=default_style_badge): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B """ window = ugfx.Container(0, 0, ugfx.width(), ugfx.height()) if false_text: true_text = "A: " + true_text false_text = "B: " + false_text ugfx.set_default_font(FONT_MEDIUM_BOLD) kb = ugfx.Keyboard(0, ugfx.height()//2, ugfx.width(), ugfx.height()//2, parent=window) edit = ugfx.Textbox(2, ugfx.height()//2-60, ugfx.width()-7, 25, text = init_text, parent=window) ugfx.set_default_font(FONT_SMALL) button_yes = ugfx.Button(2, ugfx.height()//2-30, ugfx.width()//2-6, 25 , true_text, parent=window) button_no = ugfx.Button(ugfx.width()//2+2, ugfx.height()//2-30, ugfx.width()//2-6, 25 , false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(ugfx.width()//10, ugfx.height()//10, ugfx.width()*4//5, ugfx.height()*2//5-90, description, parent=window) try: window.show() # edit.set_focus() todo: do we need this? while True: sleep.wfi() ugfx.poll() if buttons.is_triggered(buttons.Buttons.BTN_A): return edit.text() if buttons.is_triggered(buttons.Buttons.BTN_B): return None if buttons.is_triggered(buttons.Buttons.BTN_Menu): return edit.text() if buttons.is_triggered(buttons.Buttons.BTN_0): edit.text(edit.text() + "0") if buttons.is_triggered(buttons.Buttons.BTN_1): edit.text(edit.text() + "1") if buttons.is_triggered(buttons.Buttons.BTN_2): edit.text(edit.text() + "2") if buttons.is_triggered(buttons.Buttons.BTN_3): edit.text(edit.text() + "3") if buttons.is_triggered(buttons.Buttons.BTN_4): edit.text(edit.text() + "4") if buttons.is_triggered(buttons.Buttons.BTN_5): edit.text(edit.text() + "5") if buttons.is_triggered(buttons.Buttons.BTN_6): edit.text(edit.text() + "6") if buttons.is_triggered(buttons.Buttons.BTN_7): edit.text(edit.text() + "7") if buttons.is_triggered(buttons.Buttons.BTN_8): edit.text(edit.text() + "8") if buttons.is_triggered(buttons.Buttons.BTN_9): edit.text(edit.text() + "9") if buttons.is_triggered(buttons.Buttons.BTN_Hash): edit.text(edit.text() + "#") if buttons.is_triggered(buttons.Buttons.BTN_Star): edit.text(edit.text() + "*") finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy(); return
def prompt_text(description, init_text = "", true_text="OK", false_text="Back", width = 300, height = 200, font=ugfx.FONT_MEDIUM_BOLD, style=default_style_badge): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B """ window = ugfx.Container(int((ugfx.width()-width)/2), int((ugfx.height()-height)/2), width, height, style=style) if false_text: true_text = "M: " + true_text false_text = "B: " + false_text if buttons.has_interrupt("BTN_MENU"): buttons.disable_interrupt("BTN_MENU") ugfx.set_default_font(ugfx.FONT_MEDIUM) kb = ugfx.Keyboard(0, int(height/2), width, int(height/2), parent=window) edit = ugfx.Textbox(5, int(height/2)-30, int(width*4/5)-10, 25, text = init_text, parent=window) ugfx.set_default_font(ugfx.FONT_SMALL) button_yes = ugfx.Button(int(width*4/5), int(height/2)-30, int(width*1/5)-3, 25 , true_text, parent=window) button_no = ugfx.Button(int(width*4/5), int(height/2)-30-30, int(width/5)-3, 25 , false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(int(width/10), int(height/10), int(width*4/5), int(height*2/5)-60, description, parent=window) try: buttons.init() button_yes.attach_input(ugfx.BTN_MENU,0) if button_no: button_no.attach_input(ugfx.BTN_B,0) window.show() edit.set_focus() while True: pyb.wfi() ugfx.poll() #if buttons.is_triggered("BTN_A"): return edit.text() if buttons.is_triggered("BTN_B"): return None if buttons.is_triggered("BTN_MENU"): return edit.text() finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy(); return
def display_person(person): top_left_logo() # try: # resp = http.get("https://twitter.com/"+person['contact'].lstrip("@")+"/profile_image?size=mini").raise_for_status() # url2 = ure.search('href=\"([^\"]+)',resp.content).group(1).decode('ascii') # print(url2) # img = http.get(url2).raise_for_status().content # print(img) # ugfx.display_image(180, 5, bytearray(img)) # except Exception as ex: # print(ex) ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(5, 90, 230, 40, person["username"], justification=ugfx.Label.LEFTTOP) ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.text(200, 92, person["age"], ugfx.WHITE) ugfx.Label(5, 120, 230, 60, person["tag_line"]) ugfx.Label(5, 200, 230, 40, person["looking_for"]) ugfx.text(5, 190, "Looking for...", ugfx.RED) ugfx.text(5, 245, person["contact"], ugfx.BLUE) # ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None) ugfx.Button(160, 280, 100, 40, "Swipe >", parent=None, shape=ugfx.Button.RECT, style=None)
def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", font=FONT_SMALL, style=None): """A simple one and two-options dialog if 'false_text' is set to None only one button is displayed. If both 'true_text' and 'false_text' are given a boolean is returned """ global default_style_dialog if style == None: style = default_style_dialog ugfx.set_default_font(FONT_MEDIUM_BOLD) width = ugfx.width() - 10 height = ugfx.height() - 10 window = ugfx.Container(5, 5, width, height) window.show() ugfx.set_default_font(font) window.text(5, 10, title, TILDA_COLOR) window.line(0, 30, width, 30, ugfx.BLACK) if false_text: true_text = "A: " + true_text false_text = "B: " + false_text ugfx.set_default_font(font) label = ugfx.Label(5, 30, width - 10, height - 80, text = text, parent=window) ugfx.set_default_font(FONT_MEDIUM_BOLD) button_yes = ugfx.Button(5, height - 40, width // 2 - 15 if false_text else width - 15, 30 , true_text, parent=window) button_no = ugfx.Button(width // 2 + 5, height - 40, width // 2 - 15, 30 , false_text, parent=window) if false_text else None try: #button_yes.attach_input(ugfx.BTN_A,0) # todo: re-enable once working #if button_no: button_no.attach_input(ugfx.BTN_B,0) window.show() while True: sleep.wfi() if buttons.is_triggered(buttons.Buttons.BTN_A): return True if buttons.is_triggered(buttons.Buttons.BTN_B): return False finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy()
def no_more(my_profile): top_left_logo() ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(5, 90, 230, 50, "You've swiped everybody!", justification=ugfx.Label.CENTERTOP) ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.Label(5, 160, 230, 20, "Soz "+my_profile["username"], justification=ugfx.Label.CENTERTOP) ugfx.Label(5, 180, 230, 20, "Come back later ;)", justification=ugfx.Label.CENTERTOP) # ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None) ugfx.Button(160, 280, 100, 40, "Try again >", parent=None, shape=ugfx.Button.RECT, style=None)
def __init__(self, page, text, callback, data): self.btn = ugfx.Button(page.x, page.y, page.group.w, page.group.h, text, shape=ugfx.Button.ROUNDED, parent=page.group.parent) self.btn.enabled(False) self.btn.visible(False) self.callback = callback self.data = data
def prompt_text(description, init_text="", true_text="OK", false_text="Back", width=ugfx.width(), height=ugfx.height(), font="Roboto_BlackItalic24", cb=None): """Shows a dialog and keyboard that allows the user to input/change a string Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not possible to process events inside an event callback. The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container(int((ugfx.width() - width) / 2), int((ugfx.height() - height) / 2), width, height) window.show() ugfx.set_default_font("Roboto_Regular12") kb_height = int(height / 2) + 30 kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window) ugfx.set_default_font("Roboto_Regular18") edit_height = 25 edit = ugfx.Textbox(5, height - kb_height - 5 - edit_height, int(width * 4 / 5) - 10, edit_height, text=init_text, parent=window) ugfx.set_default_font("Roboto_Regular12") button_height = 25 def done(result): window.destroy() if cb: cb(result) return result def syncSuccess(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "A" wait_for_interrupt = False def syncCancel(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "B" wait_for_interrupt = False def asyncSuccess(evt): if evt: done(edit.text()) def asyncCancel(evt): if evt: done(None) button_yes = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height, int(width * 1 / 5) - 3, button_height, true_text, parent=window, cb=asyncSuccess if cb else syncSuccess) button_no = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height - button_height, int(width / 5) - 3, button_height, false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(5, 1, int(width * 4 / 5), height - kb_height - 5 - edit_height - 5, description, parent=window) def vkey_backspace(): edit.backspace() ugfx.flush() focus = 0 def toggle_focus(pressed): if pressed: if focus == 0: edit.set_focus() kb.enabled(1) ugfx.input_attach( version.btn_cancel, lambda pressed: vkey_backspace() if pressed else 0) ugfx.input_attach( version.btn_ok, lambda pressed: 0 if pressed else ugfx.flush()) focus = 1 elif focus == 1 or not button_no: button_yes.set_focus() kb.enabled(0) ugfx.input_attach(version.btn_ok, asyncSuccess if cb else syncSuccess) ugfx.input_attach(version.btn_cancel, asyncCancel if cb else syncCancel) focus = (2 if button_no else 0) else: button_no.set_focus() kb.enabled(0) ugfx.input_attach(version.btn_ok, asyncCancel if cb else syncCancel) ugfx.input_attach(version.btn_cancel, asyncCancel if cb else syncCancel) focus = 0 ugfx.flush() ugfx.input_init() ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus) ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush() if pressed else 0) toggle_focus(True) ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() wait_for_interrupt = True if cb: return else: while wait_for_interrupt: time.sleep(0.2) if (focus == 0 and no_button) or button_pushed == "B": return done(False) return done(edit.text())
def __init__(self, grp, text, callback): self.btn = ugfx.Button(grp.x, grp.y, grp.w, grp.h, text, shape=ugfx.Button.ROUNDED, parent=grp.parent) self.btn.enabled(False) self.callback = callback
def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", font=FONT_SMALL, style=None): """A simple one and two-options dialog if 'false_text' is set to None only one button is displayed. If both 'true_text' and 'false_text' are given a boolean is returned """ global default_style_dialog if style == None: style = default_style_dialog ugfx.set_default_font(FONT_MEDIUM_BOLD) width = ugfx.width() - 10 height = ugfx.height() - 10 window = ugfx.Container(5, 5, width, height) window.show() ugfx.set_default_font(font) window.text(5, 5, title, TILDA_COLOR) window.line(0, 25, width, 25, ugfx.BLACK) if false_text: true_text = "A: " + true_text false_text = "B: " + false_text ugfx.set_default_font(font) label = ugfx.Label(5, 30, width - 10, height - 80, text=text, parent=window, justification=4) ugfx.set_default_font(FONT_MEDIUM_BOLD) button_yes = ugfx.Button(5, height - 40, width // 2 - 10 if false_text else width - 15, 30, true_text, parent=window) button_no = ugfx.Button(width // 2, height - 40, width // 2 - 10, 30, false_text, parent=window) if false_text else None # Find newlines in label text to scroll. def find_all(a_str, sub): start = 0 while True: start = a_str.find(sub, start) if start == -1: return yield start + 1 # Trap: \n becomes a single character, not 2. start += len(sub) # use start += 1 to find overlapping matches new_line_pos = [0] + list(find_all(text, '\n')) text_scroll_offset = 0 try: #button_yes.attach_input(ugfx.BTN_A,0) # todo: re-enable once working #if button_no: button_no.attach_input(ugfx.BTN_B,0) window.show() while True: sleep.wfi() if buttons.is_triggered(buttons.Buttons.BTN_A): return True if buttons.is_triggered(buttons.Buttons.BTN_B): return False # Allow scrolling by new lines. if buttons.is_triggered(buttons.Buttons.JOY_Down): if text_scroll_offset < len(new_line_pos) - 1: text_scroll_offset = text_scroll_offset + 1 label.text(text[new_line_pos[text_scroll_offset]:]) if buttons.is_triggered(buttons.Buttons.JOY_Up): if (text_scroll_offset > 0): text_scroll_offset = text_scroll_offset - 1 label.text(text[new_line_pos[text_scroll_offset]:]) finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy()
def file_loader(): width = ugfx.width() height = ugfx.height() buttons.disable_menu_reset() # Create visual elements win_header = ugfx.Container(0, 0, width, 30) win_files = ugfx.Container(0, 33, int(width / 2), height - 33) win_preview = ugfx.Container( int(width / 2) + 2, 33, int(width / 2) - 2, height - 33) components = [win_header, win_files, win_preview] ugfx.set_default_font(ugfx.FONT_TITLE) components.append( ugfx.Label(3, 3, width - 10, 29, "Choose App", parent=win_header)) ugfx.set_default_font(ugfx.FONT_MEDIUM) options = ugfx.List(0, 30, win_files.width(), win_files.height() - 30, parent=win_files) btnl = ugfx.Button(5, 3, 20, 20, "<", parent=win_files) btnr = ugfx.Button(win_files.width() - 7 - 20, 3, 20, 20, ">", parent=win_files) btnr.attach_input(ugfx.JOY_RIGHT, 0) btnl.attach_input(ugfx.JOY_LEFT, 0) components.append(options) components.append(btnr) components.append(btnl) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) l_cat = ugfx.Label(30, 3, 100, 20, "Built-in", parent=win_files) components.append(l_cat) components.append( ugfx.Button(10, win_preview.height() - 25, 20, 20, "A", parent=win_preview)) components.append( ugfx.Label(35, win_preview.height() - 25, 50, 20, "Run", parent=win_preview)) components.append( ugfx.Button(80, win_preview.height() - 25, 20, 20, "B", parent=win_preview)) components.append( ugfx.Label(105, win_preview.height() - 25, 100, 20, "Back", parent=win_preview)) components.append( ugfx.Button(10, win_preview.height() - 50, 20, 20, "M", parent=win_preview)) components.append( ugfx.Label(35, win_preview.height() - 50, 100, 20, "Pin/Unpin", parent=win_preview)) ugfx.set_default_font(ugfx.FONT_SMALL) author = ugfx.Label(1, win_preview.height() - 78, win_preview.width() - 3, 20, "by: ", parent=win_preview) desc = ugfx.Label(3, 1, win_preview.width() - 10, win_preview.height() - 83, "", parent=win_preview, justification=ugfx.Label.LEFTTOP) components.append(author) components.append(desc) app_to_load = None pinned = database_get("pinned_apps", []) catergories = get_local_app_categories() c_ptr = 0 try: win_header.show() win_files.show() win_preview.show() pinned = database_get("pinned_apps", []) # apps = [] apps_path = [] if is_dir("apps"): for app in os.listdir("apps"): path = "apps/" + app if is_dir(path) and is_file(path + "/main.py"): apps_path.append(path + "/main.py") if is_dir("examples"): for app in os.listdir("examples"): path = "examples/" + app if is_file(path) and path.endswith(".py"): apps_path.append(path) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 while True: pyb.wfi() ugfx.poll() if index_prev != options.selected_index(): if options.selected_index() < len(displayed_apps): author.text("by: %s" % displayed_apps[options.selected_index()].user) desc.text( displayed_apps[options.selected_index()].description) index_prev = options.selected_index() if buttons.is_triggered("JOY_LEFT"): if c_ptr > 0: c_ptr -= 1 btnl.set_focus() l_cat.text(catergories[c_ptr]) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 if buttons.is_triggered("JOY_RIGHT"): if c_ptr < len(catergories) - 1: c_ptr += 1 btnr.set_focus() l_cat.text(catergories[c_ptr]) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 if buttons.is_triggered("BTN_MENU"): app = displayed_apps[options.selected_index()] if app.folder_name in pinned: pinned.remove(app.folder_name) else: pinned.append(app.folder_name) update_options(options, catergories[c_ptr], pinned) database_set("pinned_apps", pinned) if buttons.is_triggered("BTN_B"): return None if buttons.is_triggered("BTN_A"): return displayed_apps[options.selected_index()] finally: for component in components: component.destroy()
def prompt_option(options, index=0, text = None, title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) list_y = 30 if text: list_y += 20 window.text(5, 30, text, ugfx.BLACK) else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 260 - list_y, parent = window) options_list.disable_draw() for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.enable_draw() options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 105 if none_text else 200, 30 , select_text, parent=window) button_none = ugfx.Button(117, ugfx.height() - 50, 105, 30 , none_text, parent=window) if none_text else None try: while True: sleep.wfi() ugfx.poll() # todo: temporary hack #if (buttons.is_triggered(buttons.Buttons.JOY_Up)): # index = max(index - 1, 0) # options_list.selected_index(index) #if (buttons.is_triggered(buttons.Buttons.JOY_Down)): # index = min(index + 1, len(options) - 1) # options_list.selected_index(index) if buttons.is_triggered(buttons.Buttons.BTN_A) or buttons.is_triggered(buttons.Buttons.JOY_Center): return options[options_list.selected_index()] if button_none and buttons.is_triggered(buttons.Buttons.BTN_B): return None if button_none and buttons.is_triggered(buttons.Buttons.BTN_Menu): return None finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def prompt_boolean(text, title="SHA2017", true_text="Yes", false_text="No", width=296, height=128, font="Roboto_Regular12"): """A simple one and two-options dialog if 'false_text' is set to None only one button is displayed. If both 'false_text' and 'true_text' are given a boolean is returned, press B for false, A for true. The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container((ugfx.width() - width) // 2, (ugfx.height() - height) // 2, width, height) window.show() ugfx.set_default_font(font) window.text(5, 10, title, ugfx.BLACK) window.line(0, 30, width, 30, ugfx.BLACK) if false_text: false_text = "B: " + false_text true_text = "A: " + true_text label = ugfx.Label(5, 30, width - 10, height - 80, text=text, parent=window) button_no = ugfx.Button( 5, height - 40, width // 2 - 15, 30, false_text, parent=window) if false_text else None button_yes = ugfx.Button(width // 2 + 5 if true_text else 5, height - 40, width // 2 - 15 if false_text else width - 10, 30, true_text, parent=window) button_yes.set_focus() ugfx.input_init() window.show() ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() def done(value): window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() return value if button_no: ugfx.input_attach(ugfx.BTN_B, pressed_b) ugfx.input_attach(ugfx.BTN_A, pressed_a) wait_for_interrupt = True while wait_for_interrupt: time.sleep(0.2) if button_pushed == "B": return done(False) if button_pushed == "A": return done(True)
def prompt_text(description, init_text="", true_text="OK", false_text="Back", width=300, height=200, font="Roboto_BlackItalic24"): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container(int((ugfx.width() - width) / 2), int((ugfx.height() - height) / 2), width, height) window.show() ugfx.set_default_font("Roboto_Regular12") kb_height = int(height / 2) + 30 kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window) ugfx.set_default_font("Roboto_Regular18") edit_height = 25 edit = ugfx.Textbox(5, height - kb_height - 5 - edit_height, int(width * 4 / 5) - 10, edit_height, text=init_text, parent=window) ugfx.set_default_font("Roboto_Regular12") button_height = 25 def okay(evt): # We'd like promises here, but for now this should do global wait_for_interrupt button_pushed = "A" wait_for_interrupt = False button_yes = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height, int(width * 1 / 5) - 3, button_height, true_text, parent=window, cb=okay) button_no = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height - button_height, int(width / 5) - 3, button_height, false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(5, 1, int(width * 4 / 5), height - kb_height - 5 - edit_height - 5, description, parent=window) def vkey_backspace(): edit.backspace() ugfx.flush() focus = 0 def toggle_focus(pressed): if pressed: if focus == 0: edit.set_focus() kb.enabled(1) ugfx.input_attach( ugfx.BTN_B, lambda pressed: vkey_backspace() if pressed else 0) ugfx.input_attach( ugfx.BTN_A, lambda pressed: 0 if pressed else ugfx.flush()) focus = 1 elif focus == 1 or not button_no: button_yes.set_focus() kb.enabled(0) ugfx.input_attach(ugfx.BTN_A, pressed_a) ugfx.input_attach(ugfx.BTN_B, pressed_b) focus = (2 if button_no else 0) else: button_no.set_focus() kb.enabled(0) ugfx.input_attach(ugfx.BTN_A, pressed_a) ugfx.input_attach(ugfx.BTN_B, pressed_b) focus = 0 ugfx.flush() ugfx.input_init() ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus) ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush() if pressed else 0) toggle_focus(True) ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() wait_for_interrupt = True while wait_for_interrupt: time.sleep(0.2) def done(value): window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy() return value if (focus == 0 and no_button) or button_pushed == "B": return done(False) return done(edit.text())
if database_get("stats_upload"): chk_upload.checked(1) win_header.show() win_legend.show() ugfx.set_default_font(ugfx.FONT_MEDIUM) ugfx.set_default_style(s) graph = ugfx.Graph(0, 33, wi, hi - 33 - 33, 3, 3) graph.appearance(ugfx.Graph.STYLE_POINT, ugfx.Graph.POINT_NONE, 0, 0) wi_g = wi - 3 graph.show() ugfx.set_default_font(ugfx.FONT_SMALL) win_zoom = ugfx.Container(1, 33, 92, 25) btnl = ugfx.Button(3, 3, 20, 20, "<", parent=win_zoom) btnr = ugfx.Button(68, 3, 20, 20, ">", parent=win_zoom) l_cat = ugfx.Label(28, 3, 35, 20, "1x", parent=win_zoom) btnr.attach_input(ugfx.JOY_RIGHT, 0) btnl.attach_input(ugfx.JOY_LEFT, 0) win_zoom.show() scaling = int((hi - 33 - 33 - 30) / 2) lines = 0 names = [] seek = -1 if not is_file("log.txt"): ugfx.text(20, 100, "Log file not found", 0) wait_for_exit() pyb.hard_reset()
def prompt_option(options, index=0, text=None, title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) list_y = 30 if text: list_y += 20 window.text(5, 30, text, ugfx.BLACK) else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 260 - list_y, parent=window) options_list.disable_draw() optnum = 1 for option in options: if isinstance(option, dict) and option["title"]: title = option["title"] else: title = str(option) if optnum < 11: # mod 10 to make 10th item numbered 0 options_list.add_item("{}: {}".format((optnum % 10), title)) else: options_list.add_item(" {}".format(title)) optnum = optnum + 1 options_list.enable_draw() options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 105 if none_text else 200, 30, select_text, parent=window) button_none = ugfx.Button( 117, ugfx.height() - 50, 105, 30, none_text, parent=window) if none_text else None try: while True: sleep.wfi() ugfx.poll() # todo: temporary hack #if (buttons.is_triggered(buttons.Buttons.JOY_Up)): # index = max(index - 1, 0) # options_list.selected_index(index) #if (buttons.is_triggered(buttons.Buttons.JOY_Down)): # index = min(index + 1, len(options) - 1) # options_list.selected_index(index) if buttons.is_triggered( buttons.Buttons.BTN_A) or buttons.is_triggered( buttons.Buttons.JOY_Center): return options[options_list.selected_index()] if button_none and buttons.is_triggered(buttons.Buttons.BTN_B): return None if button_none and buttons.is_triggered(buttons.Buttons.BTN_Menu): return None # These are indexes for selected_index, 1 means "First item", ie index 0. 0 is treated as if it were 10 button_nums = { Buttons.BTN_1: 0, Buttons.BTN_2: 1, Buttons.BTN_3: 2, Buttons.BTN_4: 3, Buttons.BTN_5: 4, Buttons.BTN_6: 5, Buttons.BTN_7: 6, Buttons.BTN_8: 7, Buttons.BTN_9: 8, Buttons.BTN_0: 9, } for key, num in button_nums.items(): if buttons.is_triggered(key): # No need to check for too large an index; gwinListSetSelected validates this. options_list.selected_index(num) break if buttons.is_triggered(Buttons.BTN_Hash): # Page down idx = options_list.selected_index() + 10 cnt = options_list.count() if idx >= cnt: idx = cnt - 1 options_list.selected_index(idx) continue if buttons.is_triggered(Buttons.BTN_Star): # Page up idx = options_list.selected_index() - 10 if idx < 0: idx = 0 options_list.selected_index(idx) continue finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
import ugfx import os #options.destroy() #btn_ok.destroy() #btn_menu.destroy() ugfx.init() ugfx.set_default_font("D*") ugfx.text(40, 0, "EMF BADGE 2016", ugfx.PURPLE) ugfx.set_default_font("C*") options = ugfx.List(0, 0, 160, 150) btn_ok = ugfx.Button(200, 50, 70, 30, "A: Run") btn_menu = ugfx.Button(200, 90, 70, 30, "M: Menu") files = os.listdir() for f in files: options.add_item(f) btn_menu.attach_input(ugfx.BTN_MENU) btn_ok.attach_input(ugfx.BTN_A)
def quick_launch_screen(): wi = ugfx.width() hi = ugfx.height() win_header = ugfx.Container(0,0,wi,30) win_quick = ugfx.Container(0,33,wi,hi-33-33) win_help = ugfx.Container(0,hi-30,wi,30) DEFAULT_APPS = ["app_library", "changename", "alistair~selectwifi", "snake"] with Database() as db: pinned = [App(a) for a in db.get("pinned_apps", DEFAULT_APPS)] pinned = [app for app in pinned if app.loadable] # Filter out deleted apps pinned = pinned[:7] # Limit to 7 db.set("pinned_apps", [app.folder_name for app in pinned]) ugfx.set_default_font(ugfx.FONT_TITLE) title = ugfx.Label(3,3,wi-10,45,"EMF Camp 2016",parent=win_header) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) pinned_buttons = [] for i in range(0, 8): x = i % 2 y = i // 2 button_title = "Installed Apps" if i == 7 else "" if i < len(pinned): button_title = pinned[i].title pinned_buttons.append(ugfx.Button(35 + 155 * x, 5 + 40 * y, 120, 35, button_title, parent=win_quick)) btn_ok = ugfx.Button(10,5,20,20,"A",parent=win_help,shape=ugfx.Button.ELLIPSE) l_ok = ugfx.Label(40,5,100,20,"Run",parent=win_help) btn_back = ugfx.Button(100,5,20,20,"B",parent=win_help,shape=ugfx.Button.ELLIPSE) l_back = ugfx.Label(130,5,100,20,"Back",parent=win_help) btn_menu = ugfx.Button(200,5,20,20,"M",parent=win_help,shape=ugfx.Button.ROUNDED) l_menu = ugfx.Label(230,5,100,20,"Menu",parent=win_help) win_header.show() win_quick.show() win_help.show() buttons.init() cursor = {"x": 0, "y": 0} last_cursor = cursor.copy() _draw_cursor(0, 0, ugfx.RED, win_quick) if not database_get("quicklaunch_firstrun"): dialogs.notice("""This screen displays the most commonly used apps. Apps pinned here can also interact with the name screen. To view all apps, pin and un-pin, select 'Installed Apps' """, title="TiLDA - Quick Launch", close_text="Close") database_set("quicklaunch_firstrun", True) try: while True: pyb.wfi() if buttons.is_triggered("JOY_UP"): cursor["y"] = max(0, cursor["y"] - 1) if buttons.is_triggered("JOY_DOWN"): cursor["y"] = min(3, cursor["y"] + 1) if buttons.is_triggered("JOY_RIGHT"): cursor["x"] = 1 if buttons.is_triggered("JOY_LEFT"): cursor["x"] = 0 if cursor["x"] != last_cursor["x"] or cursor["y"] != last_cursor["y"]: # Has the cursor moved? _draw_cursor(last_cursor["x"], last_cursor["y"], dialogs.default_style_badge.background(), win_quick) _draw_cursor(cursor["x"], cursor["y"], ugfx.RED, win_quick) last_cursor = cursor.copy() if buttons.is_triggered("BTN_B"): return None #if buttons.is_triggered("BTN_MENU"): # open unpin dialog # break; if buttons.is_triggered("BTN_A"): index = cursor["x"] + cursor["y"] * 2 if index == 7: return "file_loader" if index < len(pinned): return pinned[index] finally: buttons.disable_all_interrupt() win_header.hide() win_quick.hide() win_help.hide() for b in pinned_buttons: b.destroy() btn_ok.destroy() l_ok.destroy() btn_back.destroy() l_back.destroy() btn_menu.destroy() l_menu.destroy() win_header.destroy() win_quick.destroy() win_help.destroy() title.destroy()
def prompt_boolean(text, title="SHA2017", true_text="Yes", false_text="No", width = 296, height = 128, font="Roboto_Regular12", cb=None): """A simple one and two-options dialog if 'false_text' is set to None only one button is displayed. If both 'false_text' and 'true_text' are given a boolean is returned, press B for false, A for true. Pass along a 'cb' callback to make the dialog async, which is needed to make input work when used from a callback The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container((ugfx.width() - width) // 2, (ugfx.height() - height) // 2, width, height) window.show() ugfx.set_default_font(font) window.text(5, 10, title, ugfx.BLACK) window.line(0, 30, width, 30, ugfx.BLACK) if false_text: false_text = "B: " + false_text true_text = "A: " + true_text def done(result): window.destroy() if cb: cb(result) return result def syncSuccess(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "A" wait_for_interrupt = False def syncCancel(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "B" wait_for_interrupt = False def asyncSuccess(evt): if evt: done(True) def asyncCancel(evt): if evt: done(False) label = ugfx.Label(5, 30, width - 10, height - 80, text = text, parent=window) button_no = ugfx.Button(5, height - 40, width // 2 - 15, 30, false_text, parent=window) if false_text else None button_yes = ugfx.Button(width // 2 + 5 if true_text else 5, height - 40, width // 2 - 15 if false_text else width - 10, 30, true_text, parent=window) button_yes.set_focus() ugfx.input_init() window.show() ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() if button_no: ugfx.input_attach(ugfx.BTN_B, asyncCancel if cb else syncCancel) ugfx.input_attach(ugfx.BTN_A, asyncSuccess if cb else syncSuccess) if cb: return else: wait_for_interrupt = True while wait_for_interrupt: time.sleep(0.2) if button_pushed == "B": return done(False) return done(True)