Skip to content

Commit

Permalink
Merge pull request #325 from nyaoouo/master
Browse files Browse the repository at this point in the history
fix callback in different window context
  • Loading branch information
KinoxKlark committed Apr 21, 2023
2 parents b20c0f5 + 0914acd commit 2cbd438
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
44 changes: 28 additions & 16 deletions imgui/integrations/glfw.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import queue

import glfw
import imgui

from . import compute_fb_scale
from .opengl import ProgrammablePipelineRenderer


class GlfwRenderer(ProgrammablePipelineRenderer):
def __init__(self, window, attach_callbacks:bool=True):
def __init__(self, window, attach_callbacks: bool = True):
super(GlfwRenderer, self).__init__()
self.window = window

self.queue = queue.Queue()

if attach_callbacks:
glfw.set_key_callback(self.window, self.keyboard_callback)
glfw.set_cursor_pos_callback(self.window, self.mouse_callback)
glfw.set_window_size_callback(self.window, self.resize_callback)
glfw.set_char_callback(self.window, self.char_callback)
glfw.set_scroll_callback(self.window, self.scroll_callback)
glfw.set_key_callback(self.window, lambda *a: self.queue.put((self.keyboard_callback, a)))
glfw.set_cursor_pos_callback(self.window, lambda *a: self.queue.put((self.mouse_callback, a)))
glfw.set_window_size_callback(self.window, lambda *a: self.queue.put((self.resize_callback, a)))
glfw.set_char_callback(self.window, lambda *a: self.queue.put((self.char_callback, a)))
glfw.set_scroll_callback(self.window, lambda *a: self.queue.put((self.scroll_callback, a)))

self.io.display_size = glfw.get_framebuffer_size(self.window)
self.io.get_clipboard_text_fn = self._get_clipboard_text
Expand Down Expand Up @@ -68,23 +73,23 @@ def keyboard_callback(self, window, key, scancode, action, mods):
io.keys_down[key] = False

io.key_ctrl = (
io.keys_down[glfw.KEY_LEFT_CONTROL] or
io.keys_down[glfw.KEY_RIGHT_CONTROL]
io.keys_down[glfw.KEY_LEFT_CONTROL] or
io.keys_down[glfw.KEY_RIGHT_CONTROL]
)

io.key_alt = (
io.keys_down[glfw.KEY_LEFT_ALT] or
io.keys_down[glfw.KEY_RIGHT_ALT]
io.keys_down[glfw.KEY_LEFT_ALT] or
io.keys_down[glfw.KEY_RIGHT_ALT]
)

io.key_shift = (
io.keys_down[glfw.KEY_LEFT_SHIFT] or
io.keys_down[glfw.KEY_RIGHT_SHIFT]
io.keys_down[glfw.KEY_LEFT_SHIFT] or
io.keys_down[glfw.KEY_RIGHT_SHIFT]
)

io.key_super = (
io.keys_down[glfw.KEY_LEFT_SUPER] or
io.keys_down[glfw.KEY_RIGHT_SUPER]
io.keys_down[glfw.KEY_LEFT_SUPER] or
io.keys_down[glfw.KEY_RIGHT_SUPER]
)

def char_callback(self, window, char):
Expand All @@ -104,14 +109,21 @@ def scroll_callback(self, window, x_offset, y_offset):
self.io.mouse_wheel = y_offset

def process_inputs(self):
try:
while True:
callback, args = self.queue.get_nowait()
callback(*args)
except queue.Empty:
pass

io = imgui.get_io()

window_size = glfw.get_window_size(self.window)
fb_size = glfw.get_framebuffer_size(self.window)

io.display_size = window_size
io.display_fb_scale = compute_fb_scale(window_size, fb_size)
io.delta_time = 1.0/60
io.delta_time = 1.0 / 60

if glfw.get_window_attrib(self.window, glfw.FOCUSED):
io.mouse_pos = glfw.get_cursor_pos(self.window)
Expand All @@ -128,6 +140,6 @@ def process_inputs(self):
self.io.delta_time = current_time - self._gui_time
else:
self.io.delta_time = 1. / 60.
if(io.delta_time <= 0.0): io.delta_time = 1./ 1000.
if (io.delta_time <= 0.0): io.delta_time = 1. / 1000.

