diff options
author | 2021-08-26 14:53:15 +0700 | |
---|---|---|
committer | 2021-08-26 14:53:15 +0700 | |
commit | 6821114cae2e41fd2bc69d6fcaee1e8574ac061d (patch) | |
tree | 5741859eba63251190eb0a901a72ef4e185349e7 /core | |
parent | 2d65621a3b680457e689b93c800e74f726ffc175 (diff) | |
parent | 7614127d3641cf3224798c2f0ff07b6ae57d9a53 (diff) | |
download | iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.tar.gz iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.tar.bz2 iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.zip |
Merge pull request #670 from twitchyliquid64/text_backend
Refactor textual hit testing into a `renderer::Backend` method
Diffstat (limited to 'core')
-rw-r--r-- | core/src/keyboard.rs | 2 | ||||
-rw-r--r-- | core/src/lib.rs | 1 | ||||
-rw-r--r-- | core/src/mouse.rs | 2 | ||||
-rw-r--r-- | core/src/text.rs | 29 |
4 files changed, 32 insertions, 2 deletions
diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs index cb64701a..6827a4db 100644 --- a/core/src/keyboard.rs +++ b/core/src/keyboard.rs @@ -1,4 +1,4 @@ -//! Reuse basic keyboard types. +//! Listen to keyboard events. mod event; mod hotkey; mod key_code; diff --git a/core/src/lib.rs b/core/src/lib.rs index c4288158..a0decdab 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -17,6 +17,7 @@ pub mod keyboard; pub mod menu; pub mod mouse; +pub mod text; mod align; mod background; diff --git a/core/src/mouse.rs b/core/src/mouse.rs index 25ce6ac3..48214f65 100644 --- a/core/src/mouse.rs +++ b/core/src/mouse.rs @@ -1,4 +1,4 @@ -//! Reuse basic mouse types. +//! Handle mouse events. mod button; mod event; mod interaction; diff --git a/core/src/text.rs b/core/src/text.rs new file mode 100644 index 00000000..ded22eef --- /dev/null +++ b/core/src/text.rs @@ -0,0 +1,29 @@ +//! Draw and interact with text. +use crate::Vector; + +/// The result of hit testing on text. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Hit { + /// 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 Hit { + /// Computes the cursor position corresponding to this [`HitTestResult`] . + pub fn cursor(&self) -> usize { + match self { + Self::CharOffset(i) => *i, + Self::NearestCharOffset(i, delta) => { + if delta.x > f32::EPSILON { + i + 1 + } else { + *i + } + } + } + } +} |