add module system and move sensor into core with it
This commit is contained in:
85
src/layout/event.rs
Normal file
85
src/layout/event.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::{
|
||||
layout::{Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
|
||||
util::Id,
|
||||
};
|
||||
|
||||
pub trait UiCtx {
|
||||
fn ui(&mut self) -> &mut Ui<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Event<Ctx>: Sized {
|
||||
type Module: UiModule + EventModule<Ctx, Self> + Default;
|
||||
type Data;
|
||||
}
|
||||
|
||||
pub trait EventModule<Ctx, E: Event<Ctx>> {
|
||||
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, E::Data>);
|
||||
}
|
||||
|
||||
pub trait EventFn<Ctx, Data>: FnMut(&mut Ctx, Data) + 'static {}
|
||||
impl<F: FnMut(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||
|
||||
pub trait Eventable<W, Ctx, Tag> {
|
||||
fn on<E: Event<Ctx>>(self, event: E, f: impl EventFn<Ctx, E::Data>) -> impl WidgetIdFn<W, Ctx>;
|
||||
|
||||
fn id_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget;
|
||||
|
||||
fn edit_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&mut W, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget,
|
||||
Ctx: UiCtx;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
|
||||
fn on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl EventFn<Ctx, E::Data>,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.modules
|
||||
.get_mut::<E::Module>()
|
||||
.register(id.id.duplicate(), event, f);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn id_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.with_id(move |ui, id| {
|
||||
let id2 = id.clone();
|
||||
id.on(event, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
|
||||
})
|
||||
}
|
||||
|
||||
fn edit_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&mut W::Widget, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
Ctx: UiCtx,
|
||||
{
|
||||
self.id_on(event, move |id, ctx, pos| f(&mut ctx.ui()[id], pos))
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,10 @@ impl<W> WidgetId<W> {
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn key(&self) -> Id {
|
||||
self.id.duplicate()
|
||||
}
|
||||
|
||||
pub(super) fn cast_type<W2>(self) -> WidgetId<W2> {
|
||||
// safety: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
use crate::{
|
||||
layout::{SenseShape, UiRegion},
|
||||
layout::UiRegion,
|
||||
render::{Primitive, PrimitiveHandle, Primitives},
|
||||
util::{HashMap, Id},
|
||||
util::Id,
|
||||
};
|
||||
|
||||
struct LayerNode {
|
||||
@@ -26,10 +26,10 @@ pub struct Layers {
|
||||
vec: Vec<LayerNode>,
|
||||
}
|
||||
|
||||
/// TODO: this can be replaced with Primitives itself atm
|
||||
#[derive(Default)]
|
||||
pub struct Layer {
|
||||
pub primitives: Primitives,
|
||||
pub sensors: HashMap<Id, SenseShape>,
|
||||
}
|
||||
|
||||
impl Layers {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
mod color;
|
||||
mod event;
|
||||
mod id;
|
||||
mod layer;
|
||||
mod module;
|
||||
mod num;
|
||||
mod orientation;
|
||||
mod painter;
|
||||
mod pos;
|
||||
mod sense;
|
||||
mod text;
|
||||
mod texture;
|
||||
mod ui;
|
||||
@@ -14,13 +15,14 @@ mod widget;
|
||||
mod widgets;
|
||||
|
||||
pub use color::*;
|
||||
pub use event::*;
|
||||
pub use id::*;
|
||||
pub use layer::*;
|
||||
pub use module::*;
|
||||
pub use num::*;
|
||||
pub use orientation::*;
|
||||
pub use painter::*;
|
||||
pub use pos::*;
|
||||
pub use sense::*;
|
||||
pub use text::*;
|
||||
pub use texture::*;
|
||||
pub use ui::*;
|
||||
|
||||
34
src/layout/module.rs
Normal file
34
src/layout/module.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::any::{Any, TypeId};
|
||||
|
||||
use crate::{
|
||||
layout::WidgetInstance,
|
||||
util::{HashMap, Id},
|
||||
};
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait UiModule: Any {
|
||||
fn on_draw(&mut self, inst: &WidgetInstance) {}
|
||||
fn on_undraw(&mut self, inst: &WidgetInstance) {}
|
||||
fn on_remove(&mut self, id: &Id) {}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Modules {
|
||||
map: HashMap<TypeId, Box<dyn UiModule>>,
|
||||
}
|
||||
|
||||
impl Modules {
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (dyn UiModule + 'static)> {
|
||||
self.map.values_mut().map(|m| m.as_mut())
|
||||
}
|
||||
|
||||
pub fn get_mut<M: UiModule + Default>(&mut self) -> &mut M {
|
||||
let rf = self
|
||||
.map
|
||||
.entry(TypeId::of::<M>())
|
||||
.or_insert_with(|| Box::new(M::default()))
|
||||
.as_mut();
|
||||
let any: &mut dyn Any = &mut *rf;
|
||||
any.downcast_mut().unwrap()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
layout::{
|
||||
ActiveWidgets, Layers, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||
ActiveWidgets, Layers, Modules, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||
Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
|
||||
},
|
||||
render::{Primitive, PrimitiveHandle},
|
||||
@@ -25,6 +25,7 @@ pub struct PainterCtx<'a> {
|
||||
pub textures: &'a mut Textures,
|
||||
pub text: &'a mut TextData,
|
||||
pub screen_size: Vec2,
|
||||
pub modules: &'a mut Modules,
|
||||
drawing: HashSet<Id>,
|
||||
}
|
||||
|
||||
@@ -44,6 +45,7 @@ impl<'a> PainterCtx<'a> {
|
||||
widgets: &'a Widgets,
|
||||
layers: &'a mut Layers,
|
||||
active: &'a mut ActiveWidgets,
|
||||
modules: &'a mut Modules,
|
||||
textures: &'a mut Textures,
|
||||
text: &'a mut TextData,
|
||||
screen_size: Vec2,
|
||||
@@ -55,6 +57,7 @@ impl<'a> PainterCtx<'a> {
|
||||
textures,
|
||||
text,
|
||||
screen_size,
|
||||
modules,
|
||||
drawing: HashSet::default(),
|
||||
}
|
||||
}
|
||||
@@ -165,10 +168,8 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
if self.widgets.data(id).is_some_and(|w| w.sensor) {
|
||||
self.layers[layer]
|
||||
.sensors
|
||||
.insert(id.duplicate(), instance.region);
|
||||
for m in self.modules.iter_mut() {
|
||||
m.on_draw(&instance);
|
||||
}
|
||||
self.active.insert(id.duplicate(), instance);
|
||||
}
|
||||
@@ -182,7 +183,9 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
inst.textures.clear();
|
||||
self.textures.free();
|
||||
self.layers[inst.layer].sensors.remove(id);
|
||||
for m in self.modules.iter_mut() {
|
||||
m.on_undraw(inst);
|
||||
}
|
||||
}
|
||||
inst
|
||||
}
|
||||
|
||||
@@ -1,252 +0,0 @@
|
||||
use std::ops::{BitOr, Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
layout::{Ui, UiRegion, Vec2},
|
||||
util::{HashMap, Id},
|
||||
};
|
||||
|
||||
pub trait CursorCtx {
|
||||
fn cursor_state(&self) -> &CursorState;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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 SenseCtx {
|
||||
pub cursor: Vec2,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
pub trait SenseFn<Ctx>: FnMut(&mut Ctx, SenseCtx) + 'static {}
|
||||
impl<F: FnMut(&mut Ctx, SenseCtx) + 'static, Ctx> SenseFn<Ctx> for F {}
|
||||
|
||||
pub trait UiCtx {
|
||||
fn ui(&mut self) -> &mut Ui<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// TODO: this takes ui.sensor_map and then puts it back
|
||||
/// it extends so you can add to it, but you can't actually index sensors with it
|
||||
/// should something be done about this?
|
||||
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 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> 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
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,19 @@ use image::DynamicImage;
|
||||
use crate::{
|
||||
core::{TextEdit, TextEditCtx},
|
||||
layout::{
|
||||
Layers, PainterCtx, Sensor, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
|
||||
Vec2, Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
|
||||
Layers, Modules, PainterCtx, StaticWidgetId, TextData, TextureHandle, Textures, Vec2,
|
||||
Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
|
||||
},
|
||||
util::{HashMap, Id},
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
marker::PhantomData,
|
||||
ops::{Index, IndexMut},
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
// TODO: remove generic
|
||||
pub struct Ui<Ctx> {
|
||||
root: Option<WidgetId>,
|
||||
pub(super) widgets: Widgets,
|
||||
@@ -28,7 +30,8 @@ pub struct Ui<Ctx> {
|
||||
full_redraw: bool,
|
||||
|
||||
pub(crate) active: ActiveWidgets,
|
||||
pub(super) event_map: SensorMap<Ctx>,
|
||||
pub modules: Modules,
|
||||
_pd: PhantomData<Ctx>,
|
||||
}
|
||||
|
||||
impl<Ctx> Ui<Ctx> {
|
||||
@@ -114,6 +117,7 @@ impl<Ctx> Ui<Ctx> {
|
||||
&self.widgets,
|
||||
&mut self.layers,
|
||||
&mut self.active,
|
||||
&mut self.modules,
|
||||
&mut self.textures,
|
||||
&mut self.text,
|
||||
self.size,
|
||||
@@ -137,6 +141,7 @@ impl<Ctx> Ui<Ctx> {
|
||||
&self.widgets,
|
||||
&mut self.layers,
|
||||
&mut self.active,
|
||||
&mut self.modules,
|
||||
&mut self.textures,
|
||||
&mut self.text,
|
||||
self.size,
|
||||
@@ -150,7 +155,9 @@ impl<Ctx> Ui<Ctx> {
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self) {
|
||||
for id in self.recv.try_iter() {
|
||||
self.event_map.remove(&id);
|
||||
for m in self.modules.iter_mut() {
|
||||
m.on_remove(&id);
|
||||
}
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
@@ -168,15 +175,6 @@ impl<Ctx> Ui<Ctx> {
|
||||
self.active.len()
|
||||
}
|
||||
|
||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
|
||||
self.event_map
|
||||
.entry(id.id.duplicate())
|
||||
.or_default()
|
||||
.sensors
|
||||
.push(f);
|
||||
self.widgets.data_mut(&id.id).unwrap().sensor = true;
|
||||
}
|
||||
|
||||
pub fn text(&mut self, id: &WidgetId<TextEdit>) -> TextEditCtx<'_> {
|
||||
self.updates.push(id.id.duplicate());
|
||||
TextEditCtx {
|
||||
@@ -238,10 +236,11 @@ impl<Ctx> Default for Ui<Ctx> {
|
||||
text: TextData::default(),
|
||||
full_redraw: false,
|
||||
active: Default::default(),
|
||||
event_map: Default::default(),
|
||||
send,
|
||||
recv,
|
||||
size: Vec2::ZERO,
|
||||
modules: Modules::default(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ pub struct WidgetData {
|
||||
pub label: String,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
/// whether this widget has any sensors
|
||||
pub sensor: bool,
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
@@ -85,7 +83,6 @@ impl Widgets {
|
||||
widget,
|
||||
label,
|
||||
borrowed: false,
|
||||
sensor: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user