def process_inputs(self): # todo: consider moving to init io = imgui.get_io() w, h = glfw.glfwGetWindowSize(self.handle) dw, dh = glfw.glfwGetFramebufferSize(self.handle) io.display_size = w, h io.display_fb_scale = float(dw) / w, float(dh) / h io.delta_time = 1.0 / 60 if glfw.glfwGetWindowAttrib(self.handle, glfw.GLFW_FOCUSED): io.mouse_pos = glfw.glfwGetCursorPos(self.handle) else: io.mouse_pos = -1, -1 io.mouse_down[0] = glfw.glfwGetMouseButton(self.handle, 0) io.mouse_down[1] = glfw.glfwGetMouseButton(self.handle, 1) io.mouse_down[2] = glfw.glfwGetMouseButton(self.handle, 2) current_time = glfw.glfwGetTime() if self._gui_time: self.io.delta_time = current_time - self._gui_time else: self.io.delta_time = 1. / 60. self._gui_time = current_time
def __init__(self, window, attach_callbacks=True): super(GlumpyGlfwRenderer, self).__init__() self.window = window self.handle = window._native_window if attach_callbacks: glfw.glfwSetKeyCallback(self.handle, self.keyboard_callback) glfw.glfwSetCursorPosCallback(self.handle, self.mouse_callback) glfw.glfwSetWindowSizeCallback(self.handle, self.resize_callback) glfw.glfwSetCharCallback(self.handle, self.char_callback) glfw.glfwSetScrollCallback(self.handle, self.scroll_callback) self.io.display_size = glfw.glfwGetFramebufferSize(self.handle) self._map_keys() self._gui_time = None
def get_size(self): # self._width, self._height = glfw.glfwGetWindowSize(self._native_window) self._width, self._height = glfw.glfwGetFramebufferSize( self._native_window) return self._width, self._height
def set_size(self, width, height): glfw.glfwSetWindowSize(self._native_window, width, height) self._width, self._height = glfw.glfwGetFramebufferSize( self._native_window)
def __init__(self, width=512, height=512, title=None, visible=True, aspect=None, decoration=True, fullscreen=False, config=None, context=None, color=(0, 0, 0, 1), vsync=False): window.Window.__init__(self, width=width, height=height, title=title, visible=visible, aspect=aspect, decoration=decoration, fullscreen=fullscreen, config=config, context=context, color=color) # Whether hidpi is active self._hidpi = False def on_error(error, message): log.warning(message) glfw.glfwSetErrorCallback(on_error) glfw.glfwWindowHint(glfw.GLFW_RESIZABLE, True) glfw.glfwWindowHint(glfw.GLFW_DECORATED, True) glfw.glfwWindowHint(glfw.GLFW_VISIBLE, True) if not decoration: glfw.glfwWindowHint(glfw.GLFW_DECORATED, False) if not visible: glfw.glfwWindowHint(glfw.GLFW_VISIBLE, False) if config is None: config = configuration.Configuration() set_configuration(config) self._native_window = glfw.glfwCreateWindow(self._width, self._height, self._title, None, None) if not self._native_window: log.critical("Window creation failed") __exit__() sys.exit() glfw.glfwMakeContextCurrent(self._native_window) glfw.glfwSwapInterval(1 if vsync else 0) # OSX: check framebuffer size / window size. On retina display, they # can be different so we try to correct window size such as having # the framebuffer size of the right size w, h = glfw.glfwGetFramebufferSize(self._native_window) if w != width or h != height: width, height = width // 2, height // 2 glfw.glfwSetWindowSize(self._native_window, width, height) log.info("HiDPI detected, fixing window size") self._hidpi = True def on_framebuffer_resize(win, width, height): self._width, self._height = width, height self.dispatch_event('on_resize', width, height) glfw.glfwSetFramebufferSizeCallback(self._native_window, on_framebuffer_resize) # def on_resize(win, width, height): # self._width, self._height = width, height # self.dispatch_event('on_resize', width, height) # glfw.glfwSetWindowSizeCallback(self._native_window, on_resize) def on_cursor_enter(win, entered): if entered: self.dispatch_event('on_enter') else: self.dispatch_event('on_leave') glfw.glfwSetCursorEnterCallback(self._native_window, on_cursor_enter) def on_window_close(win): self.close() glfw.glfwSetWindowCloseCallback(self._native_window, on_window_close) def on_keyboard(win, key, scancode, action, mods): symbol = self._keyboard_translate(key) modifiers = self._modifiers_translate(mods) if action in [glfw.GLFW_PRESS, glfw.GLFW_REPEAT]: self.dispatch_event('on_key_press', symbol, modifiers) else: self.dispatch_event('on_key_release', symbol, modifiers) glfw.glfwSetKeyCallback(self._native_window, on_keyboard) def on_character(win, character): self.dispatch_event('on_character', u"%c" % character) glfw.glfwSetCharCallback(self._native_window, on_character) def on_mouse_button(win, button, action, mods): x, y = glfw.glfwGetCursorPos(win) if self._hidpi: x, y = 2 * x, 2 * y button = __mouse_map__.get(button, window.mouse.UNKNOWN) if action == glfw.GLFW_RELEASE: self._button = window.mouse.NONE self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_release', x, y, button) elif action == glfw.GLFW_PRESS: self._button = button self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_press', x, y, button) glfw.glfwSetMouseButtonCallback(self._native_window, on_mouse_button) def on_mouse_motion(win, x, y): if self._hidpi: x, y = 2 * x, 2 * y dx = x - self._mouse_x dy = y - self._mouse_y self._mouse_x = x self._mouse_y = y if self._button != window.mouse.NONE: self.dispatch_event('on_mouse_drag', x, y, dx, dy, self._button) else: self.dispatch_event('on_mouse_motion', x, y, dx, dy) glfw.glfwSetCursorPosCallback(self._native_window, on_mouse_motion) def on_scroll(win, xoffset, yoffset): x, y = glfw.glfwGetCursorPos(win) if self._hidpi: x, y = 2 * x, 2 * y self.dispatch_event('on_mouse_scroll', x, y, xoffset, yoffset) glfw.glfwSetScrollCallback(self._native_window, on_scroll) self._width, self._height = self.get_size() __windows__.append(self)
def get_size(self): # self._width, self._height = glfw.glfwGetWindowSize(self._native_window) self._width, self._height = glfw.glfwGetFramebufferSize(self._native_window) return self._width, self._height
def set_size(self, width, height): glfw.glfwSetWindowSize(self._native_window, width, height) self._width, self._height = glfw.glfwGetFramebufferSize(self._native_window)
def __init__( self, width=512, height=512, title=None, visible=True, aspect=None, decoration=True, fullscreen=False, config=None, context=None, color=(0,0,0,1)): window.Window.__init__(self, width=width, height=height, title=title, visible=visible, aspect=aspect, decoration=decoration, fullscreen=fullscreen, config=config, context=context, color=color) # Whether hidpi is active self._hidpi = False def on_error(error, message): log.warning(message) glfw.glfwSetErrorCallback(on_error) glfw.glfwWindowHint(glfw.GLFW_RESIZABLE, True) glfw.glfwWindowHint(glfw.GLFW_DECORATED, True) glfw.glfwWindowHint(glfw.GLFW_VISIBLE, True) if not decoration: glfw.glfwWindowHint(glfw.GLFW_DECORATED, False) if not visible: glfw.glfwWindowHint(glfw.GLFW_VISIBLE, False) if config is None: config = configuration.Configuration() set_configuration(config) self._native_window = glfw.glfwCreateWindow( self._width, self._height, self._title, None, None) if not self._native_window: log.critical("Window creation failed") __exit__() sys.exit() glfw.glfwMakeContextCurrent(self._native_window) glfw.glfwSwapInterval(0) # OSX: check framebuffer size / window size. On retina display, they # can be different so we try to correct window size such as having # the framebuffer size of the right size w,h = glfw.glfwGetFramebufferSize(self._native_window) if w != width or h!= height: width, height = width//2, height//2 glfw.glfwSetWindowSize(self._native_window, width, height) log.info("HiDPI detected, fixing window size") self._hidpi = True def on_framebuffer_resize(win, width, height): self._width, self._height = width, height self.dispatch_event('on_resize', width, height) glfw.glfwSetFramebufferSizeCallback(self._native_window, on_framebuffer_resize) # def on_resize(win, width, height): # self._width, self._height = width, height # self.dispatch_event('on_resize', width, height) # glfw.glfwSetWindowSizeCallback(self._native_window, on_resize) def on_cursor_enter(win, entered): if entered: self.dispatch_event('on_enter') else: self.dispatch_event('on_leave') glfw.glfwSetCursorEnterCallback(self._native_window, on_cursor_enter) def on_window_close(win): self.close() glfw.glfwSetWindowCloseCallback(self._native_window, on_window_close) def on_keyboard(win, key, scancode, action, mods): symbol = self._keyboard_translate(key) modifiers = self._modifiers_translate(mods) if action in[glfw.GLFW_PRESS,glfw.GLFW_REPEAT]: self.dispatch_event('on_key_press', symbol, modifiers) else: self.dispatch_event('on_key_release', symbol, modifiers) glfw.glfwSetKeyCallback(self._native_window, on_keyboard) def on_character(win, character): self.dispatch_event('on_character', u"%c" % character) glfw.glfwSetCharCallback(self._native_window, on_character) def on_mouse_button(win, button, action, mods): x,y = glfw.glfwGetCursorPos(win) if self._hidpi: x, y = 2*x, 2*y button = __mouse_map__.get(button, window.mouse.UNKNOWN) if action == glfw.GLFW_RELEASE: self._button = window.mouse.NONE self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_release', x, y, button) elif action == glfw.GLFW_PRESS: self._button = button self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_press', x, y, button) glfw.glfwSetMouseButtonCallback(self._native_window, on_mouse_button) def on_mouse_motion(win, x, y): if self._hidpi: x, y = 2*x, 2*y dx = x - self._mouse_x dy = y - self._mouse_y self._mouse_x = x self._mouse_y = y if self._button != window.mouse.NONE: self.dispatch_event('on_mouse_drag', x, y, dx, dy, self._button) else: self.dispatch_event('on_mouse_motion', x, y, dx, dy) glfw.glfwSetCursorPosCallback(self._native_window, on_mouse_motion) def on_scroll(win, xoffset, yoffset): x,y = glfw.glfwGetCursorPos(win) if self._hidpi: x, y = 2*x, 2*y self.dispatch_event('on_mouse_scroll', x, y, xoffset, yoffset) glfw.glfwSetScrollCallback( self._native_window, on_scroll ) self._width, self._height = self.get_size() __windows__.append(self)