Skip to content

More controllers #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion fastplotlib/layouts/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def __init__(
self._camera = camera
self._controller = controller

self.controller.add_camera(self.camera)
self.controller.register_events(
self.viewport,
)
Expand Down
48 changes: 37 additions & 11 deletions fastplotlib/layouts/_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
"3d": pygfx.PerspectiveCamera,
}

controller_types = {
"2d": pygfx.PanZoomController,
"3d": pygfx.OrbitController,
pygfx.OrthographicCamera: pygfx.PanZoomController,
pygfx.PerspectiveCamera: pygfx.OrbitController,
}


def create_camera(
camera_type: str, big_camera: bool = False
camera_type: Union[pygfx.Camera, str],
big_camera: bool = False
) -> Union[pygfx.OrthographicCamera, pygfx.PerspectiveCamera]:
if isinstance(camera_type, (pygfx.OrthographicCamera, pygfx.PerspectiveCamera)):
return camera_type

camera_type = camera_type.split("-")

# kinda messy but works for now
Expand All @@ -40,7 +37,36 @@ def create_camera(
return cls()


def create_controller(controller_type: str):
controller_type = controller_type.split("-")[0]
def create_controller(
camera: Union[pygfx.OrthographicCamera, pygfx.PerspectiveCamera],
controller: Union[pygfx.Controller, None, str],
) -> pygfx.Controller:
if isinstance(controller, pygfx.Controller):
return controller

if controller is None:
# default controllers
if camera == "2d" or isinstance(camera, pygfx.OrthographicCamera):
return pygfx.PanZoomController(camera)

elif camera == "3d" or isinstance(camera, pygfx.PerspectiveCamera):
return pygfx.FlyController(camera)

return controller_types[controller_type]()
# controller specified
if controller == "fly":
return pygfx.FlyController(camera)

elif controller == "panzoom":
return pygfx.PanZoomController(camera)

elif controller == "trackball":
return pygfx.TrackballController(camera)

elif controller == "orbit":
return pygfx.OrbitController(camera)

else:
raise ValueError(
f"Invalid controller type, valid controllers are instances of `pygfx.Controller` or one of:\n"
f"'panzoom', 'fly', 'trackball', or 'orbit'. You have passed: {type(controller)}"
)
9 changes: 6 additions & 3 deletions fastplotlib/layouts/_gridplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ipywidgets import HBox, Layout, Button, ToggleButton, VBox, Dropdown

from ._utils import make_canvas_and_renderer
from ._defaults import create_controller
from ._defaults import create_camera, create_controller
from ._subplot import Subplot
from ._record_mixin import RecordMixin

Expand Down Expand Up @@ -108,7 +108,7 @@ def __init__(
if controllers.shape != self.shape:
raise ValueError

cameras = to_array(cameras)
cameras = to_array(cameras).astype(object)

self._controllers = np.empty(shape=cameras.shape, dtype=object)

Expand All @@ -130,7 +130,10 @@ def __init__(
f"Controller id: {controller} has been assigned to multiple different camera types"
)

self._controllers[controllers == controller] = create_controller(cam[0])
_cam = create_camera(cam[0])
cameras[controllers == controller] = _cam
self._controllers[controllers == controller] = create_controller(_cam, controller=None)

# else assume it's a single pygfx.Controller instance or a list of controllers
else:
if isinstance(controllers, pygfx.Controller):
Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/layouts/_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
canvas: WgpuCanvas = None,
renderer: pygfx.WgpuRenderer = None,
camera: str = "2d",
controller: Union[pygfx.PanZoomController, pygfx.OrbitController] = None,
controller: Union[pygfx.Controller, str] = None,
size: Tuple[int, int] = (500, 300),
**kwargs,
):
Expand Down
15 changes: 9 additions & 6 deletions fastplotlib/layouts/_subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np

import pygfx
from pygfx import (
Scene,
OrthographicCamera,
Expand All @@ -30,7 +31,7 @@ def __init__(
position: Tuple[int, int] = None,
parent_dims: Tuple[int, int] = None,
camera: str = "2d",
controller: Union[PanZoomController, OrbitController] = None,
controller: Union[pygfx.Controller] = None,
canvas: Union[str, WgpuCanvas, Texture] = None,
renderer: WgpuRenderer = None,
name: str = None,
Expand All @@ -55,9 +56,10 @@ def __init__(
indicates the kind of pygfx camera that will be instantiated, '2d' uses pygfx ``OrthographicCamera`` and
'3d' uses pygfx ``PerspectiveCamera``

controller: PanZoomController or OrbitOrthoController, optional
``PanZoomController`` type is used for 2D pan-zoom camera control and ``OrbitController`` type is used for
rotating the camera around a center position, used to control the camera
controller: None, str, or a pygfx controller
| if ``None``, uses a PanZoomController for "2d" camera or FlyController for "3d" camera.
| if ``str``, must be one of: `"panzoom", "fly", "trackball", or "orbit"`.
| also accepts a pygfx.Controller instance

canvas: WgpuCanvas, Texture, or one of "jupyter", "glfw", "qt", optional
Provides surface on which a scene will be rendered. Can optionally provide a WgpuCanvas instance or a str
Expand All @@ -84,8 +86,9 @@ def __init__(

self.nrows, self.ncols = parent_dims

if controller is None:
controller = create_controller(camera)
# parse camera and controller
camera = create_camera(camera)
controller = create_controller(camera, controller)

self._docks = dict()

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy