sensors now run in correct order

This commit is contained in:
2025-09-15 21:13:23 -04:00
parent 2700c31c13
commit 90cbc2524a
5 changed files with 129 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
layout::{Ui, UiRegion, Vec2},
util::{HashMap, Id, bitflags},
util::{HashMap, Id, IdVec, bitflags},
};
bitflags!(Sense, Senses, senses {
@@ -34,7 +34,7 @@ pub struct Sensor<Ctx> {
}
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
pub type ActiveSensors = HashMap<Id, SenseShape>;
pub type ActiveSensors = IdVec<SenseShape>;
pub type SenseShape = UiRegion;
pub struct SensorGroup<Ctx> {
pub hover: ActivationState,
@@ -59,15 +59,17 @@ pub trait UiCtx {
pub fn run_sensors<Ctx: UiCtx>(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
let active = std::mem::take(&mut ctx.ui().active.sensors);
let mut map = std::mem::take(&mut ctx.ui().sensor_map);
for (id, shape) in active.iter() {
for (id, shape) in active.iter().rev() {
let group = &mut map.get_mut(id).unwrap();
let region = shape.to_screen(window_size);
let in_shape = cursor.exists && region.contains(cursor.pos);
group.hover.update(in_shape);
group.cursor.update(cursor.pressed && in_shape);
let mut ran = false;
for sensor in &mut group.sensors {
if should_run(sensor.senses, group.cursor, group.hover) {
ran = true;
let sctx = SenseCtx {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
@@ -75,6 +77,9 @@ pub fn run_sensors<Ctx: UiCtx>(ctx: &mut Ctx, cursor: &CursorState, window_size:
(sensor.f)(ctx, sctx);
}
}
if ran {
break;
}
}
ctx.ui().sensor_map = map;
ctx.ui().active.sensors = active;