Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow floating windows, make a simple imgui debug window #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions fastplotlib/layouts/_imgui_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pygfx

from ._figure import Figure
from ..ui import EdgeWindow, SubplotToolbar, StandardRightClickMenu, Popup, GUI_EDGES
from ..ui import Window, EdgeWindow, SubplotToolbar, StandardRightClickMenu, Popup, GUI_EDGES
from ..ui import ColormapPicker


Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(
size: tuple[int, int] = (500, 300),
names: list | np.ndarray = None,
):
self._guis: dict[str, EdgeWindow] = {k: None for k in GUI_EDGES}
self._guis: dict[str, Window] = {k: None for k in GUI_EDGES}

super().__init__(
shape=shape,
Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(
self.register_popup(ColormapPicker)

@property
def guis(self) -> dict[str, EdgeWindow]:
def guis(self) -> dict[str, Window]:
"""GUI windows added to the Figure"""
return self._guis

Expand Down Expand Up @@ -148,30 +148,34 @@ def _draw_imgui(self) -> imgui.ImDrawData:

return imgui.get_draw_data()

def add_gui(self, gui: EdgeWindow):
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: EdgeWindow
A GUI EdgeWindow instance
gui: Window
A GUI Window instance

"""
if not isinstance(gui, EdgeWindow):
if not isinstance(gui, Window):
raise TypeError(
f"GUI must be of type: {EdgeWindow} you have passed a {type(gui)}"
f"GUI must be of type: {Window} you have passed a {type(gui)}"
)

location = gui.location
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 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}")
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

Expand Down
1 change: 1 addition & 0 deletions fastplotlib/ui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._base import BaseGUI, Window, EdgeWindow, Popup, GUI_EDGES
from ._subplot_toolbar import SubplotToolbar
from .right_click_menus import StandardRightClickMenu, ColormapPicker
from ._debug import DebugWindow
7 changes: 6 additions & 1 deletion fastplotlib/ui/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ def update(self):

class Window(BaseGUI):
"""Base class for imgui windows drawn within Figures"""
def draw_window(self):
"""
Must be implemented in subclass

pass
This must include the imgui.begin() and imgui.end() calls that define an imgui window.
"""
raise NotImplementedError


class EdgeWindow(Window):
Expand Down
35 changes: 35 additions & 0 deletions fastplotlib/ui/_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from imgui_bundle import imgui
from icecream import ic

from ._base import Window


class DebugWindow(Window):
def __init__(self, objs: list):
self._objs = objs
super().__init__()

@property
def objs(self) -> tuple:
return tuple(self._objs)

def add(self, obj):
self._objs.append(obj)

def draw_window(self):
imgui.set_next_window_pos((300, 0), imgui.Cond_.appearing)
imgui.set_next_window_pos((0, 0), imgui.Cond_.appearing)

imgui.begin("Debug", None)

info = list()

for obj in self.objs:
if callable(obj):
info.append(ic.format(obj()))
else:
info.append(ic.format(obj))

imgui.text_wrapped("\n\n".join(info))

imgui.end()