Skip to content
Draft
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
6 changes: 3 additions & 3 deletions examples/antialiasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _toggle_antialias(event: Event) -> bool:
return False


view.set_event_filter(_toggle_antialias)

snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_view_filter(view, _toggle_antialias)
view.camera.projection = projections.orthographic(2, 2, 1e5)
snx.run()
15 changes: 6 additions & 9 deletions examples/basic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def _create_line_data(angle: float = 0) -> np.ndarray:
)
line = snx.Line(vertices=original_vertices, color=line_color_model)

view = snx.View(
scene=snx.Scene(children=[line]),
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
)
view = snx.View(scene=snx.Scene(children=[line]))


pressed = False
Expand Down Expand Up @@ -71,9 +68,9 @@ def _view_event_filter(event: Event) -> bool:
return False


# Set up the event filter
view.set_event_filter(_view_event_filter)

# Show and position camera
snx.show(view)
# Show and position camera, then set up interaction
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())
ci.set_view_filter(view, _view_event_filter)
snx.run()
15 changes: 6 additions & 9 deletions examples/basic_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ def create_grid_mesh(
color=per_vertex_model,
)

view = snx.View(
scene=snx.Scene(children=[mesh]),
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
)
view = snx.View(scene=snx.Scene(children=[mesh]))


def event_filter(event: Event) -> bool:
Expand Down Expand Up @@ -107,11 +104,11 @@ def event_filter(event: Event) -> bool:
return False


# Set up the event filter
view.set_event_filter(event_filter)

# Show and position camera
snx.show(view)
# Show and position camera, then set up interaction
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())
ci.set_view_filter(view, event_filter)

print("Interactive Mesh Demo:")
print("- Move mouse over mesh to delete intersected faces")
Expand Down
11 changes: 5 additions & 6 deletions examples/basic_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@


# Since ray-point intersections are computed in canvas space, we need view+canvas
view = snx.View(
scene=snx.Scene(children=[points]), camera=snx.Camera(controller=snx.PanZoom())
)
view = snx.View(scene=snx.Scene(children=[points]), camera=snx.Camera())


def _on_view_event(event: Event) -> bool:
Expand All @@ -83,10 +81,11 @@ def _on_view_event(event: Event) -> bool:
return False


view.set_event_filter(_on_view_event)

# Show and position camera
snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())
ci.set_view_filter(view, _on_view_event)
view.camera.projection = projections.orthographic(2, 2, 1e5)
view.camera.transform = snx.Transform().translated((0.5, 0.5))
snx.run()
8 changes: 5 additions & 3 deletions examples/basic_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
),
]
),
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
on_resize=snx.Letterbox(),
camera=snx.Camera(),
)

# example of adding an object to a scene
Expand All @@ -46,7 +45,10 @@
# snx.use("pygfx")
# snx.use("vispy")

snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())
ci.set_resize_policy(view, snx.Letterbox())

if add_imgui_controls is not None:
add_imgui_controls(view)
Expand Down
6 changes: 4 additions & 2 deletions examples/basic_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
scene=snx.Scene(
children=[snx.Text(text="Hello, Scenex!", color=cmap.Color("cyan"), size=24)]
),
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
camera=snx.Camera(),
)


# Show and position camera
snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())
snx.run()
9 changes: 5 additions & 4 deletions examples/basic_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
)
]
),
camera=snx.Camera(interactive=True),
on_resize=snx.Letterbox(),
camera=snx.Camera(),
)

snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_resize_policy(view, snx.Letterbox())

# Orbit around the center of the volume
orbit_center = np.mean(np.asarray(view.scene.bounding_box), axis=0)
Expand All @@ -41,7 +42,7 @@
near=1,
far=1_000_000, # Just need something big
)
view.camera.controller = snx.Orbit(center=orbit_center)
ci.set_controller(view, snx.Orbit(center=orbit_center))


snx.run()
11 changes: 5 additions & 6 deletions examples/blending.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
children=[volume1, volume2],
interactive=True,
),
camera=snx.Camera(interactive=True),
camera=snx.Camera(),
)

