move ctx in event to be on module so run event and stuff can be used easily
This commit is contained in:
@@ -142,6 +142,17 @@ impl<Ctx> SensorModule<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait SensorCtx: UiCtx {
|
||||||
|
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Ctx: UiCtx + 'static> SensorCtx for Ctx {
|
||||||
|
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
||||||
|
SensorModule::<Ctx>::run(self, cursor, window_size);
|
||||||
|
SensorModule::<Ui>::run(self.ui(), cursor, window_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
|
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
|
||||||
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||||
let layers = std::mem::take(&mut ctx.ui().layers);
|
let layers = std::mem::take(&mut ctx.ui().layers);
|
||||||
@@ -243,20 +254,20 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> Event<Ctx> for Senses {
|
impl Event for Senses {
|
||||||
type Module = SensorModule<Ctx>;
|
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
||||||
type Data = SenseData;
|
type Data = SenseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> Event<Ctx> for Sense {
|
impl Event for Sense {
|
||||||
type Module = SensorModule<Ctx>;
|
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
||||||
type Data = SenseData;
|
type Data = SenseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Event<Ctx, Data = <Senses as Event<Ctx>>::Data> + Into<Senses>, Ctx: 'static>
|
impl<E: Event<Data = <Senses as Event>::Data> + Into<Senses>, Ctx: 'static> EventModule<E, Ctx>
|
||||||
EventModule<E, Ctx> for SensorModule<Ctx>
|
for SensorModule<Ctx>
|
||||||
{
|
{
|
||||||
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event<Ctx>>::Data>) {
|
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
|
// TODO: does not add to active if currently active
|
||||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||||
senses: senses.into(),
|
senses: senses.into(),
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ impl UiCtx for Ui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Event<Ctx>: Sized {
|
pub trait Event: Sized {
|
||||||
type Module: EventModule<Self, Ctx>;
|
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
|
||||||
type Data: Clone;
|
type Data: Clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,13 +24,13 @@ pub trait EventFn<Ctx, Data>: Fn(&mut Ctx, Data) + 'static {}
|
|||||||
impl<F: Fn(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
impl<F: Fn(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||||
|
|
||||||
pub trait Eventable<W, Tag> {
|
pub trait Eventable<W, Tag> {
|
||||||
fn on<E: Event<Ctx>, Ctx>(
|
fn on<E: Event, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl EventFn<Ctx, E::Data>,
|
f: impl EventFn<Ctx, E::Data>,
|
||||||
) -> impl WidgetIdFn<W> + Eventable<W, IdFnTag>;
|
) -> impl WidgetIdFn<W> + Eventable<W, IdFnTag>;
|
||||||
|
|
||||||
fn id_on<E: Event<Ctx>, Ctx>(
|
fn id_on<E: Event, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl Fn(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
|
f: impl Fn(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
|
||||||
@@ -38,7 +38,7 @@ pub trait Eventable<W, Tag> {
|
|||||||
where
|
where
|
||||||
W: Widget;
|
W: Widget;
|
||||||
|
|
||||||
fn edit_on<E: Event<Ui>>(
|
fn edit_on<E: Event>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl Fn(&mut W, E::Data) + 'static,
|
f: impl Fn(&mut W, E::Data) + 'static,
|
||||||
@@ -48,7 +48,7 @@ pub trait Eventable<W, Tag> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
||||||
fn on<E: Event<Ctx>, Ctx>(
|
fn on<E: Event, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl EventFn<Ctx, E::Data>,
|
f: impl EventFn<Ctx, E::Data>,
|
||||||
@@ -56,13 +56,13 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
|||||||
move |ui| {
|
move |ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.modules
|
ui.modules
|
||||||
.get_mut::<E::Module>()
|
.get_mut::<E::Module<Ctx>>()
|
||||||
.register(id.id.duplicate(), event, f);
|
.register(id.id.duplicate(), event, f);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn id_on<E: Event<Ctx>, Ctx>(
|
fn id_on<E: Event, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl Fn(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
|
f: impl Fn(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
|
||||||
@@ -76,7 +76,7 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit_on<E: Event<Ui>>(
|
fn edit_on<E: Event>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl Fn(&mut W::Widget, E::Data) + 'static,
|
f: impl Fn(&mut W::Widget, E::Data) + 'static,
|
||||||
@@ -84,7 +84,7 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
|
|||||||
where
|
where
|
||||||
W::Widget: Widget,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.id_on(event, move |id, ui, pos| f(&mut ui[id], pos))
|
self.id_on(event, move |id, ui: &mut Ui, pos| f(&mut ui[id], pos))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,12 +92,12 @@ pub trait DefaultEvent: Hash + Eq + 'static {
|
|||||||
type Data: Clone;
|
type Data: Clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: DefaultEvent, Ctx: 'static> Event<Ctx> for E {
|
impl<E: DefaultEvent> Event for E {
|
||||||
type Module = DefaultEventModule<E, Ctx>;
|
type Module<Ctx: 'static> = DefaultEventModule<E, Ctx>;
|
||||||
type Data = E::Data;
|
type Data = E::Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EventModule<E: Event<Ctx>, Ctx>: UiModule + Default {
|
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
|
||||||
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, E::Data>);
|
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, E::Data>);
|
||||||
fn run<'a>(
|
fn run<'a>(
|
||||||
&self,
|
&self,
|
||||||
@@ -107,11 +107,11 @@ pub trait EventModule<E: Event<Ctx>, Ctx>: UiModule + Default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type EventFnMap<Ctx, Data> = HashMap<Id, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
|
type EventFnMap<Ctx, Data> = HashMap<Id, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
|
||||||
pub struct DefaultEventModule<E: Event<Ctx>, Ctx> {
|
pub struct DefaultEventModule<E: Event, Ctx> {
|
||||||
map: HashMap<E, EventFnMap<Ctx, <E as Event<Ctx>>::Data>>,
|
map: HashMap<E, EventFnMap<Ctx, <E as Event>::Data>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Event<Ctx> + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, Ctx> {
|
impl<E: Event + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, Ctx> {
|
||||||
fn on_remove(&mut self, id: &Id) {
|
fn on_remove(&mut self, id: &Id) {
|
||||||
for map in self.map.values_mut() {
|
for map in self.map.values_mut() {
|
||||||
map.remove(id);
|
map.remove(id);
|
||||||
@@ -119,11 +119,11 @@ impl<E: Event<Ctx> + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HashableEvent<Ctx>: Event<Ctx> + Hash + Eq + 'static {}
|
pub trait HashableEvent: Event + Hash + Eq + 'static {}
|
||||||
impl<E: Event<Ctx> + Hash + Eq + 'static, Ctx> HashableEvent<Ctx> for E {}
|
impl<E: Event + Hash + Eq + 'static> HashableEvent for E {}
|
||||||
|
|
||||||
impl<E: HashableEvent<Ctx>, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<E, Ctx> {
|
impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<E, Ctx> {
|
||||||
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, <E as Event<Ctx>>::Data>) {
|
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||||
self.map
|
self.map
|
||||||
.entry(event)
|
.entry(event)
|
||||||
.or_default()
|
.or_default()
|
||||||
@@ -148,7 +148,7 @@ impl<E: HashableEvent<Ctx>, Ctx: 'static> EventModule<E, Ctx> for DefaultEventMo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: HashableEvent<Ctx>, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
||||||
pub fn run_all(&self, ctx: &mut Ctx, event: E, data: E::Data)
|
pub fn run_all(&self, ctx: &mut Ctx, event: E, data: E::Data)
|
||||||
where
|
where
|
||||||
E::Data: Clone,
|
E::Data: Clone,
|
||||||
@@ -163,7 +163,7 @@ impl<E: HashableEvent<Ctx>, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Event<Ctx> + 'static, Ctx: 'static> Default for DefaultEventModule<E, Ctx> {
|
impl<E: Event + 'static, Ctx: 'static> Default for DefaultEventModule<E, Ctx> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
map: Default::default(),
|
map: Default::default(),
|
||||||
@@ -172,14 +172,40 @@ impl<E: Event<Ctx> + 'static, Ctx: 'static> Default for DefaultEventModule<E, Ct
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
pub fn run_event<E: Event<Ctx>, Ctx: UiCtx, W>(
|
pub fn run_event<E: Event + Clone, Ctx: UiCtx + 'static, W>(
|
||||||
ctx: &mut Ctx,
|
ctx: &mut Ctx,
|
||||||
id: &WidgetId<W>,
|
id: &WidgetId<W>,
|
||||||
event: E,
|
event: E,
|
||||||
data: E::Data,
|
data: E::Data,
|
||||||
) {
|
) {
|
||||||
if let Some(f) = ctx.ui().modules.get_mut::<E::Module>().run(&id.id, event) {
|
Self::run_event_inner(ctx, id, event.clone(), data.clone());
|
||||||
|
Self::run_event_inner(ctx.ui(), id, event, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_event_inner<E: Event, Ctx: UiCtx + 'static, W>(
|
||||||
|
ctx: &mut Ctx,
|
||||||
|
id: &WidgetId<W>,
|
||||||
|
event: E,
|
||||||
|
data: E::Data,
|
||||||
|
) {
|
||||||
|
if let Some(f) = ctx
|
||||||
|
.ui()
|
||||||
|
.modules
|
||||||
|
.get_mut::<E::Module<Ctx>>()
|
||||||
|
.run(&id.id, event)
|
||||||
|
{
|
||||||
f(ctx, data);
|
f(ctx, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait EventCtx: UiCtx {
|
||||||
|
fn run_event<E: Event + Clone, W>(&mut self, id: &WidgetId<W>, event: E, data: E::Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Ctx: UiCtx + 'static> EventCtx for Ctx {
|
||||||
|
fn run_event<E: Event + Clone, W>(&mut self, id: &WidgetId<W>, event: E, data: E::Data) {
|
||||||
|
Ui::run_event_inner(self, id, event.clone(), data.clone());
|
||||||
|
Ui::run_event_inner(self.ui(), id, event, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ pub struct Client {
|
|||||||
focus: Option<WidgetId<TextEdit>>,
|
focus: Option<WidgetId<TextEdit>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash)]
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||||
struct Submit;
|
struct Submit;
|
||||||
|
|
||||||
impl DefaultEvent for Submit {
|
impl DefaultEvent for Submit {
|
||||||
@@ -153,7 +153,7 @@ impl Client {
|
|||||||
add_text.clone(),
|
add_text.clone(),
|
||||||
Rect::new(Color::GREEN)
|
Rect::new(Color::GREEN)
|
||||||
.on(Sense::click(), move |client: &mut Client, _| {
|
.on(Sense::click(), move |client: &mut Client, _| {
|
||||||
Ui::run_event(client, &add_text, Submit, ());
|
client.run_event(&add_text, Submit, ());
|
||||||
})
|
})
|
||||||
.size(40),
|
.size(40),
|
||||||
)
|
)
|
||||||
@@ -221,8 +221,7 @@ impl Client {
|
|||||||
}
|
}
|
||||||
if input_changed {
|
if input_changed {
|
||||||
let window_size = self.window_size();
|
let window_size = self.window_size();
|
||||||
SensorModule::run(&mut self.ui, &cursor_state, window_size);
|
self.run_sensors(&cursor_state, window_size);
|
||||||
SensorModule::run(self, &cursor_state, window_size);
|
|
||||||
}
|
}
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => event_loop.exit(),
|
WindowEvent::CloseRequested => event_loop.exit(),
|
||||||
@@ -244,7 +243,7 @@ impl Client {
|
|||||||
self.focus = None;
|
self.focus = None;
|
||||||
}
|
}
|
||||||
TextInputResult::Submit => {
|
TextInputResult::Submit => {
|
||||||
Ui::run_event(self, &sel.clone(), Submit, ());
|
self.run_event(&sel.clone(), Submit, ());
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user