-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy path_cursor.py
More file actions
420 lines (327 loc) · 13.1 KB
/
_cursor.py
File metadata and controls
420 lines (327 loc) · 13.1 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from functools import partial
from typing import Literal, Sequence, Callable
import numpy as np
import pygfx
from ..layouts import Subplot
from ..utils import RenderQueue
class Cursor:
def __init__(
self,
mode: Literal["crosshair", "marker"] = "crosshair",
size: float = 1.0, # in screen space
color: str | Sequence[float] | pygfx.Color | np.ndarray = "w",
marker: str = "+",
edge_color: str | Sequence[float] | pygfx.Color | np.ndarray = "k",
edge_width: float = 0.5,
alpha: float = 0.7,
size_space: Literal["screen", "world"] = "screen",
):
"""
A cursor that indicates the same position in world-space across subplots.
Parameters
----------
mode: "crosshair" | "marker"
cursor mode
size: float, default 1.0
* if ``mode`` == 'crosshair', this is the crosshair line thickness
* if ``mode`` == 'marker', it's the size of the marker
You probably want to use ``size > 5`` if ``mode`` is 'marker' and ``size_space`` is ``screen``
color: str | Sequence[float] | pygfx.Color | np.ndarray, default "r"
color of the marker
marker: str, default "+"
marker shape, used if mode == 'marker'
edge_color: str | Sequence[float] | pygfx.Color | np.ndarray, default "k"
marker edge color, used if ``mode`` == 'marker'
edge_width: float, default 0.5
marker edge widget, used if ``mode`` == 'marker'
alpha: float, default 0.7
alpha (transparency) of the cursor
size_space: "screen" | "world", default "screen"
size space of the cursor, if "screen" the ``size`` is exact screen pixels.
if "world" the ``size`` is in world-space
"""
self._cursors: dict[Subplot, pygfx.Points | pygfx.Group[pygfx.Line]] = dict()
self._transforms: dict[Subplot, Callable | None] = dict()
self._mode = None
self.mode = mode
self.size = size
self.color = color
self.marker = marker
self.edge_color = edge_color
self.edge_width = edge_width
self.alpha = alpha
self.size_space = size_space
self._enabled = True
self._position: list[float, float] = [0.0, 0.0]
@property
def mode(self) -> Literal["crosshair", "marker"]:
"""cursor mode, one of 'crosshair' or 'marker'"""
return self._mode
@mode.setter
def mode(self, mode: Literal["crosshair", "marker"]):
if not (mode == "crosshair" or mode == "marker"):
raise ValueError(
f"mode must be one of: 'crosshair' | 'marker', you passed: {mode}"
)
if mode == self.mode:
return
# mode has changed, clear and create new world objects
subplots = list(self._cursors.keys())
self.clear()
for subplot in subplots:
self.add_subplot(subplot)
self._mode = mode
@property
def size(self) -> float:
"""size of marker or crosshair line thickness"""
return self._size
@size.setter
def size(self, new_size: float):
for c in self._cursors.values():
if self.mode == "marker":
c.material.size = new_size
elif self.mode == "crosshair":
h, v = c.children
h.material.thickness = new_size
v.material.thickness = new_size
self._size = new_size
@property
def size_space(self) -> Literal["screen", "world"]:
"""interpret cursor size in screen or world space"""
return self._size_space
@size_space.setter
def size_space(self, space: Literal["screen", "world"]):
if space not in ["screen", "world", "model"]:
raise ValueError(
f"valid `size_space` is one of: 'screen' | 'world'. You passed: {space}"
)
for c in self._cursors.values():
if self.mode == "marker":
c.material.size_space = space
elif self.mode == "crosshair":
h, v = c.children
h.material.thickness_space = space
v.material.thickness_space = space
self._size_space = space
@property
def color(self) -> pygfx.Color:
"""cursor color"""
return self._color
@color.setter
def color(self, new_color):
new_color = pygfx.Color(new_color)
for c in self._cursors.values():
c.material.color = new_color
self._color = new_color
@property
def marker(self) -> str:
"""cursor marker shape, if `mode` is 'marker'"""
return self._marker
@marker.setter
def marker(self, new_marker: str):
if self.mode == "marker":
for c in self._cursors.values():
c.material.marker = new_marker
self._marker = new_marker
@property
def edge_color(self) -> pygfx.Color:
"""cursor marker edge color, if `mode` is 'marker'"""
return self._edge_color
@edge_color.setter
def edge_color(self, new_color: str | Sequence | np.ndarray | pygfx.Color):
new_color = pygfx.Color(new_color)
if self.mode == "marker":
for c in self._cursors.values():
c.material.edge_color = new_color
self._edge_color = new_color
@property
def edge_width(self) -> float:
"""cursor marker edge width, if `mode` is 'marker'"""
return self._edge_width
@edge_width.setter
def edge_width(self, new_width: float):
if self.mode == "marker":
for c in self._cursors.values():
c.material.edge_width = new_width
self._edge_width = new_width
@property
def alpha(self) -> float:
"""cursor alpha value"""
return self._alpha
@alpha.setter
def alpha(self, value: float):
for c in self._cursors.values():
c.material.opacity = value
self._alpha = value
@property
def enabled(self) -> bool:
"""enable/disable the cursor, if False the cursor will not respond to mouse pointer events"""
return self._enabled
@enabled.setter
def enabled(self, pause: bool):
self._enabled = bool(pause)
@property
def position(self) -> tuple[float, float]:
"""(x, y) position in world space"""
return tuple(self._position)
@position.setter
def position(self, pos: tuple[float, float]):
for subplot, cursor in self._cursors.items():
if self._transforms[subplot] is not None:
pos_transformed = self._transforms[subplot](pos)
else:
pos_transformed = pos
if self.mode == "marker":
cursor.geometry.positions.data[0, :-1] = pos_transformed
cursor.geometry.positions.update_full()
elif self.mode == "crosshair":
line_h, line_v = cursor.children
# set x vals for horizontal line
line_h.geometry.positions.data[0, 0] = pos_transformed[0] - 1
line_h.geometry.positions.data[1, 0] = pos[0] + 1
# set y value
line_h.geometry.positions.data[:, 1] = pos_transformed[1]
line_h.geometry.positions.update_full()
# set y vals for vertical line
line_v.geometry.positions.data[0, 1] = pos_transformed[1] - 1
line_v.geometry.positions.data[1, 1] = pos_transformed[1] + 1
# set x value
line_v.geometry.positions.data[:, 0] = pos_transformed[0]
line_v.geometry.positions.update_full()
# set tooltip using pick info if a graphic is at this position
# for now we just set z = 1
screen_pos = subplot.map_world_to_screen((*pos_transformed, 1))
pick_info = subplot.get_pick_info(screen_pos)
self._position[:] = pos_transformed
if pick_info is not None:
graphic = pick_info["graphic"]
if (
graphic._fpl_support_tooltip
): # some graphics don't support tooltips, ex: Text
if graphic.tooltip_format is not None:
# custom formatter
info = graphic.tooltip_format(pick_info)
else:
# default formatter for this graphic
info = graphic.format_pick_info(pick_info)
subplot.tooltip.display(screen_pos, info)
continue
# tooltip cleared if none of the above condiitionals reached the tooltip display call
subplot.tooltip.clear()
def add_subplot(self, subplot: Subplot, transform: Callable | None = None):
"""
Add a subplot to this cursor, with an optional position transform function
Parameters
----------
subplot: Subplot
subplot to add
transform: Callable[[tuple[float, float]], tuple[float, float]] | None
a transform function that takes the cursor's position and returns a transformed
position at which the cursor will visually appear.
"""
if subplot in self._cursors.keys():
raise KeyError(f"The given subplot has already been added to this cursor")
if (not callable(transform)) and (transform is not None):
raise TypeError(
f"`transform` must be a callable or `None`, you passed: {transform}"
)
if self.mode == "marker":
cursor = self._create_marker()
elif self.mode == "crosshair":
cursor = self._create_crosshair()
subplot.scene.add(cursor)
subplot.renderer.add_event_handler(
partial(self._pointer_moved, subplot), "pointer_move"
)
self._cursors[subplot] = cursor
self._transforms[subplot] = transform
# let cursor manage tooltips
subplot.renderer.remove_event_handler(subplot._fpl_set_tooltip, "pointer_move")
def remove_subplot(self, subplot: Subplot):
"""remove a subplot"""
if subplot not in self._cursors.keys():
raise KeyError("cursor not in given supblot")
subplot.scene.remove(self._cursors.pop(subplot))
# give back tooltip control to the subplot
subplot.renderer.add_event_handler(subplot._fpl_set_tooltip, "pointer_move")
def clear(self):
"""remove all subplots"""
for subplot in self._cursors.keys():
self.remove_subplot(subplot)
def _create_marker(self) -> pygfx.Points:
# creates a Point object, used for "marker" mode
point = pygfx.Points(
pygfx.Geometry(positions=np.array([[*self.position, 0]], dtype=np.float32)),
pygfx.PointsMarkerMaterial(
marker=self.marker,
size=self.size,
size_space=self.size_space,
color=self.color,
edge_color=self.edge_color,
edge_width=self.edge_width,
opacity=self.alpha,
alpha_mode="blend",
render_queue=RenderQueue.selector,
depth_test=False,
depth_write=False,
pick_write=False,
),
)
return point
def _create_crosshair(self) -> pygfx.Group:
# Creates two infinite lines, used for "crosshair" mode
x, y = self.position
line_h_data = np.array(
[
[x - 1, y, 0],
[x + 1, y, 0],
],
dtype=np.float32,
)
line_v_data = np.array(
[
[x, y - 1, 0],
[x, y + 1, 0],
],
dtype=np.float32,
)
line_h = pygfx.Line(
geometry=pygfx.Geometry(positions=line_h_data),
material=pygfx.LineInfiniteSegmentMaterial(
thickness=self.size,
thickness_space=self.size_space,
color=self.color,
opacity=self.alpha,
alpha_mode="blend",
aa=True,
render_queue=RenderQueue.selector,
depth_test=False,
depth_write=False,
pick_write=False,
),
)
line_v = pygfx.Line(
geometry=pygfx.Geometry(positions=line_v_data),
material=pygfx.LineInfiniteSegmentMaterial(
thickness=self.size,
thickness_space=self.size_space,
color=self.color,
opacity=self.alpha,
alpha_mode="blend",
aa=True,
render_queue=RenderQueue.selector,
depth_test=False,
depth_write=False,
pick_write=False,
),
)
lines = pygfx.Group()
lines.add(line_h, line_v)
return lines
def _pointer_moved(self, subplot, ev: pygfx.PointerEvent):
if not self.enabled:
return
pos = subplot.map_screen_to_world(ev)
if pos is None:
return
self.position = pos[:-1]