예제 #1
0
    def open(self,
             size=(800, 800),
             pos=(50, 50),
             gui_scale=1.0,
             ui_config=None):
        if self._window:
            return

        glfw.glfwInit()
        glfw.glfwWindowHint(glfw.GLFW_SCALE_TO_MONITOR, glfw.GLFW_TRUE)
        # Window name needs to be equal to `StartupWMClass` field in Linux .desktop file
        # else the icon will not show correctly on Linux!
        self._window = glfw.glfwCreateWindow(*size,
                                             "Pupil Invisible Monitor",
                                             monitor=None,
                                             share=None)
        glfw.glfwSetWindowSizeLimits(self._window, 200, 200,
                                     glfw.GLFW_DONT_CARE, glfw.GLFW_DONT_CARE)
        glfw.glfwSetWindowPos(self._window, *pos)
        glfw.glfwMakeContextCurrent(self._window)

        cygl.utils.init()

        self.gui = ui.UI()
        self.gui_user_scale = gui_scale

        # Adding an intermediate container fixes a pylgui display bug
        self.cont = ui.Container((0, 0), (0, 0), (0, 0))
        self.quickbar = ui.Horizontally_Stretching_Menu(
            "Quick Bar", (0.0, -120.0), (0.0, 0.0))
        self.cont.append(self.quickbar)
        self.gui.append(self.cont)

        # Register callbacks main_window
        glfw.glfwSetFramebufferSizeCallback(self._window,
                                            self.on_framebuffer_resize)
        glfw.glfwSetKeyCallback(self._window, self.on_window_key)
        glfw.glfwSetCharCallback(self._window, self.on_window_char)
        glfw.glfwSetMouseButtonCallback(self._window,
                                        self.on_window_mouse_button)
        glfw.glfwSetCursorPosCallback(self._window, self.on_pos)
        glfw.glfwSetScrollCallback(self._window, self.on_scroll)
        self.gui.configuration = ui_config or {}
        gl_utils.basic_gl_setup()

        # Perform an initial window size setup
        self.manual_resize()
예제 #2
0
    def __init__(self,
                 width=512,
                 height=512,
                 title=None,
                 visible=True,
                 aspect=None,
                 decoration=True,
                 fullscreen=False,
                 screen=None,
                 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,
                               screen=screen,
                               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)

        monitor = glfw.glfwGetMonitors()[self._screen] if fullscreen else None
        if fullscreen:
            mode = glfw.glfwGetVideoMode(monitor)
            self._width, self._height = mode[:2]

        self._native_window = glfw.glfwCreateWindow(self._width, self._height,
                                                    self._title, monitor, 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)

        imgui.create_context()
        self.imguiRenderer = GlfwRenderer(self._native_window,
                                          attach_callbacks=False)

        # 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 platform == 'darwin' and (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 imgui.get_io().want_capture_mouse: return

            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)