1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Depth-independent screen overlays composed after molecular presentation.
use molgfx_math::Rgba8;
/// Anchor in normalized target coordinates plus a physical-pixel offset.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct OverlayAnchor {
/// Normalized target coordinate from bottom-left.
pub normalized: [f32; 2],
/// Additional offset in physical pixels.
pub pixels: [f32; 2],
}
impl OverlayAnchor {
/// Validates an anchor.
///
/// # Errors
///
/// Returns [`crate::CoreError::InvalidOverlay`] for non-finite values or
/// normalized coordinates outside `[0, 1]`.
pub fn new(normalized: [f32; 2], pixels: [f32; 2]) -> Result<Self, crate::CoreError> {
if !normalized.into_iter().chain(pixels).all(f32::is_finite)
|| normalized
.into_iter()
.any(|value| !(0.0..=1.0).contains(&value))
{
return Err(crate::CoreError::InvalidOverlay {
reason: "overlay anchors must be finite and normalized coordinates need [0, 1]",
});
}
Ok(Self { normalized, pixels })
}
}
/// Typed content of one screen overlay.
#[derive(Clone, PartialEq, Debug)]
pub enum OverlayContent {
/// Caller-authored text.
Text {
/// UTF-8 content rendered by the shared label atlas.
text: String,
/// Text colour.
color: Rgba8,
/// Physical-pixel cap height.
size_pixels: f32,
},
/// Scalar colour key.
ColorLegend {
/// Display title.
title: String,
/// Inclusive scalar endpoints.
range: [f32; 2],
/// Endpoint colours.
colors: [Rgba8; 2],
/// Physical-pixel rectangle size.
size_pixels: [f32; 2],
},
/// Camera-aware molecular scale bar.
ScaleBar {
/// Represented world length in ångström.
length_angstrom: f32,
/// Bar colour.
color: Rgba8,
/// Physical-pixel line width.
width_pixels: f32,
},
/// View-coordinate XYZ tripod.
CoordinateTripod {
/// Axis length in physical pixels.
size_pixels: f32,
/// Axis width in physical pixels.
width_pixels: f32,
},
}
/// One persistent depth-independent overlay.
#[derive(Clone, PartialEq, Debug)]
pub struct ScreenOverlay {
content: OverlayContent,
anchor: OverlayAnchor,
order: i16,
visible: bool,
}
impl ScreenOverlay {
/// Creates a visible overlay after validating size and range fields.
///
/// # Errors
///
/// Returns [`crate::CoreError::InvalidOverlay`] for malformed content.
pub fn new(content: OverlayContent, anchor: OverlayAnchor) -> Result<Self, crate::CoreError> {
validate_content(&content)?;
Ok(Self {
content,
anchor,
order: 0,
visible: true,
})
}
/// Typed overlay payload.
#[must_use]
pub const fn content(&self) -> &OverlayContent {
&self.content
}
/// Replaces typed content after validation.
///
/// # Errors
///
/// Returns [`crate::CoreError::InvalidOverlay`] for malformed content.
pub fn set_content(&mut self, content: OverlayContent) -> Result<(), crate::CoreError> {
validate_content(&content)?;
self.content = content;
Ok(())
}
/// Screen anchor.
#[must_use]
pub const fn anchor(&self) -> OverlayAnchor {
self.anchor
}
/// Replaces the screen anchor.
pub const fn set_anchor(&mut self, anchor: OverlayAnchor) {
self.anchor = anchor;
}
/// Stable composition order.
#[must_use]
pub const fn order(&self) -> i16 {
self.order
}
/// Replaces composition order; lower values draw first.
pub const fn set_order(&mut self, order: i16) {
self.order = order;
}
/// Whether the overlay draws.
#[must_use]
pub const fn visible(&self) -> bool {
self.visible
}
/// Shows or hides the overlay.
pub const fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
}
fn validate_content(content: &OverlayContent) -> Result<(), crate::CoreError> {
let valid = match content {
OverlayContent::Text { size_pixels, .. } => size_pixels.is_finite() && *size_pixels > 0.0,
OverlayContent::ColorLegend {
range, size_pixels, ..
} => {
range.iter().all(|value| value.is_finite())
&& range[0] < range[1]
&& size_pixels
.iter()
.all(|value| value.is_finite() && *value > 0.0)
}
OverlayContent::ScaleBar {
length_angstrom,
width_pixels,
..
} => {
length_angstrom.is_finite()
&& *length_angstrom > 0.0
&& width_pixels.is_finite()
&& *width_pixels > 0.0
}
OverlayContent::CoordinateTripod {
size_pixels,
width_pixels,
} => {
size_pixels.is_finite()
&& *size_pixels > 0.0
&& width_pixels.is_finite()
&& *width_pixels > 0.0
}
};
valid.then_some(()).ok_or(crate::CoreError::InvalidOverlay {
reason: "overlay sizes and ranges must be finite, positive, and ordered",
})
}