TEXT SELECTION

This commit is contained in:
2025-11-21 23:56:31 -05:00
parent 246caffb34
commit 1cec56e847
10 changed files with 332 additions and 69 deletions

View File

@@ -10,14 +10,14 @@ use crate::{
util::{HashMap, Id},
};
#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
pub enum Button {
Left,
Right,
Middle,
}
#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
pub enum CursorSense {
PressStart(Button),
Pressing(Button),
@@ -34,9 +34,15 @@ impl CursorSense {
pub fn click() -> Self {
Self::PressStart(Button::Left)
}
pub fn click_or_drag() -> CursorSenses {
Self::click() | Self::Pressing(Button::Left)
}
pub fn unclick() -> Self {
Self::PressEnd(Button::Left)
}
pub fn is_dragging(&self) -> bool {
matches!(self, CursorSense::Pressing(Button::Left))
}
}
#[derive(Default, Clone)]
@@ -108,6 +114,9 @@ pub struct CursorData {
pub cursor: Vec2,
pub size: Vec2,
pub scroll_delta: Vec2,
/// the (first) sense that triggered this event
/// the senses are checked in order
pub sense: CursorSense,
}
pub struct CursorModule<Ctx> {
@@ -188,11 +197,12 @@ impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
sensed = true;
for sensor in &mut group.sensors {
if should_run(&sensor.senses, cursor, group.hover) {
if let Some(sense) = should_run(&sensor.senses, cursor, group.hover) {
let data = CursorData {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
scroll_delta: cursor.scroll_delta,
sense,
};
(sensor.f)(ctx, data);
}
@@ -210,7 +220,11 @@ impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
}
}
pub fn should_run(senses: &CursorSenses, cursor: &CursorState, hover: ActivationState) -> bool {
pub fn should_run(
senses: &CursorSenses,
cursor: &CursorState,
hover: ActivationState,
) -> Option<CursorSense> {
for sense in senses.iter() {
if match sense {
CursorSense::PressStart(button) => cursor.buttons.select(button).is_start(),
@@ -221,10 +235,10 @@ pub fn should_run(senses: &CursorSenses, cursor: &CursorState, hover: Activation
CursorSense::HoverEnd => hover.is_end(),
CursorSense::Scroll => cursor.scroll_delta != Vec2::ZERO,
} {
return true;
return Some(*sense);
}
}
false
None
}
impl ActivationState {
@@ -280,8 +294,8 @@ impl Event for CursorSense {
type Data = CursorData;
}
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static> EventModule<E, Ctx>
for CursorModule<Ctx>
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
EventModule<E, Ctx> for CursorModule<Ctx>
{
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
// TODO: does not add to active if currently active