Skip to content

Commit

Permalink
solves issue Florian-Barthel#29 + refactoring
Browse files Browse the repository at this point in the history
The camera can now be moved relative to the scene by dragging with the right mouse button, both in orbital as well as WASD mode. In my opinion using the right mouse button instead of the middle one results in a better user experience. When dragging with the middle mouse button it can easily happen that also the wheel moves a little bit.

Please try the feature out, do a code review and let me know about improvement ideas. I thought of making the dragging smoother by adding some momentum and ignoring negligible changes in the mouse position. But I suggest we leave that for another issue.
  • Loading branch information
christian-helms committed Aug 7, 2024
1 parent caea288 commit 2e8c60b
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 163 deletions.
128 changes: 72 additions & 56 deletions gui_utils/imgui_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from gui_utils.constants import *



def set_default_style(color_scheme="dark", spacing=5, indent=20, scrollbar=10):
s = imgui.get_style()
s.window_padding = [spacing, spacing]
Expand All @@ -36,65 +35,67 @@ def set_default_style(color_scheme="dark", spacing=5, indent=20, scrollbar=10):

getattr(imgui, f"style_colors_{color_scheme}")(s)

s.set_color_(COLOR_TEXT, imgui.ImVec4(1.00, 1.00, 1.00, 1.00))
s.set_color_(COLOR_TEXT_DISABLED, imgui.ImVec4(0.50, 0.50, 0.50, 1.00))
s.set_color_(COLOR_WINDOW_BACKGROUND, imgui.ImVec4(0.30, 0.30, 0.30, 1.00))
s.set_color_(COLOR_CHILD_BACKGROUND, imgui.ImVec4(0.20, 0.20, 0.20, 1.00))
s.set_color_(COLOR_POPUP_BACKGROUND, imgui.ImVec4(0.19, 0.19, 0.19, 0.92))
s.set_color_(COLOR_BORDER, imgui.ImVec4(0.19, 0.19, 0.19, 0.29))
s.set_color_(COLOR_BORDER_SHADOW, imgui.ImVec4(0.00, 0.00, 0.00, 0.00))
s.set_color_(COLOR_TEXT, imgui.ImVec4(1.00, 1.00, 1.00, 1.00))
s.set_color_(COLOR_TEXT_DISABLED, imgui.ImVec4(0.50, 0.50, 0.50, 1.00))
s.set_color_(COLOR_WINDOW_BACKGROUND, imgui.ImVec4(0.30, 0.30, 0.30, 1.00))
s.set_color_(COLOR_CHILD_BACKGROUND, imgui.ImVec4(0.20, 0.20, 0.20, 1.00))
s.set_color_(COLOR_POPUP_BACKGROUND, imgui.ImVec4(0.19, 0.19, 0.19, 0.92))
s.set_color_(COLOR_BORDER, imgui.ImVec4(0.19, 0.19, 0.19, 0.29))
s.set_color_(COLOR_BORDER_SHADOW, imgui.ImVec4(0.00, 0.00, 0.00, 0.00))

# background of all text areas and checkboxes
s.set_color_(COLOR_FRAME_BACKGROUND, imgui.ImVec4(0.15, 0.15, 0.15, 0.50))
s.set_color_(COLOR_FRAME_BACKGROUND_HOVERED, imgui.ImVec4(0.25, 0.25, 0.25, 0.50))
s.set_color_(COLOR_FRAME_BACKGROUND_ACTIVE, imgui.ImVec4(0.25, 0.25, 0.25, 1.00))

