Skip to content

Commit

Permalink
Merge pull request #387 from MyreMylar/document-draggable
Browse files Browse the repository at this point in the history
Add documentation and tests for draggable param for UIWindow
  • Loading branch information
MyreMylar authored Dec 31, 2022
2 parents 0abe9c1 + f14e7a7 commit b218330
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions tests/test_elements/test_ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b218330

Please sign in to comment.