Repository: MyreMylar/pygame_gui
Pull request #316 (2022-08-14 19:27:03)

Add clear() to text box and text line
- Also add initial text option to text line


Patch:
diff --git a/pygame_gui/elements/ui_text_box.py b/pygame_gui/elements/ui_text_box.py
index 8b514e9..5cc3659 100644
--- a/pygame_gui/elements/ui_text_box.py
+++ b/pygame_gui/elements/ui_text_box.py
@@ -1055,3 +1055,6 @@ class UITextBox(UIElement, IUITextOwnerInterface):
         self.html_text = html_text
         self.appended_text = ""  # clear appended text as it feels odd to set the text and still have appended text
         self._reparse_and_rebuild()
+
+    def clear(self):
+        self.set_text("")
diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py
index 8a84962..a3e0c65 100644
--- a/pygame_gui/elements/ui_text_entry_line.py
+++ b/pygame_gui/elements/ui_text_entry_line.py
@@ -37,6 +37,10 @@ class UITextEntryLine(UIElement):
     :param anchors: A dictionary describing what this element's relative_rect is relative to.
     :param visible: Whether the element is visible by default. Warning - container visibility
                     may override this.
+    :param: initial_text: A string that will start in this box. This text will remain when box is focused
+                          for editing.
+    :param: placeholder_text: If the text line is empty, and not focused, this placeholder text will be
+                              shown instead.
     """
 
     _number_character_set = {'en': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']}
@@ -70,6 +74,7 @@ class UITextEntryLine(UIElement):
                  anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1,
                  *,
+                 initial_text: Optional[str] = None,
                  placeholder_text: Optional[str] = None):
 
         super().__init__(relative_rect, manager, container,
@@ -82,6 +87,8 @@ class UITextEntryLine(UIElement):
                                element_id='text_entry_line')
 
         self.text = ""
+        if initial_text is not None:
+            self.text = initial_text
         self.is_text_hidden = False
         self.hidden_text_char = '●'
         self.placeholder_text = ""
@@ -1015,3 +1022,6 @@ class UITextEntryLine(UIElement):
         else:
             if self.drawable_shape is not None:
                 self.drawable_shape.set_text(translate(self.text))
+
+    def clear(self):
+        self.set_text("")


Test patch:
diff --git a/tests/test_elements/test_ui_text_box.py b/tests/test_elements/test_ui_text_box.py
index 7a7e5b3..d33d7c8 100644
--- a/tests/test_elements/test_ui_text_box.py
+++ b/tests/test_elements/test_ui_text_box.py
@@ -10,6 +10,7 @@ from tests.shared_comparators import compare_surfaces
 
 from pygame_gui import UITextEffectType
 
+
 class TestUITextBox:
 
     def test_creation(self, _init_pygame: None,
@@ -23,6 +24,34 @@ class TestUITextBox:
                              manager=default_ui_manager)
         assert text_box.image is not None
 
+    def test_set_text(self, _init_pygame: None,
+                      default_ui_manager: UIManager,
+                      _display_surface_return_none):
+        default_ui_manager.preload_fonts([{"name": "fira_code", "size:": 14, "style": "bold"},
+                                          {"name": "fira_code", "size:": 14, "style": "italic"}])
+        text_box = UITextBox(html_text="<font color=#FF0000>Some text</font> in a <b>bold box</b> using colours and "
+                                       "<i>styles</i>.",
+                             relative_rect=pygame.Rect(100, 100, 200, 300),
+                             manager=default_ui_manager)
+        assert text_box.image is not None
+
+        text_box.set_text("<b>Changed text</b>")
+        assert text_box.image is not None
+
+    def test_clear(self, _init_pygame: None,
+                   default_ui_manager: UIManager,
+                   _display_surface_return_none):
+        default_ui_manager.preload_fonts([{"name": "fira_code", "size:": 14, "style": "bold"},
+                                          {"name": "fira_code", "size:": 14, "style": "italic"}])
+        text_box = UITextBox(html_text="<font color=#FF0000>Some text</font> in a <b>bold box</b> using colours and "
+                                       "<i>styles</i>.",
+                             relative_rect=pygame.Rect(100, 100, 200, 300),
+                             manager=default_ui_manager)
+        assert text_box.image is not None
+
+        text_box.clear()
+        assert text_box.image is not None
+
     def test_creation_grow_to_fit_width(self, _init_pygame: None,
                                         default_ui_manager: UIManager,
                                         _display_surface_return_none):
diff --git a/tests/test_elements/test_ui_text_entry_line.py b/tests/test_elements/test_ui_text_entry_line.py
index 4523621..0338dd5 100644
--- a/tests/test_elements/test_ui_text_entry_line.py
+++ b/tests/test_elements/test_ui_text_entry_line.py
@@ -42,6 +42,25 @@ class TestUITextEntryLine:
 
         assert text_entry.drawable_shape.theming['text'] == "Enter name..."
 
+    def test_initial_text(self, _init_pygame, default_ui_manager):
+        text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
+                                     manager=default_ui_manager,
+                                     initial_text="Bob")
+        assert text_entry.image is not None
+        assert text_entry.drawable_shape.theming['text'] == "Bob"
+
+    def test_clear(self, _init_pygame, default_ui_manager):
+        text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
+                                     manager=default_ui_manager,
+                                     initial_text="Bob")
+        assert text_entry.image is not None
+        assert text_entry.drawable_shape.theming['text'] == "Bob"
+
+        text_entry.clear()
+
+        assert text_entry.image is not None
+        assert text_entry.drawable_shape.theming['text'] == ""
+
     def test_set_text_length_limit(self, _init_pygame, default_ui_manager):
         text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
                                      manager=default_ui_manager)