Exemple #1
0
    def handle_mouse_button(win, button, act, mods):
        global selfstatic, start_x, start_y

        if button == glfw.MOUSE_BUTTON_LEFT and act == glfw.RELEASE:
            rotate = False
            #update_rotate_vals(oo)
            xx, yy = glfw.get_cursor_pos(win)
            selfstatic.storeit = selfstatic.map.mouseLeftClick(xx, yy)

            v = ray_intersect_triangle(np.array(selfstatic.storeit[0]), np.array(selfstatic.storeit[1]), \
                np.array([[ 0.0, 1.0, 0.0], [-1.0,-1.0, 0.0],[ 1.0,-1.0, 0.0]]) )
            if v == 1:
                print('intersect (ray intersect triangle method):', v)

        elif button == glfw.MOUSE_BUTTON_MIDDLE and act == glfw.RELEASE:
            print('middle mouse, release up')
        elif button == glfw.MOUSE_BUTTON_RIGHT and act == glfw.RELEASE:
            selfstatic.move = False
            #update_move_vals(oo)

        elif button == glfw.MOUSE_BUTTON_LEFT and glfw.PRESS:
            x, y = glfw.get_cursor_pos(win)
            mouse(x, y)
            selfstatic.rotate = True
        elif button == glfw.MOUSE_BUTTON_MIDDLE and glfw.PRESS:
            print('middle mouse click down')
        elif button == glfw.MOUSE_BUTTON_RIGHT and glfw.PRESS:
            selfstatic.move = True
            start_x, start_y = glfw.get_cursor_pos(win)