s.set_color_(COLOR_TITLE_BACKGROUND, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_ACTIVE, imgui.ImVec4(0.06, 0.06, 0.06, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_COLLAPSED, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_MENUBAR_BACKGROUND, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_SCROLLBAR_BACKGROUND, imgui.ImVec4(0.05, 0.05, 0.05, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB, imgui.ImVec4(0.34, 0.34, 0.34, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_HOVERED, imgui.ImVec4(0.40, 0.40, 0.40, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_ACTIVE, imgui.ImVec4(0.56, 0.56, 0.56, 0.54))
s.set_color_(COLOR_CHECK_MARK, imgui.ImVec4(0.33, 0.67, 0.86, 1.00))
s.set_color_(COLOR_SLIDER_GRAB, imgui.ImVec4(0.90, 0.70, 0.00, 0.75))
s.set_color_(COLOR_SLIDER_GRAB_ACTIVE, imgui.ImVec4(0.90, 0.70, 0.00, 0.90))
s.set_color_(COLOR_BUTTON, imgui.ImVec4(0.90, 0.70, 0.00, 0.75))
s.set_color_(COLOR_BUTTON_HOVERED, imgui.ImVec4(0.90, 0.70, 0.00, 0.90))
s.set_color_(COLOR_BUTTON_ACTIVE, imgui.ImVec4(0.90, 0.70, 0.00, 1.00))
s.set_color_(COLOR_HEADER, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_HEADER_HOVERED, imgui.ImVec4(0.00, 0.00, 0.00, 0.36))
s.set_color_(COLOR_HEADER_ACTIVE, imgui.ImVec4(0.20, 0.22, 0.23, 0.33))
s.set_color_(COLOR_SEPARATOR, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_SEPARATOR_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_SEPARATOR_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_RESIZE_GRIP, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_TAB, imgui.ImVec4(0.10, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_HOVERED, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_TAB_ACTIVE, imgui.ImVec4(0.20, 0.20, 0.20, 0.36))
s.set_color_(COLOR_TAB_UNFOCUSED, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_UNFOCUSED_ACTIVE, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_PLOT_LINES, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_LINES_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_PLOT_HISTOGRAM, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_HISTOGRAM_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_TEXT_SELECTED_BACKGROUND, imgui.ImVec4(0.20, 0.22, 0.23, 1.00))
s.set_color_(COLOR_NAV_HIGHLIGHT, imgui.ImVec4(1.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_FRAME_BACKGROUND, imgui.ImVec4(0.15, 0.15, 0.15, 0.50))
s.set_color_(COLOR_FRAME_BACKGROUND_HOVERED, imgui.ImVec4(0.25, 0.25, 0.25, 0.50))
s.set_color_(COLOR_FRAME_BACKGROUND_ACTIVE, imgui.ImVec4(0.25, 0.25, 0.25, 1.00))

s.set_color_(COLOR_TITLE_BACKGROUND, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_ACTIVE, imgui.ImVec4(0.06, 0.06, 0.06, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_COLLAPSED, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_MENUBAR_BACKGROUND, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_SCROLLBAR_BACKGROUND, imgui.ImVec4(0.05, 0.05, 0.05, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB, imgui.ImVec4(0.34, 0.34, 0.34, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_HOVERED, imgui.ImVec4(0.40, 0.40, 0.40, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_ACTIVE, imgui.ImVec4(0.56, 0.56, 0.56, 0.54))
s.set_color_(COLOR_CHECK_MARK, imgui.ImVec4(0.33, 0.67, 0.86, 1.00))
s.set_color_(COLOR_SLIDER_GRAB, imgui.ImVec4(0.90, 0.70, 0.00, 0.75))
s.set_color_(COLOR_SLIDER_GRAB_ACTIVE, imgui.ImVec4(0.90, 0.70, 0.00, 0.90))
s.set_color_(COLOR_BUTTON, imgui.ImVec4(0.90, 0.70, 0.00, 0.75))
s.set_color_(COLOR_BUTTON_HOVERED, imgui.ImVec4(0.90, 0.70, 0.00, 0.90))
s.set_color_(COLOR_BUTTON_ACTIVE, imgui.ImVec4(0.90, 0.70, 0.00, 1.00))
s.set_color_(COLOR_HEADER, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_HEADER_HOVERED, imgui.ImVec4(0.00, 0.00, 0.00, 0.36))
s.set_color_(COLOR_HEADER_ACTIVE, imgui.ImVec4(0.20, 0.22, 0.23, 0.33))
s.set_color_(COLOR_SEPARATOR, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_SEPARATOR_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_SEPARATOR_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_RESIZE_GRIP, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_TAB, imgui.ImVec4(0.10, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_HOVERED, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_TAB_ACTIVE, imgui.ImVec4(0.20, 0.20, 0.20, 0.36))
s.set_color_(COLOR_TAB_UNFOCUSED, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_UNFOCUSED_ACTIVE, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_PLOT_LINES, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_LINES_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_PLOT_HISTOGRAM, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_HISTOGRAM_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_TEXT_SELECTED_BACKGROUND, imgui.ImVec4(0.20, 0.22, 0.23, 1.00))
s.set_color_(COLOR_NAV_HIGHLIGHT, imgui.ImVec4(1.00, 0.00, 0.00, 1.00))

# color of selected text
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(0.90, 0.60, 0.00, 0.70))
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(0.90, 0.60, 0.00, 0.20))
s.set_color_(COLOR_NAV_WINDOWING_DIM_BACKGROUND,imgui.ImVec4(0.90, 0.60, 0.00, 0.35))
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(0.90, 0.60, 0.00, 0.70))
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(0.90, 0.60, 0.00, 0.20))
s.set_color_(
COLOR_NAV_WINDOWING_DIM_BACKGROUND, imgui.ImVec4(0.90, 0.60, 0.00, 0.35)
)


@contextlib.contextmanager
def eval_color():
imgui.push_style_color(COLOR_HEADER, imgui.ImVec4(0.19, 0.22, 0.25, 1.00))
imgui.push_style_color(COLOR_HEADER_HOVERED, imgui.ImVec4(0.21, 0.24, 0.27, 1.00))
imgui.push_style_color(COLOR_HEADER_ACTIVE, imgui.ImVec4(0.21, 0.24, 0.27, 1.00))
imgui.push_style_color(COLOR_HEADER, imgui.ImVec4(0.19, 0.22, 0.25, 1.00))
imgui.push_style_color(COLOR_HEADER_HOVERED, imgui.ImVec4(0.21, 0.24, 0.27, 1.00))
imgui.push_style_color(COLOR_HEADER_ACTIVE, imgui.ImVec4(0.21, 0.24, 0.27, 1.00))
yield
imgui.pop_style_color(3)

Expand Down Expand Up @@ -132,7 +133,9 @@ def button(label, width=0, enabled=True):
return clicked


def collapsing_header(text, visible=None, flags=0, default=False, enabled=True, show=True):
def collapsing_header(
text, visible=None, flags=0, default=False, enabled=True, show=True
):
expanded = False
if show:
if default:
Expand All @@ -155,7 +158,9 @@ def drag_previous_control(enabled=True):
dragging = False
dx = 0
dy = 0
if imgui.begin_drag_drop_source(imgui.DragDropFlags_.source_no_preview_tooltip.value):
if imgui.begin_drag_drop_source(
imgui.DragDropFlags_.source_no_preview_tooltip.value
):
if enabled:
dragging = True
dx, dy = imgui.get_mouse_drag_delta()
Expand All @@ -175,8 +180,19 @@ def drag_hidden_window(label, x, y, width, height, enabled=True):
imgui.push_style_color(COLOR_BORDER, 0)
imgui.set_next_window_pos(imgui.ImVec2(x, y))
imgui.set_next_window_size(imgui.ImVec2(width, height))
imgui.begin(label, p_open=False, flags=(WINDOW_NO_TITLE_BAR | WINDOW_NO_RESIZE | WINDOW_NO_MOVE))
dragging, dx, dy = drag_previous_control(enabled=enabled)
imgui.begin(
label,
p_open=False,
flags=(WINDOW_NO_TITLE_BAR | WINDOW_NO_RESIZE | WINDOW_NO_MOVE),
)
dragging, dx, dy = drag_previous_control(enabled=enabled, mouse_button=2)
imgui.end()
imgui.pop_style_color(2)
return dragging, dx, dy


def did_drag_start_in_window(x, y, width, height, drag_delta):
mouse_pos_at_drag_start = imgui.get_mouse_pos() - drag_delta
return (x <= mouse_pos_at_drag_start.x <= x + width) and (
y <= mouse_pos_at_drag_start.y <= y + height
)
60 changes: 41 additions & 19 deletions run_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def __init__(self, data_path=None, use_gan_decoder=False):
self.code_font_path = "fonts/jetbrainsmono/JetBrainsMono-Regular.ttf"
self.regular_font_path = "fonts/source_sans_pro/SourceSansPro-Regular.otf"

super().__init__(title="splatviz", window_width=1920, window_height=1080, font=self.regular_font_path, code_font=self.code_font_path)
super().__init__(
title="splatviz",
window_width=1920,
window_height=1080,
font=self.regular_font_path,
code_font=self.code_font_path,
)

self.code_font = imgui.get_io().fonts.add_font_from_file_ttf(self.code_font_path, 14)
self.regular_font = imgui.get_io().fonts.add_font_from_file_ttf(self.code_font_path, 14)
self.code_font = imgui.get_io().fonts.add_font_from_file_ttf(
self.code_font_path, 14
)
self.regular_font = imgui.get_io().fonts.add_font_from_file_ttf(
self.code_font_path, 14
)
self._imgui_renderer.refresh_font_texture()

# Internals.
Expand Down Expand Up @@ -108,14 +118,7 @@ def draw_frame(self):
self.button_large_w = self.font_size * 10
self.label_w = round(self.font_size * 5.5) + 100

# Detect mouse dragging in the result area.
dragging, dx, dy = imgui_utils.drag_hidden_window(
"##result_area", x=self.pane_w, y=0, width=self.content_width - self.pane_w, height=self.content_height
)
if dragging:
self.cam_widget.drag(dx, dy)

# Begin control pane.
### Begin control pane. ###
imgui.set_next_window_pos(imgui.ImVec2(0, 0))
imgui.set_next_window_size(imgui.ImVec2(self.pane_w, self.content_height))
imgui.begin(
Expand All @@ -124,7 +127,7 @@ def draw_frame(self):
flags=(WINDOW_NO_TITLE_BAR | WINDOW_NO_RESIZE | WINDOW_NO_MOVE),
)

# Widgets.
### Widgets. ###

expanded, _visible = imgui_utils.collapsing_header("Load", default=True)
imgui.indent()
Expand All @@ -143,7 +146,13 @@ def draw_frame(self):

expanded, _visible = imgui_utils.collapsing_header("Camera", default=False)
imgui.indent()
self.cam_widget(expanded)
active_region = EasyDict({
"x": self.pane_w,
"y": 0,
"width": self.content_width - self.pane_w,
"height": self.content_height
})
self.cam_widget(active_region)
imgui.unindent()

if self.use_gan_decoder:
Expand Down Expand Up @@ -175,7 +184,7 @@ def draw_frame(self):

# imgui.show_style_editor()

# Render.
### Render. ###
if self.is_skipping_frames():
pass
elif self._defer_rendering > 0:
Expand All @@ -186,15 +195,19 @@ def draw_frame(self):
if result is not None:
self.result = result

# Display.
### Display. ###
max_w = self.content_width - self.pane_w
max_h = self.content_height
pos = np.array([self.pane_w + max_w / 2, max_h / 2])
if "image" in self.result:
if self._tex_img is not self.result.image:
self._tex_img = self.result.image
if self._tex_obj is None or not self._tex_obj.is_compatible(image=self._tex_img):
self._tex_obj = gl_utils.Texture(image=self._tex_img, bilinear=False, mipmap=False)
if self._tex_obj is None or not self._tex_obj.is_compatible(
image=self._tex_img
):
self._tex_obj = gl_utils.Texture(
image=self._tex_img, bilinear=False, mipmap=False
)
else:
self._tex_obj.update(self._tex_img)
zoom = min(max_w / self._tex_obj.width, max_h / self._tex_obj.height)
Expand All @@ -205,7 +218,11 @@ def draw_frame(self):
self.result.message = str(self.result.error)
if "message" in self.result:
tex = text_utils.get_texture(
self.result.message, size=self.font_size, max_width=max_w, max_height=max_h, outline=2
self.result.message,
size=self.font_size,
max_width=max_w,
max_height=max_h,
outline=2,
)
tex.draw(pos=pos, align=0.5, rint=True, color=1)
if "eval" in self.result:
Expand All @@ -220,7 +237,12 @@ def draw_frame(self):


@click.command()
@click.option("--data_path", help="Where to search for .ply files", metavar="PATH", default="./sample_scenes")
@click.option(
"--data_path",
help="Where to search for .ply files",
metavar="PATH",
default="./sample_scenes",
)
@click.option("--use_decoder", help="Visualizes the results of a decoder", is_flag=True)
def main(data_path, use_decoder):
viz = Visualizer(data_path=data_path, use_gan_decoder=use_decoder)
Expand Down
Loading

0 comments on commit 2e8c60b

Please sign in to comment.