Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)