def event_button(window, button, action, mods):
    global pos_button_start

    if (button == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS
            and (mods not in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.start_rotation()
        pos_button_start = glfw.get_cursor_pos(window)
    if (button == glfw.MOUSE_BUTTON_LEFT and action == glfw.RELEASE
            and (mods not in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.stop_rotation()

    if (button == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS
            and (mods in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.start_translation()
        pos_button_start = glfw.get_cursor_pos(window)

    if (button == glfw.MOUSE_BUTTON_LEFT and action == glfw.RELEASE
            and (mods in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.stop_translation()

    if (button == glfw.MOUSE_BUTTON_RIGHT and action == glfw.PRESS
            and (mods not in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.start_append()
    if (button == glfw.MOUSE_BUTTON_RIGHT and action == glfw.PRESS
            and (mods in [glfw.MOD_CONTROL, glfw.MOD_SHIFT])):
        R.start_remove()

    return
Exemple #3
0
 def _mouse_button(self, window, button, action, mods):
     if action == glfw.PRESS:
         self.mouse.fire_button_down(button,
                                     *glfw.get_cursor_pos(self.window))
     if action == glfw.RELEASE:
         self.mouse.fire_button_up(button,
                                   *glfw.get_cursor_pos(self.window))
Exemple #4
0
def button_callback(window, button, action, mod):
    global azi, ele, left, right, up, pre, cu

    if button == glfw.MOUSE_BUTTON_LEFT:
        if action == glfw.PRESS:
            pre = glfw.get_cursor_pos(window)
            cur = glfw.get_cursor_pos(window)
            left = 1
        elif action == glfw.RELEASE:
            left = 0
            circle = 2 * np.pi

            if ele >= circle:
                while ele >= circle:
                    ele -= circle
            if ele <= 0:
                while ele <= 0:
                    ele += circle

            if azi >= circle:
                while azi >= circle:
                    azi -= circle
            if azi <= 0:
                while azi <= 0:
                    azi += circle

    elif button == glfw.MOUSE_BUTTON_RIGHT:
        if action == glfw.PRESS:
            pre = glfw.get_cursor_pos(window)
            cur = glfw.get_cursor_pos(window)
            right = 1
        elif action == glfw.RELEASE:
            right = 0
Exemple #5
0
def cursor_callback(window, xpos, ypos):
    global azimuth
    global elevation
    global oldpos
    global newpos
    global enableOrbit
    global origin
    global up
    global w
    global u
    global v
    if enableOrbit:
        oldpos = newpos
        newpos = glfw.get_cursor_pos(window)
        elevation -= (oldpos[1] - newpos[1])/100
        if np.cos(elevation)<0:
            up = -1
        else:
            up = 1
        azimuth += face*(oldpos[0] - newpos[0])/100
        
    if enablePanning:
        oldpos = newpos
        newpos = glfw.get_cursor_pos(window)
        origin += ((oldpos[0] - newpos[0])*u+(oldpos[1] - newpos[1])*v)/500
Exemple #6
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])
Exemple #7
0
def button_callback(window, button, action, mod):
    global azimuth
    global elevation
    global oldpos
    global newpos
    global enableOrbit
    global enablePanning
    global face
    global up
    if button==glfw.MOUSE_BUTTON_LEFT:
        if action==glfw.PRESS:
            oldpos = glfw.get_cursor_pos(window)
            newpos = glfw.get_cursor_pos(window)
            enableOrbit = True
        elif action==glfw.RELEASE:
            enableOrbit = False
            face = up
            while azimuth > 2*np.pi:
                azimuth -= 2*np.pi
            while azimuth < 0:
                azimuth += 2*np.pi
            while elevation > 2*np.pi:
                elevation -= 2*np.pi
            while elevation < 0:
                elevation += 2*np.pi
    if button==glfw.MOUSE_BUTTON_RIGHT:
        if action==glfw.PRESS:
            oldpos = glfw.get_cursor_pos(window)
            newpos = glfw.get_cursor_pos(window)
            enablePanning = True
        elif action==glfw.RELEASE:
            enablePanning = False
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)
def glfw_mouse_button_callback(window, key, action, mods):
    x = ffi.new('double*')
    y = ffi.new('double*')
    if key == glfw.MOUSE_BUTTON_1 and action == glfw.PRESS:
        glfw.get_cursor_pos(window, x, y)
        """log click position"""
        log('click pos: x=%s ,y=%s' % (x[0], y[0]))
        cursor_position_callback_at_phase(window, 2, x[0], y[0])
        glfw.set_cursor_pos_callback(window, cursor_position_callback)

    if key == glfw.MOUSE_BUTTON_1 and action == glfw.RELEASE:
        glfw.get_cursor_pos(window, x, y)
        cursor_position_callback_at_phase(window, 1, x[0], y[0])
        glfw.set_cursor_pos_callback(window, ffi.NULL)
Exemple #10
0
def button_callback(window, button, action, mod):
    global x, y, left, right, at
    if button == glfw.MOUSE_BUTTON_LEFT:
        if action == glfw.PRESS or action == glfw.REPEAT:
            x, y = glfw.get_cursor_pos(window)
            left = True
        elif action == glfw.RELEASE:
            left = False
    if button == glfw.MOUSE_BUTTON_RIGHT:
        if action == glfw.PRESS or action == glfw.REPEAT:
            x, y = glfw.get_cursor_pos(window)
            right = True
        elif action == glfw.RELEASE:
            right = False
Exemple #11
0
 def start_drag(self):
     glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_HIDDEN)
     wx, wy = glfw.get_window_pos(self.window)
     cx, cy = glfw.get_cursor_pos(self.window)
     self.drag_start_winpos = ivec2(wx, wy)
     self.drag_offset = ivec2(cx, cy)
     self.is_drag = True
Exemple #12
0
 def on_mouse_button(self, _win, _button, action, _mods):
     if action == glfw.PRESS:
         self.mouse_pos_click = np.array(glfw.get_cursor_pos(
             self.win)) / self.size
     elif action == glfw.RELEASE:
         self.mouse_pos += self.mouse_offset
         self.mouse_offset = np.array([0, 0])
Exemple #13
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"
                    )
Exemple #14
0
 def on_mouse_button(self, window, button, action, mods):
     if button == glfw.MOUSE_BUTTON_LEFT:
         if action == glfw.PRESS:
             self.is_drag = True
             self.prevpos = vec2(*glfw.get_cursor_pos(window))
         elif action == glfw.RELEASE:
             self.is_drag = False
Exemple #15
0
def key_callback(window, key, scancode, action, mods):
	global gComposedM
	if key==glfw.KEY_Q:
		if action==glfw.PRESS:
			new=np.array([[1.0,0.0,-0.1],
                                      [0.0,1.0,.0],
                                      [0.0,0.0,1.0]])
			gComposedM = new @ gComposedM
	if key==glfw.KEY_E:
		if action==glfw.PRESS:
			new=np.array([[1.0,0.0,0.1],
                                      [0.0,1.0,.0],
                                      [0.0,0.0,1.0]])
			gComposedM = new @ gComposedM
	if key==glfw.KEY_A:
		if action==glfw.PRESS:
			th = np.radians(10)
			new=np.array([[np.cos(th),-np.sin(th),.0],
                                       [np.sin(th),np.cos(th),.0],
                                       [.0,.0,1.0]])
			gComposedM = gComposedM @ new
	if key==glfw.KEY_D:
		if action==glfw.PRESS:
			th = np.radians(10)
			new=np.array([[np.cos(th),np.sin(th),.0],
                                       [-np.sin(th),np.cos(th),.0],
                                       [.0,.0,1.0]])
			gComposedM = gComposedM @ new
	if key==glfw.KEY_1:
		if action==glfw.PRESS:
			gComposedM = np.identity(3)
	elif key==glfw.KEY_SPACE and action==glfw.PRESS:
		print ('press space: (%d, %d)'%glfw.get_cursor_pos(window))
Exemple #16
0
 def mouse_exclusivity(self, value: bool):
     self._mouse_exclusivity = value
     if value is True:
         self._mouse_pos = glfw.get_cursor_pos(self._window)
         glfw.set_input_mode(self._window, glfw.CURSOR, glfw.CURSOR_DISABLED)
     else:
         self.cursor = self._cursor
Exemple #17
0
def delete_point():
    global points
    if len(points) >= 1:
        x, y = glfw.get_cursor_pos(window)
        num = select_point(x, y)
        if not num == None:
            points = np.delete(points, num, axis=0)
Exemple #18
0
def button_callback(window, button, action, mod):
    global xpos_start, ypos_start, x_for_panning, y_for_panning

    if button == glfw.MOUSE_BUTTON_LEFT:
        if action == glfw.PRESS:
            (xpos_start, ypos_start) = glfw.get_cursor_pos(window)
            glfw.set_cursor_pos_callback(window, cursor_callback1)
        elif action == glfw.RELEASE:
            glfw.set_cursor_pos_callback(window, cursor_callback_end)

    if button == glfw.MOUSE_BUTTON_RIGHT:
        if action == glfw.PRESS:
            (x_for_panning, y_for_panning) = glfw.get_cursor_pos(window)
            glfw.set_cursor_pos_callback(window, cursor_callback2)
        elif action == glfw.RELEASE:
            glfw.set_cursor_pos_callback(window, cursor_callback_end)
Exemple #19
0
def _onGLFWMouseButton(*args, **kwargs):
    """Callback for mouse press events. Both press and release actions are
    handled by this function as they both invoke the same callback.

    """
    global mouseButtons, mouseClick, mouseTimes
    now = psychopy.core.getTime()
    win_ptr, button, action, modifier = args
    # win = glfw.get_window_user_pointer(win_ptr)

    # get current position of the mouse
    # this might not be at the exact location of the mouse press
    x, y = glfw.get_cursor_pos(win_ptr)

    # process actions
    if action == glfw.PRESS:
        if button == glfw.MOUSE_BUTTON_LEFT:
            mouseButtons[0] = 1
            mouseTimes[0] = now - mouseClick[0].getLastResetTime()
        elif button == glfw.MOUSE_BUTTON_MIDDLE:
            mouseButtons[1] = 1
            mouseTimes[1] = now - mouseClick[1].getLastResetTime()
        elif button == glfw.MOUSE_BUTTON_RIGHT:
            mouseButtons[2] = 1
            mouseTimes[2] = now - mouseClick[2].getLastResetTime()
    elif action == glfw.RELEASE:
        if button == glfw.MOUSE_BUTTON_LEFT:
            mouseButtons[0] = 0
        elif button == glfw.MOUSE_BUTTON_MIDDLE:
            mouseButtons[1] = 0
        elif button == glfw.MOUSE_BUTTON_RIGHT:
            mouseButtons[2] = 0
Exemple #20
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()
Exemple #21
0
 def on_window_mouse_button(self, window, button, action, mods):
     # self.gui.update_button(button,action,mods)
     if action == glfw.PRESS:
         self.input["button"] = button
         self.input["mouse"] = glfw.get_cursor_pos(window)
     if action == glfw.RELEASE:
         self.input["button"] = None
Exemple #22
0
    def on_mouse_button(self, window, button, action, mods):
        if mods == 0:
            x, y = glfw.get_cursor_pos(window)
            x, y = x / WIDTH, y / HEIGHT
            if action == glfw.PRESS:

                if button == glfw.MOUSE_BUTTON_LEFT:
                    self.is_drag = True
                    self.drag_prepos = vec2(x, y)

                elif button == glfw.MOUSE_BUTTON_RIGHT:
                    pass

            elif action == glfw.RELEASE:
                if button == glfw.MOUSE_BUTTON_LEFT:
                    self.is_drag = False
                    self.drag_prepos = vec2(x, y)

                elif button == glfw.MOUSE_BUTTON_RIGHT:
                    ray_from = self.camera_pos
                    camdir = (self.perspective * self.view *
                              vec4(ray_from, 1.0)).xyz
                    ray_to = self.camera_pos + camdir * 1000.0

                    ray = Ray(self.gl, ray_from, ray_to)
                    self.debug_scene.append(ray)

                    rayquery = pb.rayTest([*self.camera_pos], [*ray_to])[0]
                    objid, linkid, fract, pos, norm = rayquery
                    if objid > 0:
                        print("OBJID FOUND", objid, linkid, fract, pos, norm)
Exemple #23
0
 def calculate_mouse_ray(self):
     mouse_x, mouse_y = glfw.get_cursor_pos(self.camera.window)
     normalized_coords = self.get_normalized_device_coords(mouse_x, mouse_y)
     clip_coords = vec(normalized_coords[0], normalized_coords[1], -1, 1)
     eye_coords = self.to_eye_coords(clip_coords)
     world_ray = self.to_world_coords(eye_coords)
     return world_ray
Exemple #24
0
    def recent_events(self, events):
        frame = events.get("frame")
        if not frame:
            return
        if self.drag_offset is not None:
            pos = glfw.get_cursor_pos(glfw.get_current_context())
            pos = normalize(pos, glfw.get_window_size(glfw.get_current_context()))
            pos = denormalize(
                pos, (frame.img.shape[1], frame.img.shape[0])
            )  # Position in img pixels
            self.pos[0] = pos[0] + self.drag_offset[0]
            self.pos[1] = pos[1] + self.drag_offset[1]

        if self.watermark is not None:
            # keep in image bounds, do this even when not dragging because the image sizes could change.
            self.pos[1] = max(
                0,
                min(frame.img.shape[0] - self.watermark.shape[0], max(self.pos[1], 0)),
            )
            self.pos[0] = max(
                0,
                min(frame.img.shape[1] - self.watermark.shape[1], max(self.pos[0], 0)),
            )
            pos = int(self.pos[0]), int(self.pos[1])
            img = frame.img
            roi = (
                slice(pos[1], pos[1] + self.watermark.shape[0]),
                slice(pos[0], pos[0] + self.watermark.shape[1]),
            )
            w_roi = slice(0, img.shape[0] - pos[1]), slice(0, img.shape[1] - pos[0])
            img[roi] = self.watermark[w_roi] * self.alpha_mask[w_roi] + img[roi] * (
                1 - self.alpha_mask[w_roi]
            )
Exemple #25
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()
Exemple #26
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
Exemple #27
0
 def _update_mouse_pos(self):
     if self._viewport_scale > 0:
         x, y = glfw.get_cursor_pos(self._window)
         pyxel.mouse_x = int(
             (x - self._viewport_left) / self._viewport_scale)
         pyxel.mouse_y = int(
             (y - self._viewport_top) / self._viewport_scale)
Exemple #28
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()
Exemple #29
0
def _onGLFWMouseButton(*args, **kwargs):
    """Callback for mouse press events. Both press and release actions are
    handled by this function as they both invoke the same callback.

    """
    global mouseButtons, mouseClick, mouseTimes
    now = psychopy.core.getTime()
    win_ptr, button, action, modifier = args
    # win = glfw.get_window_user_pointer(win_ptr)

    # get current position of the mouse
    # this might not be at the exact location of the mouse press
    x, y = glfw.get_cursor_pos(win_ptr)

    # process actions
    if action == glfw.PRESS:
        if button == glfw.MOUSE_BUTTON_LEFT:
            mouseButtons[0] = 1
            mouseTimes[0] = now - mouseClick[0].getLastResetTime()
        elif button == glfw.MOUSE_BUTTON_MIDDLE:
            mouseButtons[1] = 1
            mouseTimes[1] = now - mouseClick[1].getLastResetTime()
        elif button == glfw.MOUSE_BUTTON_RIGHT:
            mouseButtons[2] = 1
            mouseTimes[2] = now - mouseClick[2].getLastResetTime()
    elif action == glfw.RELEASE:
        if button == glfw.MOUSE_BUTTON_LEFT:
            mouseButtons[0] = 0
        elif button == glfw.MOUSE_BUTTON_MIDDLE:
            mouseButtons[1] = 0
        elif button == glfw.MOUSE_BUTTON_RIGHT:
            mouseButtons[2] = 0
    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
Exemple #31
0
	def toggle_look_mode(self):
		self.look_mode = not self.look_mode
		if self.look_mode:
			glfw.set_input_mode(self.view.window, glfw.CURSOR, glfw.CURSOR_DISABLED)
		else:
			glfw.set_input_mode(self.view.window, glfw.CURSOR, glfw.CURSOR_NORMAL)
		self.mouse_pos = np.array(glfw.get_cursor_pos(self.view.window))/self.view.resolution
Exemple #32
0
def grid_editor_loop():

    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"]
            # left click
            if (button == glfw.MOUSE_BUTTON_LEFT and\
                    action == glfw.PRESS):
                x, y = glfw.get_cursor_pos(globs.window)
                i, j, isUpper = position_to_grid_index(x, y)
                p1, p2, p3 = position_to_triangle_points(x, y)
                #print(p1, p2, p3)
                #print(i, j, isUpper)
                # click on a grid tile: make that tile the cursor tile
                if p1.x + p1.y > 0:
                    poly_i = get_polygon_index(p1, p2, p3, isUpper)
                    #print("clicked polygon at index=", poly_i)
                    #poly_p1, poly_p2, poly_p3 = globs.polygons[poly_i].get_points()
                    #print("polygon at index=", poly_i, " at points ", poly_p1, poly_p2, poly_p3)
                    if poly_i > -1:
                        globs.numberClicksThisLevel += 1  # related to score
                        globs.tri["updatedNeighbors"] = [
                        ]  # reset color change tally
                        change_polygon_color_cascade(poly_i)
                        increment_color_index()
Exemple #33
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)
Exemple #34
0
    def handleMouseButtonEvent(self, window, button, action, mods):
        updated = False

        if button == glfw.MOUSE_BUTTON_LEFT:
            self.cameraRotate = action == glfw.PRESS
            if self.cameraRotate:
                pos = glfw.get_cursor_pos(window)
                self.cameraCursor = [*pos]
                updated = True
        elif button == glfw.MOUSE_BUTTON_MIDDLE:
            self.cameraMove = action == glfw.PRESS
            if self.cameraMove:
                pos = glfw.get_cursor_pos(window)
                self.cameraCursor = [*pos]
                updated = True
        if updated:
            self.updateMatrix(self.viewport)
            self.redisplay = True
Exemple #35
0
def _mouse_click_callback(window, button, status, modifiers):
    """ Mouse click event handler for GLFW. """
    global _mouse_dragging, _previous_mouse_position
    if button == glfw.MOUSE_BUTTON_LEFT:
        if status == glfw.RELEASE:
            _mouse_dragging = False
            _previous_mouse_position = None
        elif status == glfw.PRESS:
            _mouse_dragging = True
            _previous_mouse_position = glfw.get_cursor_pos(window)
Exemple #36
0
def button_callback(window, button, action, mod):
    global points, gEditingPoint
    if button==glfw.MOUSE_BUTTON_LEFT:
        x, y = glfw.get_cursor_pos(window)
        y = 640 - y
        if action==glfw.PRESS:
            for k, p in points.items():
                if np.abs(x-p[0])<10 and np.abs(y-p[1])<10:
                    gEditingPoint = k
        elif action==glfw.RELEASE:
            gEditingPoint = ''
Exemple #37
0
 def shoot_on(self):
     '''
     Process mouse left down:
     Determine click on buttons or start shooting
     '''
     # In menu determine click on the "START"-button
     if self.mode == MODE_MENU:
         # Convert window coordinates to OpenGL coordinates
         x, y = glfw.get_cursor_pos(self.window)
         x = float(x) / self.gui.getWindowWidth() * 2.0 * self.gui.aspect - self.gui.aspect
         y = 1.0 - float(y) / self.gui.getWindowHeight() * 2.0
         # If button clicked start game
         if x > -1 and x < 1 and y > -0.5 and y < 0.5:
             self.__init_game()
         return
     # In game...
     if self.cam_flag:
         # If camera mode set shooting flag to True
         self.shoot = True
     else:
         # Else determine click on update buttons
         if self.game.getSP():
             # Convert window coordinates to OpenGL coordinates
             x, y = glfw.get_cursor_pos(self.window)
             x = float(x) / self.gui.getWindowWidth() * 2.0 * self.gui.aspect - self.gui.aspect
             y = 1.0 - float(y) / self.gui.getWindowHeight() * 2.0
             # Update power button
             if x > - self.gui.aspect + 0.6 and x < - self.gui.aspect + 0.7 and y > 0.8 and y < 0.9:
                 if self.game.getMainPlayer().getPower() < 10:
                     self.game.getMainPlayer().addPower(1)
                     self.game.decSP()
             # Update defence button
             if x > - self.gui.aspect + 0.6 and x < - self.gui.aspect + 0.7 and y > 0.65 and y < 0.75:
                 if self.game.getMainPlayer().getDefence() < 10:
                     self.game.getMainPlayer().addDefence(1)
                     self.game.decSP()
             # Update speed button
             if x > - self.gui.aspect + 0.6 and x < - self.gui.aspect + 0.7 and y > 0.5 and y < 0.6:
                 if self.game.getMainPlayer().getSpeed() < 10:
                     self.game.getMainPlayer().addSpeed(1)
                     self.game.decSP()
Exemple #38
0
 def __process_camera(self):
     '''
     Calculate camera rotation caused by mouse moving
     '''
     x, y = glfw.get_cursor_pos(self.window)
     dx = x - self.gui.window_width / 2.0
     dy = y - self.gui.window_height / 2.0
     cam_dir = v4_v3(mul_v(rotate(-dy * 0.5, cross(self.cam_dir, self.cam_up)), v3_v4(self.cam_dir)))
     cam_up  = v4_v3(mul_v(rotate(-dy * 0.5, cross(self.cam_dir, self.cam_up)), v3_v4(self.cam_up)))
     cam_dir = v4_v3(mul_v(rotate(-dx * 0.5, cam_up), v3_v4(cam_dir)))
     self.cam_dir = normalize(cam_dir)
     self.cam_up  = normalize(cam_up)
     glfw.set_cursor_pos(self.window, self.gui.window_width / 2, self.gui.window_height / 2)
    def mouse_button_event(self, button, action, mods):
        """Called when a mouse button is pressed or released

        :param int button: The pressed/released mouse button
        :param int action: glfw.PRESS or glfw.RELEASE
        :param int mods: Bit field describing which modifier keys were held down.
        """
        if button == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS:
            width, height = glfw.get_framebuffer_size(self.window)
            x, y = glfw.get_cursor_pos(self.window)  # integer positions relative to the upper left corner of the window
            x = x - (width / 2)
            y = (height / 2) - y
            self.heightmap.generate(game_core.Point(x, y))
            self.generate_texture(width, height)
Exemple #40
0
    def mouse_button_callback(self, window, button, action, mods):
        """
        Handle mouse button events and forward them to the example
        """
        # Offset button index by 1 to make it match the other libraries
        button += 1
        # Support left and right mouse button for now
        if button not in [1, 2]:
            return

        xpos, ypos = glfw.get_cursor_pos(self.window)

        if action == glfw.PRESS:
            self.example.mouse_press_event(xpos, ypos, button)
        else:
            self.example.mouse_release_event(xpos, ypos, button)
Exemple #41
0
    def integrate(self, time, delta_time, window):
        # type: (float, float, Window) -> None

        # determine how far the cursor has moved since the last time
        # we integrated. Store the new cursor position so we can do this
        # again next time we integrate.
        cursor_position = glfw.get_cursor_pos(window.window)
        if self._last_cursor_position is None:
            cursor_movement = (0.0, 0.0)
            self._last_cursor_position = cursor_position
        else:
            cursor_movement = (
                cursor_position[0] - self._last_cursor_position[0],
                cursor_position[1] - self._last_cursor_position[1],
            )
            self._last_cursor_position = cursor_position

        # turn the cursor movement into rotational values
        self._rotx -= cursor_movement[1] * 0.01
        self._rotx = self.clamp_angle(self._rotx)
        self._roty -= cursor_movement[0] * 0.01
        ry = self._get_roty_matrix()
        rx = self._get_rotx_matrix()

        # turn key presses into translation values and add that translation
        # to this camera's world position
        translate = game_core.Vector()
        if glfw.KEY_W in window.pressed_keys:
            translate.z -= self.acceleration_rate
        if glfw.KEY_S in window.pressed_keys:
            translate.z += self.acceleration_rate
        if glfw.KEY_A in window.pressed_keys:
            translate.x -= self.acceleration_rate
        if glfw.KEY_D in window.pressed_keys:
            translate.x += self.acceleration_rate
        if glfw.KEY_SPACE in window.pressed_keys:
            translate.y += self.acceleration_rate
        if glfw.KEY_LEFT_SHIFT in window.pressed_keys:
            translate.y -= self.acceleration_rate
        translate *= delta_time
        translate *= ry
        self._pos += translate

        # resolve orientation and position components to a full matrix
        self.matrix = rx * ry
        for i in xrange(3):
            self.matrix[3, i] = self._pos[i]
def mouse_button_callback(window, button, action, mods):
    global vertices, to_redraw, window_height, window_width
    if action == glfw.PRESS:
        to_redraw = True
        coordinates_of_click = glfw.get_cursor_pos(window)

        # get rounded coordinates for pixel matrix
        coordinates = ((coordinates_of_click[0] - window_width / 2),
                       (window_height - coordinates_of_click[1] - window_height / 2))
        # print(coordinates)
        if to_draw_line:
            length = len(line)
            if length == 0:
                line.append(coordinates)
            elif length == 1:
                line.append(coordinates)
            else:
                line.pop(0)
                line.append(coordinates)
        else:
            if len(vertices) == 0 or (vertices[-1][0] != coordinates[0] and vertices[-1][1] != coordinates[1]):
                vertices.append(coordinates)
Exemple #43
0
    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()
def computeMatricesFromInputs(window):
    global lastTime
    global position
    global horizontalAngle
    global verticalAngle
    global initialFoV
    global ViewMatrix
    global ProjectionMatrix

    # glfwGetTime is called only once, the first time this function is called
    if lastTime == None:
        lastTime = glfw.get_time()

    # Compute time difference between current and last frame
    currentTime = glfw.get_time()
    deltaTime = currentTime - lastTime

    # Get mouse position
    xpos,ypos = glfw.get_cursor_pos(window)

    # Reset mouse position for next frame
    glfw.set_cursor_pos(window, 1024/2, 768/2);

    # Compute new orientation
    horizontalAngle += mouseSpeed * float(1024.0/2.0 - xpos );
    verticalAngle   += mouseSpeed * float( 768.0/2.0 - ypos );

    # Direction : Spherical coordinates to Cartesian coordinates conversion
    direction = vec3(
        mathf.cos(verticalAngle) * mathf.sin(horizontalAngle), 
        mathf.sin(verticalAngle),
        mathf.cos(verticalAngle) * mathf.cos(horizontalAngle)
    )
    
    # Right vector
    right = vec3(
        mathf.sin(horizontalAngle - 3.14/2.0), 
        0.0,
        mathf.cos(horizontalAngle - 3.14/2.0)
    )
    
    # Up vector
    up = vec3.cross( right, direction )

    # Move forward
    if glfw.get_key( window, glfw.KEY_UP ) == glfw.PRESS or glfw.get_key( window, glfw.KEY_W ) == glfw.PRESS:
        position += direction * deltaTime * speed;
    
    # Move backward
    if glfw.get_key( window, glfw.KEY_DOWN ) == glfw.PRESS or glfw.get_key( window, glfw.KEY_S ) == glfw.PRESS:
        position -= direction * deltaTime * speed
    
    # Strafe right
    if glfw.get_key( window, glfw.KEY_RIGHT ) == glfw.PRESS or glfw.get_key( window, glfw.KEY_D ) == glfw.PRESS:
        position += right * deltaTime * speed
    
    # Strafe left
    if glfw.get_key( window, glfw.KEY_LEFT ) == glfw.PRESS or glfw.get_key( window, glfw.KEY_A ) == glfw.PRESS:
        position -= right * deltaTime * speed
    

    FoV = initialFoV# - 5 * glfwGetMouseWheel(); # Now GLFW 3 requires setting up a callback for this. It's a bit too complicated for this beginner's tutorial, so it's disabled instead.

    # Projection matrix : 45 Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    ProjectionMatrix = mat4.perspective(FoV, 4.0 / 3.0, 0.1, 100.0)
    # Camera matrix
    ViewMatrix       = mat4.lookat(
                                position,           # Camera is here
                                position+direction, # and looks here : at the same position, plus "direction"
                                up                  # Head is up (set to 0,-1,0 to look upside-down)
                           )

    # For the next frame, the "last time" will be "now"
    lastTime = currentTime
Exemple #45
0
 def get_mouse_position(self):
     return glm.vec2(*glfw.get_cursor_pos(self._wnd))
Exemple #46
0
 def update(self):
     self.mouse = glfw.get_cursor_pos(self.window)
Exemple #47
0
 def on_button(self, window, button, action, mods):
     pos = glfw.get_cursor_pos(window)
Exemple #48
0
    def retrieve_mouse_data(self):
        window_size = glfw.get_framebuffer_size(self.window)
        window_center = [window_size[0] / 2, window_size[1] / 2]

        mouse_pos = [float(p - window_center[i]) / window_center[i] for i, p in enumerate(glfw.get_cursor_pos(self.window))]
        self.mouse_movement = (mouse_pos[0], mouse_pos[1])

        glfw.set_cursor_pos(self.window, *window_center)
Exemple #49
0
 def init_input(self, window):
     self.last_mx, self.last_my = glfw.get_cursor_pos(window)
     self.last_time = glfw.get_time()
     self.activate_mouse = False