Implement textual hit testing

This commit is contained in:
Tom 2021-08-21 10:31:26 -07:00
parent 8333b8f88c
commit aa63841e2c
13 changed files with 341 additions and 75 deletions

28
core/src/hit_test.rs Normal file
View file

@ -0,0 +1,28 @@
use crate::Vector;
/// The result of hit testing on text.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HitTestResult {
/// The point was within the bounds of the returned character index.
CharOffset(usize),
/// The provided point was not within the bounds of a glyph. The index
/// of the character with the closest centeroid position is returned,
/// as well as its delta.
NearestCharOffset(usize, Vector),
}
impl HitTestResult {
/// Computes the cursor position corresponding to this [`HitTestResult`] .
pub fn cursor(&self) -> usize {
match self {
HitTestResult::CharOffset(i) => *i,
HitTestResult::NearestCharOffset(i, delta) => {
if delta.x > f32::EPSILON {
i + 1
} else {
*i
}
}
}
}
}

View file

@ -22,6 +22,7 @@ mod align;
mod background;
mod color;
mod font;
mod hit_test;
mod length;
mod padding;
mod point;
@ -33,6 +34,7 @@ pub use align::{Align, HorizontalAlignment, VerticalAlignment};
pub use background::Background;
pub use color::Color;
pub use font::Font;
pub use hit_test::HitTestResult;
pub use length::Length;
pub use menu::Menu;
pub use padding::Padding;