Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed gizmo responding to mouse input through other UI elements (#73) #83

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/transform-gizmo-bevy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ fn update_gizmos(

let gizmo_interaction = GizmoInteraction {
cursor_pos: (cursor_pos.x, cursor_pos.y),
hovered: true,
drag_started: mouse.just_pressed(MouseButton::Left),
dragging: mouse.any_pressed([MouseButton::Left]),
};
Expand Down
10 changes: 9 additions & 1 deletion crates/transform-gizmo-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//! ```
//!
//!
use egui::{epaint::Vertex, Mesh, PointerButton, Pos2, Rgba, Ui};
use egui::{epaint::Vertex, Mesh, PointerButton, Pos2, Rgba, Sense, Ui, Vec2};

use transform_gizmo::math::Transform;
pub use transform_gizmo::*;
Expand Down Expand Up @@ -84,9 +84,17 @@ impl GizmoExt for Gizmo {
..*self.config()
});

let interaction = ui.interact(
Rect::from_center_size(cursor_pos, Vec2::splat(1.0)),
ui.id().with("_interaction"),
Sense::click_and_drag(),
);
let hovered = interaction.hovered();

let gizmo_result = self.update(
GizmoInteraction {
cursor_pos: (cursor_pos.x, cursor_pos.y),
hovered,
drag_started: ui
.input(|input| input.pointer.button_pressed(PointerButton::Primary)),
dragging: ui.input(|input| input.pointer.button_down(PointerButton::Primary)),
Expand Down
9 changes: 8 additions & 1 deletion crates/transform-gizmo/src/gizmo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ impl Gizmo {
/// # let cursor_pos = Default::default();
/// # let drag_started = true;
/// # let dragging = true;
/// # let hovered = true;
/// # let mut transforms = vec![];
///
/// let interaction = GizmoInteraction {
/// cursor_pos,
/// hovered,
/// drag_started,
/// dragging
/// };
Expand Down Expand Up @@ -130,7 +132,7 @@ impl Gizmo {

// If there is no active subgizmo, find which one of them
// is under the mouse pointer, if any.
if self.active_subgizmo_id.is_none() {
if self.active_subgizmo_id.is_none() && interaction.hovered {
if let Some(subgizmo) = self.pick_subgizmo(pointer_ray) {
subgizmo.set_focused(true);

Expand Down Expand Up @@ -636,6 +638,11 @@ impl Gizmo {
pub struct GizmoInteraction {
/// Current cursor position in window coordinates.
pub cursor_pos: (f32, f32),
/// Whether the gizmo is hovered this frame.
/// Some other UI element might be covering the gizmo,
/// and in such case you may not want the gizmo to be
/// interactable.
pub hovered: bool,
/// Whether dragging was started this frame.
/// Usually this is set to true if the primary mouse
/// button was just pressed.
Expand Down
Loading