Skip to content

gui/improve UIDraggableMixin and update doc strings #2510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions arcade/gui/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from pyglet.event import EVENT_HANDLED, EVENT_UNHANDLED
from typing_extensions import override

from arcade.gui.events import UIMouseDragEvent, UIMouseEvent
import arcade
from arcade.gui.events import UIMouseDragEvent, UIMouseEvent, UIMousePressEvent, UIMouseReleaseEvent
from arcade.gui.widgets import UILayout, UIWidget


Expand All @@ -17,8 +18,7 @@ class UIDraggableMixin(UILayout):
class DraggablePane(UITexturePane, UIDraggableMixin):
...

This does overwrite :class:`UILayout` behavior which position themselves,
like :class:`UIAnchorWidget`
This will not work when placed in a layout, as the layout will overwrite the position.

warning:

Expand All @@ -27,33 +27,37 @@ class DraggablePane(UITexturePane, UIDraggableMixin):
It does not respect the layout system and can break other widgets
which rely on the layout system.

Further the dragging is not smooth, as it uses a very simple approach.

Will be fixed in future versions, but might break existing code within a minor update.

"""

_dragging = False

@override
def do_layout(self):
# FIXME this breaks all rules, let us not do this

# Preserve top left alignment, this overwrites self placing behavior like
# from :class:`UIAnchorWidget`
# FIXME this breaks core UI rules "Widgets are placed by parents", let us not do this
rect = self.rect
super().do_layout()
self.rect = self.rect.align_top(rect.top).align_left(rect.left)

@override
def on_event(self, event) -> Optional[bool]:
"""Handle dragging of the widget."""
if isinstance(event, UIMouseDragEvent) and self.rect.point_in_rect(event.pos):
if isinstance(event, UIMousePressEvent):
if event.button == arcade.MOUSE_BUTTON_LEFT and self.rect.point_in_rect(event.pos):
self._dragging = True
return EVENT_HANDLED

if isinstance(event, UIMouseDragEvent) and self._dragging:
self.rect = self.rect.move(event.dx, event.dy)
self.trigger_full_render()

if super().on_event(event):
return EVENT_HANDLED

return EVENT_UNHANDLED
if isinstance(event, UIMouseReleaseEvent):
if event.button == arcade.MOUSE_BUTTON_LEFT and self._dragging:
self._dragging = False
self.trigger_full_render()
return EVENT_HANDLED

return super().on_event(event)


class UIMouseFilterMixin(UIWidget):
Expand Down
Loading