Skip to main content

polyscope_rs/
gizmo.rs

1use crate::{GizmoMode, GizmoSpace, Mat4, with_context, with_context_mut};
2
3/// Selects a structure for gizmo manipulation.
4///
5/// Only one structure can be selected at a time. The gizmo will appear
6/// at the selected structure's position when enabled.
7pub fn select_structure(type_name: &str, name: &str) {
8    with_context_mut(|ctx| {
9        ctx.select_structure(type_name, name);
10    });
11}
12
13/// Deselects the currently selected structure.
14pub fn deselect_structure() {
15    with_context_mut(|ctx| {
16        ctx.deselect_structure();
17    });
18}
19
20/// Returns the currently selected structure, if any.
21#[must_use]
22pub fn get_selected_structure() -> Option<(String, String)> {
23    with_context(|ctx| {
24        ctx.selected_structure()
25            .map(|(t, n)| (t.to_string(), n.to_string()))
26    })
27}
28
29/// Returns whether any structure is currently selected.
30#[must_use]
31pub fn has_selection() -> bool {
32    with_context(polyscope_core::Context::has_selection)
33}
34
35/// Sets the gizmo mode (translate, rotate, or scale).
36pub fn set_gizmo_mode(mode: GizmoMode) {
37    with_context_mut(|ctx| {
38        ctx.gizmo_mut().mode = mode;
39    });
40}
41
42/// Returns the current gizmo mode.
43#[must_use]
44pub fn get_gizmo_mode() -> GizmoMode {
45    with_context(|ctx| ctx.gizmo().mode)
46}
47
48/// Sets the gizmo coordinate space (world or local).
49pub fn set_gizmo_space(space: GizmoSpace) {
50    with_context_mut(|ctx| {
51        ctx.gizmo_mut().space = space;
52    });
53}
54
55/// Returns the current gizmo coordinate space.
56#[must_use]
57pub fn get_gizmo_space() -> GizmoSpace {
58    with_context(|ctx| ctx.gizmo().space)
59}
60
61/// Sets whether the gizmo is visible.
62pub fn set_gizmo_visible(visible: bool) {
63    with_context_mut(|ctx| {
64        ctx.gizmo_mut().visible = visible;
65    });
66}
67
68/// Returns whether the gizmo is visible.
69#[must_use]
70pub fn is_gizmo_visible() -> bool {
71    with_context(|ctx| ctx.gizmo().visible)
72}
73
74/// Sets the translation snap value for the gizmo.
75///
76/// When non-zero, translations will snap to multiples of this value.
77pub fn set_gizmo_snap_translate(snap: f32) {
78    with_context_mut(|ctx| {
79        ctx.gizmo_mut().snap_translate = snap;
80    });
81}
82
83/// Sets the rotation snap value for the gizmo (in degrees).
84///
85/// When non-zero, rotations will snap to multiples of this value.
86pub fn set_gizmo_snap_rotate(snap_degrees: f32) {
87    with_context_mut(|ctx| {
88        ctx.gizmo_mut().snap_rotate = snap_degrees;
89    });
90}
91
92/// Sets the scale snap value for the gizmo.
93///
94/// When non-zero, scale will snap to multiples of this value.
95pub fn set_gizmo_snap_scale(snap: f32) {
96    with_context_mut(|ctx| {
97        ctx.gizmo_mut().snap_scale = snap;
98    });
99}
100
101/// Sets the transform of the currently selected structure.
102///
103/// Does nothing if no structure is selected.
104pub fn set_selected_transform(transform: Mat4) {
105    with_context_mut(|ctx| {
106        if let Some((type_name, name)) = ctx.selected_structure.clone() {
107            if let Some(structure) = ctx.registry.get_mut(&type_name, &name) {
108                structure.set_transform(transform);
109            }
110        }
111    });
112}
113
114/// Gets the transform of the currently selected structure.
115///
116/// Returns identity matrix if no structure is selected.
117#[must_use]
118pub fn get_selected_transform() -> Mat4 {
119    with_context(|ctx| {
120        if let Some((type_name, name)) = ctx.selected_structure() {
121            ctx.registry
122                .get(type_name, name)
123                .map_or(Mat4::IDENTITY, polyscope_core::Structure::transform)
124        } else {
125            Mat4::IDENTITY
126        }
127    })
128}
129
130/// Resets the transform of the currently selected structure to identity.
131///
132/// Does nothing if no structure is selected.
133pub fn reset_selected_transform() {
134    set_selected_transform(Mat4::IDENTITY);
135}