blend_modes = list(scenex.model.BlendMode)
Expand All @@ -75,11 +75,10 @@ def change_blend_mode(event: Event) -> bool:
return False


view.set_event_filter(change_blend_mode)


snx.use("vispy")
snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_view_filter(view, change_blend_mode)

# Orbit around the center of the volume
orbit_center = np.mean(np.asarray(view.scene.bounding_box), axis=0)
Expand All @@ -93,6 +92,6 @@ def change_blend_mode(event: Event) -> bool:
near=1,
far=1_000_000, # Just need something big
)
view.camera.controller = snx.Orbit(center=orbit_center)
ci.set_controller(view, snx.Orbit(center=orbit_center))

snx.run()
5 changes: 3 additions & 2 deletions examples/cursor_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
)

view = snx.View(scene=snx.Scene(children=[points]))
view.camera.controller = snx.PanZoom()
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())


def _cursor_filter(event: Event) -> bool:
Expand All @@ -40,6 +41,6 @@ def _cursor_filter(event: Event) -> bool:
return False


view.set_event_filter(_cursor_filter)
ci.set_view_filter(view, _cursor_filter)

snx.run()
9 changes: 4 additions & 5 deletions examples/draggable_rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@

# ── Scene / View ──────────────────────────────────────────────────────────────
scene = snx.Scene(children=[bg, rect_mesh])
view = snx.View(
scene=scene,
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
)
view = snx.View(scene=scene, camera=snx.Camera())
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())

# ── Drag state ────────────────────────────────────────────────────────────────
_anchor: tuple[float, float] | None = None # resize: fixed opposite corner
Expand Down Expand Up @@ -214,7 +213,7 @@ def _on_vertices_changed(vertices: np.ndarray) -> None:


# Connect the event filter to listen for user events
view.set_event_filter(_event_filter)
ci.set_view_filter(view, _event_filter)

# Connect the vertex change callback
_on_vertices_changed(rect_mesh.vertices) # initialize display
Expand Down
6 changes: 3 additions & 3 deletions examples/event_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _view_filter(event: Event) -> bool:
return True


view.set_event_filter(_view_filter)

snx.show(view)
canvas = snx.show(view)
ci = snx.CanvasInteractor(canvas)
ci.set_view_filter(view, _view_filter)
snx.run()
19 changes: 12 additions & 7 deletions examples/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self) -> None:
# plot
self.view = snx.View(
scene=snx.Scene(name="main scene"),
camera=snx.Camera(interactive=True),
camera=snx.Camera(),
)
self.view.layout.x_start = f"{_AXIS}px"
self.view.layout.y_end = f"-{_AXIS}px"
Expand Down Expand Up @@ -213,13 +213,11 @@ def _init_main_view(self) -> None:
self.controls.order = 1
self.view.scene.add_child(self.controls)

# Set up event handlers and controllers
self.view.camera.controller = snx.PanZoom(lock_y=True)
self._pan_zoom = snx.PanZoom(lock_y=True)

self.view.camera.events.transform.connect(self._update_x_axis)
self.view.camera.events.projection.connect(self._update_x_axis)
self.canvas.events.width.connect(self._update_x_axis)
self.view.set_event_filter(self._on_main_view)

def _on_main_view(self, event: events.Event) -> bool:
# If we have a mouse event...
Expand All @@ -236,7 +234,7 @@ def _on_main_view(self, event: events.Event) -> bool:
]
if len(intersections):
self._grabbed = intersections[0]
self.view.camera.interactive = False
self.ci.set_controller(self.view, None)
# ... and are double-pressing the gamma handle, reset the gamma
elif isinstance(event, events.MouseDoublePressEvent):
if (
Expand Down Expand Up @@ -281,7 +279,7 @@ def _on_main_view(self, event: events.Event) -> bool:
# If we have a mouse release or leave event, end any drag
if isinstance(event, events.MouseReleaseEvent | events.MouseLeaveEvent):
self._grabbed = None
self.view.camera.interactive = True
self.ci.set_controller(self.view, self._pan_zoom)
return False

def set_clims(self, clims: tuple[float, float]) -> None:
Expand Down Expand Up @@ -356,6 +354,12 @@ def set_data(self, source: np.ndarray) -> None:
if first_data:
self.set_range()

def setup_interactor(self, ci: snx.CanvasInteractor) -> None:
"""Wire up the CanvasInteractor for the main view."""
self.ci = ci
ci.set_controller(self.view, self._pan_zoom)
ci.set_view_filter(self.view, self._on_main_view)

def set_range(self) -> None:
"""Sets the range of the x axis."""
projections.zoom_to_fit(self.view, "orthographic", zoom_factor=1)
Expand Down Expand Up @@ -500,8 +504,9 @@ def _hist_counts_to_mesh(

# Create the histogram
histogram = Histogram()
# Show the histogram
# Show the histogram and wire up interaction
snx.show(histogram.canvas)
histogram.setup_interactor(snx.CanvasInteractor(histogram.canvas))
# Add some data
data = gaussian_dataset(n=10000)
histogram.set_data(data)
Expand Down
9 changes: 4 additions & 5 deletions examples/keyboard_pan_zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
for y, x in coords:
data[y - 3 : y + 3, x - 3 : x + 3] = 255

view = snx.View(
scene=snx.Scene(children=[snx.Image(data=data)]),
camera=snx.Camera(controller=snx.PanZoom(), interactive=True),
)
view = snx.View(scene=snx.Scene(children=[snx.Image(data=data)]))
canvas = snx.Canvas(views=[view])
ci = snx.CanvasInteractor(canvas)
ci.set_controller(view, snx.PanZoom())

_PAN_STEP = 20.0 # world units per arrow-key press
_ZOOM_STEP = 1.25 # multiplicative factor per +/- press
Expand Down Expand Up @@ -61,7 +60,7 @@ def _key_filter(event: Event) -> bool:
return True


canvas.set_event_filter(_key_filter)
ci.set_event_filter(_key_filter)

snx.show(canvas)
snx.run()
16 changes: 5 additions & 11 deletions examples/multi_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,8 @@ def _make_scene() -> snx.Scene:


# We'll make two views on the same scene
view1 = snx.View(
scene=_make_scene(),
camera=snx.Camera(interactive=True),
)
view2 = snx.View(
scene=_make_scene(),
camera=snx.Camera(interactive=True),
)
view1 = snx.View(scene=_make_scene(), camera=snx.Camera())
view2 = snx.View(scene=_make_scene(), camera=snx.Camera())

# Partition the canvas into two halves for the first two views
view1.layout.x_end = view2.layout.x_start = "50%"
Expand Down Expand Up @@ -99,9 +93,9 @@ def _view1_event_filter(event: Event) -> bool:
return False


view1.set_event_filter(_view1_event_filter)

snx.show(canvas)
ci = snx.CanvasInteractor(canvas)
ci.set_view_filter(view1, _view1_event_filter)

# Orbit around the center of the volume
orbit_center = np.mean(np.asarray(view2.scene.bounding_box), axis=0)
Expand All @@ -115,7 +109,7 @@ def _view1_event_filter(event: Event) -> bool:
near=1,
far=1_000_000, # Just need something big
)
view1.camera.controller = snx.Orbit(center=orbit_center)
ci.set_controller(view1, snx.Orbit(center=orbit_center))

# The second camera can just look down (-z) at the center of the volume
view2.camera.transform = Transform().translated(orbit_center).translated((0, 0, 300))
Expand Down
4 changes: 2 additions & 2 deletions examples/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def _on_click(event: events.Event) -> bool:
return False


view.set_event_filter(_on_click)

name, *_ = REGIONS[region_idx]
print(f"[{region_idx + 1}/{len(REGIONS)}] {name}")
print("Click the view to cycle through layout options.")

snx.show(canvas)
ci = snx.CanvasInteractor(canvas)
ci.set_view_filter(view, _on_click)
zoom_to_fit(view)
snx.run()
Loading
Loading