Repository: MyreMylar/pygame_gui
Pull request #464 (2023-06-08 19:22:27)

Try adjusting ui panel positioning
As title.

Not 100% on this being right, but it was also wrong before. Could use more testing and looking into other containers.


Patch:
diff --git a/pygame_gui/py.typed b/py.typed
similarity index 100%
rename from pygame_gui/py.typed
rename to py.typed
diff --git a/pygame_gui/elements/ui_panel.py b/pygame_gui/elements/ui_panel.py
index dc8a831..911d22a 100644
--- a/pygame_gui/elements/ui_panel.py
+++ b/pygame_gui/elements/ui_panel.py
@@ -87,8 +87,8 @@ class UIPanel(UIElement, IContainerLikeInterface):
         else:
             self.container_margins = margins
 
-        container_rect = pygame.Rect(self.relative_rect.left + self.container_margins['left'],
-                                     self.relative_rect.top + self.container_margins['top'],
+        container_rect = pygame.Rect(self.relative_rect.left,
+                                     self.relative_rect.top,
                                      self.relative_rect.width - (self.container_margins['left'] +
                                                                  self.container_margins['right']),
                                      self.relative_rect.height - (self.container_margins['top'] +
@@ -178,10 +178,7 @@ class UIPanel(UIElement, IContainerLikeInterface):
                                     self.relative_rect.height - (self.container_margins['top'] +
                                                                  self.container_margins['bottom']))
         if new_container_dimensions != self.get_container().get_size():
-            container_rel_pos = (self.relative_rect.x + self.container_margins['left'],
-                                 self.relative_rect.y + self.container_margins['top'])
             self.get_container().set_dimensions(new_container_dimensions)
-            self.get_container().set_relative_position(container_rel_pos)
 
     def set_relative_position(self, position: Union[pygame.math.Vector2,
                                                     Tuple[int, int],
@@ -193,9 +190,7 @@ class UIPanel(UIElement, IContainerLikeInterface):
 
         """
         super().set_relative_position(position)
-        container_rel_pos = (self.relative_rect.x + self.container_margins['left'],
-                             self.relative_rect.y + self.container_margins['top'])
-        self.get_container().set_relative_position(container_rel_pos)
+        self.get_container().set_relative_position(self.relative_rect.topleft)
 
     def set_position(self, position: Union[pygame.math.Vector2,
                                            Tuple[int, int],
@@ -207,9 +202,7 @@ class UIPanel(UIElement, IContainerLikeInterface):
 
         """
         super().set_position(position)
-        container_rel_pos = (self.relative_rect.x + self.container_margins['left'],
-                             self.relative_rect.y + self.container_margins['top'])
-        self.get_container().set_relative_position(container_rel_pos)
+        self.get_container().set_relative_position(self.relative_rect.topleft)
 
     def rebuild_from_changed_theme_data(self):
         """
diff --git a/pygame_gui/elements/ui_text_entry_box.py b/pygame_gui/elements/ui_text_entry_box.py
index 5012416..9c60804 100644
--- a/pygame_gui/elements/ui_text_entry_box.py
+++ b/pygame_gui/elements/ui_text_entry_box.py
@@ -4,7 +4,7 @@ from typing import Union, Tuple, Dict, Optional
 from pygame_gui.core.utility import clipboard_paste, clipboard_copy
 
 from pygame import Rect, MOUSEBUTTONDOWN, MOUSEBUTTONUP, BUTTON_LEFT, KEYDOWN, TEXTINPUT
-from pygame import KMOD_SHIFT, KMOD_META, KMOD_CTRL, K_a, K_x, K_c, K_v
+from pygame import KMOD_META, KMOD_CTRL, 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
@@ -333,140 +333,7 @@ class UITextEntryBox(UITextBox):
 
         """
         consumed_event = False
-        # modded versions of LEFT and RIGHT must go first otherwise the simple
-        # cases will absorb the events
-        if event.key == K_HOME or (event.key == K_LEFT
-            and (event.mod & KMOD_CTRL or event.mod & KMOD_META)
-            ):
-            try:
-                next_pos = self.edit_position - self.html_text[:self.edit_position][::-1].index("\n")
-            except ValueError:
-                next_pos = 0
-            # include case when shift held down to select everything to start
-            # of current line.
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if event.mod & KMOD_SHIFT:
-                    if self.edit_position == self.select_range[1]:
-                        # undo selection to right, create to left
-                        self.select_range = [next_pos, self.select_range[0]]
-                    else:
-                        # extend left
-                        self.select_range = [next_pos, self.select_range[1]]
-                else:
-                    self.select_range = [0, 0]
-            else:
-                if event.mod & KMOD_SHIFT:
-                    self.select_range = [next_pos, self.edit_position]
-                else:
-                    self.select_range = [0, 0]
-            self.edit_position = next_pos
-            self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == K_END or (event.key == K_RIGHT
-            and (event.mod & KMOD_CTRL or event.mod & KMOD_META)
-            ):
-            try:
-                next_pos = self.edit_position + self.html_text[self.edit_position:].index("\n")
-            except ValueError:
-                next_pos = len(self.html_text)
-            # include case when shift held down to select everything to end
-            # of current line.
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if event.mod & KMOD_SHIFT:
-                    if self.edit_position == self.select_range[0]:
-                        # undo selection to left, create to right
-                        self.select_range = [self.select_range[1], next_pos]
-                    else:
-                        # extend right
-                        self.select_range = [self.select_range[0], next_pos]
-                else:
-                    self.select_range = [0, 0]
-            else:
-                if event.mod & KMOD_SHIFT:
-                    self.select_range = [self.edit_position, next_pos]
-                else:
-                    self.select_range = [0, 0]
-            self.edit_position = next_pos
-            self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == K_LEFT and event.mod & KMOD_SHIFT:
-            # keyboard-based selection
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                # existing selection, so edit_position should correspond to one
-                # end of it
-                if self.edit_position == self.select_range[0]:
-                    # try to extend to the left
-                    self.select_range = [max(0, self.edit_position - 1),
-                                         self.select_range[1]]
-                elif self.edit_position == self.select_range[1]:
-                    # reduce to the left
-                    self.select_range = [self.select_range[0],
-                                         max(0, self.edit_position - 1)]
-            else:
-                self.select_range = [max(0, self.edit_position - 1),
-                                     self.edit_position]
-            if self.edit_position > 0:
-                self.edit_position -= 1
-                self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == K_RIGHT and event.mod & KMOD_SHIFT:
-            # keyboard-based selection
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if self.edit_position == self.select_range[1]:
-                    # try to extend to the right
-                    self.select_range = [self.select_range[0],
-                                         min(len(self.html_text), self.edit_position + 1)]
-                elif self.edit_position == self.select_range[0]:
-                    # reduce to the right
-                    self.select_range = [min(len(self.html_text), self.edit_position + 1),
-                                         self.select_range[1]]
-            else:
-                self.select_range = [self.edit_position, min(len(self.html_text),
-                                                             self.edit_position + 1)]
-            if self.edit_position < len(self.html_text):
-                self.edit_position += 1
-                self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == K_UP and event.mod & KMOD_SHIFT:
-            # keyboard-based selection
-            new_pos = self.text_box_layout.get_cursor_pos_move_up_one_row(self.last_horiz_cursor_index)
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                # existing selection, so edit_position should correspond to one
-                # end of it
-                if self.edit_position == self.select_range[0]:
-                    # try to extend to the left
-                    self.select_range = [max(0, new_pos),
-                                         self.select_range[1]]
-                elif self.edit_position == self.select_range[1]:
-                    # reduce to the left
-                    self.select_range = [self.select_range[0],
-                                         max(0, new_pos)]
-            else:
-                self.select_range = [max(0, new_pos),
-                                     self.edit_position]
-            if self.edit_position > 0:
-                self.edit_position = new_pos
-                self.cursor_has_moved_recently = True
-        elif event.key == K_DOWN and event.mod & KMOD_SHIFT:
-            # keyboard-based selection
-            new_pos = self.text_box_layout.get_cursor_pos_move_down_one_row(self.last_horiz_cursor_index)
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if self.edit_position == self.select_range[1]:
-                    # try to extend to the right
-                    self.select_range = [self.select_range[0],
-                                                 min(len(self.html_text), new_pos)]
-                elif self.edit_position == self.select_range[0]:
-                    # reduce to the right
-                    self.select_range = [min(len(self.html_text), new_pos),
-                                                 self.select_range[1]]
-            else:
-                self.select_range = [self.edit_position, min(len(self.html_text),
-                                                                     new_pos)]
-            if self.edit_position < len(self.html_text):
-                self.edit_position = new_pos
-                self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == K_LEFT:
+        if event.key == K_LEFT:
             if abs(self.select_range[0] - self.select_range[1]) > 0:
                 self.edit_position = min(self.select_range[0], self.select_range[1])
                 self.select_range = [0, 0]
@@ -682,9 +549,8 @@ class UITextEntryBox(UITextBox):
                     self.html_text = (self.html_text[:self.edit_position] +
                                       new_text +
                                       self.html_text[self.edit_position:])
-                    original_edit_pos = self.edit_position
                     self.set_text(self.html_text)
-                    self.edit_position = original_edit_pos + len(new_text)
+                    self.edit_position += len(new_text)
                     self.text_box_layout.set_cursor_position(self.edit_position)
                     self.redraw_from_text_block()
                     self.cursor_has_moved_recently = True
@@ -740,3 +606,4 @@ class UITextEntryBox(UITextBox):
     @staticmethod
     def convert_all_line_endings_to_unix(input_str: str):
         return input_str.replace('\r\n', '\n').replace('\r', '\n')
+
diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py
index e56255f..01fad4f 100644
--- a/pygame_gui/elements/ui_text_entry_line.py
+++ b/pygame_gui/elements/ui_text_entry_line.py
@@ -579,91 +579,7 @@ class UITextEntryLine(UIElement):
 
         """
         consumed_event = False
-        # modded versions of LEFT and RIGHT must go first otherwise the simple
-        # cases will absorb the events
-        if event.key == pygame.K_HOME or (event.key == pygame.K_LEFT
-            and (event.mod & pygame.KMOD_CTRL or event.mod & pygame.KMOD_META)
-            ):
-            # include case when shift held down to select everything to start
-            # of current line.
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if event.mod & pygame.KMOD_SHIFT:
-                    if self.edit_position == self.select_range[1]:
-                        # undo selection to right, create to left
-                        self.select_range = [0, self.select_range[0]]
-                    else:
-                        # extend left
-                        self.select_range = [0, self.select_range[1]]
-                else:
-                    self.select_range = [0, 0]
-            else:
-                if event.mod & pygame.KMOD_SHIFT:
-                    self.select_range = [0, self.edit_position]
-                else:
-                    self.select_range = [0, 0]
-            self.edit_position = 0
-            self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == pygame.K_END or (event.key == pygame.K_RIGHT
-            and (event.mod & pygame.KMOD_CTRL or event.mod & pygame.KMOD_META)
-            ):
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if event.mod & pygame.KMOD_SHIFT:
-                    if self.edit_position == self.select_range[0]:
-                        # undo selection to left, create to right
-                        self.select_range = [self.select_range[1], len(self.text)]
-                    else:
-                        # extend right
-                        self.select_range = [self.select_range[0], len(self.text)]
-                else:
-                    self.select_range = [0, 0]
-            else:
-                if event.mod & pygame.KMOD_SHIFT:
-                    self.select_range = [self.edit_position, len(self.text)]
-                else:
-                    self.select_range = [0, 0]
-            self.edit_position = len(self.text)
-            self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == pygame.K_LEFT and event.mod & pygame.KMOD_SHIFT:
-            # keyboard-based selection
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                # existing selection, so edit_position should correspond to one
-                # end of it
-                if self.edit_position == self.select_range[0]:
-                    # try to extend to the left
-                    self.select_range = [max(0, self.edit_position - 1),
-                                         self.select_range[1]]
-                elif self.edit_position == self.select_range[1]:
-                    # reduce to the left
-                    self.select_range = [self.select_range[0], max(0,
-                                                    self.edit_position - 1)]
-            else:
-                self.select_range = [max(0, self.edit_position - 1),
-                                     self.edit_position]
-            if self.edit_position > 0:
-                self.edit_position -= 1
-                self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == pygame.K_RIGHT and event.mod & pygame.KMOD_SHIFT:
-            # keyboard-based selection
-            if abs(self.select_range[0] - self.select_range[1]) > 0:
-                if self.edit_position == self.select_range[1]:
-                    # try to extend to the right
-                    self.select_range = [self.select_range[0],
-                                         min(len(self.text), self.edit_position + 1)]
-                elif self.edit_position == self.select_range[0]:
-                    # reduce to the right
-                    self.select_range = [min(len(self.text), self.edit_position + 1),
-                                         self.select_range[1]]
-            else:
-                self.select_range = [self.edit_position, min(len(self.text),
-                                                    self.edit_position + 1)]
-            if self.edit_position < len(self.text):
-                self.edit_position += 1
-                self.cursor_has_moved_recently = True
-            consumed_event = True
-        elif event.key == pygame.K_LEFT:
+        if event.key == pygame.K_LEFT:
             if abs(self.select_range[0] - self.select_range[1]) > 0:
                 self.edit_position = min(self.select_range[0], self.select_range[1])
                 self.select_range = [0, 0]


Test patch:
diff --git a/tests/test_elements/test_ui_text_entry_box.py b/tests/test_elements/test_ui_text_entry_box.py
index 254a622..9b1fced 100644
--- a/tests/test_elements/test_ui_text_entry_box.py
+++ b/tests/test_elements/test_ui_text_entry_box.py
@@ -775,7 +775,7 @@ class TestUITextEntryBox:
                                   manager=default_ui_manager)
         assert text_box.image is not None
         assert len(text_box.text_box_layout.layout_rows) == 3
-
+        
     def test_rebuild_select_area_1(self, _init_pygame, default_ui_manager,
                                    _display_surface_return_none):
         text_entry = UITextEntryBox(relative_rect=pygame.Rect(100, 100, 200, 30),
@@ -1148,8 +1148,7 @@ class TestUITextEntryBox:
         text_entry.focus()
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
@@ -1164,8 +1163,7 @@ class TestUITextEntryBox:
         text_entry.focus()
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
@@ -1179,8 +1177,7 @@ class TestUITextEntryBox:
         assert text_entry.edit_position == 3
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_LEFT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_LEFT}))
 
         assert processed_key_event
         assert text_entry.edit_position == 2
@@ -1195,26 +1192,12 @@ class TestUITextEntryBox:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_HOME,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_HOME}))
 
         assert processed_key_event
         assert text_entry.select_range == [0, 0]
         assert text_entry.edit_position == 0
 
-        text_entry.set_text('daniel\nmulti-line\ntext')
-        text_entry.edit_position = 3
-        text_entry.focus()
-        text_entry.select_range = [1, 3]
-
-        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_HOME,
-                                                                           'mod': pygame.KMOD_SHIFT}))
-
-        assert processed_key_event
-        assert text_entry.select_range == [0, 1]
-        assert text_entry.edit_position == 0
-
     def test_process_event_end(self, _init_pygame: None, default_ui_manager: UIManager,
                                _display_surface_return_none: None):
         text_entry = UITextEntryBox(relative_rect=pygame.Rect(100, 100, 200, 30),
@@ -1225,26 +1208,12 @@ class TestUITextEntryBox:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_END,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_END}))
 
         assert processed_key_event
         assert text_entry.select_range == [0, 0]
         assert text_entry.edit_position == 3
 
-        text_entry.set_text('daniel\nmulti-line\ntext')
-        text_entry.edit_position = 3
-        text_entry.focus()
-        text_entry.select_range = [1, 3]
-
-        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_END,
-                                                                           'mod': pygame.KMOD_SHIFT}))
-
-        assert processed_key_event
-        assert text_entry.select_range == [1, len("daniel")]
-        assert text_entry.edit_position == len("daniel")
-
     def test_process_event_text_right_select_range(self, _init_pygame: None,
                                                    default_ui_manager: UIManager,
                                                    _display_surface_return_none: None):
@@ -1256,8 +1225,7 @@ class TestUITextEntryBox:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
@@ -1272,8 +1240,7 @@ class TestUITextEntryBox:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_LEFT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_LEFT}))
 
         assert processed_key_event
 
diff --git a/tests/test_elements/test_ui_text_entry_line.py b/tests/test_elements/test_ui_text_entry_line.py
index 3aa3f93..b7fd453 100644
--- a/tests/test_elements/test_ui_text_entry_line.py
+++ b/tests/test_elements/test_ui_text_entry_line.py
@@ -651,22 +651,10 @@ class TestUITextEntryLine:
         text_entry.focus()
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
-        text_entry.edit_position = 1
-        text_entry.select_range = [1, 3]
-        text_entry.focus()
-
-        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': pygame.KMOD_SHIFT}))
-        assert processed_key_event
-        assert text_entry.select_range == [2, 3]
-        assert text_entry.edit_position == 2
-
     def test_process_event_text_right_actually_move(self, _init_pygame: None,
                                                     default_ui_manager: UIManager,
                                                     _display_surface_return_none: None):
@@ -678,8 +666,7 @@ class TestUITextEntryLine:
         text_entry.focus()
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
@@ -693,8 +680,7 @@ class TestUITextEntryLine:
         assert text_entry.edit_position == 3
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_LEFT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_LEFT}))
 
         assert processed_key_event
         assert text_entry.edit_position == 2
@@ -709,8 +695,7 @@ class TestUITextEntryLine:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_HOME,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_HOME}))
 
         assert processed_key_event
         assert text_entry.select_range == [0, 0]
@@ -726,25 +711,12 @@ class TestUITextEntryLine:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_END,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_END}))
 
         assert processed_key_event
         assert text_entry.select_range == [0, 0]
         assert text_entry.edit_position == 3
 
-        text_entry.edit_position = 1
-        text_entry.select_range = [1, 3]
-        text_entry.focus()
-
-        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_END,
-                                                                           'mod': pygame.KMOD_SHIFT}))
-        assert processed_key_event
-        assert text_entry.select_range == [3, 3]
-        assert text_entry.edit_position == 3
-
-
     def test_process_event_text_right_select_range(self, _init_pygame: None,
                                                    default_ui_manager: UIManager,
                                                    _display_surface_return_none: None):
@@ -756,8 +728,7 @@ class TestUITextEntryLine:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_RIGHT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_RIGHT}))
 
         assert processed_key_event
 
@@ -772,8 +743,7 @@ class TestUITextEntryLine:
         text_entry.select_range = [0, 2]
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
-                                                                          {'key': pygame.K_LEFT,
-                                                                           'mod': 0}))
+                                                                          {'key': pygame.K_LEFT}))
 
         assert processed_key_event