-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy path_subplot.py
More file actions
448 lines (344 loc) · 13 KB
/
Copy path_subplot.py
File metadata and controls
448 lines (344 loc) · 13 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
from typing import Literal, Union
import numpy as np
import pygfx
from rendercanvas import BaseRenderCanvas
from ..graphics import TextGraphic
from ._utils import create_camera, create_controller
from ._plot_area import PlotArea
from ._frame import Frame
from ..axes import Axes
class Subplot(PlotArea):
def __init__(
self,
parent: Union["Figure"],
camera: Literal["2d", "3d"] | pygfx.PerspectiveCamera,
controller: pygfx.Controller | str,
canvas: BaseRenderCanvas | pygfx.Texture,
rect: np.ndarray = None,
extent: np.ndarray = None,
resizeable: bool = True,
renderer: pygfx.WgpuRenderer = None,
name: str = None,
):
"""
Subplot class.
.. important::
``Subplot`` is not meant to be constructed directly, it only exists as part of a ``Figure``
Parameters
----------
parent: 'Figure' | None
parent Figure instance
camera: str or pygfx.PerspectiveCamera, default '2d'
indicates the FOV for the camera, '2d' sets ``fov = 0``, '3d' sets ``fov = 50``.
``fov`` can be changed at any time.
controller: str or pygfx.Controller, optional
| if ``None``, uses a PanZoomController for "2d" camera or FlyController for "3d" camera.
| if ``str``, must be one of: `"panzoom", "fly", "trackball", or "orbit"`.
| also accepts a pygfx.Controller instance
canvas: BaseRenderCanvas, or a pygfx.Texture
Provides surface on which a scene will be rendered.
renderer: WgpuRenderer
object used to render scenes using wgpu
name: str, optional
name of the subplot, will appear as ``TextGraphic`` above the subplot
"""
camera = create_camera(camera)
controller = create_controller(controller_type=controller, camera=camera)
self._docks = dict()
toolbar_visible = "Imgui" in parent.__class__.__name__
super().__init__(
parent=parent,
camera=camera,
controller=controller,
scene=pygfx.Scene(),
canvas=canvas,
renderer=renderer,
name=name,
)
for pos in ["left", "top", "right", "bottom"]:
dv = Dock(self, size=0)
dv.name = pos
self.docks[pos] = dv
self.children.append(dv)
# imgui windows confined to this subplot, keyed by location
self._imgui_windows = {loc: None for loc in ["left", "right", "top", "bottom", "toolbar"]}
self._imgui_right_click = None
self._axes = Axes(self)
self.scene.add(self.axes.world_object)
self._frame = Frame(
viewport=self.viewport,
rect=rect,
extent=extent,
resizeable=resizeable,
title=name,
docks=self.docks,
imgui_windows=self._imgui_windows,
toolbar_visible=toolbar_visible,
canvas_rect=parent.get_pygfx_render_area(),
)
@property
def axes(self) -> Axes:
"""Axes object"""
return self._axes
@property
def name(self) -> str:
"""Subplot name"""
return self._name
@name.setter
def name(self, name: str):
if name is None:
self._name = None
return
for subplot in self.get_figure(self):
if (subplot is self) or (subplot is None):
continue
if subplot.name == name:
raise ValueError("subplot names must be unique")
self._name = name
@property
def docks(self) -> dict:
"""
The docks of this plot area. Each ``dock`` is basically just a PlotArea too.
The docks are: ["left", "top", "right", "bottom"]
Returns
-------
Dict[str, Dock]
{dock_name: Dock}
"""
return self._docks
@property
def toolbar(self) -> bool:
"""show/hide toolbar"""
return self.frame.toolbar_visible
@toolbar.setter
def toolbar(self, visible: bool):
self.frame.toolbar_visible = visible
self.frame.reset_viewport()
def _render(self):
self.axes.update_using_camera()
super()._render()
@property
def title(self) -> TextGraphic:
"""subplot title"""
return self._frame.title_graphic
@title.setter
def title(self, text: str):
text = str(text)
self.title.text = text
@property
def frame(self) -> Frame:
"""Frame that the subplot lives in"""
return self._frame
@property
def imgui_windows(self) -> dict:
"""
The imgui windows of this subplot, keyed by location.
The locations are the four edges ["left", "right", "top", "bottom"] and "toolbar"
Returns
-------
dict[str, ImguiWindow]
{location: ImguiWindow}
"""
return self._imgui_windows
def add_imgui_window(
self,
window=None,
*,
location: str = None,
size: int = None,
title: str = None,
window_flags=None,
):
"""
Add an imgui window confined to this subplot. Can also be used as a decorator, see the
``Figure.add_imgui_window`` examples.
Edge windows ("left", "right", "top", "bottom") reserve space outboard of the subplot dock on that edge.
The "toolbar" location replaces the subplot toolbar. An existing window at a ``location`` is replaced.
Parameters
----------
window: ImguiWindow, optional
an ``ImguiWindow`` instance, omit when decorating
location: str, "left" | "right" | "top" | "bottom" | "toolbar"
edge windows reserve canvas space, "toolbar" replaces the subplot toolbar
size: int
edge or toolbar thickness in pixels, required for edge windows
title: str, optional
window title, drawn as a title bar for edge windows. If ``None`` no title bar is drawn.
window_flags: ``imgui.WindowFlags_``, optional
imgui window flags, used when decorating, uses the ``ImguiWindow`` default flags if not provided
"""
figure = self.get_figure()
if "Imgui" not in figure.__class__.__name__:
raise TypeError("imgui windows can only be added to a subplot of an ImguiFigure")
from ..ui._base import ImguiWindow, EDGES, _wrap_update_call
valid = EDGES + ["toolbar"]
if location not in valid:
raise ValueError(
f"subplot imgui window location must be one of: {valid}, you have passed: {location}"
)
if location in EDGES and size is None:
raise ValueError(f"must provide `size` for an edge window, location: {location}")
hook_kwargs = dict(figure=figure, subplot=self, location=location, size=size, title=title)
if window_flags is not None:
hook_kwargs["window_flags"] = window_flags
def decorator(_window):
if isinstance(_window, ImguiWindow):
win = _window
elif callable(_window):
win = ImguiWindow(update_call=_wrap_update_call(_window, self))
else:
raise TypeError(
"add_imgui_window() must be used as a decorator on a function, or given an `ImguiWindow` instance"
)
win._fpl_add_hook(**hook_kwargs)
self._imgui_windows[location] = win
# edge windows reserve space, reset the layout
if location in EDGES:
figure._fpl_reset_layout()
return _window
if window is None:
return decorator
decorator(window)
return window
def append_imgui_window(self, gui=None, *, location: str = None):
"""
Append imgui elements to an existing window of this subplot. Can also be used as a decorator. Useful for
appending elements to the subplot toolbar with ``location="toolbar"``.
Parameters
----------
gui: callable, optional
function that draws imgui elements, omit when decorating
location: str, "left" | "right" | "top" | "bottom" | "toolbar"
location of the existing window to append to
"""
from ..ui._base import _wrap_update_call
window = self._imgui_windows.get(location)
if window is None:
raise ValueError(f"no imgui window at location to append to: {location}")
def decorator(_gui):
window._update_calls.append(_wrap_update_call(_gui, self))
return _gui
if gui is None:
return decorator
return decorator(gui)
def remove_imgui_window(self, location: str):
"""
Remove and return the imgui window at the given location
Parameters
----------
location: str
"left" | "right" | "top" | "bottom" | "toolbar"
Returns
-------
ImguiWindow
the removed window, it can be added again later
"""
from ..ui._base import EDGES
window = self._imgui_windows.get(location)
self._imgui_windows[location] = None
# edge windows reserve space, reset the layout
if location in EDGES:
self.get_figure()._fpl_reset_layout()
return window
@property
def imgui_right_click(self):
"""
The imgui popup that is opened by a right-click within this subplot.
Returns
-------
ImguiPopup | None
"""
return self._imgui_right_click
def set_imgui_right_click(self, popup=None, *, window_flags=None):
"""
Set the imgui popup that is opened by a right-click within this subplot, replaces the Figure's popup within
this subplot. Can also be used as a decorator, see the ``ImguiFigure.set_imgui_right_click`` examples.
Parameters
----------
popup: ImguiPopup | callable, optional
an ``ImguiPopup`` instance, or a function that draws imgui elements. Omit when decorating.
window_flags: ``imgui.WindowFlags_``, optional
imgui window flags for the popup
"""
figure = self.get_figure()
if "Imgui" not in figure.__class__.__name__:
raise TypeError(
"imgui right-click popups can only be set on a subplot of an ImguiFigure"
)
from ..ui._base import ImguiPopup, _wrap_update_call
def decorator(_popup):
if isinstance(_popup, ImguiPopup):
p = _popup
elif callable(_popup):
p = ImguiPopup(update_call=_wrap_update_call(_popup, self))
else:
raise TypeError(
"set_imgui_right_click() must be used as a decorator, or given an `ImguiPopup` instance or a "
"function that draws imgui elements"
)
p._fpl_add_hook(figure=figure, parent=self, window_flags=window_flags)
self._imgui_right_click = p
return _popup
if popup is None:
return decorator
decorator(popup)
return popup
def append_imgui_right_click(self, gui=None):
"""
Append imgui elements to the right-click popup of this subplot. Can also be used as a decorator.
Parameters
----------
gui: callable, optional
function that draws imgui elements, omit when decorating
"""
from ..ui._base import _wrap_update_call
popup = self._imgui_right_click
if popup is None:
raise ValueError(
"no imgui right-click popup set on this subplot to append to, set one using "
"`subplot.set_imgui_right_click()`"
)
def decorator(_gui):
popup._update_calls.append(_wrap_update_call(_gui, self))
return _gui
if gui is None:
return decorator
return decorator(gui)
def remove_imgui_right_click(self):
"""
Remove and return the right-click popup of this subplot
Returns
-------
ImguiPopup
the removed popup, it can be set again later
"""
popup = self._imgui_right_click
self._imgui_right_click = None
return popup
class Dock(PlotArea):
def __init__(
self,
parent: Subplot,
size: int,
):
self._size = size
super().__init__(
parent=parent,
camera=pygfx.OrthographicCamera(),
controller=pygfx.PanZoomController(),
scene=pygfx.Scene(),
canvas=parent.canvas,
renderer=parent.renderer,
)
@property
def size(self) -> int:
"""Get or set the size of this dock"""
return self._size
@size.setter
def size(self, s: int):
self._size = s
self.get_figure()._fpl_reset_layout()
def _render(self):
if self.size == 0:
return
super()._render()