Skip to content

Commit

Permalink
Merge pull request #556 from JordanKarp/main
Browse files Browse the repository at this point in the history
UITextEntryBox text editing shortcuts into sub functions
  • Loading branch information
MyreMylar authored Apr 6, 2024
2 parents 61a6ea5 + f72c5a2 commit 8d48670
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 57 deletions.
97 changes: 74 additions & 23 deletions pygame_gui/elements/ui_text_entry_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
121 changes: 87 additions & 34 deletions pygame_gui/elements/ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Union, List, Dict, Optional, Tuple

import pygame
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

Expand Down Expand Up @@ -725,45 +727,96 @@ 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.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)
: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.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

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 UITextEntryBox
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.text[low_end:high_end])

def _process_paste_event(self, event: pygame.event.Event) -> bool:
"""
Process a paste shortcut event. (CTRL+ V)
Expand Down

0 comments on commit 8d48670

Please sign in to comment.