Skip to content

Commit

Permalink
keydown: handle mouse buttons as well
Browse files Browse the repository at this point in the history
  • Loading branch information
harskish committed May 21, 2024
1 parent ea5aea3 commit 52fd294
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
41 changes: 27 additions & 14 deletions pyviewer/gl_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def __init__(self, title, inifile=None, swap_interval=0, hidden=False):
self._editables = {}
self.tex_interp_mode = gl.GL_LINEAR
self.default_font_size = 15

self.renderer = None

fname = inifile or "".join(c for c in title.lower() if c.isalnum())
self._inifile = Path(fname).with_suffix('.ini')

Expand Down Expand Up @@ -425,6 +426,7 @@ def editable(self, name, **kwargs):
self._editables[name] = _editable(name)
self._editables[name].run(**kwargs)

# Includes keyboard (glfw.KEY_A) and mouse (glfw.MOUSE_BUTTON_LEFT)
def keydown(self, key):
return key in self._pressed_keys

Expand Down Expand Up @@ -504,18 +506,11 @@ def set_default_style(self, color_scheme='dark', spacing=9, indent=23, scrollbar
s.colors[imgui.COLOR_POPUP_BACKGROUND] = [x * 0.7 + y * 0.3 for x, y in zip(c0, c1)][:3] + [1]

def start(self, loopfunc, workers = (), glfw_init_callback = None):
# allow single thread object
if not hasattr(workers, '__len__'):
workers = (workers,)

for i in range(len(workers)):
workers[i].start()

with self.lock():
impl = GlfwRenderer(self._window)

self._pressed_keys = set()
self._hit_keys = set()

with self.lock():
self.renderer = GlfwRenderer(self._window)

def on_key(window, key, scan, pressed, mods):
if pressed:
Expand All @@ -526,17 +521,35 @@ def on_key(window, key, scan, pressed, mods):
if key in self._pressed_keys:
self._pressed_keys.remove(key) # check seems to be needed over RDP sometimes
if key != glfw.KEY_ESCAPE: # imgui erases text with escape (??)
impl.keyboard_callback(window, key, scan, pressed, mods)
self.renderer.keyboard_callback(window, key, scan, pressed, mods)

def on_mouse_button(window, key, action, mods):
if action == glfw.PRESS:
if key not in self._pressed_keys:
self._hit_keys.add(key)
self._pressed_keys.add(key)
else:
if key in self._pressed_keys:
self._pressed_keys.remove(key)
self.renderer.keyboard_callback(window, key, None, action, mods)

glfw.set_key_callback(self._window, on_key)
glfw.set_mouse_button_callback(self._window, on_mouse_button)

# For settings custom callbacks etc.
if glfw_init_callback is not None:
glfw_init_callback(self._window)

# allow single thread object
if not hasattr(workers, '__len__'):
workers = (workers,)

for i in range(len(workers)):
workers[i].start()

while not glfw.window_should_close(self._window):
glfw.poll_events()
impl.process_inputs()
self.renderer.process_inputs()

if self.keyhit(glfw.KEY_ESCAPE):
glfw.set_window_should_close(self._window, 1)
Expand Down Expand Up @@ -566,7 +579,7 @@ def on_key(window, key, scan, pressed, mods):
gl.glClearColor(0, 0, 0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

impl.render(imgui.get_draw_data())
self.renderer.render(imgui.get_draw_data())

# TODO: compute thread has to wait until sync is done
# and lock is released if calling upload_image()?
Expand Down
4 changes: 4 additions & 0 deletions pyviewer/toolbar_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def update_image(self, img_hwc):
self.img_shape = [C, H, W]
self.v.upload_image(self.output_key, img_hwc)

# Includes keyboard (glfw.KEY_A) and mouse (glfw.MOUSE_BUTTON_LEFT)
def keydown(self, key):
return self.v.keydown(key)

#------------------------
# User-provided functions

Expand Down

0 comments on commit 52fd294

Please sign in to comment.