|
| 1 | +from typing import * |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import pygfx |
| 6 | +from pygfx import TransformGizmo, Color |
| 7 | +from ipywidgets import IntSlider |
| 8 | + |
| 9 | +from ..graphics._base import Graphic |
| 10 | + |
| 11 | + |
| 12 | +class LineSlider(Graphic): |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + orientation: str = "v", |
| 16 | + x_pos: float = None, |
| 17 | + y_pos: float = None, |
| 18 | + bounds: Tuple[int, int] = None, |
| 19 | + slider: IntSlider = None, |
| 20 | + thickness: float = 2.5, |
| 21 | + color: Any = "w", |
| 22 | + name: str = None, |
| 23 | + ): |
| 24 | + if orientation == "v": |
| 25 | + if x_pos is None: |
| 26 | + raise ValueError("Must pass `x_pos` if orientation is 'v'") |
| 27 | + |
| 28 | + xs = np.zeros(2) |
| 29 | + ys = np.array([bounds[0], bounds[1]]) |
| 30 | + zs = np.zeros(2) |
| 31 | + |
| 32 | + data = np.ascontiguousarray(np.array([xs, ys, zs]).T).astype(np.float32) |
| 33 | + |
| 34 | + elif orientation == "h": |
| 35 | + raise ValueError("'h' not yet supported") |
| 36 | + if y_pos is None: |
| 37 | + raise ValueError("Must pass `y_pos` if orientation is 'h'") |
| 38 | + |
| 39 | + else: |
| 40 | + raise ValueError("`orientation` must be one of 'v' or 'h'") |
| 41 | + |
| 42 | + if thickness < 1.1: |
| 43 | + material = pygfx.LineThinMaterial |
| 44 | + else: |
| 45 | + material = pygfx.LineMaterial |
| 46 | + |
| 47 | + colors_inner = np.repeat([Color("w")], 2, axis=0).astype(np.float32) |
| 48 | + colors_outer = np.repeat([Color([1., 1., 1., 0.25])], 2, axis=0).astype(np.float32) |
| 49 | + |
| 50 | + line_inner = pygfx.Line( |
| 51 | + # self.data.feature_data because data is a Buffer |
| 52 | + geometry=pygfx.Geometry(positions=data, colors=colors_inner), |
| 53 | + material=material(thickness=thickness, vertex_colors=True) |
| 54 | + ) |
| 55 | + |
| 56 | + line_outer = pygfx.Line( |
| 57 | + geometry=pygfx.Geometry(positions=data, colors=colors_outer), |
| 58 | + material=material(thickness=thickness + 4, vertex_colors=True) |
| 59 | + ) |
| 60 | + |
| 61 | + self._world_object = pygfx.Group() |
| 62 | + |
| 63 | + self._world_object.add(line_outer) |
| 64 | + self._world_object.add(line_inner) |
| 65 | + |
| 66 | + self.position.x = x_pos |
| 67 | + |
| 68 | + self.slider = slider |
| 69 | + self.slider.observe(self.set_position, "value") |
| 70 | + |
| 71 | + self.name = name |
| 72 | + |
| 73 | + def set_position(self, change): |
| 74 | + self.position.x = change["new"] |
| 75 | + |
| 76 | + # def _add_plot_area_hook(self, viewport, camera): |
| 77 | + # self.gizmo = TransformGizmo(self.world_object) |
| 78 | + # self.gizmo.add_default_event_handlers(viewport, camera) |
0 commit comments