|
| 1 | +### Author: EMF Badge team |
| 2 | +### Author: SHA2017Badge team |
| 3 | +### Description: Some basic UGFX powered dialogs |
| 4 | +### License: MIT |
| 5 | + |
| 6 | +import ugfx, badge, utime as time |
| 7 | + |
| 8 | +def notice(text, title="SHA2017", close_text="Close", width = 296, height = 128, font="Roboto_Regular12"): |
| 9 | + prompt_boolean(text, title = title, true_text = close_text, false_text = None, width = width, height = height, font=font) |
| 10 | + |
| 11 | +def prompt_boolean(text, title="SHA2017", true_text="Yes", false_text="No", width = 296, height = 128, font="Roboto_Regular12"): |
| 12 | + """A simple one and two-options dialog |
| 13 | +
|
| 14 | + if 'false_text' is set to None only one button is displayed. |
| 15 | + If both 'true_text' and 'false_text' are given a boolean is returned |
| 16 | + """ |
| 17 | + window = ugfx.Container((ugfx.width() - width) // 2, (ugfx.height() - height) // 2, width, height) |
| 18 | + window.show() |
| 19 | + ugfx.set_default_font(font) |
| 20 | + window.text(5, 10, title, TILDA_COLOR) |
| 21 | + window.line(0, 30, width, 30, ugfx.BLACK) |
| 22 | + |
| 23 | + if false_text: |
| 24 | + true_text = "A: " + true_text |
| 25 | + false_text = "B: " + false_text |
| 26 | + |
| 27 | + label = ugfx.Label(5, 30, width - 10, height - 80, text = text, parent=window) |
| 28 | + button_yes = ugfx.Button(5, height - 40, width // 2 - 15 if false_text else width - 15, 30 , true_text, parent=window) |
| 29 | + button_no = ugfx.Button(width // 2 + 5, height - 40, width // 2 - 15, 30 , false_text, parent=window) if false_text else None |
| 30 | + |
| 31 | + try: |
| 32 | + ugfx.input_init() |
| 33 | + |
| 34 | + # button_yes.attach_input(ugfx.BTN_A,0) |
| 35 | + # if button_no: button_no.attach_input(ugfx.BTN_B,0) |
| 36 | + |
| 37 | + window.show() |
| 38 | + |
| 39 | + # while True: |
| 40 | + # pyb.wfi() # TODO make this!! |
| 41 | + # if buttons.is_triggered("BTN_A"): return True |
| 42 | + # if buttons.is_triggered("BTN_B"): return False |
| 43 | + |
| 44 | + finally: |
| 45 | + window.hide() |
| 46 | + window.destroy() |
| 47 | + button_yes.destroy() |
| 48 | + if button_no: button_no.destroy() |
| 49 | + label.destroy() |
| 50 | + |
| 51 | +def prompt_text(description, init_text = "", true_text="OK", false_text="Back", width = 300, height = 200, font="Roboto_BlackItalic24"): |
| 52 | + """Shows a dialog and keyboard that allows the user to input/change a string |
| 53 | +
|
| 54 | + Returns None if user aborts with button B |
| 55 | + """ |
| 56 | + |
| 57 | + window = ugfx.Container(int((ugfx.width()-width)/2), int((ugfx.height()-height)/2), width, height) |
| 58 | + |
| 59 | + if false_text: |
| 60 | + true_text = "M: " + true_text |
| 61 | + false_text = "B: " + false_text |
| 62 | + |
| 63 | + # if buttons.has_interrupt("BTN_MENU"): |
| 64 | + # buttons.disable_interrupt("BTN_MENU") |
| 65 | + |
| 66 | + ugfx.set_default_font("Roboto_Regular18") |
| 67 | + kb = ugfx.Keyboard(0, int(height/2), width, int(height/2), parent=window) |
| 68 | + edit = ugfx.Textbox(5, int(height/2)-30, int(width*4/5)-10, 25, text = init_text, parent=window) |
| 69 | + ugfx.set_default_font("Roboto_Regular12") |
| 70 | + button_yes = ugfx.Button(int(width*4/5), int(height/2)-30, int(width*1/5)-3, 25 , true_text, parent=window) |
| 71 | + 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 |
| 72 | + ugfx.set_default_font(font) |
| 73 | + label = ugfx.Label(int(width/10), int(height/10), int(width*4/5), int(height*2/5)-60, description, parent=window) |
| 74 | + |
| 75 | + try: |
| 76 | + ugfx.input_init() |
| 77 | + |
| 78 | + # button_yes.attach_input(ugfx.BTN_MENU,0) |
| 79 | + # if button_no: button_no.attach_input(ugfx.BTN_B,0) |
| 80 | + # TODO ^^ |
| 81 | + |
| 82 | + window.show() |
| 83 | + edit.set_focus() |
| 84 | + # while True: |
| 85 | + # pyb.wfi() |
| 86 | + # ugfx.poll() |
| 87 | + # #if buttons.is_triggered("BTN_A"): return edit.text() |
| 88 | + # if buttons.is_triggered("BTN_B"): return None |
| 89 | + # if buttons.is_triggered("BTN_MENU"): return edit.text() |
| 90 | + |
| 91 | + finally: |
| 92 | + window.hide() |
| 93 | + window.destroy() |
| 94 | + button_yes.destroy() |
| 95 | + if button_no: button_no.destroy() |
| 96 | + label.destroy() |
| 97 | + kb.destroy() |
| 98 | + edit.destroy(); |
| 99 | + return |
| 100 | + |
| 101 | +def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None): |
| 102 | + """Shows a dialog prompting for one of multiple options |
| 103 | +
|
| 104 | + If none_text is specified the user can use the B or Menu button to skip the selection |
| 105 | + if title is specified a blue title will be displayed about the text |
| 106 | + """ |
| 107 | + ugfx.set_default_font("Roboto_Regular12") |
| 108 | + window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) |
| 109 | + window.show() |
| 110 | + |
| 111 | + list_y = 30 |
| 112 | + if title: |
| 113 | + window.text(5, 10, title, ugfxBLACK) |
| 114 | + window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) |
| 115 | + window.text(5, 30, text, ugfx.BLACK) |
| 116 | + list_y = 50 |
| 117 | + else: |
| 118 | + window.text(5, 10, text, ugfx.BLACK) |
| 119 | + |
| 120 | + options_list = ugfx.List(5, list_y, ugfx.width() - 25, 180 - list_y, parent = window) |
| 121 | + |
| 122 | + for option in options: |
| 123 | + if isinstance(option, dict) and option["title"]: |
| 124 | + options_list.add_item(option["title"]) |
| 125 | + else: |
| 126 | + options_list.add_item(str(option)) |
| 127 | + options_list.selected_index(index) |
| 128 | + |
| 129 | + select_text = "A: " + select_text |
| 130 | + if none_text: |
| 131 | + none_text = "B: " + none_text |
| 132 | + |
| 133 | + button_select = ugfx.Button(5, ugfx.height() - 50, 140 if none_text else ugfx.width() - 25, 30 , select_text, parent=window) |
| 134 | + button_none = ugfx.Button(ugfx.width() - 160, ugfx.height() - 50, 140, 30 , none_text, parent=window) if none_text else None |
| 135 | + |
| 136 | + try: |
| 137 | + ugfx.input_init() |
| 138 | + |
| 139 | + # while True: |
| 140 | + # pyb.wfi() # BLABLA TODO |
| 141 | + # ugfx.poll() |
| 142 | + # if buttons.is_triggered("BTN_A"): return options[options_list.selected_index()] |
| 143 | + # if button_none and buttons.is_triggered("BTN_B"): return None |
| 144 | + # if button_none and buttons.is_triggered("BTN_MENU"): return None |
| 145 | + |
| 146 | + finally: |
| 147 | + window.hide() |
| 148 | + window.destroy() |
| 149 | + options_list.destroy() |
| 150 | + button_select.destroy() |
| 151 | + if button_none: button_none.destroy() |
| 152 | + ugfx.poll() |
| 153 | + |
| 154 | +class WaitingMessage: |
| 155 | + """Shows a dialog with a certain message that can not be dismissed by the user""" |
| 156 | + def __init__(self, text = "Please Wait...", title="TiLDA"): |
| 157 | + self.window = ugfx.Container(30, 30, ugfx.width() - 60, ugfx.height() - 60) |
| 158 | + self.window.show() |
| 159 | + self.window.text(5, 10, title, TILDA_COLOR) |
| 160 | + self.window.line(0, 30, ugfx.width() - 60, 30, ugfx.BLACK) |
| 161 | + self.label = ugfx.Label(5, 40, self.window.width() - 10, ugfx.height() - 40, text = text, parent=self.window) |
| 162 | + |
| 163 | + # Indicator to show something is going on |
| 164 | + self.indicator = ugfx.Label(ugfx.width() - 100, 0, 20, 20, text = "...", parent=self.window) |
| 165 | + self.timer = pyb.Timer(3) |
| 166 | + self.timer.init(freq=3) |
| 167 | + self.timer.callback(lambda t: self.indicator.visible(not self.indicator.visible())) |
| 168 | + |
| 169 | + def destroy(self): |
| 170 | + self.timer.deinit() |
| 171 | + self.label.destroy() |
| 172 | + self.indicator.destroy() |
| 173 | + self.window.destroy() |
| 174 | + |
| 175 | + def text(self): |
| 176 | + return self.label.text() |
| 177 | + |
| 178 | + def text(self, value): |
| 179 | + self.label.text(value) |
| 180 | + |
| 181 | + def __enter__(self): |
| 182 | + return self |
| 183 | + |
| 184 | + def __exit__(self, exc_type, exc_value, traceback): |
| 185 | + self.destroy() |
0 commit comments