Add button type to click events

This commit is contained in:
JMS55 2023-12-24 13:51:05 -08:00
parent 17f963e4cc
commit 39b45a747b
3 changed files with 18 additions and 11 deletions

View file

@ -59,7 +59,7 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
rsx! {
for (entity, name) in entities {
Button {
onclick: move |event: Event<()>| {
onclick: move |event: Event<PointerButton>| if *event.data == PointerButton::Primary {
if Some(entity) == ***selected_entity {
selected_entity.set(None);
} else {
@ -79,7 +79,7 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
}
}
Button {
onclick: move |event: Event<()>| {
onclick: move |event: Event<PointerButton>| if *event.data == PointerButton::Primary {
spawn_entity();
event.stop_propagation();
},
@ -148,8 +148,8 @@ fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element<'a> {
render! {
node {
onclick: move |event| cx.props.onclick.call(event),
onclick_down: |_| clicked.set(true),
onclick_up: |_| clicked.set(false),
onclick_down: |event| if *event.data == PointerButton::Primary { clicked.set(true) },
onclick_up: |event| if *event.data == PointerButton::Primary { clicked.set(false) },
onmouse_enter: |_| hovered.set(true),
onmouse_exit: |_| { hovered.set(false); clicked.set(false) },
padding: "8",
@ -161,7 +161,7 @@ fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element<'a> {
#[derive(Props)]
struct ButtonProps<'a> {
onclick: EventHandler<'a, Event<()>>,
onclick: EventHandler<'a, Event<PointerButton>>,
base_color: Option<&'a str>,
click_color: Option<&'a str>,
hover_color: Option<&'a str>,

View file

@ -17,16 +17,22 @@ use std::{any::Any, mem, rc::Rc};
// TODO: Other events
pub mod events {
use bevy_mod_picking::pointer::PointerButton;
super::impl_event! [
();
onclick
onclick_down
onclick_up
onmouse_over
onmouse_out
onmouse_enter
onmouse_exit
];
super::impl_event! [
PointerButton;
onclick
onclick_down
onclick_up
];
}
#[derive(Resource, Default)]
@ -53,13 +59,13 @@ impl EventReaders {
) -> Vec<(Entity, &'static str, Rc<dyn Any>, bool)> {
let mut events: Vec<(Entity, &'static str, Rc<dyn Any>, bool)> = Vec::new();
for event in self.click.read(click) {
events.push((event.target, "click", Rc::new(()), true));
events.push((event.target, "click", Rc::new(event.button), true));
}
for event in self.click_down.read(click_down) {
events.push((event.target, "click_down", Rc::new(()), true));
events.push((event.target, "click_down", Rc::new(event.button), true));
}
for event in self.click_up.read(click_up) {
events.push((event.target, "click_up", Rc::new(()), true));
events.push((event.target, "click_up", Rc::new(event.button), true));
}
for event in self.mouse_over.read(mouse_over) {
events.push((event.target, "mouse_over", Rc::new(()), false));

View file

@ -30,6 +30,7 @@ pub mod prelude {
pub use super::ecs_hooks::*;
pub use super::elements::*;
pub use super::{DioxusUiBundle, DioxusUiPlugin, DioxusUiRoot};
pub use bevy_mod_picking::pointer::PointerButton;
pub use dioxus;
pub use dioxus::prelude::*;
}