senses are now bitflags

This commit is contained in:
2025-08-25 22:36:38 -04:00
parent e9037cdc14
commit 9780724126
7 changed files with 150 additions and 104 deletions

View File

@@ -1,19 +1,17 @@
use crate::{
layout::{Ui, UiRegion, Vec2, WidgetId},
util::HashMap,
util::Id,
util::{HashMap, Id, bitflags},
};
#[derive(Clone, Copy, PartialEq)]
pub enum Sense {
PressStart,
Pressing,
PressEnd,
HoverStart,
Hovering,
HoverEnd,
NotHovering,
}
bitflags!(Sense, Senses, senses {
1 << 0; PressStart, PRESS_START,
1 << 1; Pressing, PRESSING,
1 << 2; PressEnd, PRESS_END,
1 << 3; HoverStart, HOVER_START,
1 << 4; Hovering, HOVERING,
1 << 5; HoverEnd, HOVER_END,
1 << 6; NotHovering, NOT_HOVERING,
});
pub struct CursorState {
pub pos: Vec2,
@@ -31,18 +29,13 @@ pub enum ActivationState {
}
pub struct Sensor {
pub sense: Sense,
pub senses: Senses,
pub f: Box<dyn SenseFn>,
}
pub type SensorMap = HashMap<Id, SensorGroup>;
pub type ActiveSensors = HashMap<Id, SenseShape>;
pub type SenseShape = UiRegion;
#[derive(Clone)]
pub struct SenseTrigger {
pub shape: SenseShape,
pub sense: Sense,
}
#[derive(Default)]
pub struct SensorGroup {
pub hover: ActivationState,
@@ -78,7 +71,7 @@ impl Ui {
group.cursor.update(cursor.pressed && in_shape);
for sensor in &mut group.sensors {
if should_run(sensor.sense, group.cursor, group.hover) {
if should_run(sensor.senses, group.cursor, group.hover) {
(sensor.f.box_clone())(self);
}
}
@@ -88,16 +81,21 @@ impl Ui {
}
}
pub fn should_run(sense: Sense, cursor: ActivationState, hover: ActivationState) -> bool {
match sense {
Sense::PressStart => cursor.is_start(),
Sense::Pressing => cursor.is_on(),
Sense::PressEnd => cursor.is_end(),
Sense::HoverStart => hover.is_start(),
Sense::Hovering => hover.is_on(),
Sense::HoverEnd => hover.is_end(),
Sense::NotHovering => hover.is_off(),
pub fn should_run(senses: Senses, cursor: ActivationState, hover: ActivationState) -> bool {
for sense in senses.iter() {
if match sense {
Sense::PressStart => cursor.is_start(),
Sense::Pressing => cursor.is_on(),
Sense::PressEnd => cursor.is_end(),
Sense::HoverStart => hover.is_start(),
Sense::Hovering => hover.is_on(),
Sense::HoverEnd => hover.is_end(),
Sense::NotHovering => hover.is_off(),
} {
return true;
}
}
false
}
impl ActivationState {
@@ -147,7 +145,7 @@ impl Clone for SensorGroup {
impl Clone for Sensor {
fn clone(&self) -> Self {
Self {
sense: self.sense,
senses: self.senses,
f: self.f.box_clone(),
}
}