-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_sync.py
More file actions
90 lines (68 loc) · 2.52 KB
/
_sync.py
File metadata and controls
90 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from . import LinearSelector
from typing import *
class Synchronizer:
def __init__(
self, *selectors: LinearSelector, key_bind: Union[str, None] = "Shift"
):
"""
Synchronize the movement of `Selectors`. Selectors will move in sync only when the selected `"key_bind"` is
used during the mouse movement event. Valid key binds are: ``"Control"``, ``"Shift"`` and ``"Alt"``.
If ``key_bind`` is ``None`` then the selectors will always be synchronized.
Parameters
----------
selectors
selectors to synchronize
key_bind: str, default ``"Shift"``
one of ``"Control"``, ``"Shift"`` and ``"Alt"`` or ``None``
"""
self._selectors = list()
self.key_bind = key_bind
for s in selectors:
self.add(s)
self.block_event = False
self.enabled: bool = True
@property
def selectors(self):
"""Selectors managed by the Synchronizer"""
return self._selectors
def add(self, selector):
"""add a selector"""
selector.selection.add_event_handler(self._handle_event)
self._selectors.append(selector)
def remove(self, selector):
"""remove a selector"""
selector.selection.remove_event_handler(self._handle_event)
self._selectors.remove(selector)
def clear(self):
for i in range(len(self.selectors)):
self.remove(self.selectors[0])
def _handle_event(self, ev):
if self.block_event:
# because infinite recursion
return
if not self.enabled:
return
self.block_event = True
source = ev.pick_info["graphic"]
delta = ev.pick_info["delta"]
pygfx_ev = ev.pick_info["pygfx_event"]
# only moves when modifier is used
if pygfx_ev is None:
self.block_event = False
return
if self.key_bind is not None:
if self.key_bind not in pygfx_ev.modifiers:
self.block_event = False
return
if delta is not None:
self._move_selectors(source, delta)
self.block_event = False
def _move_selectors(self, source, delta):
for s in self.selectors:
# must use == and not is to compare Graphics because they are weakref proxies!
if s == source:
# if it's the source, since it has already moved
continue
s._move_graphic(delta)
def __del__(self):
self.clear()