From 9af7e8fddc2d4ce60af5ad4da13cec4eb9466a08 Mon Sep 17 00:00:00 2001 From: Ethan <108598670+echo-lalia@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:30:20 -0700 Subject: [PATCH] Hydefix (#137) * Fix textbox colors * Fix cursor position in HyDE Determining cursor position by slicing the text and then measuring its width did not work because the width of off-screen characters affected the text position, but not the cursor position. The solution made here has to measure all text before the cursor, which is probably not ideal for performance, but there is lots of room for other performance improvements in HyDE. This commit also removes line breaks before calculating cursor position to prevent other odd issues. Resolves #128 * Fix non-py syntax hilighting --- src/launcher/HyDE.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/launcher/HyDE.py b/src/launcher/HyDE.py index b2d3c96..595564e 100644 --- a/src/launcher/HyDE.py +++ b/src/launcher/HyDE.py @@ -238,7 +238,7 @@ def exit_options(target_file, overlay, editor): def boot_into_file(target_file, overlay): """Restart and load into target file.""" - overlay.draw_textbox("Restarting...", _MH_DISPLAY_WIDTH//2, _MH_DISPLAY_HEIGHT//2) + overlay.draw_textbox("Restarting...") DISPLAY.show() RTC.memory(target_file) @@ -248,7 +248,7 @@ def boot_into_file(target_file, overlay): def run_file_here(filepath, overlay, editor): """Try running the target file here""" editor.save_file(filepath) - overlay.draw_textbox("Running...", _MH_DISPLAY_WIDTH//2, _MH_DISPLAY_HEIGHT//2) + overlay.draw_textbox("Running...") DISPLAY.show() try: # you have to slice off the ".py" to avoid importerror @@ -912,7 +912,13 @@ def get_total_width(self,line): return width def draw_cursor(self): - cursor_x = self.get_total_width(self.lines[self.cursor_index[1]][self.display_index[0]:self.cursor_index[0]+1]) + _LEFT_PADDING + line = format_display_line( + self.lines[self.cursor_index[1]][:self.cursor_index[0]] + ) + cursor_x = self.get_total_width(line) \ + - (self.display_index[0] * 8) \ + + _LEFT_PADDING + cursor_y = (_SMALL_FONT_HEIGHT * 3) + (self.cursor_index[1] - self.display_index[1] - 3) * _TEXT_HEIGHT if time.ticks_ms() % _CURSOR_BLINK_MS < _CURSOR_BLINK_HALF: DISPLAY.vline(cursor_x, cursor_y, 16, CONFIG.palette[9]) @@ -929,7 +935,7 @@ def draw_bg(self): def save_file(self, filepath): """Reverse temporary formatting and Save the file.""" - self.overlay.draw_textbox("Saving...",_MH_DISPLAY_WIDTH//2,_MH_DISPLAY_HEIGHT//2) + self.overlay.draw_textbox("Saving...") DISPLAY.show() with open(filepath,"w") as file: for line in self.lines: @@ -973,7 +979,7 @@ def del_line(self): def main_loop(): """Main loop of the program.""" - global STR_COLOR, DARK_STR_COLOR, KEYWORD_COLOR, COMMENT_COLOR, DARK_COMMENT_COLOR, USE_TABS + global STR_COLOR, DARK_STR_COLOR, KEYWORD_COLOR, NUM_COLOR, OP_COLOR, COMMENT_COLOR, DARK_COMMENT_COLOR, USE_TABS DISPLAY.fill(CONFIG.palette[2]) overlay = popup.UIOverlay() @@ -985,11 +991,17 @@ def main_loop(): if target_file == "": target_file = "/log.txt" - # remove syntax hilighting for plain txt files. - if target_file.endswith('.txt'): - STR_COLOR = CONFIG.palette[8]; DARK_STR_COLOR = CONFIG.palette[8] + # subtle syntax hilighting for non-py files. + if not target_file.endswith('.py'): + print('text file') + STR_COLOR = CONFIG.palette[7] + DARK_STR_COLOR = CONFIG.palette[6] + NUM_COLOR = CONFIG.palette[7] + OP_COLOR = CONFIG.palette[7] + KEYWORD_COLOR = CONFIG.palette[8] - COMMENT_COLOR = CONFIG.palette[8]; DARK_COMMENT_COLOR = CONFIG.palette[8] + COMMENT_COLOR = CONFIG.palette[8] + DARK_COMMENT_COLOR = CONFIG.palette[6] try: