예제 #1
0
    def process_inputs(self):
        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

        if glfw.get_window_attrib(self.window, glfw.FOCUSED):
            io.mouse_pos = glfw.get_cursor_pos(self.window)
        else:
            io.mouse_pos = -1, -1

        io.mouse_down[0] = glfw.get_mouse_button(self.window, 0)
        io.mouse_down[1] = glfw.get_mouse_button(self.window, 1)
        io.mouse_down[2] = glfw.get_mouse_button(self.window, 2)

        current_time = glfw.get_time()

        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
예제 #2
0
 def onMouseButton(self, win, button, action, mods):
     #print("mouse button: ", win, button, action, mods)
     if button == glfw.MOUSE_BUTTON_LEFT:
         if mods == glfw.MOD_SHIFT:
             p = list(glfw.get_cursor_pos(win))
             p[1] = self.height - p[1]
             self.index = 0
             for point in self.scene.points:
                 self.index += 1
                 a = abs(p[0] - point[0])
                 b = abs(p[1] - point[1])
                 dist = (a, b)
                 if dist[0] < 20 and dist[1] < 20:
                     self.selectedPoint = point
                 self.changeWeights = True
                 self.scene.updateDeboor()
                 if glfw.get_mouse_button(win, button) == glfw.PRESS:
                     self.changeWeights = True
                 if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                     self.changeWeights = False
                     self.selectedPoint = []
         else:
             if glfw.get_mouse_button(win, button) == glfw.PRESS:
                 pass
             if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                 pos = glfw.get_cursor_pos(win)
                 self.scene.addPoint(pos[0], self.height - pos[1])
예제 #3
0
def checkmouse(window, prev_mouse_pos):
    m_is_pressed = 0
    if glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_1):
        current_mouse_pos = glfw.get_cursor_pos(window)
        rotx = current_mouse_pos[0] - prev_mouse_pos[0]
        roty = current_mouse_pos[1] - prev_mouse_pos[1]
        m_is_pressed = 1
    else:
        rotx, roty = 0, 0

    if glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_3):
        current_mouse_pos2 = glfw.get_cursor_pos(window)
        xtr = current_mouse_pos2[0] - prev_mouse_pos[0]
        ytr = current_mouse_pos2[1] - prev_mouse_pos[1]
        m_is_pressed = 3
    else:
        xtr, ytr = 0, 0

    if glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_4):
        sc = 1.1
        m_is_pressed = 4
    elif glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_2):
        sc = 0.9
        m_is_pressed = 2
    else:
        sc = 1

    return m_is_pressed, rotx, roty, sc, (xtr, ytr)
예제 #4
0
파일: mjviewer.py 프로젝트: zhmz90/rllab
    def handle_mouse_button(self, window, button, act, mods):
        # update button state
        self.button_left_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS
        self.button_middle_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_MIDDLE) == glfw.PRESS
        self.button_right_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS

        # update mouse position
        x, y = glfw.get_cursor_pos(window)
        self.last_mouse_x = int(self.scale * x)
        self.last_mouse_y = int(self.scale * y)

        if not self.model:
            return

        self.gui_lock.acquire()

        # save info
        if act == glfw.PRESS:
            self.last_button = button
            self.last_click_time = glfw.get_time()

        self.gui_lock.release()
    def process_inputs(self):
        # todo: consider moving to init
        io = imgui.get_io()

        w, h = glfw.get_window_size(self.window)
        dw, dh = glfw.get_framebuffer_size(self.window)

        io.display_size = w, h
        io.display_fb_scale = float(dw) / w, float(dh) / h

        io.delta_time = 1.0 / 60

        if glfw.get_window_attrib(self.window, glfw.FOCUSED):
            io.mouse_pos = glfw.get_cursor_pos(self.window)
        else:
            io.mouse_pos = -1, -1

        io.mouse_down[0] = glfw.get_mouse_button(self.window, 0)
        io.mouse_down[1] = glfw.get_mouse_button(self.window, 1)
        io.mouse_down[2] = glfw.get_mouse_button(self.window, 2)

        current_time = glfw.get_time()

        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
예제 #6
0
    def handle_mouse_button(self, window, button, act, mods):
        # update button state
        self._button_left_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS
        self._button_middle_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_MIDDLE) == glfw.PRESS
        self._button_right_pressed = \
                glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS

        # update mouse position
        x, y = glfw.get_cursor_pos(window)
        self._last_mouse_x = int(self._scale * x)
        self._last_mouse_y = int(self._scale * y)

        if not self.model:
            return

        self.gui_lock.acquire()

        # save info
        if act == glfw.PRESS:
            self._last_button = button
            self._last_click_time = glfw.get_time()

        self.gui_lock.release()
예제 #7
0
 def on_mouse_move(self, win, xpos, ypos):
     """ Rotate on left-click & drag, pan on right-click & drag """
     old = self.mouse
     self.mouse = (xpos, glfw.get_window_size(win)[1] - ypos)
     if glfw.get_mouse_button(win, glfw.MOUSE_BUTTON_LEFT):
         self.drag(old, self.mouse, glfw.get_window_size(win))
     if glfw.get_mouse_button(win, glfw.MOUSE_BUTTON_RIGHT):
         self.pan(old, self.mouse)
예제 #8
0
    def _mouse_button_callback(self, window, button, act, mods):
        self._button_left_pressed = (
            glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS)
        self._button_right_pressed = (
            glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS)

        x, y = glfw.get_cursor_pos(window)
        self._last_mouse_x = int(self._scale * x)
        self._last_mouse_y = int(self._scale * y)
예제 #9
0
    def _mouse_button_callback(self, window, button, act, mods):
        self._button_left_pressed = (glfw.get_mouse_button(
            window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS)
        self._button_right_pressed = (glfw.get_mouse_button(
            window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS)

        x, y = glfw.get_cursor_pos(window)
        self._last_mouse_x = int(self._scale * x)
        self._last_mouse_y = int(self._scale * y)
예제 #10
0
 def input_events(self):
     if glfw.get_mouse_button(self._win, 0):
         glRotate(-2, 0, 1, 0)
     if glfw.get_mouse_button(self._win, 1):
         glRotate(2, 0, 1, 0)
     if glfw.get_key(self._win, 32):  #TeclaS
         if not self.is_rotating:
             self.is_rotating = True
     if glfw.get_key(self._win, 68):  #TeclaS
         for polygon in self._list_of_polygons:
             polygon.rotate_around_corner(-2)
예제 #11
0
    def get_mouse_state(self):
        mouse_x, mouse_y = glfw.get_cursor_pos(self.window)
        state_mbl = glfw.get_mouse_button(self.window, glfw.MOUSE_BUTTON_LEFT)
        state_mbm = glfw.get_mouse_button(self.window,
                                          glfw.MOUSE_BUTTON_MIDDLE)
        state_mbr = glfw.get_mouse_button(self.window, glfw.MOUSE_BUTTON_RIGHT)

        return (
            mouse_x,
            mouse_y,
            1 if state_mbl == glfw.PRESS else 0
            | 1 if state_mbm == glfw.PRESS else 0
            | 1 if state_mbr == glfw.PRESS else 0,
        )
예제 #12
0
def hud_loop():
    # look for clicks that fall in the areas covered by HUD elements
    while True:
        events = yield from listen.any(pos=listen.on_cursor_pos(globs.window),
                                       button=listen.on_mouse_button(
                                           globs.window))

        # editing the terrain on cursor movement or click
        if "button" in events or "pos" in events:
            if "button" in events:
                x, y = glfw.get_cursor_pos(globs.window)
            else:
                _, x, y = events["pos"]
            hud_element = mouse_to_hud(
                x, y)  # identify which HUD element the mouse is over

            if glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_LEFT
                                     ) == glfw.PRESS and hud_element != "":
                # left click might select a UI button
                if hud_element == "test":
                    print("Click received. Run test function for HUD element")
                    # TODO add meaningful functions for HUD elements being clicked
                elif hud_element == "inventory":
                    print(
                        "Click received. Run inventory function for HUD element"
                    )
예제 #13
0
파일: viewer.py 프로젝트: rvergne/LandSeed
 def on_mouse_pos(self, _win, x, y):
     if not self.pause:
         if glfw.get_mouse_button(self.win,
                                  glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS:
             self.mouse_offset = (
                 ([x, y] / self.size) - self.mouse_pos_click) * [2, -2]
             self.reload = True
예제 #14
0
    def _poll_mouse(self, window):
        hover_state = glfw.get_window_attrib(window, glfw.HOVERED)
        self._hover = True if hover_state == 1 else False

        if self._hover:
            x, y = glfw.get_cursor_pos(window)
            self._dx = self._x - x
            self._dy = self._y - y
            self._x = x
            self._y = y

            for btn, btn_state in self._buttons.items():
                cur_state = glfw.get_mouse_button(window, btn)
                pressed = cur_state == glfw.PRESS
                released = cur_state == glfw.RELEASE

                if pressed and not btn_state.pressed and not btn_state.held:
                    btn_state.pressed = True
                    btn_state.released = False
                    btn_state.held = True
                elif pressed and btn_state.held:
                    btn_state.pressed = False
                    btn_state.released = False
                    btn_state.held = True
                elif released and btn_state.held:
                    btn_state.pressed = False
                    btn_state.released = True
                    btn_state.held = False
                else:
                    btn_state.pressed = False
                    btn_state.released = False
                    btn_state.held = False
예제 #15
0
 def mousemove(handle, x1, y1):
     nonlocal x0, y0
     if glfw.get_mouse_button(window, 0):
         dx = x1-x0
         dy = y1-y0
         self.camera.transform = glm.inverse(puregl.transform.orbit(glm.inverse(self.camera.transform), dx * 2, dy * 2))
     x0, y0 = x1, y1
예제 #16
0
 def mousemove(handle, x1, y1):
     nonlocal x0, y0
     if glfw.get_mouse_button(window, 0):
         dx = x1-x0
         dy = y1-y0
         self.view = puregl.transform.orbit(self.view, dx * 2, dy * 2)
     x0, y0 = x1, y1
예제 #17
0
    def new_frame(self):
        # todo: consider moving to init
        if not self._font_texture:
            self._create_device_objects()

        io = imgui.get_io()

        w, h = glfw.get_window_size(self.window)
        dw, dh = glfw.get_framebuffer_size(self.window)

        io.display_size = w, h
        io.display_fb_scale = float(dw) / w, float(dh) / h

        io.delta_time = 1.0 / 60

        if glfw.get_window_attrib(self.window, glfw.FOCUSED):
            io.mouse_pos = glfw.get_cursor_pos(self.window)
        else:
            io.mouse_pos = -1, -1

        # todo: py3k compat
        for i in xrange(3):
            io.mouse_down[i] = glfw.get_mouse_button(self.window, i)

        imgui.new_frame()
예제 #18
0
    def get_button_status(self, button):
        """
        ask glfw current button status

        :param button: int, {0:left, 1:right, 2:middle}
        :return: current button status
        """
        return glfw.get_mouse_button(self.window.context.glfw_window, button)
예제 #19
0
    def _on_mouse_move(self, window, x, y):
        mpos = vec2(x, self.height - y)
        diff = mpos - self.prev_mpos
        self.prev_mpos = mpos

        if glfw.get_mouse_button(
                window, glfw.MOUSE_BUTTON_LEFT
        ) == glfw.PRESS and not imgui.get_io().want_capture_mouse:
            self.mvel += 3.0 * diff / self.uniHolder.zoom.value
예제 #20
0
    def _mouse_button_callback(self, window, button, act, mods):
        self._button_left_pressed = (glfw.get_mouse_button(
            window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS)
        self._button_right_pressed = (glfw.get_mouse_button(
            window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS)

        x, y = glfw.get_cursor_pos(window)
        self._last_mouse_x = int(self._scale * x)
        self._last_mouse_y = int(self._scale * y)

        # debug, detect double-click (250ms)
        if act == glfw.PRESS and glfw.get_time(
        ) - self._last_click_time < 0.25 and button == self._last_button:
            # print('double clicked')
            if button == glfw.MOUSE_BUTTON_LEFT:
                self._needselect = 1
            else:
                self._needselect = 2

        if act == glfw.PRESS:
            self._last_button = button
            self._last_click_time = glfw.get_time()

        # debug
        if act == glfw.PRESS:
            newperturb = 0
            if mods & glfw.MOD_ALT:
                # print('enter mod control')
                if self.pert.select > 0:
                    if self._button_right_pressed:
                        newperturb = 1
                    elif self._button_left_pressed:
                        newperturb = 2

                    if newperturb and not self.pert.active:
                        self.init_perturbforce()
            # print('newperturb', newperturb)
            self.pert.active = newperturb

        if act == glfw.RELEASE and mods & glfw.MOD_ALT:
            self.pert.active = 0
예제 #21
0
def mouse_pressed(win, button, action, mods):
    global zooming, rotate_b, translating, actOri, angle, axis, start_p, zoom, mouse_x, mouse_y

    print("Pressing mouse: ", button)

    if button == glfw.MOUSE_BUTTON_MIDDLE:
        if glfw.get_mouse_button(win, button) == glfw.PRESS:
            zooming = True
            start_p = [mouse_x, mouse_y, 0]
            zoom = start_p[1]
        if glfw.get_mouse_button(win, button) == glfw.RELEASE:
            zooming = False

    if button == glfw.MOUSE_BUTTON_LEFT:
        r = min(WIDTH, HEIGHT) / 2.0
        if glfw.get_mouse_button(win, button) == glfw.PRESS:
            rotate_b = True
            start_p = projectOnSphere(mouse_x, mouse_y, r)
        if glfw.get_mouse_button(win, button) == glfw.RELEASE:
            rotate_b = False
            actOri = actOri * rotate(angle, axis)
            angle = 0

    if button == glfw.MOUSE_BUTTON_RIGHT:
        if glfw.get_mouse_button(win, button) == glfw.PRESS:
            translating = True
            start_p = [mouse_x, mouse_y, 0]
        if glfw.get_mouse_button(win, button) == glfw.RELEASE:
            translating = False
예제 #22
0
    def onMouseButton(self, win, button, action, mods):

        if button == glfw.MOUSE_BUTTON_LEFT:
            r = min(self.width, self.height) / 2.0
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.doRotation = True
                self.scene.startP = self.projectOnSphere(
                    self.mouseX, self.mouseY, r)
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.doRotation = False
                self.scene.actOri = self.scene.actOri * self.scene.rotate(
                    self.scene.angle, self.scene.axis)
                self.scene.angle = 0

        if button == glfw.MOUSE_BUTTON_MIDDLE:
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.doZoom = True
                self.scene.startP = [self.mouseX, self.mouseY, 0]
                self.scene.zoom = self.scene.startP[1]
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.doZoom = False

        if button == glfw.MOUSE_BUTTON_RIGHT:
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.doTranslate = True
                self.scene.startP = [self.mouseX, self.mouseY, 0]
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.doTranslate = False
예제 #23
0
    def handle_mouse_move(win, xpos, ypos):
        global selfstatic

        mouse = (xpos, glfw.get_window_size(win)[1] - ypos)
        if glfw.get_mouse_button(win, glfw.MOUSE_BUTTON_LEFT):
            print('yes, left button held', xpos, ypos)
            i = xpos
            j = ypos
            if selfstatic.rotate:
                selfstatic.oo.rx = float(selfstatic.oo.rx) + float(i) / 80.
                selfstatic.oo.ry = float(selfstatic.oo.ry) + float(j) / 80.

        if glfw.get_mouse_button(win, glfw.MOUSE_BUTTON_RIGHT):
            print('yes, right button held', xpos, ypos)

            end_x, end_y = glfw.get_cursor_pos(win)
            i = end_x - start_x
            j = start_y - end_y
            print('relative coordinates', i, j)
            if selfstatic.move:
                selfstatic.oo.tx = float(selfstatic.oo.tx) + float(i) / 20.
                selfstatic.oo.ty = float(selfstatic.oo.ty) + float(j) / 20.
예제 #24
0
    def update(self, elapsed):
        mouse_sensitivity = 0.1
        speed = 3

        # Mouse
        if glfw.get_key(self.window, glfw.KEY_ESCAPE):
            glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_NORMAL)

        if glfw.CURSOR_NORMAL == glfw.get_input_mode(self.window, glfw.CURSOR):
            if glfw.get_mouse_button(self.window, glfw.MOUSE_BUTTON_LEFT):
                glfw.set_input_mode(self.window, glfw.CURSOR,
                                    glfw.CURSOR_DISABLED)
                glfw.set_cursor_pos(self.window, 0, 0)

        else:
            x, y = glfw.get_cursor_pos(self.window)
            self.camera.offset_orientation(mouse_sensitivity * x,
                                           mouse_sensitivity * y)
            glfw.set_cursor_pos(self.window, 0, 0)

        # Keyboard
        if any((glfw.get_key(self.window, glfw.KEY_LEFT_SHIFT),
                glfw.get_key(self.window, glfw.KEY_RIGHT_SHIFT))):
            speed *= 5

        # X & Z (wasd)
        if glfw.get_key(self.window, glfw.KEY_W):
            self.camera.position += elapsed * speed * self.camera.forward()
        elif glfw.get_key(self.window, glfw.KEY_S):
            self.camera.position += elapsed * speed * -self.camera.forward()
        if glfw.get_key(self.window, glfw.KEY_A):
            self.camera.position += elapsed * speed * -self.camera.right()
        elif glfw.get_key(self.window, glfw.KEY_D):
            self.camera.position += elapsed * speed * self.camera.right()

        # Y (space / ctrl)
        if glfw.get_key(self.window, glfw.KEY_SPACE):
            self.camera.position += elapsed * speed * glm.vec3(0, 1, 0)
        elif any((glfw.get_key(self.window, glfw.KEY_LEFT_CONTROL),
                  glfw.get_key(self.window, glfw.KEY_RIGHT_CONTROL))):
            self.camera.position += elapsed * speed * -glm.vec3(0, 1, 0)

        # Center view
        if glfw.get_key(self.window, glfw.KEY_C):
            self.camera.look_at(glm.vec3(0, 0, 0))
        # Reset camera
        if glfw.get_key(self.window, glfw.KEY_R):
            self.camera.position = glm.vec3(1.5, 1.5, 1.5)
            self.camera.look_at(glm.vec3(0, 0, 0))
예제 #25
0
def main():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    ip_address = "localhost:3333"
    #imgui.set_window_font_scale(1.0)
    comms = MouseSocket()
    click_guard = True

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()
        imgui.set_next_window_size(300, 120)
        imgui.set_next_window_position(10, 0)
        imgui.begin("", False, imgui.WINDOW_NO_RESIZE)
        changed, ip_address = imgui.input_text(label="IP:PORT",
                                               value=ip_address,
                                               buffer_length=30)
        clicked = imgui.button(label="CONNECT")
        if clicked:
            comms.connect(ip_address)

        max_x, max_y = glfw.get_window_size(window)
        x, y = glfw.get_cursor_pos(window)

        norm_x = x / max_x
        norm_y = y / max_y

        state = glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_LEFT)
        if state == glfw.PRESS:
            click_guard = False
        elif state == glfw.RELEASE and not click_guard:
            click_guard = True
            comms.send_click(norm_x, norm_y)

        comms.send_mouse_coords(norm_x, norm_y)

        imgui.text("Mouse Coords: {:.2f}, {:.2f}".format(norm_x, norm_y))
        imgui.text("Connected: {}".format(comms.is_connected))
        imgui.end()

        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
예제 #26
0
    def cursor_pos_callback(window, xpos, ypos):
        viewer._cursor_pos_callback(window, xpos, ypos)
        middle_pressed = glfw.get_mouse_button(
            window, glfw.MOUSE_BUTTON_MIDDLE) == glfw.PRESS
        if middle_pressed:
            w, h = glfw.get_window_size(window)
            aspect_ratio = w / h
            x, y = xpos, ypos
            sel_point = np.zeros(3)
            res = mujoco_py.functions.mjv_select(sim.model, sim.data,
                                                 viewer.vopt, aspect_ratio,
                                                 x / w, (h - y) / h,
                                                 viewer.scn, sel_point)
            sel_body, sel_geom = '?', '?'
            if res != -1:
                sel_body = sim.model.body_id2name(sim.model.geom_bodyid[res])
                sel_geom = sim.model.geom_id2name(res)
            print(f'Selected {sel_body} ({sel_geom}) at {sel_point}')

            if callable(callback):
                callback(sel_point, x / w, (h - y) / h)
예제 #27
0
파일: app.py 프로젝트: chxzh/shadyn
    def poll_event(self, window):
        flag = False
        cur_time = glfw.get_time()
        del_time = cur_time - self.last_time
        march = sidle = dive = 0.
        if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS:            
            march = self.speed * del_time
        elif glfw.get_key(window, glfw.KEY_S) == glfw.PRESS:            
            march = - self.speed * del_time
        if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS:            
            sidle = self.speed * del_time
        elif glfw.get_key(window, glfw.KEY_A) == glfw.PRESS:            
            sidle = - self.speed * del_time
        if glfw.get_key(window, glfw.KEY_SPACE) == glfw.PRESS:            
            dive = self.speed * del_time
        elif glfw.get_key(window, glfw.KEY_V) == glfw.PRESS:            
            dive = - self.speed * del_time
        if march or sidle or dive:
            self.translate(march, sidle, dive)
            flag = True
        cur_mx, cur_my = glfw.get_cursor_pos(window)
        if glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS:
            del_mx, del_my = cur_mx - self.last_mx, cur_my - self.last_my
            spin = tilt = 0.
            if del_mx:
                spin = self.rev * del_mx * del_time
#                 self.spin += self.rev * del_mx
            if del_my:
                tilt = self.rev * del_my * del_time
            if spin or tilt:
                self.rotate(spin, tilt)
                flag = True
        self.last_mx, self.last_my = cur_mx, cur_my
        self.last_time = cur_time
        if flag:
            self._update_orientation()
            self._update_view_mat()
예제 #28
0
    def onMouseButton(self, win, button, action, mods):
        print("mouse button: ", win, button, action, mods)

        r = min(self.width, self.height) / 2.0
        if button == glfw.MOUSE_BUTTON_LEFT:
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.scene.doRotate = True
                self.scene.startPoint = self.scene.projectOnSphere(self.mouse_x, self.mouse_y, r)
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.scene.doRotate = False
                self.scene.curOrientation = self.scene.curOrientation*self.scene.rotate(self.scene.angle, self.scene.axis)
                self.scene.angle = 0
        if button == glfw.MOUSE_BUTTON_MIDDLE:
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.scene.zooming = True
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.scene.zooming = False
        if button == glfw.MOUSE_BUTTON_RIGHT:
            if glfw.get_mouse_button(win, button) == glfw.PRESS:
                self.scene.translation = True
            if glfw.get_mouse_button(win, button) == glfw.RELEASE:
                self.scene.translation = False
예제 #29
0
def grid_editor_loop():

    # currently selected asset
    cursor_asset = 0 # an index in globs.grid.assets

    # a quadarray for the cursor
    cursor_array = QuadArray(globs.assets[globs.grid.assets[cursor_asset]])
    cursor_array.update()
    globs.quadarrays.append(cursor_array)

    # convert mouse coordinates to grid coordinates with rounding
    def mouse_to_grid(x,y):
        coords = mouse_coords(x,y)
        x0,y0 = np.floor(coords.x),np.floor(coords.y)
        while x0 > x: x0 -= 1
        while y0 > y: y0 -= 1
        return x0,y0

    while True:
        events = yield from listen.any(
                enter=listen.on_cursor_enter(globs.window),
                pos=listen.on_cursor_pos(globs.window),
                scroll=listen.on_scroll(globs.window),
                button=listen.on_mouse_button(globs.window)
                )

        if globs.mode != "grid_edit": continue

        if "button" in events:
            _, button, action, mods = events["button"]

            # releasing any mouse button saves
            if action == glfw.RELEASE:
                globs.grid.save()

            # middle click
            if (button == glfw.MOUSE_BUTTON_MIDDLE and\
                    action == glfw.PRESS):
                x,y = glfw.get_cursor_pos(globs.window)
                x,y = mouse_to_grid(x,y)
                if (x,y) in globs.grid:
                    # click on a grid tile: make that tile the cursor tile
                    asset = globs.grid[x,y]
                    cursor_asset = globs.grid.assets.index(asset)

                    cursor_array.asset.clear()
                    cursor_array.asset.enqueue(globs.assets[globs.grid.assets[cursor_asset]],loop=True)
                    cursor_array.update()
                else:
                    # click on empty space with middle mouse: place the player there and go into play mode
                    place_player(x,y)
                    center_camera(x,y)
                    # delete the cursor quad if we have it
                    if len(cursor_array.quads) > 0:
                        del cursor_array.quads[0]
                        cursor_array.update()
                    globs.mode = "play"

        # editing the terrain on cursor movement or click
        if "button" in events or "pos" in events:
            if "button" in events: x,y = glfw.get_cursor_pos(globs.window)
            else: _,x,y = events["pos"]
            x,y = mouse_to_grid(x,y)

            if glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS:
                # left click places the current asset
                asset = globs.grid.assets[cursor_asset]
                globs.grid[x,y] = asset
            elif glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS:
                # right click deletes
                del globs.grid[x,y]


        # scrolling changes the cursor asset
        if "scroll" in events:
            _, _, dy = events["scroll"]

            if dy > 0:
                cursor_asset += 1
                if cursor_asset == len(globs.grid.assets):
                    cursor_asset = 0

            if dy < 0:
                cursor_asset -= 1
                if cursor_asset == -1:
                    cursor_asset = len(globs.grid.assets)-1

            cursor_array.asset.clear()
            cursor_array.asset.enqueue(globs.assets[globs.grid.assets[cursor_asset]],loop=True)
            cursor_array.update()

        # remove the cursor quad when the mouse leaves the window
        if "enter" in events:
            _, entered = events["enter"]
            if not entered:
                if len(cursor_array.quads) > 0:
                    del cursor_array.quads[0]
                cursor_array.update()

        # move the cursor quad to the mouse position
        if "pos" in events:
            _,x,y = events["pos"]

            x,y = mouse_to_grid(x,y)

            if len(cursor_array.quads) == 0:
                cursor_array.quads.append(Vec(0,0,0.5))

            cursor_array.quads[0].x = x
            cursor_array.quads[0].y = y
            cursor_array.update()
예제 #30
0
# I may place this code in a helper function in the future

currentTime = glfw.get_time()
prevMouseX, prevMouseY = glfw.get_cursor_pos(window)

while not glfw.window_should_close(window):
    prevTime = currentTime
    currentTime = glfw.get_time()
    dt = currentTime - prevTime

    keyStateMap = {}
    for name, id in g_glfwKeymap.items():
        keyStateMap[name] = glfw.get_key(window, id) == glfw.PRESS

    for name, id in g_glfwMouseMap.items():
        keyStateMap[name] = glfw.get_mouse_button(window, id) == glfw.PRESS

    imgui.new_frame()
    imgui.set_next_window_size(430.0, 450.0, imgui.FIRST_USE_EVER)
    imgui.begin("Tweak variables")

    mouseX, mouseY = glfw.get_cursor_pos(window)
    g_mousePos = [mouseX, mouseY]
    mouseDelta = [mouseX - prevMouseX, mouseY - prevMouseY]
    prevMouseX, prevMouseY = mouseX, mouseY

    # Udpate 'game logic'
    imIo = imgui.get_io()
    if imIo.want_capture_mouse:
        mouseDelta = [0, 0]
    update(dt, keyStateMap, mouseDelta)
예제 #31
0
 def is_mouse_pressed(self, key):
     return glfw.get_mouse_button(self.window, key)
예제 #32
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 800, 600

    # glfw.window_hint(glfw.RESIZABLE, GL_FALSE)

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)

    obj = ObjLoader()
    obj.load_model("res/face.obj")

    texture_offset = len(obj.vertex_index) * 12
    normal_offset = (texture_offset + len(obj.texture_index) * 8)

    shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs",
                                         "shaders/video_18_frag.fs")

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model),
                 obj.model, GL_STATIC_DRAW)

    # positions
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2,
                          ctypes.c_void_p(texture_offset))
    glEnableVertexAttribArray(1)
    # normals
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3,
                          ctypes.c_void_p(normal_offset))
    glEnableVertexAttribArray(2)

    texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texture)
    # Set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    # Set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    # load image
    image = Image.open("res/african_head_diffuse.tga")
    flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
    img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0,
                 GL_RGB, GL_UNSIGNED_BYTE, img_data)
    glEnable(GL_TEXTURE_2D)

    glUseProgram(shader)

    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)
    # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

    view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0,
                                                               -3.0]))
    projection = pyrr.matrix44.create_perspective_projection_matrix(
        65.0, w_width / w_height, 0.1, 100.0)
    model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0,
                                                                0.0]))

    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "projection")
    model_loc = glGetUniformLocation(shader, "model")
    transform_loc = glGetUniformLocation(shader, "transform")
    light_loc = glGetUniformLocation(shader, "light")

    glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
    glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)

    position = None
    position1 = 0
    position2 = 0
    coef = -0.05

    while not glfw.window_should_close(window):
        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        # rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() )

        if (glfw.get_mouse_button(window, 1) == 1):
            if position is None:
                if position2 is not None:
                    position = coef * glfw.get_cursor_pos(
                        window)[0] - position2
                else:
                    position = coef * glfw.get_cursor_pos(window)[0]
                position1 = position
            rot_y = pyrr.Matrix44.from_y_rotation(position1 - position)
            position1 = coef * glfw.get_cursor_pos(window)[0]
            position2 = None
        else:
            if position2 is None:
                position2 = position1 - position
            rot_y = pyrr.Matrix44.from_y_rotation(position2)
            position = None

        glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y)
        glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_y)

        glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))

        glfw.swap_buffers(window)

    glfw.terminate()
예제 #33
0
def runProgram(title,
               startWidth,
               startHeight,
               renderFrame,
               initResources=None,
               drawUi=None,
               update=None):
    global g_simpleShader
    global g_vertexArrayObject
    global g_vertexDataBuffer
    global g_mousePos
    global g_coordinateSystemModel
    global g_numMsaaSamples
    global g_currentMsaaSamples

    if not glfw.init():
        sys.exit(1)

    window, impl = initGlFwAndResources(title, startWidth, startHeight,
                                        initResources)

    currentTime = glfw.get_time()
    prevMouseX, prevMouseY = glfw.get_cursor_pos(window)

    while not glfw.window_should_close(window):
        prevTime = currentTime
        currentTime = glfw.get_time()
        dt = currentTime - prevTime

        keyStateMap = {}
        for name, id in g_glfwKeymap.items():
            keyStateMap[name] = glfw.get_key(window, id) == glfw.PRESS

        for name, id in g_glfwMouseMap.items():
            keyStateMap[name] = glfw.get_mouse_button(window, id) == glfw.PRESS

        mouseX, mouseY = glfw.get_cursor_pos(window)
        g_mousePos = [mouseX, mouseY]

        # Udpate 'game logic'
        if update:
            imIo = imgui.get_io()
            mouseDelta = [mouseX - prevMouseX, mouseY - prevMouseY]
            if imIo.want_capture_mouse:
                mouseDelta = [0, 0]
            update(dt, keyStateMap, mouseDelta)
        prevMouseX, prevMouseY = mouseX, mouseY

        width, height = glfw.get_framebuffer_size(window)

        imgui.new_frame()

        imgui.set_next_window_position(5.0, 5.0)
        imgui.set_next_window_size(400.0, 620.0, imgui.FIRST_USE_EVER)
        imgui.begin("UI", 0)

        if drawUi:
            drawUi(width, height)

        drawWidth = width
        uiWidth = 0

        drawWidth -= uiWidth

        renderFrame(uiWidth, drawWidth, height)

        #drawCoordinateSystem()

        #mgui.show_test_window()

        imgui.end()

        imgui.render()
        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()
        impl.process_inputs()

    glfw.terminate()