-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_imgui_figure.py
More file actions
249 lines (189 loc) · 6.95 KB
/
_imgui_figure.py
File metadata and controls
249 lines (189 loc) · 6.95 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
from pathlib import Path
from typing import Literal, Iterable
import numpy as np
import imgui_bundle
from imgui_bundle import imgui, icons_fontawesome_6 as fa
from wgpu.utils.imgui import ImguiRenderer, Stats
from rendercanvas import BaseRenderCanvas
import pygfx
from ._figure import Figure
from ..ui import Window, EdgeWindow, SubplotToolbar, StandardRightClickMenu, Popup, GUI_EDGES
from ..ui import ColormapPicker
class ImguiFigure(Figure):
def __init__(
self,
shape: tuple[int, int] = (1, 1),
rects: list[tuple | np.ndarray] = None,
extents: list[tuple | np.ndarray] = None,
cameras: (
Literal["2d", "3d"]
| Iterable[Iterable[Literal["2d", "3d"]]]
| pygfx.PerspectiveCamera
| Iterable[Iterable[pygfx.PerspectiveCamera]]
) = "2d",
controller_types: (
Iterable[Iterable[Literal["panzoom", "fly", "trackball", "orbit"]]]
| Iterable[Literal["panzoom", "fly", "trackball", "orbit"]]
) = None,
controller_ids: (
Literal["sync"]
| Iterable[int]
| Iterable[Iterable[int]]
| Iterable[Iterable[str]]
) = None,
controllers: pygfx.Controller | Iterable[Iterable[pygfx.Controller]] = None,
canvas: str | BaseRenderCanvas | pygfx.Texture = None,
renderer: pygfx.WgpuRenderer = None,
canvas_kwargs: dict = None,
size: tuple[int, int] = (500, 300),
names: list | np.ndarray = None,
):
self._guis: dict[str, Window] = {k: None for k in GUI_EDGES}
super().__init__(
shape=shape,
rects=rects,
extents=extents,
cameras=cameras,
controller_types=controller_types,
controller_ids=controller_ids,
controllers=controllers,
canvas=canvas,
renderer=renderer,
canvas_kwargs=canvas_kwargs,
size=size,
names=names,
)
self._imgui_renderer = ImguiRenderer(self.renderer.device, self.canvas)
fronts_path = str(
Path(imgui_bundle.__file__).parent.joinpath(
"assets", "fonts", "Font_Awesome_6_Free-Solid-900.otf"
)
)
io = imgui.get_io()
self._fa_icons = io.fonts.add_font_from_file_ttf(
fronts_path, 16, glyph_ranges_as_int_list=[fa.ICON_MIN_FA, fa.ICON_MAX_FA]
)
io.fonts.build()
self.imgui_renderer.backend.create_fonts_texture()
self.imgui_renderer.set_gui(self._draw_imgui)
self._subplot_toolbars: np.ndarray[SubplotToolbar] = np.empty(
shape=self._subplots.size, dtype=object
)
for i, subplot in enumerate(self._subplots.ravel()):
toolbar = SubplotToolbar(subplot=subplot, fa_icons=self._fa_icons)
self._subplot_toolbars[i] = toolbar
self._right_click_menu = StandardRightClickMenu(
figure=self, fa_icons=self._fa_icons
)
self._popups: dict[str, Popup] = {}
self.imgui_show_fps = False
self._stats = Stats(self.renderer.device, self.canvas)
self.register_popup(ColormapPicker)
@property
def guis(self) -> dict[str, Window]:
"""GUI windows added to the Figure"""
return self._guis
@property
def imgui_renderer(self) -> ImguiRenderer:
"""imgui renderer"""
return self._imgui_renderer
def _render(self, draw=False):
if self.imgui_show_fps:
with self._stats:
super()._render(draw)
else:
super()._render(draw)
self.imgui_renderer.render()
# needs to be here else events don't get processed
self.canvas.request_draw()
def _draw_imgui(self) -> imgui.ImDrawData:
imgui.new_frame()
for subplot, toolbar in zip(
self._subplots.ravel(), self._subplot_toolbars.ravel()
):
if not subplot.toolbar:
# if subplot.toolbar is False
continue
toolbar.update()
for gui in self.guis.values():
if gui is not None:
gui.draw_window()
for popup in self._popups.values():
popup.update()
self._right_click_menu.update()
imgui.end_frame()
imgui.render()
return imgui.get_draw_data()
def add_gui(self, gui: Window):
"""
Add a GUI to the Figure. GUIs can be added to the top, bottom, left or right edge.
Parameters
----------
gui: Window
A GUI Window instance
"""
if not isinstance(gui, Window):
raise TypeError(
f"GUI must be of type: {Window} you have passed a {type(gui)}"
)
if isinstance(gui, EdgeWindow):
location = gui.location
if location not in GUI_EDGES:
raise ValueError(
f"GUI does not have a valid location, valid locations are: {GUI_EDGES}, you have passed: {location}"
)
if self.guis[location] is not None:
raise ValueError(f"GUI already exists in the desired location: {location}")
else:
# just use mem address for a location key
location = hex(id(gui))
self.guis[location] = gui
self._fpl_reset_layout()
def get_pygfx_render_area(self, *args) -> tuple[int, int, int, int]:
"""
Get rect for the portion of the canvas that the pygfx renderer draws to,
i.e. non-imgui, part of canvas
Returns
-------
tuple[int, int, int, int]
x_pos, y_pos, width, height
"""
width, height = self.canvas.get_logical_size()
for edge in ["left", "right"]:
if self.guis[edge]:
width -= self._guis[edge].size
for edge in ["top", "bottom"]:
if self.guis[edge]:
height -= self._guis[edge].size
if self.guis["left"]:
xpos = self.guis["left"].size
else:
xpos = 0
if self.guis["top"]:
ypos = self.guis["top"].size
else:
ypos = 0
return xpos, ypos, max(1, width), max(1, height)
def register_popup(self, popup: Popup.__class__):
"""
Register a popup class. Note that this takes the class, not an instance
Parameters
----------
popup: Popup subclass
"""
self._popups[popup.name] = popup(self)
def open_popup(self, name: str, pos: tuple[int, int], **kwargs):
"""
Open a registered popup
Parameters
----------
name: str
The registered name of the popup
pos: int, int
x_pos, y_pos for the popup
kwargs
any additional kwargs to pass to the Popup's open() method
"""
if self._popups[name].is_open:
return
self._popups[name].open(pos, **kwargs)