5
5
from pyglet .event import EVENT_HANDLED , EVENT_UNHANDLED
6
6
from typing_extensions import override
7
7
8
- from arcade .gui .events import UIMouseDragEvent , UIMouseEvent
8
+ import arcade
9
+ from arcade .gui .events import UIMouseDragEvent , UIMouseEvent , UIMousePressEvent , UIMouseReleaseEvent
9
10
from arcade .gui .widgets import UILayout , UIWidget
10
11
11
12
@@ -17,8 +18,7 @@ class UIDraggableMixin(UILayout):
17
18
class DraggablePane(UITexturePane, UIDraggableMixin):
18
19
...
19
20
20
- This does overwrite :class:`UILayout` behavior which position themselves,
21
- like :class:`UIAnchorWidget`
21
+ This will not work when placed in a layout, as the layout will overwrite the position.
22
22
23
23
warning:
24
24
@@ -27,33 +27,37 @@ class DraggablePane(UITexturePane, UIDraggableMixin):
27
27
It does not respect the layout system and can break other widgets
28
28
which rely on the layout system.
29
29
30
- Further the dragging is not smooth, as it uses a very simple approach.
31
-
32
- Will be fixed in future versions, but might break existing code within a minor update.
33
-
34
30
"""
35
31
32
+ _dragging = False
33
+
36
34
@override
37
35
def do_layout (self ):
38
- # FIXME this breaks all rules, let us not do this
39
-
40
- # Preserve top left alignment, this overwrites self placing behavior like
41
- # from :class:`UIAnchorWidget`
36
+ # FIXME this breaks core UI rules "Widgets are placed by parents", let us not do this
42
37
rect = self .rect
43
38
super ().do_layout ()
44
39
self .rect = self .rect .align_top (rect .top ).align_left (rect .left )
45
40
46
41
@override
47
42
def on_event (self , event ) -> Optional [bool ]:
48
43
"""Handle dragging of the widget."""
49
- if isinstance (event , UIMouseDragEvent ) and self .rect .point_in_rect (event .pos ):
44
+ if isinstance (event , UIMousePressEvent ):
45
+ if event .button == arcade .MOUSE_BUTTON_LEFT and self .rect .point_in_rect (event .pos ):
46
+ self ._dragging = True
47
+ return EVENT_HANDLED
48
+
49
+ if isinstance (event , UIMouseDragEvent ) and self ._dragging :
50
50
self .rect = self .rect .move (event .dx , event .dy )
51
51
self .trigger_full_render ()
52
-
53
- if super ().on_event (event ):
54
52
return EVENT_HANDLED
55
53
56
- return EVENT_UNHANDLED
54
+ if isinstance (event , UIMouseReleaseEvent ):
55
+ if event .button == arcade .MOUSE_BUTTON_LEFT and self ._dragging :
56
+ self ._dragging = False
57
+ self .trigger_full_render ()
58
+ return EVENT_HANDLED
59
+
60
+ return super ().on_event (event )
57
61
58
62
59
63
class UIMouseFilterMixin (UIWidget ):
0 commit comments