make run sensors sane and adjust on_edit to just use ui as ctx (so two run calls needed)

This commit is contained in:
2025-09-24 22:46:55 -04:00
parent 3463682d62
commit 443e13f094
4 changed files with 78 additions and 82 deletions

View File

@@ -3,7 +3,7 @@ use crate::prelude::*;
use std::ops::{BitOr, Deref, DerefMut};
use crate::{
layout::{UiModule, UiRegion, Vec2, WidgetId},
layout::{UiModule, UiRegion, Vec2},
util::{HashMap, Id},
};
@@ -123,61 +123,65 @@ impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
}
impl<Ctx> SensorModule<Ctx> {
pub fn set<W>(&mut self, id: WidgetId<W>, sensor: Sensor<Ctx>) {
// TODO: currently does not add to active if it exists
self.map.entry(id.key()).or_default().sensors.push(sensor);
pub fn merge(&mut self, other: Self) {
for (id, group) in other.map {
for sensor in group.sensors {
self.map
.entry(id.duplicate())
.or_default()
.sensors
.push(sensor);
}
}
}
}
pub fn run_sensors<Ctx: UiCtx + 'static>(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
let mut layers = std::mem::take(&mut ctx.ui().layers);
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
let mut layers = std::mem::take(&mut ctx.ui().layers);
let mut module = std::mem::take(ctx.ui().modules.get_mut::<Self>());
// TODO: temp, need to actually make reverse
let mut layer_idxs = Vec::new();
for (i, _) in layers.iter_mut() {
layer_idxs.push(i);
}
// TODO: temp, need to actually make reverse
let mut layer_idxs = Vec::new();
for (i, _) in layers.iter_mut() {
layer_idxs.push(i);
}
for l in &layer_idxs {
let m = ctx.ui().modules.get_mut::<SensorModule<Ctx>>();
let Some(list) = m.active.get_mut(l) else {
continue;
};
let list = list.clone();
let mut ran = false;
for (id, shape) in list.iter() {
let m = ctx.ui().modules.get_mut::<SensorModule<Ctx>>();
let mut group = m.map.remove(id).unwrap();
let region = shape.to_screen(window_size);
let in_shape = cursor.exists && region.contains(cursor.pos);
group.hover.update(in_shape);
if group.hover == ActivationState::Off {
// TODO: this does not merge if you happen to add a sensor to the widget itself
let m = ctx.ui().modules.get_mut::<SensorModule<Ctx>>();
m.map.insert(id.clone(), group);
for l in &layer_idxs {
let Some(list) = module.active.get_mut(l) else {
continue;
}
};
let mut ran = false;
for (id, shape) in list.iter() {
let group = module.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);
if group.hover == ActivationState::Off {
continue;
}
for sensor in &mut group.sensors {
if should_run(&sensor.senses, &cursor.buttons, group.hover) {
ran = true;
let sctx = SenseData {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
};
(sensor.f)(ctx, sctx);
for sensor in &mut group.sensors {
if should_run(&sensor.senses, &cursor.buttons, group.hover) {
ran = true;
let sctx = SenseData {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
};
(sensor.f)(ctx, sctx);
}
}
}
// TODO: this does not merge if you happen to add a sensor to the widget itself
let m = ctx.ui().modules.get_mut::<SensorModule<Ctx>>();
m.map.insert(id.clone(), group);
if ran {
break;
}
}
if ran {
break;
}
}
ctx.ui().layers = layers;
let ui_mod = ctx.ui().modules.get_mut::<Self>();
std::mem::swap(ui_mod, &mut module);
ui_mod.merge(module);
ctx.ui().layers = layers;
}
}
pub fn should_run(senses: &Senses, cursor: &CursorButtons, hover: ActivationState) -> bool {
@@ -260,6 +264,7 @@ impl<Ctx: 'static> Event<Ctx> for Sense {
impl<Ctx: 'static> EventModule<Ctx, Sense> for SensorModule<Ctx> {
fn register(&mut self, id: Id, sense: Sense, f: impl EventFn<Ctx, SenseData>) {
// TODO: does not add to active if currently active
self.map.entry(id).or_default().sensors.push(Sensor {
senses: sense.into(),
f: Box::new(f),