renaming & comments

This commit is contained in:
2025-09-27 22:16:42 -04:00
parent 8afe2c68e8
commit dc9340b26c
3 changed files with 54 additions and 50 deletions

View File

@@ -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<Sense>);
pub struct CursorSenses(Vec<CursorSense>);
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<Ctx, Data> {
pub senses: Senses,
pub senses: CursorSenses,
pub f: Rc<dyn EventFn<Ctx, Data>>,
}
@@ -109,12 +110,12 @@ pub struct CursorData {
pub scroll_delta: Vec2,
}
pub struct SensorModule<Ctx> {
pub struct CursorModule<Ctx> {
map: SensorMap<Ctx, CursorData>,
active: HashMap<usize, HashMap<Id, SenseShape>>,
}
impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
fn on_draw(&mut self, inst: &WidgetInstance) {
if self.map.contains_key(&inst.id) {
self.active
@@ -146,7 +147,7 @@ impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
}
}
impl<Ctx> SensorModule<Ctx> {
impl<Ctx> CursorModule<Ctx> {
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<Ctx: UiCtx + 'static> SensorCtx for Ctx {
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
SensorModule::<Ctx>::run(self, cursor, window_size);
CursorModule::<Ctx>::run(self, cursor, window_size);
}
}
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
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::<Self>());
@@ -209,16 +210,16 @@ impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
}
}
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<Ctx: 'static> = SensorModule<Ctx>;
impl Event for CursorSenses {
type Module<Ctx: 'static> = CursorModule<Ctx>;
type Data = CursorData;
}
impl Event for Sense {
type Module<Ctx: 'static> = SensorModule<Ctx>;
impl Event for CursorSense {
type Module<Ctx: 'static> = CursorModule<Ctx>;
type Data = CursorData;
}
impl<E: Event<Data = <Senses as Event>::Data> + Into<Senses>, Ctx: 'static> EventModule<E, Ctx>
for SensorModule<Ctx>
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static> EventModule<E, Ctx>
for CursorModule<Ctx>
{
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
// TODO: does not add to active if currently active
@@ -324,44 +325,44 @@ impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
}
}
impl Deref for Senses {
type Target = Vec<Sense>;
impl Deref for CursorSenses {
type Target = Vec<CursorSense>;
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<Sense> for Senses {
fn from(val: Sense) -> Self {
Senses(vec![val])
impl From<CursorSense> 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<Sense> for Senses {
impl BitOr<CursorSense> 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<Ctx> Default for SensorModule<Ctx> {
impl<Ctx> Default for CursorModule<Ctx> {
fn default() -> Self {
Self {
map: Default::default(),

View File

@@ -53,7 +53,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
}
fn scroll(self) -> impl WidgetIdFn<Offset> {
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);
})
}

View File

@@ -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, _| {
.edit_on(
CursorSense::HoverStart | CursorSense::unclick(),
move |r, _| {
r.color = color.brighter(0.2);
})
.edit_on(Sense::HoverEnd, move |r, _| {
},
)
.edit_on(CursorSense::HoverEnd, move |r, _| {
r.color = color;
});
(rect, text(label).size(30)).stack()