From a1b81466e2bc150b9fccd9303528ef7304385805 Mon Sep 17 00:00:00 2001 From: Jordan Karp Date: Tue, 2 Apr 2024 22:46:46 -0700 Subject: [PATCH 1/5] Broke down shortcut events into sub functions --- pygame_gui/elements/ui_text_entry_box.py | 97 ++++++++++++++++++------ 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/pygame_gui/elements/ui_text_entry_box.py b/pygame_gui/elements/ui_text_entry_box.py index 35fd2687..67c4d9f3 100644 --- a/pygame_gui/elements/ui_text_entry_box.py +++ b/pygame_gui/elements/ui_text_entry_box.py @@ -737,42 +737,93 @@ def _process_keyboard_shortcut_event(self, event: Event) -> bool: :return: True if event consumed. + """ + consumed_event = False + if self._process_select_all_event(event): + consumed_event = True + elif self._process_cut_event(event): + consumed_event = True + elif self._process_copy_event(event): + consumed_event = True + elif self._process_paste_event(event): + consumed_event = True + return consumed_event + + def _process_select_all_event(self, event: Event) -> bool: + """ + Process a select all shortcut event. (CTRL+ A) + + :param event: The event to process. + + :return: True if the event is consumed. + """ consumed_event = False if (event.key == K_a and (event.mod & KMOD_CTRL or event.mod & KMOD_META) and not (event.mod & KMOD_ALT)): # hopefully enable diacritic letters) - self.select_range = [0, len(self.html_text)] - self.edit_position = len(self.html_text) - self.cursor_has_moved_recently = True + self._do_select_all() consumed_event = True - elif (event.key == K_x and + return consumed_event + + def _do_select_all(self): + self.select_range = [0, len(self.html_text)] + self.edit_position = len(self.html_text) + self.cursor_has_moved_recently = True + + def _process_cut_event(self, event: Event) -> bool: + """ + Process a cut shortcut event. (CTRL+ X) + + :param event: The event to process. + + :return: True if the event is consumed. + + """ + consumed_event = False + if (event.key == K_x and (event.mod & KMOD_CTRL or event.mod & KMOD_META) and not (event.mod & KMOD_ALT)): - if abs(self.select_range[0] - self.select_range[1]) > 0: - low_end = min(self.select_range[0], self.select_range[1]) - high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.html_text[low_end:high_end]) - self.text_box_layout.delete_selected_text() - self.edit_position = low_end - self.html_text = self.html_text[:low_end] + self.html_text[high_end:] - self.text_box_layout.set_cursor_position(self.edit_position) - self.select_range = [0, 0] - self.redraw_from_text_block() - self.cursor_has_moved_recently = True - consumed_event = True - elif (event.key == K_c and + self._do_cut() + consumed_event = True + return consumed_event + + def _do_cut(self): + if abs(self.select_range[0] - self.select_range[1]) > 0: + low_end = min(self.select_range[0], self.select_range[1]) + high_end = max(self.select_range[0], self.select_range[1]) + clipboard_copy(self.html_text[low_end:high_end]) + self.text_box_layout.delete_selected_text() + self.edit_position = low_end + self.html_text = self.html_text[:low_end] + self.html_text[high_end:] + self.text_box_layout.set_cursor_position(self.edit_position) + self.select_range = [0, 0] + self.redraw_from_text_block() + self.cursor_has_moved_recently = True + + def _process_copy_event(self, event: Event) -> bool: + """ + Process a copy shortcut event. (CTRL+ C) + + :param event: The event to process. + + :return: True if the event is consumed. + + """ + consumed_event = False + if (event.key == K_c and (event.mod & KMOD_CTRL or event.mod & KMOD_META) and not (event.mod & KMOD_ALT)): - if abs(self.select_range[0] - self.select_range[1]) > 0: - low_end = min(self.select_range[0], self.select_range[1]) - high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.html_text[low_end:high_end]) - consumed_event = True - elif self._process_paste_event(event): + self._do_copy() consumed_event = True return consumed_event + def _do_copy(self): + if abs(self.select_range[0] - self.select_range[1]) > 0: + low_end = min(self.select_range[0], self.select_range[1]) + high_end = max(self.select_range[0], self.select_range[1]) + clipboard_copy(self.html_text[low_end:high_end]) + def _process_paste_event(self, event: Event) -> bool: """ Process a paste shortcut event. (CTRL+ V) From eb7f56fb36c629c860c48f8cf77319d1991b1c58 Mon Sep 17 00:00:00 2001 From: Jordan Karp Date: Fri, 5 Apr 2024 22:37:59 -0700 Subject: [PATCH 2/5] Split Keyboard Shortcuts into subfunctions --- pygame_gui/elements/ui_text_entry_line.py | 116 +++++++++++++++------- 1 file changed, 82 insertions(+), 34 deletions(-) diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py index f84d2b73..eb13abcd 100644 --- a/pygame_gui/elements/ui_text_entry_line.py +++ b/pygame_gui/elements/ui_text_entry_line.py @@ -720,45 +720,93 @@ def _process_keyboard_shortcut_event(self, event: pygame.event.Event) -> bool: """ consumed_event = False - if (event.key == pygame.K_a and - (event.mod & pygame.KMOD_CTRL or event.mod & pygame.KMOD_META) and - not (event.mod & pygame.KMOD_ALT)): # hopefully enable diacritic letters - self.select_range = [0, len(self.text)] - self.edit_position = len(self.text) - self.cursor_has_moved_recently = True + if self._process_select_all_event(event): + consumed_event = True + elif self._process_cut_event(event): + consumed_event = True + elif self._process_copy_event(event): consumed_event = True - elif (event.key == pygame.K_x and - (event.mod & pygame.KMOD_CTRL or event.mod & pygame.KMOD_META) and - not self.is_text_hidden and - not (event.mod & pygame.KMOD_ALT)): # hopefully enable diacritic letters - if abs(self.select_range[0] - self.select_range[1]) > 0: - low_end = min(self.select_range[0], self.select_range[1]) - high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.text[low_end:high_end]) - if self.drawable_shape is not None: - self.drawable_shape.text_box_layout.delete_selected_text() - self.drawable_shape.apply_active_text_changes() - self.edit_position = low_end - self.text = self.text[:low_end] + self.text[high_end:] - if self.drawable_shape is not None: - self.drawable_shape.text_box_layout.set_cursor_position(self.edit_position) - self.drawable_shape.apply_active_text_changes() - self.select_range = [0, 0] - self.cursor_has_moved_recently = True - consumed_event = True - elif (event.key == pygame.K_c and - (event.mod & pygame.KMOD_CTRL or event.mod & pygame.KMOD_META) and - not self.is_text_hidden and - not (event.mod & pygame.KMOD_ALT)): # hopefully enable diacritic letters: - if abs(self.select_range[0] - self.select_range[1]) > 0: - low_end = min(self.select_range[0], self.select_range[1]) - high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.text[low_end:high_end]) - consumed_event = True elif self._process_paste_event(event): consumed_event = True return consumed_event + def _process_select_all_event(self, event: Event) -> bool: + """ + Process a select all shortcut event. (CTRL+ A) + + :param event: The event to process. + + :return: True if the event is consumed. + + """ + consumed_event = False + if (event.key == K_a and + (event.mod & KMOD_CTRL or event.mod & KMOD_META) and + not (event.mod & KMOD_ALT)): # hopefully enable diacritic letters) + self._do_select_all() + consumed_event = True + return consumed_event + + def _do_select_all(self): + self.select_range = [0, len(self.html_text)] + self.edit_position = len(self.html_text) + self.cursor_has_moved_recently = True + + def _process_cut_event(self, event: Event) -> bool: + """ + Process a cut shortcut event. (CTRL+ X) + + :param event: The event to process. + + :return: True if the event is consumed. + + """ + consumed_event = False + if (event.key == K_x and + (event.mod & KMOD_CTRL or event.mod & KMOD_META) and + not self.is_text_hidden and # This is not in UITextEntryBox + not (event.mod & KMOD_ALT)): + self._do_cut() + consumed_event = True + return consumed_event + + def _do_cut(self): + if abs(self.select_range[0] - self.select_range[1]) > 0: + low_end = min(self.select_range[0], self.select_range[1]) + high_end = max(self.select_range[0], self.select_range[1]) + clipboard_copy(self.html_text[low_end:high_end]) + self.text_box_layout.delete_selected_text() + self.edit_position = low_end + self.html_text = self.html_text[:low_end] + self.html_text[high_end:] + self.text_box_layout.set_cursor_position(self.edit_position) + self.select_range = [0, 0] + self.redraw_from_text_block() + self.cursor_has_moved_recently = True + + def _process_copy_event(self, event: Event) -> bool: + """ + Process a copy shortcut event. (CTRL+ C) + + :param event: The event to process. + + :return: True if the event is consumed. + + """ + consumed_event = False + if (event.key == K_c and + (event.mod & KMOD_CTRL or event.mod & KMOD_META) and + not self.is_text_hidden and #This line is not in UITextEntryBoox + not (event.mod & KMOD_ALT)): + self._do_copy() + consumed_event = True + return consumed_event + + def _do_copy(self): + if abs(self.select_range[0] - self.select_range[1]) > 0: + low_end = min(self.select_range[0], self.select_range[1]) + high_end = max(self.select_range[0], self.select_range[1]) + clipboard_copy(self.html_text[low_end:high_end]) + def _process_paste_event(self, event: pygame.event.Event) -> bool: """ Process a paste shortcut event. (CTRL+ V) From 618a640a4e1fd6a69f83fd20d59192a3cf6e8151 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sat, 6 Apr 2024 10:18:00 +0100 Subject: [PATCH 3/5] Add Event imports --- pygame_gui/elements/ui_text_entry_line.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py index eb13abcd..49c8b5f0 100644 --- a/pygame_gui/elements/ui_text_entry_line.py +++ b/pygame_gui/elements/ui_text_entry_line.py @@ -4,6 +4,7 @@ from typing import Union, List, Dict, Optional, Tuple import pygame +from pygame.event import Event, post from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface From f321cef9a86fbf47876b21b46b8124f54437a56a Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sat, 6 Apr 2024 10:20:11 +0100 Subject: [PATCH 4/5] Add all the pygame imports --- pygame_gui/elements/ui_text_entry_line.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py index 49c8b5f0..3ea1e446 100644 --- a/pygame_gui/elements/ui_text_entry_line.py +++ b/pygame_gui/elements/ui_text_entry_line.py @@ -4,6 +4,10 @@ from typing import Union, List, Dict, Optional, Tuple import pygame +from pygame import Rect, MOUSEBUTTONDOWN, MOUSEBUTTONUP, BUTTON_LEFT, KEYDOWN, TEXTINPUT +from pygame import KMOD_SHIFT, KMOD_META, KMOD_CTRL, KMOD_ALT, K_a, K_x, K_c, K_v +from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_HOME, K_END, K_BACKSPACE, K_DELETE, K_RETURN +from pygame import key from pygame.event import Event, post from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface From f72c5a2b7a1229069fcfee487dbe10486c77e240 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sat, 6 Apr 2024 10:30:41 +0100 Subject: [PATCH 5/5] fix errors with cut --- pygame_gui/elements/ui_text_entry_line.py | 46 +++++++++++------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py index 3ea1e446..36ffbd9f 100644 --- a/pygame_gui/elements/ui_text_entry_line.py +++ b/pygame_gui/elements/ui_text_entry_line.py @@ -4,11 +4,8 @@ from typing import Union, List, Dict, Optional, Tuple import pygame -from pygame import Rect, MOUSEBUTTONDOWN, MOUSEBUTTONUP, BUTTON_LEFT, KEYDOWN, TEXTINPUT -from pygame import KMOD_SHIFT, KMOD_META, KMOD_CTRL, KMOD_ALT, K_a, K_x, K_c, K_v -from pygame import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_HOME, K_END, K_BACKSPACE, K_DELETE, K_RETURN -from pygame import key -from pygame.event import Event, post +from pygame import KMOD_META, KMOD_CTRL, KMOD_ALT, K_a, K_x, K_c +from pygame.event import Event from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface @@ -728,7 +725,7 @@ def _process_keyboard_shortcut_event(self, event: pygame.event.Event) -> bool: if self._process_select_all_event(event): consumed_event = True elif self._process_cut_event(event): - consumed_event = True + consumed_event = True elif self._process_copy_event(event): consumed_event = True elif self._process_paste_event(event): @@ -747,16 +744,16 @@ def _process_select_all_event(self, event: Event) -> bool: consumed_event = False if (event.key == K_a and (event.mod & KMOD_CTRL or event.mod & KMOD_META) and - not (event.mod & KMOD_ALT)): # hopefully enable diacritic letters) + not (event.mod & KMOD_ALT)): # hopefully enable diacritic letters self._do_select_all() consumed_event = True return consumed_event def _do_select_all(self): - self.select_range = [0, len(self.html_text)] - self.edit_position = len(self.html_text) + self.select_range = [0, len(self.text)] + self.edit_position = len(self.text) self.cursor_has_moved_recently = True - + def _process_cut_event(self, event: Event) -> bool: """ Process a cut shortcut event. (CTRL+ X) @@ -768,9 +765,9 @@ def _process_cut_event(self, event: Event) -> bool: """ consumed_event = False if (event.key == K_x and - (event.mod & KMOD_CTRL or event.mod & KMOD_META) and - not self.is_text_hidden and # This is not in UITextEntryBox - not (event.mod & KMOD_ALT)): + (event.mod & KMOD_CTRL or event.mod & KMOD_META) and + not self.is_text_hidden and # This is not in UITextEntryBox + not (event.mod & KMOD_ALT)): self._do_cut() consumed_event = True return consumed_event @@ -779,15 +776,18 @@ def _do_cut(self): if abs(self.select_range[0] - self.select_range[1]) > 0: low_end = min(self.select_range[0], self.select_range[1]) high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.html_text[low_end:high_end]) - self.text_box_layout.delete_selected_text() + clipboard_copy(self.text[low_end:high_end]) + if self.drawable_shape is not None: + self.drawable_shape.text_box_layout.delete_selected_text() + self.drawable_shape.apply_active_text_changes() self.edit_position = low_end - self.html_text = self.html_text[:low_end] + self.html_text[high_end:] - self.text_box_layout.set_cursor_position(self.edit_position) + self.text = self.text[:low_end] + self.text[high_end:] + if self.drawable_shape is not None: + self.drawable_shape.text_box_layout.set_cursor_position(self.edit_position) + self.drawable_shape.apply_active_text_changes() self.select_range = [0, 0] - self.redraw_from_text_block() self.cursor_has_moved_recently = True - + def _process_copy_event(self, event: Event) -> bool: """ Process a copy shortcut event. (CTRL+ C) @@ -799,9 +799,9 @@ def _process_copy_event(self, event: Event) -> bool: """ consumed_event = False if (event.key == K_c and - (event.mod & KMOD_CTRL or event.mod & KMOD_META) and - not self.is_text_hidden and #This line is not in UITextEntryBoox - not (event.mod & KMOD_ALT)): + (event.mod & KMOD_CTRL or event.mod & KMOD_META) and + not self.is_text_hidden and # This line is not in UITextEntryBox + not (event.mod & KMOD_ALT)): self._do_copy() consumed_event = True return consumed_event @@ -810,7 +810,7 @@ def _do_copy(self): if abs(self.select_range[0] - self.select_range[1]) > 0: low_end = min(self.select_range[0], self.select_range[1]) high_end = max(self.select_range[0], self.select_range[1]) - clipboard_copy(self.html_text[low_end:high_end]) + clipboard_copy(self.text[low_end:high_end]) def _process_paste_event(self, event: pygame.event.Event) -> bool: """