diff --git a/src/core/sense.rs b/src/core/sense.rs index 6031f5a..b5989ab 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -10,10 +10,6 @@ use crate::{ util::{HashMap, Id}, }; -pub trait CursorCtx { - fn cursor_state(&self) -> &CursorState; -} - #[derive(PartialEq)] pub enum Button { Left, @@ -22,7 +18,7 @@ pub enum Button { } #[derive(PartialEq)] -pub enum Sense { +pub enum CursorSense { PressStart(Button), Pressing(Button), PressEnd(Button), @@ -32,9 +28,9 @@ pub enum Sense { Scroll, } -pub struct Senses(Vec); +pub struct CursorSenses(Vec); -impl Sense { +impl CursorSense { pub fn click() -> Self { Self::PressStart(Button::Left) } @@ -90,8 +86,13 @@ pub enum ActivationState { Off, } +/// this and other similar stuff has a generic +/// because I kind of want to make CursorModule generic +/// or basically have some way to have custom senses +/// that depend on active widget positions +/// but I'm not sure how or if worth it pub struct Sensor { - pub senses: Senses, + pub senses: CursorSenses, pub f: Rc>, } @@ -109,12 +110,12 @@ pub struct CursorData { pub scroll_delta: Vec2, } -pub struct SensorModule { +pub struct CursorModule { map: SensorMap, active: HashMap>, } -impl UiModule for SensorModule { +impl UiModule for CursorModule { fn on_draw(&mut self, inst: &WidgetInstance) { if self.map.contains_key(&inst.id) { self.active @@ -146,7 +147,7 @@ impl UiModule for SensorModule { } } -impl SensorModule { +impl CursorModule { pub fn merge(&mut self, other: Self) { for (id, group) in other.map { for sensor in group.sensors { @@ -162,11 +163,11 @@ pub trait SensorCtx: UiCtx { impl SensorCtx for Ctx { fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) { - SensorModule::::run(self, cursor, window_size); + CursorModule::::run(self, cursor, window_size); } } -impl SensorModule { +impl CursorModule { pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) { let layers = std::mem::take(&mut ctx.ui().layers); let mut module = std::mem::take(ctx.ui().modules.get_mut::()); @@ -209,16 +210,16 @@ impl SensorModule { } } -pub fn should_run(senses: &Senses, cursor: &CursorState, hover: ActivationState) -> bool { +pub fn should_run(senses: &CursorSenses, cursor: &CursorState, hover: ActivationState) -> bool { for sense in senses.iter() { if match sense { - Sense::PressStart(button) => cursor.buttons.select(button).is_start(), - Sense::Pressing(button) => cursor.buttons.select(button).is_on(), - Sense::PressEnd(button) => cursor.buttons.select(button).is_end(), - Sense::HoverStart => hover.is_start(), - Sense::Hovering => hover.is_on(), - Sense::HoverEnd => hover.is_end(), - Sense::Scroll => cursor.scroll_delta != Vec2::ZERO, + CursorSense::PressStart(button) => cursor.buttons.select(button).is_start(), + CursorSense::Pressing(button) => cursor.buttons.select(button).is_on(), + CursorSense::PressEnd(button) => cursor.buttons.select(button).is_end(), + CursorSense::HoverStart => hover.is_start(), + CursorSense::Hovering => hover.is_on(), + CursorSense::HoverEnd => hover.is_end(), + CursorSense::Scroll => cursor.scroll_delta != Vec2::ZERO, } { return true; } @@ -269,18 +270,18 @@ impl ActivationState { } } -impl Event for Senses { - type Module = SensorModule; +impl Event for CursorSenses { + type Module = CursorModule; type Data = CursorData; } -impl Event for Sense { - type Module = SensorModule; +impl Event for CursorSense { + type Module = CursorModule; type Data = CursorData; } -impl::Data> + Into, Ctx: 'static> EventModule - for SensorModule +impl::Data> + Into, Ctx: 'static> EventModule + for CursorModule { fn register(&mut self, id: Id, senses: E, f: impl EventFn::Data>) { // TODO: does not add to active if currently active @@ -324,44 +325,44 @@ impl Default for SensorGroup { } } -impl Deref for Senses { - type Target = Vec; +impl Deref for CursorSenses { + type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } } -impl DerefMut for Senses { +impl DerefMut for CursorSenses { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl From for Senses { - fn from(val: Sense) -> Self { - Senses(vec![val]) +impl From for CursorSenses { + fn from(val: CursorSense) -> Self { + CursorSenses(vec![val]) } } -impl BitOr for Sense { - type Output = Senses; +impl BitOr for CursorSense { + type Output = CursorSenses; fn bitor(self, rhs: Self) -> Self::Output { - Senses(vec![self, rhs]) + CursorSenses(vec![self, rhs]) } } -impl BitOr for Senses { +impl BitOr for CursorSenses { type Output = Self; - fn bitor(mut self, rhs: Sense) -> Self::Output { + fn bitor(mut self, rhs: CursorSense) -> Self::Output { self.0.push(rhs); self } } -impl Default for SensorModule { +impl Default for CursorModule { fn default() -> Self { Self { map: Default::default(), diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index ec881ce..8f8e3ec 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -53,7 +53,7 @@ impl, Tag> CoreWidget for W { } fn scroll(self) -> impl WidgetIdFn { - self.offset(UiVec2::ZERO).edit_on(Sense::Scroll, |w, data| { + self.offset(UiVec2::ZERO).edit_on(CursorSense::Scroll, |w, data| { w.amt += UiVec2::abs(data.scroll_delta * 50.0); }) } diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 634ab24..748c3a9 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -81,7 +81,7 @@ impl Client { let add_button = rect(Color::LIME) .radius(30) - .on(Sense::click(), move |ctx: &mut Client, _| { + .on(CursorSense::click(), move |ctx: &mut Client, _| { let child = ctx .ui .add(image(include_bytes!("assets/sungals.png")).center()) @@ -93,7 +93,7 @@ impl Client { let del_button = rect(Color::RED) .radius(30) - .on(Sense::click(), move |ctx: &mut Client, _| { + .on(CursorSense::click(), move |ctx: &mut Client, _| { ctx.ui[span_add].children.pop(); }) .sized(150) @@ -133,14 +133,14 @@ impl Client { let add_text = text_edit("add") .text_align(Align::Left) .size(30) - .id_on(Sense::click(), |id, client: &mut Client, ctx| { + .id_on(CursorSense::click(), |id, client: &mut Client, ctx| { client.ui.text(id).select(ctx.cursor, ctx.size); client.focus = Some(id.clone()); }) .id_on(Submit, move |id, client: &mut Client, _| { let content = client.ui.text(id).take(); let text = text_edit(content).size(30).text_align(Align::Left).id_on( - Sense::click(), + CursorSense::click(), |id, client: &mut Client, ctx| { client.ui.text(id).select(ctx.cursor, ctx.size); client.focus = Some(id.clone()); @@ -160,7 +160,7 @@ impl Client { ( add_text.clone(), Rect::new(Color::GREEN) - .on(Sense::click(), move |client: &mut Client, _| { + .on(CursorSense::click(), move |client: &mut Client, _| { client.run_event(&add_text, Submit, ()); }) .sized(40), @@ -178,14 +178,17 @@ impl Client { let switch_button = |color, to, label| { let rect = rect(color) - .id_on(Sense::click(), move |id, ui: &mut Ui, _| { + .id_on(CursorSense::click(), move |id, ui: &mut Ui, _| { ui[main].inner.set_static(to); ui[id].color = color.darker(0.3); }) - .edit_on(Sense::HoverStart | Sense::unclick(), move |r, _| { - r.color = color.brighter(0.2); - }) - .edit_on(Sense::HoverEnd, move |r, _| { + .edit_on( + CursorSense::HoverStart | CursorSense::unclick(), + move |r, _| { + r.color = color.brighter(0.2); + }, + ) + .edit_on(CursorSense::HoverEnd, move |r, _| { r.color = color; }); (rect, text(label).size(30)).stack()