add module system and move sensor into core with it
This commit is contained in:
@@ -1,64 +1,366 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait Sensable<W, Ctx, Tag> {
|
||||
fn on(self, sense: impl Into<Senses>, f: impl SenseFn<Ctx>) -> impl WidgetIdFn<W, Ctx>;
|
||||
fn id_on(
|
||||
self,
|
||||
senses: impl Into<Senses>,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ctx, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget;
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: impl Into<Senses>,
|
||||
f: impl FnMut(&mut W, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget,
|
||||
Ctx: UiCtx;
|
||||
use std::ops::{BitOr, Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
layout::{UiModule, UiRegion, Vec2, WidgetId},
|
||||
util::{HashMap, Id},
|
||||
};
|
||||
|
||||
pub trait CursorCtx {
|
||||
fn cursor_state(&self) -> &CursorState;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
||||
fn on(
|
||||
self,
|
||||
senses: impl Into<Senses>,
|
||||
f: impl SenseFn<Ctx>,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.add_sensor(
|
||||
&id,
|
||||
Sensor {
|
||||
senses: senses.into(),
|
||||
f: Box::new(f),
|
||||
},
|
||||
);
|
||||
id
|
||||
}
|
||||
pub enum Button {
|
||||
Left,
|
||||
Right,
|
||||
Middle,
|
||||
}
|
||||
|
||||
pub enum Sense {
|
||||
PressStart(Button),
|
||||
Pressing(Button),
|
||||
PressEnd(Button),
|
||||
HoverStart,
|
||||
Hovering,
|
||||
HoverEnd,
|
||||
}
|
||||
|
||||
pub struct Senses(Vec<Sense>);
|
||||
|
||||
impl Sense {
|
||||
pub fn click() -> Self {
|
||||
Self::PressStart(Button::Left)
|
||||
}
|
||||
fn id_on(
|
||||
self,
|
||||
senses: impl Into<Senses>,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.with_id(move |ui, id| {
|
||||
let id2 = id.clone();
|
||||
id.on(senses, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
|
||||
})
|
||||
}
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: impl Into<Senses>,
|
||||
mut f: impl FnMut(&mut W::Widget, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
Ctx: UiCtx,
|
||||
{
|
||||
self.id_on(senses, move |id, ctx, pos| f(&mut ctx.ui()[id], pos))
|
||||
pub fn unclick() -> Self {
|
||||
Self::PressEnd(Button::Left)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct CursorState {
|
||||
pub pos: Vec2,
|
||||
pub exists: bool,
|
||||
pub buttons: CursorButtons,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct CursorButtons {
|
||||
pub left: ActivationState,
|
||||
pub middle: ActivationState,
|
||||
pub right: ActivationState,
|
||||
}
|
||||
|
||||
impl CursorButtons {
|
||||
pub fn select(&self, button: &Button) -> &ActivationState {
|
||||
match button {
|
||||
Button::Left => &self.left,
|
||||
Button::Right => &self.right,
|
||||
Button::Middle => &self.middle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
self.left.end_frame();
|
||||
self.middle.end_frame();
|
||||
self.right.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
||||
pub enum ActivationState {
|
||||
Start,
|
||||
On,
|
||||
End,
|
||||
#[default]
|
||||
Off,
|
||||
}
|
||||
|
||||
pub struct Sensor<Ctx> {
|
||||
pub senses: Senses,
|
||||
pub f: Box<dyn SenseFn<Ctx>>,
|
||||
}
|
||||
|
||||
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
|
||||
pub type SenseShape = UiRegion;
|
||||
pub struct SensorGroup<Ctx> {
|
||||
pub hover: ActivationState,
|
||||
pub sensors: Vec<Sensor<Ctx>>,
|
||||
}
|
||||
|
||||
pub struct SenseData {
|
||||
pub cursor: Vec2,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
pub trait SenseFn<Ctx>: FnMut(&mut Ctx, SenseData) + 'static {}
|
||||
impl<F: FnMut(&mut Ctx, SenseData) + 'static, Ctx> SenseFn<Ctx> for F {}
|
||||
|
||||
// pub fn run_sensors<Ctx: UiCtx>(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
// let mut layers = std::mem::take(&mut ctx.ui().layers);
|
||||
// let mut map = std::mem::take(&mut ctx.ui().event_map);
|
||||
// // temp solution, should probably cache somewhere
|
||||
// let mut bruh = Vec::new();
|
||||
// for (i, _) in layers.iter_mut() {
|
||||
// bruh.push(i)
|
||||
// }
|
||||
// for i in bruh.into_iter().rev() {
|
||||
// let layer = &layers[i];
|
||||
// let mut ran = false;
|
||||
// for (id, shape) in &layer.sensors {
|
||||
// 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);
|
||||
// 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 = SenseCtx {
|
||||
// cursor: cursor.pos - region.top_left,
|
||||
// size: region.bot_right - region.top_left,
|
||||
// };
|
||||
// (sensor.f)(ctx, sctx);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if ran {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// let ui = ctx.ui();
|
||||
// std::mem::swap(&mut ui.event_map, &mut map);
|
||||
// // TODO: this removes existing sensors (if a new one is added to an id) lol
|
||||
// // the proper code is easy but writing this comment is easier and I'm tired
|
||||
// ui.event_map.extend(map);
|
||||
// ui.layers = layers;
|
||||
// }
|
||||
|
||||
pub struct SensorModule<Ctx> {
|
||||
map: SensorMap<Ctx>,
|
||||
active: HashMap<usize, HashMap<Id, SenseShape>>,
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
|
||||
fn on_draw(&mut self, inst: &WidgetInstance) {
|
||||
if self.map.contains_key(&inst.id) {
|
||||
self.active
|
||||
.entry(inst.layer)
|
||||
.or_default()
|
||||
.insert(inst.id.duplicate(), inst.region);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_undraw(&mut self, inst: &WidgetInstance) {
|
||||
if let Some(layer) = self.active.get_mut(&inst.layer) {
|
||||
layer.remove(&inst.id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_remove(&mut self, id: &Id) {
|
||||
self.map.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
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 run_sensors<Ctx: UiCtx + 'static>(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
let mut layers = std::mem::take(&mut ctx.ui().layers);
|
||||
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
ctx.ui().layers = layers;
|
||||
}
|
||||
|
||||
pub fn should_run(senses: &Senses, cursor: &CursorButtons, hover: ActivationState) -> bool {
|
||||
for sense in senses.iter() {
|
||||
if match sense {
|
||||
Sense::PressStart(button) => cursor.select(button).is_start(),
|
||||
Sense::Pressing(button) => cursor.select(button).is_on(),
|
||||
Sense::PressEnd(button) => cursor.select(button).is_end(),
|
||||
Sense::HoverStart => hover.is_start(),
|
||||
Sense::Hovering => hover.is_on(),
|
||||
Sense::HoverEnd => hover.is_end(),
|
||||
} {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
impl ActivationState {
|
||||
pub fn is_start(&self) -> bool {
|
||||
*self == Self::Start
|
||||
}
|
||||
pub fn is_on(&self) -> bool {
|
||||
*self == Self::Start || *self == Self::On
|
||||
}
|
||||
pub fn is_end(&self) -> bool {
|
||||
*self == Self::End
|
||||
}
|
||||
pub fn is_off(&self) -> bool {
|
||||
*self == Self::End || *self == Self::Off
|
||||
}
|
||||
pub fn update(&mut self, on: bool) {
|
||||
*self = match *self {
|
||||
Self::Start => match on {
|
||||
true => Self::On,
|
||||
false => Self::End,
|
||||
},
|
||||
Self::On => match on {
|
||||
true => Self::On,
|
||||
false => Self::End,
|
||||
},
|
||||
Self::End => match on {
|
||||
true => Self::Start,
|
||||
false => Self::Off,
|
||||
},
|
||||
Self::Off => match on {
|
||||
true => Self::Start,
|
||||
false => Self::Off,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
match self {
|
||||
Self::Start => *self = Self::On,
|
||||
Self::End => *self = Self::Off,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> Event<Ctx> for Senses {
|
||||
type Module = SensorModule<Ctx>;
|
||||
type Data = SenseData;
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> EventModule<Ctx, Senses> for SensorModule<Ctx> {
|
||||
fn register(&mut self, id: Id, senses: Senses, f: impl EventFn<Ctx, SenseData>) {
|
||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||
senses,
|
||||
f: Box::new(f),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> Event<Ctx> for Sense {
|
||||
type Module = SensorModule<Ctx>;
|
||||
type Data = SenseData;
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> EventModule<Ctx, Sense> for SensorModule<Ctx> {
|
||||
fn register(&mut self, id: Id, sense: Sense, f: impl EventFn<Ctx, SenseData>) {
|
||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||
senses: sense.into(),
|
||||
f: Box::new(f),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for SensorGroup<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hover: Default::default(),
|
||||
sensors: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Senses {
|
||||
type Target = Vec<Sense>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Senses {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sense> for Senses {
|
||||
fn from(val: Sense) -> Self {
|
||||
Senses(vec![val])
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for Sense {
|
||||
type Output = Senses;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
Senses(vec![self, rhs])
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr<Sense> for Senses {
|
||||
type Output = Self;
|
||||
|
||||
fn bitor(mut self, rhs: Sense) -> Self::Output {
|
||||
self.0.push(rhs);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for SensorModule<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: Default::default(),
|
||||
active: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user