self._gui_time = current_time
20 changes: 15 additions & 5 deletions imgui/integrations/glumpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import numpy as np
import queue

import ctypes
from ctypes import *
Expand Down Expand Up @@ -58,12 +59,14 @@ def __init__(self, window, attach_callbacks=True ):

self.window = window

self.queue = queue.Queue()

if attach_callbacks:
glfw.set_key_callback(self.window, self.keyboard_callback)
glfw.set_cursor_pos_callback(self.window, self.mouse_callback)
glfw.set_window_size_callback(self.window, self.resize_callback)
glfw.set_char_callback(self.window, self.char_callback)
glfw.set_scroll_callback(self.window, self.scroll_callback)
glfw.set_key_callback(self.window, lambda *a: self.queue((self.keyboard_callback,a)))
glfw.set_cursor_pos_callback(self.window, lambda *a: self.queue((self.mouse_callback,a)))
glfw.set_window_size_callback(self.window, lambda *a: self.queue((self.resize_callback,a)))
glfw.set_char_callback(self.window, lambda *a: self.queue((self.char_callback,a)))
glfw.set_scroll_callback(self.window, lambda *a: self.queue((self.scroll_callback,a)))

self.io.display_size = glfw.get_framebuffer_size(self.window)
#self.io.get_clipboard_text_fn = self._get_clipboard_text
Expand Down Expand Up @@ -152,6 +155,13 @@ def scroll_callback(self, window, x_offset, y_offset):
def process_inputs(self):
io = imgui.get_io()

try:
while True:
callback, args = self.queue.get_nowait()
callback(*args)
except queue.Empty:
pass

window_size = glfw.get_window_size(self.window)
fb_size = glfw.get_framebuffer_size(self.window)

Expand Down
28 changes: 19 additions & 9 deletions imgui/integrations/pyglet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import warnings
from distutils.version import LooseVersion

import queue

from pyglet.window import key, mouse, Window
import pyglet
import pyglet.clock
Expand Down Expand Up @@ -55,6 +57,7 @@ def __init__(self):
super(PygletMixin, self).__init__()
self._cursor = -2
self._window = None
self.queue = queue.Queue()
# Let Dear imgui know we have mouse cursor support
self.io.backend_flags |= imgui.BACKEND_HAS_MOUSE_CURSORS

Expand All @@ -78,15 +81,15 @@ def _set_pixel_ratio(self, window):
def _attach_callbacks(self, window):
self._window = window
window.push_handlers(
self.on_mouse_motion,
self.on_key_press,
self.on_key_release,
self.on_text,
self.on_mouse_drag,
self.on_mouse_press,
self.on_mouse_release,
self.on_mouse_scroll,
self.on_resize,
on_mouse_motion = lambda *a: self.queue.put((self.on_mouse_motion, a)),
on_key_press = lambda *a: self.queue.put((self.on_key_press, a)),
on_key_release = lambda *a: self.queue.put((self.on_key_release, a)),
on_text = lambda *a: self.queue.put((self.on_text, a)),
on_mouse_drag = lambda *a: self.queue.put((self.on_mouse_drag, a)),
on_mouse_press = lambda *a: self.queue.put((self.on_mouse_press, a)),
on_mouse_release = lambda *a: self.queue.put((self.on_mouse_release, a)),
on_mouse_scroll = lambda *a: self.queue.put((self.on_mouse_scroll, a)),
on_resize = lambda *a: self.queue.put((self.on_resize, a)),
)


Expand Down Expand Up @@ -184,6 +187,13 @@ def on_resize(self, width, height):
self.io.display_size = width, height

def process_inputs(self):
try:
while True:
callback, args = self.queue.get_nowait()
callback(*args)
except queue.Empty:
pass

io = imgui.get_io()

current_time = pyglet.clock.tick()
Expand Down

0 comments on commit 2cbd438

Please sign in to comment.