Code Monkey home page Code Monkey logo

Comments (2)

aganders3 avatar aganders3 commented on June 7, 2024 1

The mouse event handling for this camera is defined here:

def viewbox_mouse_event(self, event):
"""
The viewbox received a mouse event; update transform
accordingly.
Parameters
----------
event : instance of Event
The event.
"""
if event.handled or not self.interactive:
return
PerspectiveCamera.viewbox_mouse_event(self, event)
if event.type == 'mouse_release':
self._event_value = None # Reset
elif event.type == 'mouse_press':
event.handled = True
elif event.type == 'mouse_move':
if event.press_event is None:
return
if 1 in event.buttons and 2 in event.buttons:
return
modifiers = event.mouse_event.modifiers
p1 = event.mouse_event.press_event.pos
p2 = event.mouse_event.pos
d = p2 - p1
if 1 in event.buttons and not modifiers:
# Rotate
self._update_rotation(event)
elif 2 in event.buttons and not modifiers:
# Zoom
if self._event_value is None:
self._event_value = (self._scale_factor, self._distance)
zoomy = (1 + self.zoom_factor) ** d[1]
self.scale_factor = self._event_value[0] * zoomy
# Modify distance if its given
if self._distance is not None:
self._distance = self._event_value[1] * zoomy
self.view_changed()
elif 1 in event.buttons and keys.SHIFT in modifiers:
# Translate
norm = np.mean(self._viewbox.size)
if self._event_value is None or len(self._event_value) == 2:
self._event_value = self.center
dist = (p1 - p2) / norm * self._scale_factor
dist[1] *= -1
# Black magic part 1: turn 2D into 3D translations
dx, dy, dz = self._dist_to_trans(dist)
# Black magic part 2: take up-vector and flipping into account
ff = self._flip_factors
up, forward, right = self._get_dim_vectors()
dx, dy, dz = right * dx + forward * dy + up * dz
dx, dy, dz = ff[0] * dx, ff[1] * dy, dz * ff[2]
c = self._event_value
self.center = c[0] + dx, c[1] + dy, c[2] + dz
elif 2 in event.buttons and keys.SHIFT in modifiers:
# Change fov
if self._event_value is None:
self._event_value = self._fov
fov = self._event_value - d[1] / 5.0
self.fov = min(180.0, max(0.0, fov))

So you can copy that and modify it as you like, then make sure it's what gets called for your camera instance. There are a number of ways to do this (e.g. sub-classing to make a new camera), but here is one way to do it by binding a new method to your camera instance:

viewbox_mouse_event(self, event):
    """
    The viewbox received a mouse event; update transform
    accordingly.

    Parameters
    ----------
    event : instance of Event
        The event.
    """
    from vispy.scene.cameras.perspective import PerspectiveCamera
    import numpy as np

    if event.handled or not self.interactive:
        return

    PerspectiveCamera.viewbox_mouse_event(self, event)

    if event.type == "mouse_release":
        self._event_value = None  # Reset
    elif event.type == "mouse_press":
        event.handled = True
    elif event.type == "mouse_move":
        if event.press_event is None:
            return
        if 1 in event.buttons and 2 in event.buttons:
            return

        modifiers = event.mouse_event.modifiers
        p1 = event.mouse_event.press_event.pos
        p2 = event.mouse_event.pos

        if 1 in event.buttons and not modifiers:
            # Rotate
            self._update_rotation(event)

        elif 2 in event.buttons and not modifiers:
            # Translate
            norm = np.mean(self._viewbox.size)
            if self._event_value is None or len(self._event_value) == 2:
                self._event_value = self.center
            dist = (p1 - p2) / norm * self._scale_factor
            dist[1] *= -1
            # Black magic part 1: turn 2D into 3D translations
            dx, dy, dz = self._dist_to_trans(dist)
            # Black magic part 2: take up-vector and flipping into account
            ff = self._flip_factors
            up, forward, right = self._get_dim_vectors()
            dx, dy, dz = right * dx + forward * dy + up * dz
            dx, dy, dz = ff[0] * dx, ff[1] * dy, dz * ff[2]
            c = self._event_value
            self.center = c[0] + dx, c[1] + dy, c[2] + dz


if __name__ == "__main__" and sys.flags.interactive == 0:
    view.camera.viewbox_mouse_event = types.MethodType(viewbox_mouse_event, view.camera)
    canvas.app.run()

from vispy.

xiaoqiang-cheng avatar xiaoqiang-cheng commented on June 7, 2024

Thank you for your help; I completed this feature with your guidance.

from vispy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.