diff --git a/pygame_gui/elements/ui_window.py b/pygame_gui/elements/ui_window.py index 7b6f584e..12755d2f 100644 --- a/pygame_gui/elements/ui_window.py +++ b/pygame_gui/elements/ui_window.py @@ -30,6 +30,7 @@ class UIWindow(UIElement, IContainerLikeInterface, IWindowInterface): :param resizable: Whether this window is resizable or not, defaults to False. :param visible: Whether the element is visible by default. Warning - container visibility may override this. + :param draggable: Whether this window is draggable or not, defaults to True. """ def __init__(self, @@ -280,17 +281,17 @@ def update(self, time_delta: float): self.starting_grab_difference = (mouse_x - self.rect.x, mouse_y - self.rect.y) - current_grab_difference = (mouse_x - self.rect.x, - mouse_y - self.rect.y) + if self.draggable: + current_grab_difference = (mouse_x - self.rect.x, + mouse_y - self.rect.y) - adjustment_required = (current_grab_difference[0] - - self.starting_grab_difference[0], - current_grab_difference[1] - - self.starting_grab_difference[1]) + adjustment_required = (current_grab_difference[0] - + self.starting_grab_difference[0], + current_grab_difference[1] - + self.starting_grab_difference[1]) - if self.draggable: self.set_relative_position((self.relative_rect.x + adjustment_required[0], - self.relative_rect.y + adjustment_required[1])) + self.relative_rect.y + adjustment_required[1])) else: self.grabbed_window = False diff --git a/tests/test_elements/test_ui_window.py b/tests/test_elements/test_ui_window.py index 3d25d105..6ec3184c 100644 --- a/tests/test_elements/test_ui_window.py +++ b/tests/test_elements/test_ui_window.py @@ -235,6 +235,28 @@ def test_update(self, _init_pygame, default_ui_manager, default_ui_manager.mouse_position = (25, 25) window.update(time_delta=0.05) + window = UIWindow(pygame.Rect(0, 0, 200, 200), window_display_title="Test Window", + manager=default_ui_manager, draggable=True) + + window.title_bar.held = True + default_ui_manager.mouse_position = (100, 10) + window.update(time_delta=0.05) + default_ui_manager.mouse_position = (150, 10) + window.update(time_delta=0.05) + + assert window.get_relative_rect().topleft == (50, 0) + + window = UIWindow(pygame.Rect(0, 0, 200, 200), window_display_title="Test Window", + manager=default_ui_manager, draggable=False) + + window.title_bar.held = True + default_ui_manager.mouse_position = (100, 10) + window.update(time_delta=0.05) + default_ui_manager.mouse_position = (150, 10) + window.update(time_delta=0.05) + + assert window.get_relative_rect().topleft == (0, 0) + def test_check_hover(self, _init_pygame, default_ui_manager: UIManager, _display_surface_return_none: None): window = UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window",